RDNWiki: DOWScar/Tutorials/Advanced/SpawnArmyExample ...

RDN Wiki Home | Page Index | Recent Changes | Recently Commented | Users | Registration | Login:  Password:  

Spawn Army


Contents


Example of how to spawn an army using tables.


--[[ SPAWN AN ARMY ]]

--[[ Using a Table to Define an Army ]]

function Rule_SpawnArmy_Trigger()

	t_Army = {
		sgroup_name = { "sg_name1", "sg_name2", "sg_name3", "sg_name4", "sg_name5"},  
		blueprint = {"blueprint_name", "blueprint_name", "blueprint_name", "blueprint_name", "blueprint_name"},
		marker_spawn = {"mkr_spawn_name1", "mkr_spawn_name2", "mkr_spawn_name3", "mkr_spawn_name4", "mkr_spawn_name5"}, 
		marker_goto = {"mkr_goto_name1", "mkr_goto_name2", "mkr_goto_name3", "mkr_goto_name4", "mkr_goto_name5"}, 
		squad_num = {1, 1, 1, 1, 1}, 
		squad_size = {5, 5, 5, 5, 5}, 
		squad_health = {1, 1, 1, 1, 1},
		squad_stance = {STANCE_Hold, STANCE_Hold, STANCE_Hold, STANCE_Hold, STANCE_Hold }, 
		weapon_upgrade = {1, 1, 1, 1, 1}, 
		weapon_num = {2, 2, 2, 2, 2}, 
		weapon_name = {"weapon_name", "weapon_name", "weapon_name", "weapon_name", "weapon_name"}
	}

	g_Army_iter = 1
	
	Rule_AddInterval(Rule_SpawnArmy, 5)
	
end

function Rule_SpawnArmy()

	local i = g_Army_iter --[[ "i" will be used to represent which member of the table set we are dealing with ]] 

	--[[ Create Army ]]
	if g_Army_iter <= table.getn(t_Army.sgroup_name)then --[[ this "if/then" arguement is the gate to the function, when "g_Army_iter" is larger than the size of the table it will skip to the "else" ]]
		
		--[[ here we create the sgroup, the utility function seen here returns the sgroupID that will be used below, "local" means no other function outside this one can use the variable ]]
		local sgroupID = Util_CreateSquadsAtMarkerEx(playerID, t_Army.sgroup_name[i], t_Army.blueprint[i], t_Army.marker_spawn[i], t_Army.squad_num[i], t_Army.squad_size[i])
		--[[ adjust the squad health ]]
		if t_Army.squad_health[i] == 0 then
			SGroup_SetHealthInvulnerable(t_Army.sgroup_name[i], true)
		else
			SGroup_SetAvgHealth(t_Army.sgroup_name[i], t_Army.squad_health[i])
		end
		
		--[[ set the squad weapon status ]]
		if t_Army.weapon_upgrade[i] == 1 then
			for y = 1, t_Army.squad_num[i] do --[[ this is a "for loop", look it up! They're lots of fun! ]]
				Squad_ForceUpgradeWeapons(SGroup_GetSpawnedSquadAt(sgroupID, y), t_Army.weapon_name[i], t_Army.weapon_num[i])
			end
		end
		
		--[[ set the squad stance ]]
		Cmd_SetStance(t_Army.sgroup_name[i], t_Army.squad_stance[i])
		--[[ order the squad to move to a new location ]]
		Cmd_MoveToMarker(t_Army.sgroup_name[i], t_Army.marker_goto[i])
		
	else
		--[[ once the iteration has exceeded the size of the table, it is time to remove the function and/or trigger the next function ]]
		Rule_Remove(Rule_SpawnArmy)
	end
	
	--[[ once everything else is done, we increment the iteration variable ]]
	g_Army_iter = g_Army_iter+1
end

--[[ THE ADVANTAGE ]]
--[[ There are a lot of advantages to using a table in this way.
a) Once the function works, all of your balancing info is in one place, no need to go hunting for it amongst your functions.
b) It's very scalable, just add in new entries to the table and the function will accomodate you without a problem
c) It keeps the purpose and layout of your functions clear and efficient, you do in 1 command what would otherwise take you as many lines as iterations needed
d) It makes you look wicked cool!

Here's an example of what it might look like without the table... ]]

function Rule_SpawnArmy()

	--[[ Create Army ]]
	local sgroupID1 = Util_CreateSquadsAtMarkerEx(playerID, "sgroup_name1", "blueprint_name", "marker_name", 2, 5)
	local sgroupID2 = Util_CreateSquadsAtMarkerEx(playerID, "sgroup_name2", "blueprint_name", "marker_name", 2, 5)
	local sgroupID3 = Util_CreateSquadsAtMarkerEx(playerID, "sgroup_name3", "blueprint_name", "marker_name", 3, 5)
	local sgroupID4 = Util_CreateSquadsAtMarkerEx(playerID, "sgroup_name4", "blueprint_name", "marker_name", 1, 5)
	local sgroupID5 = Util_CreateSquadsAtMarkerEx(playerID, "sgroup_name5", "blueprint_name", "marker_name", 2, 5)
	
	--[[ adjust the squad health ]]
	SGroup_SetAvgHealth("sgroup_name1", 0.5)
	SGroup_SetHealthInvulnerable("sgroup_name2", true)
	SGroup_SetAvgHealth("sgroup_name3", 1)
	SGroup_SetAvgHealth("sgroup_name4", 0.1)
	SGroup_SetAvgHealth("sgroup_name5", 0.8)

	--[[ set the squad stance ]]
	Cmd_SetStance("sgroup_name1", STANCE_Hold)
	Cmd_SetStance("sgroup_name2", STANCE_Hold)
	Cmd_SetStance("sgroup_name3", STANCE_Hold)
	Cmd_SetStance("sgroup_name4", STANCE_Hold)
	Cmd_SetStance("sgroup_name5", STANCE_Hold)

	--[[ set the squad weapon status ]]
	for y = 1, 2 do
		Squad_ForceUpgradeWeapons(SGroup_GetSpawnedSquadAt(sgroupID1, y), "weapon_name", 2)
	end
	for y = 1, 2 do
		Squad_ForceUpgradeWeapons(SGroup_GetSpawnedSquadAt(sgroupID2, y), "weapon_name", 2)
	end
	for y = 1, 3 do
		Squad_ForceUpgradeWeapons(SGroup_GetSpawnedSquadAt(sgroupID3, y), "weapon_name", 2)
	end
	
	Squad_ForceUpgradeWeapons(SGroup_GetSpawnedSquadAt(sgroupID4, y), "weapon_name", 2)
	
	for y = 1, 2 do
		Squad_ForceUpgradeWeapons(SGroup_GetSpawnedSquadAt(sgroupID5, y), "weapon_name", 2)
	end
	
	--[[ order the squad to move to a new location ]]
	Cmd_MoveToMarker("sgroup_name1", "marker_name")
	Cmd_MoveToMarker("sgroup_name2", "marker_name")
	Cmd_MoveToMarker("sgroup_name3", "marker_name")
	Cmd_MoveToMarker("sgroup_name4", "marker_name")
	Cmd_MoveToMarker("sgroup_name5", "marker_name")

end

--[[ Now this may look simple and easy to read... easier than the table perhaps. But when it comes to adding more entries 
or editing/balancing the data for numerous selections, having explicit entries for each sgroup means a lot more work in 
the long run. But you should use whatever you're comfortable with and that works best with your end goals. ]]

Note 1: Make sure your weapon_num.i never returns a 0 (ie only spawn weapons you want, don't cancel out spawning by changin weapon_num.i = 0 so you don't have to change the weapon name list)
A function that would equate to the equivalent of the following, applied to a 8 men space marine squad
Squad_ForceUpgradeWeapons(SGroup_GetSpawnedSquadAt(SGroup_ID, y), “space_marine_missile_launcher_tactical”,0)
Squad_ForceUpgradeWeapons(SGroup_GetSpawnedSquadAt(SGroup_ID, y), “space_marine_plasma_gun”, 3)
would result in a squad that has 5 missile launchers (total of 8, 3 overwritten by plasma guns) and 3 plasma guns, so a total of 8 weapons as of Dow ver. 1.10 -theBlind


Note 2: weapon_name should be a 2D list to contain several upgrades for a single squad. -theBlind

Links


SCAR
Wiki Home

End of Page


Referring pages: DOWScar
DOWScar/Tutorials
DOWScar/Tutorials/Advanced

There are no files on this page. [Display files/form]
There is no comment on this page. [Display comments/form]