Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- this script will simply split a squad into two or remerge them again.
- -- table to store all the squad info we need.
- split_merge_squad_info = {}
- -- main function
- function Squad_SplitOrMerge(sid)
- -- get the string ID of the squad, not the engine ID.
- local sidN = sid["id"]
- -- shorten the table for ease of use
- local t = split_merge_squad_info
- -- first find out if the sid is in the table
- if not t[sidN] then
- -- they are not already in the table, therefore they have not been split yet.
- -- first check if they can be split (aka they have more then one man)
- if Squad_Count > 1 then
- -- get the squad size and half it so we can split the squad in two.
- local half = math.floor(Squad_Count(sid)/2) -- math.floor rounds the number down so we get a whole number not a decimal
- -- split them and save the new squad ID
- local secondSid = Squad_Split(sid, half)
- -- now add both ID's to the table and pair them with their other half
- t[sidN] = secondSid
- t[secondSid["id"]] = sid
- -- all done, we now have two squads, and their details are saved in a table for the merge section.
- end
- else
- -- they are present in the table, therefore they have already been split and need to be merged.
- -- grab the ID of the their pair
- local secondSid = t[sidN]
- -- check their partner is alive
- 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
- -- they are still alive, so carry on.
- -- make the second squad run towards the first squad
- -- need to shove them in an SGroup to do this
- local sg = SGroup_CreateIfNotFound("temp_move_sg" .. secondSid["id"])
- -- do the command
- Command_Squad(Squad_GetPlayerOwner(secondSid), sg, SCMD_Move, false)
- -- now we will wait for them to get close
- -- this function will check if the squad is within 5m of the other paired squad
- -- holding a time stamp, so the function can time out if needed
- local distance_check_timeStamp
- local check_distance = function()
- -- check the status of both squads
- if Squad_IsValid(sid) and Squad_GetHealth(sid) > 0 then
- if Squad_IsValid(secondSid) and Squad_GetHealth(secondSid) > 0 then
- -- check the time stamp
- if distance_check_timeStamp == nil then
- --- when it is nil, it is only the first iteration, so set the base time.
- distance_check_timeStamp = World_GetGameTime()
- else
- -- it is not the first time, so make sure it has not taken more than say... 3 mins
- if World_GetGameTime() - distance_check_timeStamp > 3*60 then
- -- it has taken too long
- -- need to time out function
- -- stop the squad
- -- remove the sid's from the table
- t[sidN] = nil
- t[secondSid["id"]] = nil
- else
- -- all is good, contiue
- -- check distance
- if World_DistancePointToPoint(Squad_GetPosition(sid), Squad_GetPosition(secondSid)) <= 5 then
- -- we can continue
- -- just merge the two squads
- Squad_Merge(sid, secondSid)
- -- now remove them from the table
- t[sidN] = nil
- t[secondSid["id"]] = nil
- else
- -- check again
- check_distance()
- end
- end
- end
- else
- -- moving squad is dead, need to stop function and remove from table
- t[sidN] = nil
- t[secondSid["id"]] = nil
- end
- else
- -- the non moving squad is dead, stop the moving one
- Cmd_Stop(sg)
- -- remove the sid's from the table
- t[sidN] = nil
- t[secondSid["id"]] = nil
- end
- end
- -- fire off the above function
- check_distance()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement