Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. -- this script will simply split a squad into two or remerge them again.
  2.  
  3. -- table to store all the squad info we need.
  4. split_merge_squad_info = {}
  5.  
  6. -- main function
  7. function Squad_SplitOrMerge(sid)
  8.     -- get the string ID of the squad, not the engine ID.
  9.     local sidN = sid["id"]
  10.    
  11.     -- shorten the table for ease of use
  12.     local t = split_merge_squad_info
  13.    
  14.     -- first find out if the sid is in the table
  15.     if not t[sidN] then
  16.         -- they are not already in the table, therefore they have not been split yet.
  17.        
  18.         -- first check if they can be split (aka they have more then one man)
  19.         if Squad_Count > 1 then
  20.             -- get the squad size and half it so we can split the squad in two.
  21.             local half = math.floor(Squad_Count(sid)/2) -- math.floor rounds the number down so we get a whole number not a decimal
  22.            
  23.             -- split them and save the new squad ID
  24.             local secondSid = Squad_Split(sid, half)
  25.            
  26.             -- now add both ID's to the table and pair them with their other half
  27.             t[sidN] = secondSid
  28.             t[secondSid["id"]] = sid
  29.            
  30.             -- all done, we now have two squads, and their details are saved in a table for the merge section.
  31.         end
  32.     else
  33.         -- they are present in the table, therefore they have already been split and need to be merged.
  34.        
  35.         -- grab the ID of the their pair
  36.         local secondSid = t[sidN]
  37.        
  38.         -- check their partner is alive
  39.         if Squad_IsValid(secondSid) and Squad_GetHealth(secondSid) > 0 then -- sometimes this is silly and wont work, so gonna check if they are valid too
  40.             -- they are still alive, so carry on.
  41.             -- make the second squad run towards the first squad
  42.             -- need to shove them in an SGroup to do this
  43.             local sg = SGroup_CreateIfNotFound("temp_move_sg" .. secondSid["id"])
  44.            
  45.             -- do the command
  46.             Command_Squad(Squad_GetPlayerOwner(secondSid), sg, SCMD_Move, false)
  47.            
  48.             -- now we will wait for them to get close
  49.             -- this function will check if the squad is within 5m of the other paired squad
  50.             -- holding a time stamp, so the function can time out if needed
  51.             local distance_check_timeStamp
  52.             local check_distance = function()
  53.                 -- check the status of both squads
  54.                 if Squad_IsValid(sid) and Squad_GetHealth(sid) > 0 then
  55.                     if Squad_IsValid(secondSid) and Squad_GetHealth(secondSid) > 0 then
  56.                         -- check the time stamp
  57.                         if distance_check_timeStamp == nil then
  58.                             --- when it is nil, it is only the first iteration, so set the base time.
  59.                             distance_check_timeStamp = World_GetGameTime()
  60.                         else
  61.                             -- it is not the first time, so make sure it has not taken more than say... 3 mins
  62.                             if World_GetGameTime() - distance_check_timeStamp > 3*60 then
  63.                                 -- it has taken too long
  64.                                 -- need to time out function
  65.                                 -- stop the squad
  66.                                 -- remove the sid's from the table
  67.                                 t[sidN] = nil
  68.                                 t[secondSid["id"]] = nil
  69.                             else
  70.                                 -- all is good, contiue
  71.                                 -- check distance
  72.                                 if World_DistancePointToPoint(Squad_GetPosition(sid), Squad_GetPosition(secondSid)) <= 5 then
  73.                                     -- we can continue
  74.                                     -- just merge the two squads
  75.                                     Squad_Merge(sid, secondSid)
  76.                                    
  77.                                     -- now remove them from the table
  78.                                     t[sidN] = nil
  79.                                     t[secondSid["id"]] = nil
  80.                                    
  81.                                 else
  82.                                     -- check again
  83.                                     check_distance()
  84.                                 end
  85.                             end
  86.                         end
  87.                     else
  88.                         -- moving squad is dead, need to stop function and remove from table
  89.                         t[sidN] = nil
  90.                         t[secondSid["id"]] = nil
  91.                     end
  92.                 else
  93.                     -- the non moving squad is dead, stop the moving one
  94.                     Cmd_Stop(sg)
  95.                     -- remove the sid's from the table
  96.                     t[sidN] = nil
  97.                     t[secondSid["id"]] = nil
  98.                 end
  99.                
  100.             end
  101.            
  102.             -- fire off the above function
  103.             check_distance()
  104.         end
  105.     end
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement