Polls

Best Build 21 Screenshot

Log in to vote!

Latest Posts

Topic: How to add trunks over time?

PkK
Joined: 2012-01-06, 12:19
Posts: 202
Ranking
Widelands-Forum-Junkie
Posted at: 2014-03-18, 17:20
I want to create a starting condition, that gives additional trunks over time. I naively tried modifying the existing stating condition file by canging the end like this
   for i=1,3 do
     sleep(10)
     hq:set_wares("trunk", hq:get_wares("trunk") + 1)
   end


But I just get an error message

Error in Lua Coroutine
[/tmp/buildd/widelands-18/src/scripting/scripting.h:42] [string "tribe_barbarians:sc64_headquarters_medium_c..."]:71: attempt to index global 'hq' (a nil value)
Send message to all players and pause gameForcing flag at (53, 17)


Now, I have no idea about lua, but I saw that there is a hq = ... just above, so hq should be a variable I can use.

I just want to give the AI periodically some resources, to make the game more challenging.

Philipp

Top Quote
fk
Avatar
Joined: 2013-07-30, 22:58
Posts: 150
Ranking
At home in WL-forums
Posted at: 2014-03-18, 18:43

@PkK The function 'prefilled_buildings' returns no value. face-wink.png

hq = wl.Game().map:get_field(sf.x, sf.y).immovable

Edited: 2014-03-18, 18:56

Top Quote
PkK
Joined: 2012-01-06, 12:19
Posts: 202
Ranking
Widelands-Forum-Junkie
Posted at: 2014-03-18, 18:52
So what purpose does the variable hq serve in the init script that comes with widelands?

I now changed my lines to
   for i=1,10 do
     sleep(3)
     local hq = wl.Game().map:get_field(sf.x,sf.y)
     hq:set_wares("trunk", hq:get_wares("trunk") + 1)   
   end


and get

Error in Lua Coroutine
[/tmp/buildd/widelands-18/src/scripting/scripting.h:42] [string "tribe_barbarians:sc64_headquarters_medium_c..."]:74: attempt to call method 'get_wares' (a nil value)
Send message to all players and pause gamelastserial: 0
sound_handler 1 mal geschlossen. freq 22050, format 32784, chan 2


Top Quote
fk
Avatar
Joined: 2013-07-30, 22:58
Posts: 150
Ranking
At home in WL-forums
Posted at: 2014-03-18, 18:58

See above, I just edited my previous answer.

The "hq = " serves no purpose, as far as I can see.


Top Quote
PkK
Joined: 2012-01-06, 12:19
Posts: 202
Ranking
Widelands-Forum-Junkie
Posted at: 2014-03-18, 19:01
Thanks. That one works. I also considered
local hq = wl.Game().players[player.number].get_buildings("headquarters")


just in case the hq is not at the startng loation, but even though documentation says that get_buildings() accepts a string, widelands complains that the argument is not a table.
Top Quote
fk
Avatar
Joined: 2013-07-30, 22:58
Posts: 150
Ranking
At home in WL-forums
Posted at: 2014-03-18, 19:13

In case of a startup script I expect that it is save to assume that the hq is at the starting location.

By the way, in the newest widelands version you will have to change "trunk" to "log".


Top Quote
PkK
Joined: 2012-01-06, 12:19
Posts: 202
Ranking
Widelands-Forum-Junkie
Posted at: 2014-03-18, 19:17

What happens if there is no hq (e.g. when the enemy has already destroyed it)?

Philipp


Top Quote
PkK
Joined: 2012-01-06, 12:19
Posts: 202
Ranking
Widelands-Forum-Junkie
Posted at: 2014-03-18, 19:19
I now use this, which I hope means the AI will never run out of basic building materials:

   for i=1,3000 do
     sleep(10000)
     --local hq = wl.Game().players[player.number].get_buildings("headquarters")
     local hq = wl.Game().map:get_field(sf.x, sf.y).immovable
     if hq:get_wares("trunk") < 10 then
       hq:set_wares("trunk", hq:get_wares("trunk") + 1)
     end
     if i % 2 == 0 and hq:get_wares("raw_stone") < 10 then
       hq:set_wares("raw_stone", hq:get_wares("raw_stone") + 1)
     end
     if i % 8 == 0 and hq:get_wares("coal") < 10 then
       hq:set_wares("coal", hq:get_wares("coal") + 1)
     end
   end


P.S.: I'm using build18, and the init scripts use "trunk" everywhere.

Top Quote
SirVer
Joined: 2009-02-19, 15:18
Posts: 1439
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-03-18, 19:22

This is wrong

 wl.Game().players[player.number].get_buildings("headquarters")

either

 wl.Game().players[player.number].get_buildings(wl.Game().players[player.number], "headquarters")

or the short form:

wl.Game().players[player.number]:get_buildings("headquarters")  -- note the : instead of .

Top Quote
fk
Avatar
Joined: 2013-07-30, 22:58
Posts: 150
Ranking
At home in WL-forums
Posted at: 2014-03-18, 19:29
"What happens if there is no hq (e.g. when the enemy has already destroyed it)?"

You can test to see if the immovable is there (slightly adapted code snippet):

local hq = wl.Game().map:get_field(sf.x, sf.y).immovable
if hq then
	if hq.type == "flag" or hq.type == "warehouse" or hq.type == "productionsite" then
		hq:set_wares(ware, hq:get_wares(ware) + amount)
	end
end

Top Quote