Changes in WikiSandbox
Revision Differences of Revision 81
Test [[ wikisyntax | wiki syntax ]] here :-) ¶¶
[TOC] ¶
¶
Some initial help for creating a campaign. What you should know before reading on: ¶
¶
* [First steps for a scenario](https://wl.widelands.org/docs/wl/tutorial/) ¶
* Make sure to use a plain text editor when writing scripts ¶
* You may also take a look in the scripting files of the implemented campaigns, which you can find in the data directory of you widelands installation. Look into the folder data/campaigns. ¶
¶
To follow this example, create an empty map with only one player set at position 0,0 . To find this position watch the values at the bottom right in the editor. This values represents: (x position, y position, height). Make sure you have set the tribe 'Empire' in the menu 'set player'. ¶
¶
## init.lua ¶
¶
You have read that the file `your_map/scripting/init.lua` is the main entry for your scenario. So this file is the starting point and should contain necessary global settings. This file contains usually different parts: ¶
¶
### Including
¶
~~~~ ¶
include "scripting/coroutine.lua" ¶
include "scripting/messages.lua" ¶
include "scripting/ui.lua" ¶
include "scripting/richtext.lua" ¶
~~~~ ¶
¶
The files which get included here contain functions which you may need in your project. E.g. the file
¶
### Global variables ¶
¶
In this part of the
¶
~~~~ ¶
plr = wl.Game().players[1] ¶
map = wl.Game().map ¶
plr_sf = map.player_slots[1].starting_field ¶
~~~~ ¶
¶
What this code does: ¶
¶
* `plr = wl.Game().players[1]`: This reads the first defined player of the map (the one you have set) and assign it to the variable 'plr'.
* `map = wl.Game().map`: The variable 'map' gives you now access to many properties of the map itself, e.g. to the function `get_field()` in [Map](https://wl.widelands.org/docs/wl/autogen_wl_map/#wl.map.Map). This will be the most used function in your scenario. ¶
* `plr_sf = map.player_slots[1].starting_field`: You have set a player in the map editor on a field, so this assigns this field to the global variable 'plr_sf'. Note that we use here already the variable 'map' which we have defined one line above. Also note that there is a point between 'map', 'player_slots' and 'starting_field'. I tell you later why this is important. ¶
¶
### Including the main control script ¶
¶
Now we are ready with initialization and can work on to get the story alive. To split of initialization and other stuff, we include another file in the last line in our `init.lua` : ¶
¶
~~~~ ¶
include "map:scripting/main_thread.lua" ¶
~~~~ ¶
¶
The file
¶
## main_thread.lua ¶
¶
In this file we create our scenario and add conditions and objectives a player has to fulfill. ¶
¶
The map you have created is empty by default. You have set a player, but no buildings at all. It is our part to place buildings. ¶
¶
### Place buildings ¶
¶
First we place the headquarters for player 1. Do you remember you have set the global variables `plr` and `plr_sf`? Here we use them: ¶
¶
~~~~ ¶
local hq = plr:place_building("empire_headquarters", plr_sf, false, true) ¶
~~~~ ¶
¶
We call the function [place_building()](https://wl.widelands.org/docs/wl/autogen_wl_bases/#wl.bases.PlayerBase) with the needed arguments (the entries in the brackets). The first argument for the function is a string (enclosed in double quotation marks). You could find the cor
¶
Th
¶
__Importand remark about local:__ Whenever you assign variables with values, you should do it this way: `local name_of_variable = value`. The keyword _local_ makes sure that this variable is only valid in this scope (file or function). The only exception: If you really know that you will use this variable elsewhere, you can omit the keyword `local`. But in this case you should give the variable a name which is absolutely clear! ¶
¶
Note the colon between `plr` and the functions name. Do you remember that we had already a point as a separator? Time for a small explanation about the difference between point and colon: ¶
¶
Consider to have an object like a car. This car has a property: color. But this car has also some functions: drive_straight(), drive_left(), drive_right(). If you want to get the color, you want to access the property: `clr = car.color.` If you want to drive the car you need to call the functions: `car:drive_straight(100 meter)`. Do you see the difference? Accessing a property needs a point, calling a function needs a colon! If you go through the documentation you may notice that some entries have brackets, and others not. The entries with brackets are functions, other are properties. Examples: ¶
¶
* Properties (can be accessed through a point): [Player.name](https://wl.widelands.org/docs/wl/autogen_wl_game/#wl.game.Player.name) ¶
* Functions (must be called with a colon): [Player:sees_field(f)](https://wl.widelands.org/docs/wl/autogen_wl_game/#wl.game.Player.sees_field) ¶
¶
Now its time for a first test. Run: ¶
¶
~~~~ ¶
./widelands --scenario=/full/path/to/the/map.wmf ¶
~~~~ ¶
¶
You should see the placed headquarters after loading is complete. Look at the stock... the headquarters is empty, no wares and workers are in it. We should change that. ¶
¶
### Fill buildings ¶
¶
The function [place_building()](https://wl.widelands.org/docs/wl/autogen_wl_bases/#wl.bases.PlayerBase) returns a reference to the created building. We have assigned this reference to the local variable `hq`. ¶
¶
Now we can use this variable and the function [set_wares()](https://wl.widelands.org/docs/wl/autogen_wl_map/#wl.map.HasWares.set_wares) to fill the building: ¶
¶
~~~~ ¶
hq:set_wares("log",10) ¶
hq:set_wares("planks",10) ¶
hq:set_wares("granite",10) ¶
~~~~ ¶
¶
Because setting wares is a common task, there is a shortcut by passing a table to set_wares(): ¶
¶
~~~~ ¶
hq:set_wares({ ¶
log = 10, ¶
planks = 20, ¶
granite = 20, ¶
marble = 5, ¶
}) ¶
~~~~ ¶
¶
Same goes for setting workers and soldiers: ¶
¶
~~~~ ¶
hq:set_workers({ ¶
empire_builder = 5, ¶
empire_carpenter = 2, ¶
empire_lumberjack = 5, ¶
}) ¶
¶
hq:set_soldiers({0,0,0,0}, 45) ¶
~~~~ ¶
¶
Like with the buildings name you will find the right names for wares and workers in the corresponding _'init.lua'_ in the subfolders of _'data/tribes/'_. Add as many wares and workers as you like. After your'e done you should make a test run again. ¶
¶
The build in campaigns have those initial settings in an extra file called _'starting_conditions.lua'_ and included it through _'init.lua'_. This is good habit so create a new file _'starting_conditions.lua'_ ,move the inital settings to that file and include it in _'init.lua'_ one line before the include of the main_thread: ¶
~~~~ ¶
... ¶
include "map:scripting/starting_conditions.lua" ¶
include "map:scripting/main_thread.lua" ¶
~~~~ ¶
¶
### Create a message box ¶
¶
You may have seen that our scenarios have some message boxes to inf
¶
~~~~ ¶
campaign_message_box({title = "This is the title", body = p("Hi, this is my first scenario")}) ¶
~~~~ ¶
¶
We give the function a table (enclosed with curly braces {}). The
¶
File _'texts.lua_': ¶
~~~~ ¶
first_message = { ¶
title = "This is the title", ¶
body = p("Hi, this is my first scenario") ¶
} ¶
~~~~ ¶
¶
File _'main_thread.lua'_: ¶
~~~~ ¶
-- Including tables of text ¶
include "map:scripting/texts.lua" ¶
campaign_message_box(first_message) ¶
~~~~ ¶
¶
The first line in the above example (beginning with two hyphens) is a comment. You should comment your code as mush as possible but not more than needed. When you start this scenario the following will happen: ¶
¶
1. The program looks for a file called _'scripting/init.lua'_ in the map directory. If it is present, load it: ¶
2. Reading _'init.lua'_ from top to bottom. So all functions of included files and global variables are loaded. The last line include our file _'main_thread.lua'_ so this will be read ¶
3. Reading file _'main_thread.lua'_ from top to bottom. Here we include _'texts.lua'_ and all tables will be loaded. Then show the message box. ¶
¶
### Triggering actions ¶
¶
For now we have some basics, but we need some trigger to make the campaign alive. Such triggers are the most important things in campaigns. They offer functionality like: ¶
¶
* Show message boxes when things happens, e.g.: ¶
* Running out of building material ¶
* The player has reached a specific point on the map ¶
* A specific building has been build ¶
* An objective has been solved ¶
* and so on ¶
¶
Usually a trigger is combined with an objective, but creating objectives is part of the next section. In this example we create a trigger if the player reaches a specific point on the map. There are two possibilities to get this work: ¶
¶
* The function [Player:sees_field()](https://wl.widelands.org/docs/wl/autogen_wl_game/#wl.game.Player.sees_field) ¶
* The property [Field.owner](https://wl.widelands.org/docs/wl/autogen_wl_map/#wl.map.Field.owner) ¶
¶
Both possibilities needs a field to work with. So the first thing is to get a field. Fields are part of the map and can be accessed
¶
~~~~ ¶
local f = map:get_field(10,
print("Field: ", f , "Owner: ", f.owner) ¶
~~~~ ¶
¶
If you run the scenario the field and the owner of that field will be printed in the console. What we need further is a mechanism to check if the owner of that field changed. We need a loop that runs the same code over and over until the owner has changed. The problem with such loops is that
¶
~~~~ ¶
function check_field() ¶
local field = map:get_field(10,0) ¶
while true do ¶
print("Field: ", field , "Owner: ", field.owner) ¶
sleep(1000) ¶
end ¶
end ¶
¶
run(check_field) ¶
~~~~ ¶
¶
Now every second the field coordinates and the owner of the field get printed onto the console. The row `sleep(1000)` is __really__ important. If you forget that, this loop prevents every interaction between you and the program itself. The only way to stop it is to kill the process with your taskmanager... Ok, now it's time for some magic: Run your scenario and explore into the direction of the field (to the right). As soon the field is owned by you, the output will change. Why is this magic? The variable _f_ is defined outside of the loop so it is initialized only once. One could think that the fields state (the owner in this case) is also only initialized once, but this is not the case. That is because the variable _f_ is a __reference__ to the field. Whenever this field changes the changes are also reflected in the variable _f_! ¶
¶
Of course this loop is an infinite loop, because it will never end. We change that now by adding a condition to the loop. All we have to do is to compare `field.owner` with our global variable `plr` (which we have set in [Global variables](#global-variables): ¶
~~~~ ¶
function check_field() ¶
local field = map:get_field(10,0) ¶
while field.owner ~= plr do ¶
sleep(1000) ¶
end ¶
campaign_message_box({ title = "Field found", body = p("We have reached the field")}) ¶
end ¶
~~~~ ¶
¶
The condition `field.owner ~= plr` means: field.owner __not equal__ plr. Such a comparison returns either 'true' or 'false'. See also [Rational Operators](https://www.lua.org/pil/3.2.html) for other types. ¶
¶
As soon the message box appears, the loop has been ended. Now, as an exercise, change that function to use [Player:sees_field()](https://wl.widelands.org/docs/wl/autogen_wl_game/#wl.game.Player.sees_field). You have to choose another field though, one which is outside the area the player sees. Move also the table of the message box into the file _'texts.lua'_ and make the corresponding changes. ¶
¶