Lua Gadget: Detect what a builder was building when it died?

Lua Gadget: Detect what a builder was building when it died?

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Lua Gadget: Detect what a builder was building when it died?

Post by yanom »

So I've been helping out with the Lua AI for Conflict Terra. And I need a way to detect what (if anything) a constructor unit was building when it dies (perhaps in the gadget:unitDestroyed function?), so that it's job can be rescheduled to another builder.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Lua Gadget: Detect what a builder was building when it died?

Post by knorke »

unitID_that_unit_is_building = Spring.GetUnitIsBuilding (unitID)
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: Lua Gadget: Detect what a builder was building when it died?

Post by yanom »

knorke wrote:unitID_that_unit_is_building = Spring.GetUnitIsBuilding (unitID)
so, unitID_that_unit_is_building is the ID of the unit (still in nanoframe ) that (fully alive) unit unitID is building , right?

Does this still count if the builder is walking to the buildsite and has not yet started on the structure?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Lua Gadget: Detect what a builder was building when it died?

Post by FLOZi »

yanom wrote:
knorke wrote:unitID_that_unit_is_building = Spring.GetUnitIsBuilding (unitID)
so, unitID_that_unit_is_building is the ID of the unit (still in nanoframe ) that (fully alive) unit unitID is building , right?
Yes.
Does this still count if the builder is walking to the buildsite and has not yet started on the structure?
No. For that, you'll have to grab the command queue of the destroyed unit, and loop over it looking for build commands.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Lua Gadget: Detect what a builder was building when it died?

Post by zwzsg »

Since it is for an AI, you might find it simpler to store in table when the order is given, and check that table when it dies.

Like:

local BuilderOnMission={} -- table indexed by unitID
local DoItAgain={} -- table indexed by 1...n

...

function gadget:GameFrame(n)
...
Spring.GiveOrderToUnit(...
BuilderOnMission[UnitID]={some data that will help you know what and where it was building, like the cmd id and cmd parameters}
...
for Index,OrderToGiveAgain in ipairs(DoItAgain) do
-- type here some code to find a suitable builder and reissue order
...
table.remove(OrderToGiveAgain,index)
...
end
end



function gadget:UnitDestroyed(...
if BuilderOnMission[UnitID] then
table.insert(DoItAgain)=BuilderOnMission[UnitID]
BuilderOnMission[UnitID]=nil
end
end
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Lua Gadget: Detect what a builder was building when it died?

Post by knorke »

yes, what zwzsg said.
also because GetUnitIsBuilding will probally fail on morphing/cloneing.
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: Lua Gadget: Detect what a builder was building when it died?

Post by yanom »

FLOZi wrote: No. For that, you'll have to grab the command queue of the destroyed unit, and loop over it looking for build commands.
how do I do that?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Lua Gadget: Detect what a builder was building when it died?

Post by FLOZi »

You don't. Do it the way zwzsg suggested, it's a much better implementation.
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: Lua Gadget: Detect what a builder was building when it died?

Post by yanom »

... this is beyond me.

I've never programmed much before.

But I'll keep trying...
Post Reply

Return to “Lua Scripts”