Latest Posts

Topic: AI: init.lua blocks the game

Tibor

Topic Opener
Joined: 2009-03-23, 22:24
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2014-01-29, 18:21

Hi,

I am working on my LUA based AI :) and run into big problem.

My init.lua doesnt hand over the control to the game in spite of useing sleep() from coroutines.

To simplify the problem I can use the code from wiki:

use("aux", "coroutine")

function print_a_word(word)
while 1 do
print(word)
sleep(1000)
end
end

run(print_a_word, "Hello World!")

It works fine, but the game screen stays black and obviously the game is not running.

So what is the propery way to sleep() the init.lua?


Top Quote
SirVer

Joined: 2009-02-19, 14:18
Posts: 1445
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-30, 13:56

I think everything is working as intended, the problem is just that you have nothing on your map - i.e. you have no buildings that give you vision. When you are running a scenario, none of the other starting conditions of the game are loaded by default, so you have to place your headquarters and so on yourself. Take a look at the scenarios in campaigns/ - they all place headquarters. You can also use some of the starting conditions by use()ing them.


Top Quote
Tibor

Topic Opener
Joined: 2009-03-23, 22:24
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2014-01-30, 21:48

Well, this is not the best news, but not the worst either.

Allright I will investigate it. But this is another obstacle and another additional work face-sad.png

This practically means that as by now plug&play LUA AI is not possible. I mean, it is not possible just attach any potential LUA AI to any random map...


Top Quote
SirVer

Joined: 2009-02-19, 14:18
Posts: 1445
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-31, 07:22

Tibor wrote:

Well, this is not the best news, but not the worst either.

Allright I will investigate it. But this is another obstacle and another additional work face-sad.png

use("tribe_barbarians", "sc00_headquarters_medium")
for idx, plr in ipairs(wl.Game().players) do
     init.func(plr)
end

This only works if all players are defined as barbarians in the the map though.

This practically means that as by now plug&play LUA AI is not possible. I mean, it is not possible just attach any potential LUA AI to any random map...

There are ways: Start a game on the map you are interested in with the configuration of players you want to test (make sure the player you want to control is set to the None AI). Safe the game. Load the safegame from the commandline (--loadgame) and start your script immediately (--script). Of course this will not work in multiplayer games, but it should be enough to get you started.

Edited: 2014-01-31, 07:23

Top Quote
fk
Avatar
Joined: 2013-07-30, 21:58
Posts: 151
Ranking
At home in WL-forums
Posted at: 2014-01-31, 12:51

SirVer wrote:

    init.func(plr)

Sorry for my unexpected interruption, but does this mean that "shared_in_start" can be omitted?


Top Quote
Tibor

Topic Opener
Joined: 2009-03-23, 22:24
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2014-01-31, 20:39

SirVer,

it works flawlessly, thanks!!!


Top Quote
SirVer

Joined: 2009-02-19, 14:18
Posts: 1445
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-31, 20:55

fk wrote:

SirVer wrote:

    init.func(plr)

Sorry for my unexpected interruption, but does this mean that "shared_in_start" can be omitted?

This is a 'feature' of the Lua langue:

You can call a function with a number of arguments different from its number of parameters. Lua adjusts the number of arguments to the number of parameters, as it does in a multiple assignment: Extra arguments are thrown away; extra parameters get nil. You can call a function with a number of arguments different from its number of parameters. Lua adjusts the number of arguments to the number of parameters, as it does in a multiple assignment: Extra arguments are thrown away; extra parameters get nil.

This quote is from "Programming in Lua": http://www.lua.org/pil/5.html


Top Quote
SirVer

Joined: 2009-02-19, 14:18
Posts: 1445
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-31, 20:56

Tibor wrote:

SirVer,

it works flawlessly, thanks!!!

Glad to be of service - actually all this discussion got me interested in writing a Lua AI too - if there were not sooo much more work to be done for Widelands face-smile.png


Top Quote
Tibor

Topic Opener
Joined: 2009-03-23, 22:24
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2014-01-31, 21:10

Today I spent some time on a road algorithm, though an algorithm itself is logical and reliable, to put it into lua is too complex for me. I am thinking about other approach now...

BTW, what is miss most in LUA is break and continue


Top Quote
SirVer

Joined: 2009-02-19, 14:18
Posts: 1445
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-31, 22:00

break is in the language. In lua 5.2 there is also goto which can simulate a continue - unfortunately, lua 5.2 support will only be merged after b18.


Top Quote