# infrastructure.lua¶

This script contains function that ease setting up an initial infrastructure for maps. This includes placing buildings and roads.

To make these functions available include this file at the beginning of a script via:

include "scripting/infrastructure.lua"

connected_road(roadtype, plr, sflag, descr[, create_carriers = true])

This is a convenience function to create roads and waterways. It is basically a frontend to wl.bases.PlayerBase.place_road(). The road is forced, that is other stuff in the way is removed. A sample usage:

local game = wl.Game()
field = game.map:get_field(20, 20)
building = game.players[1]:place_building("barbarians_sentry", field, true, true)


After placing a building this would create a road starting from the building’s flag and goes from there 2 steps right (east), places a new flag. Then it goes one step bottom-right and one step right, places the next flag and then goes two steps right again and places the last flag. If a flag at this last position already exists, the road gets connected to the existing flag.

Parameters
• roadtype (string) – "normal", "busy", or "waterway"

• plr (wl.game.Player) – The player for which the road is created

• sflag (wl.map.Flag) – The starting flag for this road.

• descr – The description of this road. This are comma separated directions as those accepted by wl.bases.PlayerBase.place_road(). A | defines a flag.

• create_carriers (boolean) – If this is true carriers are created for the roads. Otherwise no carriers will be created.

prefilled_buildings(plr, b1_descr[, b2_descr, ...])

Create pre-filled buildings. Each description is an array which contains building type, x and y coordinates and pre-fill information. A sample usage:

prefilled_buildings(wl.Game().players[1],
{"empire_sentry", 57, 9},                               -- Sentry completely full with soldiers
{"empire_sentry", 57, 9, soldiers={[{0,0,0,0}]=1}},     -- Sentry with one soldier
{"empire_bakery", 55, 20, inputs = {wheat=6, water=6}}, -- bakery with wares and workers
{"empire_well", 52, 30},                                -- a well with workers
)

Parameters
• plr (wl.game.Player) – The player for which the building is created

• b1_descr (array) –

An array of tables. Each table must contain at least the name of the building, and the x and y position of the field where the building should be created. Optional entries are:

wares

A table of {"ware_name",count} as expected by wl.map.HasWares.set_wares(). This is valid for wl.map.Warehouse and must not be used otherwise.

inputs

A table of {"name",count} as expected by wl.map.HasInputs.set_inputs(). Inputs are wares or workers which are consumed by the building. This is valid for wl.map.ProductionSite and must not be used otherwise.

soldiers

A table of {{soldier_descr},count} as expected by wl.map.HasSoldiers.set_soldiers(). If this is nil, the site will be filled with {0,0,0,0} soldiers.

workers

A table of {"worker_name",count} as expected by wl.map.HasWorkers.set_workers(). Note that ProductionSites are filled with workers by default.

Returns

A table of created buildings

place_building_in_region(plr, building, region[, opts])

Tries to place a building randomly in the given region. It places houses using prefilled_buildings(), therefore uses the same options. If it fails, an error is thrown. This is a most useful function when defining starting conditions (initializations) in normal games.

Parameters
Returns

The wl.map.Building created

is_building(immovable)

Checks whether an immovable is a finished building, i.e. not a construction site.

Parameters

immovable – The immovable to test

Returns

true if the immovable is a building

Example:

field = wl.Game().map:get_field(20, 45)
if field.immovable then
if is_building(field.immovable) then
-- do something ..

add_wares_to_warehouse(player, warehouse, waretable)

Adds(subtracts) wares to the first warehouse of specified type

Parameters
• player (wl.game.Player) – the player to add wares to

• warehouse ("string") – The type of warehouse to add wares to, e.g. "empire_warehouse"

• waretable (table) – a table of pairs {"warename" = value}

check_trees_rocks_poor_hamlet(player, sf, warehouse, waretable_rocks, waretable_trees)

Used for starting condition “Poor Hamlet”. Checks for rocks or trees in the region of the player starting field and adds wares to the warehouse if no immovables were found.

Parameters
• player (wl.game.Player) – the player to check

• sf (starting_field) – starting field of the player

• warehouse (string) – The type of warehouse to add wares to

• waretable_rocks (table) – a table of pairs {"name" = value} to add if no rocks were found nearby

• waretable_trees (table) – a table of pairs {"name" = value} to add if no trees were found nearby