Latest Posts

Topic: How to add a custom starting condition?

PkK

Topic Opener
Joined: 2012-01-06, 12:19
Posts: 236
Ranking
Widelands-Forum-Junkie
Posted at: 2016-02-03, 21:11

I used to play with custom starting conditions for some players. To do that I added .lua files in tribes/barbarians/scripting/, etc. It seems the default starting conditions are now in data/tribes/scripting/starting_conditions/empire/, etc. But if I put a .lua file in there, it doesn't show up in game as a starting condition to select.

Philipp


Top Quote
GunChleoc
Avatar
Joined: 2013-10-07, 15:56
Posts: 3324
Ranking
One Elder of Players
Location: RenderedRect
Posted at: 2016-02-03, 21:53

You have to register the new scripts in data/tribes/preload.lua


Busy indexing nil values

Top Quote
PkK

Topic Opener
Joined: 2012-01-06, 12:19
Posts: 236
Ranking
Widelands-Forum-Junkie
Posted at: 2016-02-04, 10:18

Thanks, now I can add my starting condition. I had to rename a few wares, etc to make it not crash with current widelands, but there is one remaining issue: The periodic goods addition is not happening.
Barbarian example:

include "scripting/infrastructure.lua"

set_textdomain("tribes")

init = {
   descname = _ "Headquarters cheat",
   func = function(player, shared_in_start)

   local sf = wl.Game().map.player_slots[player.number].starting_field
   if shared_in_start then
      sf = shared_in_start
   else
      player:allow_workers("all")
   end

   hq = prefilled_buildings(player, { "barbarians_headquarters", sf.x, sf.y,
      wares = {
         ax = 5,
         bread_paddle = 2,
         blackwood = 32,
         cloth = 5,
         coal = 12,
         felling_ax = 4,
         fire_tongs = 2,
         fish = 6,
         fishing_rod = 2,
         gold = 4,
         grout = 12,
         hammer = 12,
         hunting_spear = 2,
         iron = 12,
         iron_ore = 5,
         kitchen_tools = 4,
         meal = 4,
         meat = 6,
         pick = 8,
         barbarians_bread = 8,
         ration = 12,
         granite = 40,
         scythe = 6,
         shovel = 4,
         snack = 3,
         thatch_reed = 24,
         log = 80,
      },
      workers = {
         barbarians_blacksmith = 2,
         barbarians_brewer = 1,
         barbarians_builder = 10,
         barbarians_charcoal_burner = 1,
         barbarians_carrier = 40,
         barbarians_gardener = 1,
         barbarians_geologist = 4,
         barbarians_lime_burner = 1,
         barbarians_lumberjack = 3,
         barbarians_miner = 4,
         barbarians_ranger = 1,
         barbarians_stonemason = 2,
         barbarians_ox = 5,
      },
      soldiers = {
         [{0,0,0,0}] = 45,
      }
   })

      place_building_in_region(player, "barbarians_battlearena", sf:region(12), {
         wares = {
            barbarians_bread = 8,
            fish = 6,
            meat = 6,
         }
      })

   for i=1,100000 do
     sleep(600000)
     --local hq = wl.Game().players[player.number].get_buildings("headquarters")
     local hq = wl.Game().map:get_field(sf.x, sf.y).immovable

     if hq and hq.type == "warehouse" then
       if hq:get_wares("water") < 1280 then
         hq:set_wares("water", hq:get_wares("water") + 128)
       end
       if hq:get_wares("log") < 1280 then
         hq:set_wares("log", hq:get_wares("log") + 128)
       end
       if hq:get_wares("raw_stone") < 640 then
         hq:set_wares("raw_stone", hq:get_wares("raw_stone") + 64)
       end
       if hq:get_wares("coal") < 240 then
         hq:set_wares("coal", hq:get_wares("coal") + 24)
       end
       if hq:get_wares("ironore") < 80 then
         hq:set_wares("ironore", hq:get_wares("ironore") + 8)
       end
       if hq:get_wares("fish") < 100 then
         hq:set_wares("fish", hq:get_wares("fish") + 4)
       end
       if hq:get_wares("gold") < 100 then
         hq:set_wares("gold", hq:get_wares("gold") + 4)
       end
     end
   end
end
}

return init


Top Quote
GunChleoc
Avatar
Joined: 2013-10-07, 15:56
Posts: 3324
Ranking
One Elder of Players
Location: RenderedRect
Posted at: 2016-02-04, 10:25

The problem here is that the headquarters have different names now, depending on the tribe. So, you need to get the wl.map.TribeDescription for the player, then call get tribe.headquarters to get the building name.

https://wl.widelands.org/docs/wl/autogen_wl_map/#module-interfaces

Something like this should work:

local hq_name = wl.Game().players[player.number].tribe.headquarters
local hq = wl.Game().players[player.number].get_buildings(hq_name)

Busy indexing nil values

Top Quote
PkK

Topic Opener
Joined: 2012-01-06, 12:19
Posts: 236
Ranking
Widelands-Forum-Junkie
Posted at: 2016-02-04, 10:39

GunChleoc wrote:

The problem here is that the headquarters have different names now, depending on the tribe. So, you need to get the wl.map.TribeDescription for the player, then call get tribe.headquarters to get the building name.

But the code doesn't even use the name of the headquarter. The line that has the name is commented out.

Philipp


Top Quote
GunChleoc
Avatar
Joined: 2013-10-07, 15:56
Posts: 3324
Ranking
One Elder of Players
Location: RenderedRect
Posted at: 2016-02-04, 12:26

I just noticed that you have an old ware name in there. Try raw_stone => granite

You might find this table useful - it's the translatable names rather than the internal names, but it might still help you as a guideline to which names have been changed. All the workers, buildings ships etc. now have a prefix in their name to denote the tribe, but the wares generally don't except for bread.

Edited: 2016-02-04, 12:32

Busy indexing nil values

Top Quote
PkK

Topic Opener
Joined: 2012-01-06, 12:19
Posts: 236
Ranking
Widelands-Forum-Junkie
Posted at: 2016-02-04, 13:02

GunChleoc wrote:

I just noticed that you have an old ware name in there. Try raw_stone => granite

Thanks. Unfortunately, execution doesn't even make it to that point in the code.

Philipp


Top Quote
PkK

Topic Opener
Joined: 2012-01-06, 12:19
Posts: 236
Ranking
Widelands-Forum-Junkie
Posted at: 2016-02-04, 13:45

GunChleoc wrote:

The problem here is that the headquarters have different names now, depending on the tribe. So, you need to get the wl.map.TribeDescription for the player, then call get tribe.headquarters to get the building name.

https://wl.widelands.org/docs/wl/autogen_wl_map/#module-interfaces

Something like this should work:

~~~~ local hq_name = wl.Game().players[player.number].tribe.headquarters local hq = wl.Game().players[player.number].get_buildings(hq_name) ~~~~

Would this work with the if test even when the player has been defeated? Would it crash or return 0, NULL or whatever it is in Lua for hq?

Philipp


Top Quote
PkK

Topic Opener
Joined: 2012-01-06, 12:19
Posts: 236
Ranking
Widelands-Forum-Junkie
Posted at: 2016-02-04, 14:07

GunChleoc wrote:

The problem here is that the headquarters have different names now, depending on the tribe. So, you need to get the wl.map.TribeDescription for the player, then call get tribe.headquarters to get the building name.

https://wl.widelands.org/docs/wl/autogen_wl_map/#module-interfaces

Something like this should work:

~~~~ local hq_name = wl.Game().players[player.number].tribe.headquarters local hq = wl.Game().players[player.number].get_buildings(hq_name) ~~~~

Your suggestion gives:

PANIC: unprotected error in call to Lua API (attempt to persist a light C function (0x11a090c) (root.sleep.upvalues._ENV.__coroutine_registry[8].stack[14]))

Top Quote
PkK

Topic Opener
Joined: 2012-01-06, 12:19
Posts: 236
Ranking
Widelands-Forum-Junkie
Posted at: 2016-02-04, 14:11

PkK wrote:

GunChleoc wrote:

The problem here is that the headquarters have different names now, depending on the tribe. So, you need to get the wl.map.TribeDescription for the player, then call get tribe.headquarters to get the building name.

https://wl.widelands.org/docs/wl/autogen_wl_map/#module-interfaces

Something like this should work:

~~~~ local hq_name = wl.Game().players[player.number].tribe.headquarters local hq = wl.Game().players[player.number].get_buildings(hq_name) ~~~~

Your suggestion gives:

~~~~ PANIC: unprotected error in call to Lua API (attempt to persist a light C function (0x11a090c) (root.sleep.upvalues._ENV.__coroutine_registry[8].stack[14])) ~~~~

Hmm, I get the same when I put the tribe-specific name manually.

Philipp


Top Quote