Can anyone help 'randomize' this code?

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
Hi guys,

I have written a functioning piece of code that is pretty straight forward, but I would like to make it more randomized if possible. The function performs checks against the current turn to see if the USSR receives reserve units from eastern Russia. Right now the code spawns units on an exact turn, but I would like to make this a bit more random if possible.

I was hoping the more advanced lua gurus might glance over it and suggest a good approach.

First here is the code:
Code:
-- ######################################################################################
--	Eastern Reserves
-- ######################################################################################
-- Function checks turn and determines if USSR receives reserve troops from Eastern locations
-- Historically Siberian, Transbaikal and Central Asian military districts provided units to west.
function DeployEasternReserves(playerID)

	local turn = Game.GetGameTurn()
	local random = math.random

	if turn < EASTERN_RESERVES_END_TURN then
		local triggered = false
		local player = Players [playerID]
		Dprint("--------------------------------------------------------------------------------------------------------")
		Dprint(" SCENARIO EVENT TRIGGERED: Eastern Reserves (Turn: "..turn..")")
		Dprint("--------------------------------------------------------------------------------------------------------")
		for i, unitData in ipairs (g_EasternReserveUnits) do
			local turnTrigger = unitData.Turn
			
			if turnTrigger == turn then
				local spawnPlot = GetPlot(unitData.X, unitData.Y)
				if spawnPlot:GetOwner() == playerID then		
					triggered = true
					local unit = InitNewUnit(player, GameInfoTypes[unitData.UnitType], spawnPlot:GetX(), spawnPlot:GetY())
					unit:SetName(unitData.UnitName)
					Dprint("Reserve unit "..unit:GetName().." has arrived at plot "..spawnPlot:GetX()..","..spawnPlot:GetY())
				end
			end
		end

		if triggered == false then
			Dprint("No reserves received this turn")
		end

		Dprint("-------- SCENARIO EVENT COMPLETED ----------------------------------------------------------------------")
	end

end

and here is a snipped version of the global table it refers to:
Code:
g_EasternReserveUnits = {
	-- Far East transfers (Plot: Sverdlovsk)
	{Turn = 1, X = 89, Y = 67, UnitType = "UNIT_SOV_INF_1" , UnitName = "91" },
	{Turn = 1, X = 89, Y = 67, UnitType = "UNIT_SOV_INF_1" , UnitName = "119" },
}

Now as mentioned, the above code does work so don't go thinking there is any problem. What I want is ideas or examples of how I could make the code randomize the arrival turn by plus 3 or minus 3 turns when compared to the "Turn" field in g_EasternReserveUnits table

This is the first time I have tried something like this and figure I need a way to track if a unit has appeared or something.

Any one up to the challenge please post your suggestion. Full credit will be given of course if a workable model is implemented in my scenario.

Cheers
 
I suggest you read this wiki page about storing data. You will need to decide if you want to try to store the entire g_EasternReserveUnits or just a bool if a unit has spawned.

Storing g_EasternReserveUnits is more difficult. Storing just the bool you need a way to identify which bool belong to which unit entry, while keeping the name you store unique enough it's unlikely to cause conflicts with other mods.


As for the random chance of ± 3 turns, something like this
Code:
if turn >= (turnTrigger - 3) then *check bool that it hasn't already triggered here too*
	local chance = turnTrigger - turn + 4
	if Game.Rand(chance, "Some String Here (for logging)") == 0 then
		*spawn unit stuff and set bool that it has spawned*
	end
end

This will give it a 1/7 chance of firing on the first possible turn, 1/6 the second turn etc until finally 100% chance on turnTrigger + 3.
 
Thanks for the response.

Rf900, I am familiar with random function but that is part of the answer as snarko has outlined.

Snarko, will run through your example and see how things go.

Much thanks for the help from both of you. :goodjob:
 
A different approach that doesn't involve having to remember which units have already spawned

1) Move the table from Lua into the mod database associated with the saved game - you can always cache this into Lua if necessary
2) Replace your ipairs loop with a SQL query to just retrieve units that will spawn this turn

then

3) at initial game start up loop all the rows in the table and add math.random(-3, +3) from use (use math.max(1, newValue) to make sure they don't dip below turn 1)

HTH

W
 
Thanks whoward.

Will look into this. I am away from a computer this week.
 
Back
Top Bottom