Optimizing my code...any suggestions?

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
I was hoping some of you Lua gurus might be able to suggest how I can cut down on iterating the same code with almost identical variables.

Here is the code as I plan to reiterate it for each 'local' entry:

Code:
	local counterFG = g_GlobalSupplyCounters[playerID].Counter_FG	
	local counterV = g_GlobalSupplyCounters[playerID].Counter_V	
	local counterR = g_GlobalSupplyCounters[playerID].Counter_R 
	local counterLT = g_GlobalSupplyCounters[playerID].Counter_LT
	local counterLSP = g_GlobalSupplyCounters[playerID].Counter_LSP
	local counterLSP2 = g_GlobalSupplyCounters[playerID].Counter_LSP2
	local counterLTD = g_GlobalSupplyCounters[playerID].Counter_LTD
	local counterMT = g_GlobalSupplyCounters[playerID].Counter_MT
	local counterMSP = g_GlobalSupplyCounters[playerID].Counter_MSP
	local counterMTD = g_GlobalSupplyCounters[playerID].Counter_MTD
	local counterHT = g_GlobalSupplyCounters[playerID].Counter_HT
	local counterHSP = g_GlobalSupplyCounters[playerID].Counter_HSP
	local counterHTD = g_GlobalSupplyCounters[playerID].Counter_HTD
	local counterF = g_GlobalSupplyCounters[playerID].Counter_F
	local counterGA = g_GlobalSupplyCounters[playerID].Counter_GA
	local counterFB = g_GlobalSupplyCounters[playerID].Counter_FB
	local counterMB = g_GlobalSupplyCounters[playerID].Counter_MB

	-- Don't waste metals and food if cap for equipment has been reached as overflow is lost!
	if counterFG >= capFG then -- dont produce any more field guns
		g_GlobalSupplyCounters[playerID].Counter_FG = capFG
		g_GlobalSupplyCounters[playerID].EqFlux_FG = capFG - counterFG -- Existing equipment lost if over cap (represents lost in captured buildings/supply dumps)
	else
		outputFG = outputFG - floor((outputFG * powerProdPenalty)/100)
		outputFG = outputFG - floor((outputFG * steelProdPenalty)/100)
		outputFG = outputFG - floor((outputFG * foodProdPenalty)/100)
		g_GlobalSupplyCounters[playerID].Counter_FG = counterFG + outputFG
		g_GlobalSupplyCounters[playerID].EqFlux_FG = outputFG
	end
	g_GlobalSupplyCounters[playerID].EqCap_FG = capFG

In the above code, I have 17 equipment types in which I need to increment 3 global counters after applying penalties for some resources. I have provided what the IF statement looks like for the first equipment type (FG - Field Guns).

Is there a way to avoid writing 17 unique IF statements by using some kind of FOR statement given that local variables are the same except for a few letters at after the underscore symbol?
 
You might want to use "FG", "V", etc. as and index values (rather than variable name suffex). Then it is easy:

Code:
local typesTable = {"FG", "V", etc...}
local counter = {}
for i = 1, #typesTable do
	local type = typesTable[i]
	counter[type] = g_GlobalSupplyCounters[playerID][type]
end

then iterate over typeTable again for all of your subsequent IF functions, using type as index. You would have to set up "outputFG" as a table keyed by type and so on

Running string functions on variable names is bad for two reasons:
  1. We don't have access to _G from our modded Lua states (this is the Lua global where all the variable names are held).
  2. String manipulations are generally slow, and Lua is particluarly slow at this (for the trade-off of making them immutable and better/faster as indexes).
 
Thanks for the pointers Pazyryk. Appreciate it :)

After much procrastinating, I took the simpler route of reworking my code to eliminate equipment capacities. It seems that tracking counters against a capacity limit is a nightmare as too many variables are at play.

This cut down my code by about 1/3 its size.
 
Back
Top Bottom