ww2commander
Emperor
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:
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?
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?