Loading...
  OR  Zero-K Name:    Password:   

Minefield Grid Widget by Autowar

14 posts, 832 views
Post comment
Filter:    Player:  
sort
9 years ago
https://github.com/Autowar/Minefield_Grid

press Alt-D while selecting blastwings, roaches, ticks, or skuttles, to make than spread apart so they don't chain explode.
This widgets works for up to 25 units, and spreads them in a square grid.
The grid is not generated, i input the values for each location manually.
Feel free to critique or make pull requests.

Where do i find the values for the keyboard keys? I only know the the "D" key id is "100" because of ivand's self-reclaim widget.
+1 / -0
9 years ago
If d is 100, sounds like ascii values to me.



(dec and chr columns)
+0 / -0
function widget:KeyPress(key, mods, isRepeat)
--somehow "D" key id is 100
local dKey=100

D 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.D


Your 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_WeaponDefs

And/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 = blaThing
Maybe 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.MOVE


Declarations 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
+0 / -0
forum wins.
+6 / -0


9 years ago
Inb4 that stack overflow zalgo answer.
+0 / -0
9 years ago
Does anyone else here know that awkward dissonant feeling of encouragement+discouragement when a professional gives you genuine advice for improving something you barely scraped together?
+1 / -0
9 years ago
quote:

Your commands are always given with {"shift"}) no matter if player pressed that key or not.


I don't understand the impact of using "shift" versus "ctrl" and "alt". What problem is that particular usage causing?

quote:
local CMD_UNIT_SET_TARGET = 34923 // this one is not like the others!


I don't know how to properly reference CMD_UNIT_SET_TARGET. What am i doing something wrong in that implementation?

quote:
At top of file, all this:
local blablabla.Thing = blaThing
Maybe personal taste, but I find it not helpful.
For performance it is mostly irrelevant except for drawing graphics etc.
...
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.MOVE


I copied that behavior from widgets/gadgets written by AUrankAdminGoogleFrog, because it looks cleaner and neater to my eyes. Is there something objectively performance reducing about those methods?

[quote]
-same thing multiple times: spGetUnitDefID(selectedUnits[i])
-the conditons look messy, the unit's name are not so lost in all the ([]=][[){])
...
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.
[/quote]

The more compressed code is, the harder i find it to decode mentally, and even harder to debug. Is that a bad habit/mindset/lack of skill i need to change?

quote:
Half the local-things you do not even use. = Forms bad habits.
...

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.


Ok, i can fix that! I will comment out all of the locals i am not using.

I am working to generate a copy of all the various useful spring functions to keep locally in all of my widgets, so i can stop getting lost in the spring-wiki and other such reference sources to double-check how something should be used!
+0 / -0
quote:
I don't know how to properly reference CMD_UNIT_SET_TARGET. What am i doing something wrong in that implementation?

Set target is not an engine command, which means the engine does not put its ID in the CMD table. Behold: you can include a header file containing custom commands' IDs to use the CMD_BLA mnemonics instead of numbers thus (at least in ZK):
VFS.Include("LuaRules/Configs/customcmds.h.lua")

quote:
I don't understand the impact of using "shift" versus "ctrl" and "alt". What problem is that particular usage causing?

These modifiers have the same meaning they normally would, so in this case your orders always go to the end of the queue (even if the player is not holding shift because he wants the split now).

quote:
The more compressed code is, the harder i find it to decode mentally, and even harder to debug. Is that a bad habit/mindset/lack of skill i need to change?

Extreme verbosity makes code more difficult to read (and debug), for it makes it easier to miss the important parts in the flood of the copypasta. In your case it is also inferior performance-wise because you're calling the same function many times. The stacked if's are non-scaling because making the widget support eg. 500 units with this method would be insane, whereas creating a loop is not only much cleaner but allows you to work with arbitrarily high numbers.

quote:
spBla vs Spring.Bla: Is there something objectively performance reducing about those methods?

Using localized functions, you can save about a second of CPU time on a million function calls so using them for functions that will get called maybe 3 times a game is not useful.

quote:
I am working to generate a copy of all the various useful spring functions to keep locally in all of my widgets, so i can stop getting lost in the spring-wiki and other such reference sources to double-check how something should be used!

Why not improve the wiki for everyone instead?
+0 / -0
Thanks for the explanations PLrankAdminSprung! I understood most of your suggestions and will try to implement them in the future :D

quote:
Set target is not an engine command, which means the engine does not put its ID in the CMD table. Behold: you can include a header file containing custom commands' IDs to use the CMD_BLA mnemonics instead of numbers thus (at least in ZK):
VFS.Include("LuaRules/Configs/customcmds.h.lua")


where do i find the engine commands? i've been looking for "guard" and have yet to find it inside the ZK master.

quote:
Why not improve the wiki for everyone instead?


I am an amateur, my idea of "improving" would make the whole wiki even more of a mess.

[Spoiler]

quote:
The stacked if's are non-scaling because making the widget support eg. 500 units with this method would be insane, whereas creating a loop is not only much cleaner but allows you to work with arbitrarily high numbers.


This is good to know! I would like to use a for loop for the placement of units, but it is currently beyond my capacity to properly envision the logic for these things, even though DErank[2up]knorke and RUrankAdminikinz and one or two others have spelled it out for me! :(
+0 / -0
Here's some random bla which you can pull to get a mostly-done implementation of various improvements.

quote:
where do i find the engine commands? i've been looking for "guard"

They are in the CMD global table. In this case that'd be CMD.GUARD
+0 / -0
9 years ago
Pull request has been merged into main! I'll test it now.

quote:
They are in the CMD global table. In this case that'd be CMD.GUARD


I do not know where/recognize the CMD global table can be found. What is the filepath?
+0 / -0
I was so lost after 15 seconds of scrolling down the page O-O
+0 / -0

9 years ago
quote:
I do not know where/recognize the CMD global table can be found.

It's available by default. You can for example type CMD.GUARD and it's valid without any prerequisites.
+0 / -0
9 years ago
everything is on https://springrts.com/wiki/Lua_Scripting

commands too: https://springrts.com/wiki/Lua_CMDs
+0 / -0