Warzone 2100 Resurrection

Lua

From WarzoneWiki

Jump to: navigation, search

The Lua branch aims to convert all uses of WZScript to Lua. Lua is a powerful scripting language that is especially designed to be easy to embed in an application.

Contents

Current Status

  • The infrastructure is in place, and is only slightly hackish at certain points.
  • The first few levels of the campaign are fully playable.
  • Saving and loading works.

Most work currently lies in converting all script functions, which can be called from scripts, from the WZScript interface to Lua.

FAQ

What happens on luaL_error()?

luaL_error will throw a Lua error. This error will propagate back through Lua code untill it is catched or it reaches C code again. The C code will print the error message and a backtrace to strerr.

Note: This function does not return!

Does Lua have any preprocessor?

No, Lua does not come with a preprocessor. However, you can easily define and pass functions to provide pieces of custom code, or use loadstring to eval a string.

Architecture

Events

There are two types of events:

  • The pure Lua repeating, delayed and conditional events, registerd by data/script/event.lua:conditionalEvent or repeatingEvent or delayedEvent
  • The C to Lua callback event registered using data/script/event.lua:callbackEvent

The Lua events will be evaluated every tick, and when it is time to fire the trigger the associated function is called. To prevent an event handler that called pause to be active multiple times, an event handler is deactivated when called, and reactivated after it returns.

When something happens in C code that the scripts could be interested about, the function src/scriptcb.c:eventFireCallbackTrigger is called. This function will call the Lua function data/script/event.lua:doCallbacksFor with as parameters the parameters for the callback functions. doCallbacksFor will checks its list of registered callbacks and invoke all callback functions that are interested in this event.

Game Objects

Game objects like buildings and droids are stored in Lua as Lua objects with a type and an id pointing to the object itself. Various fields, like player, x, y and z are stored in the Lua object but not actually updated when the object moves in the game. The various functions in src/scriptobj.c (near the bottom) provide ways to get the original object back from the Lua stack.

Saving & Loading

Saving and loading is achieved using data/script/save.lua. This script uses a lot of magic to find all Lua objects that were created after it was loaded itself. Some more magic causes it to find a name to give to these objects. Functions itself will not be saved but be referenced to by name. Groups are a reference to C group objects, and these are first converted to a list of droid IDs. The output of this whole procedure is a Lua script, that when run will restore the script state.

On loading the scripts comprising the level are loaded first, and then the saved Lua file is executed.

Retrieved from "http://wiki.wz2100.net/Lua"