function widget:KeyPress(key, mods, isRepeat)
--somehow "D" key id is 100
local dKey=100D key is 100 because
https://github.com/spring/spring/blob/develop/cont/base/springcontent/LuaHandler/Utilities/keysym.lua=>
include("keysym.h.lua")
local dKey = KEYSYMS.DYour commands are always given with {"shift"}) no matter if player pressed that key or not.
Might want to look into:
-custom commands
-whatever zK does for hotkey system
[b]local tickAOE = 196 --armtick
...
if spGetUnitDefID(selectedUnits[i]) == UnitDefNames["armtick"].id then
unitAOE=tickAOE
elseif spGetUnitDefID(selectedUnits[i]) == UnitDefNames["corroach"].id then
unitAOE=roachAOE
elseif spGetUnitDefID(selectedUnits[i]) == UnitDefNames["corsktl"].id then
unitAOE=skuttleAOE
elseif spGetUnitDefID(selectedUnits[i]) == UnitDefNames["blastwing"].id then
unitAOE=blastwingAOE[/b]
-same thing multiple times: spGetUnitDefID(selectedUnits[i])
-the conditons look messy, the unit's name are not so lost in all the ([]=][[){])
Imo nicer to read like:
selectedUnitName = UnitDefs[spGetUnitDefID(selectedUnits[i])]
if (selectedUnitName == "corroach") then
unitAOE=roachAOE
elseif (selectedUnitName == "corskt")
then unitAOE=skuttleAOE
elseif (selectedUnitName == "blastwing") then
unitAOE=blastwingAOE
end
BUT:
=> Lua can read unit & weapon defs:
https://springrts.com/wiki/Lua_UnitDefs ,
https://springrts.com/wiki/Lua_WeaponDefsAnd/or use a table instead of the if-chain like:
[b]
unitsSeperationDistance =
{
[UnitDefNames["blastwing"].id] = 50,
[UnitDefNames["corsktl"].id] = 100,
...
}[/b]
This is 25 times similiar thing:
[b]
if i==1 then
x,y,z = spGetUnitPosition(selectedUnits[i])
spGiveOrderToUnit(selectedUnits[i], CMD_MOVE, {x,0,z}, {"shift"})
elseif i==2 then
spGiveOrderToUnit(selectedUnits[i], CMD_MOVE, {x,0,z+unitAOE}, {"shift"})
...
elseif i==25 then --
spGiveOrderToUnit(selectedUnits[i], CMD_MOVE, {x+(unitAOE*2),0,z-(unitAOE*2)}, {"shift"})
[/b]
Find a better way to convert the unit's number i to X-Z coordinates.
Something along:
[b]
function getXZ(i)
local size = 4
return i%size, math.floor(i/size)
end
...
local x,z = getXZ (i)
spGiveOrderToUnit(selectedUnits[i], CMD_MOVE, {x+(unitAOE*2),0,z-(unitAOE*2)} [/b]
or
[b]
--define the coordinates of each position
position [1] = {x=0,z=0}
position [2] = {x=0,z=1}
position [2] = {x=1,z=1}
...
position [15] = {x=7,z=2}
function getXZ (i)
return position[i].x * seperationDistance, position[i].z * seperationDistance
end
[/b]
At top of file, all this:
local blablabla.Thing = blaThingMaybe personal taste, but I find it not helpful.
For performance it is mostly irrelevant except for drawing graphics etc.
Half the local-things you do not even use. = Forms bad habits.
It only saves 5 characters to type sp VS Spring.
If removing those 5 letters are supposed to make a line of code look nicer, then the chance is there is too much things in one line anyway.
This is not even shorter:
CMD_MOVE = CMD.MOVEDeclarations of constants or variables sometimes get mixed into the local-things, like here:
...
local CMD_MOVE = CMD.MOVE
local CMD_UNIT_SET_TARGET = 34923 // this one is not like the others!
local CMD_GUARD = CMD.GUARD
...
Looking at variables/constants is quick way to figure out what a script does:
This makes them harder to spot.
Also seeing the CMD_GUARD makes reader assume it will be used in script, but it is not.
Risk to accidently create evil errors like:
local spGetMyTeamID = Spring.GetMyTeamID()VS
local spGetMyTeamID = Spring.GetMyTeamID