October 24 2000
|
This document describes how to install, load, and unload ShipWars Server plugins and how to program your own plugins. Please note, currently only the Linux version of the ShipWars Server supports plugins. |
Once you have obtained a plugin source (either from a third party or one of the sample plugins that came with the ShipWars Server), compile it and install it in acorrdance with the instructions provided by the third party.
Typically, you would first build the plugins by running make
and then install them by running make install. This should
place the compiled plugin binary in /home/swserv/plugins
or possibly an alternate location if your ShipWars Server home directory
is in a different location.
Make sure that you change the prefix install location in the Makefile or configure script as needed before installing.
There are two ways to load plugins:
Edit your ShipWars Server configuration file (usually located in
/home/swserv/etc) and add the following at the end
of the configuration file (note newer configuration files have
a section marked off for plugins, put it there if you find
such a section):
BeginPlugin
Path = myplugin
Flags =
Arguments = -autostart
EndPlugin
Path
specifies the path and file name of the plugin, if you do not specify an
absolute path (a path starting with a / character) then the value
contained in the parameter PluginsDir will be prefixed to the
value you specify for Path.
The Flags
parameter should be left blank for now, currently there are no settable flags.
Arguments contains a space seperated list of arguments you
want to pass to the plugin upon startup. Note that each argument may not
contain spaces or else it will be considered as two arguments. The argument
-autostart must be specified as the first
argument for any plugin designed to be loaded at startup.
Once you have added the plugins you want to load at startup, run the ShipWars Server as you normally do. Take note of any error messages during startup. When you shutdown the server, all plugins will be automatically unloaded (in reverse order from which they were loaded) before the server begins its standard shutdown sequence.
To load a plugin when the ShipWars Server is currently running, use any ShipWars client capable of issuing server commands to connect to the universe in question. When connected, send the server command:
plugin
plugin command.
Next make sure your vessel has a sufficient access level (access level
of 0 is required on most universes to use the plugin command),
then type:
plugin l myplugin arg1 arg2 arg3 arg4...
myplugin is the file name of the plugin you want to load.
If the file name specified is not an absolute path
(a path starting with a / character), then the plugins
directory will be prefixed to the file name that you specified.
Any arguments that follow will be passed to the plugin when it is loaded.
Plugins are automatically unloaded when:
plugin. First to see a listing of plugins
currently loaded, type:
plugin
plugin u 12345
12345 should be replaced with the ID of the plugin
you want to unload.
To begin writing your own plugins, you must first make sure you have the following:
The ShipWars Server source comes with a sample plugin called
hellouniverse. It is located in
xsw#.#/server/plugins/samples, go to that directory and glance
through hellouniverse.c. Get a general idea of it (don't worry
about not being able to fully understand it right away).
Take note of the three primary functions, each of which is stated using #defines:
SWPLUGIN_INIT_FUNCRTN
SWPLUGIN_INIT_FUNC(SWPLUGIN_INIT_FUNCINPUT)
SWPLUGIN_MANAGE_FUNCRTN
SWPLUGIN_MANAGE_FUNC(SWPLUGIN_MANAGE_FUNCINPUT)
SWPLUGIN_SHUTDOWN_FUNCRTN
SWPLUGIN_SHUTDOWN_FUNC(SWPLUGIN_SHUTDOWN_FUNCINPUT)
xsw#.#/server/plugins.h. Note that
xsw#.#/server/plugins/include/plugins.h is what your
plugins need to #include, but this header file just basically
#includes xsw#.#/server/plugins.h.
The golden rule is that each ShipWars Plugin needs three basic functions:
Compiling a plugin is the same as compiling a regular shared library
under Linux. Simply put -shared in your compiler command.
For example, to compile the hellouniverse program you would type:
cc hellouniverse.c -o hellouniverse -shared
/home/swserv/plugins.
You should recompile your plugins whenever you upgrade to a newer ShipWars Server version, just in case there is a change in the prototype declarations or structure members.
ShipWars Plugins follow the library procedural language, which basically says there needs to be three functions:
The initialization function (prototyped as
SWPLUGIN_INIT_FUNCRTN SWPLUGIN_INIT_FUNC(SWPLUGIN_INIT_FUNCINPUT))
is the very first function that is executed when the plugin is loaded.
This is your chance to allocate resources and set up global variables
for the very first time. If this function returns 0, then the ShipWars Server
will consider that this plugin was initialized properly. If this function
returns -1, then the ShipWars server will abort the loading of this plugin
and consider that an error has occured.
The management function (prototyped as
SWPLUGIN_MANAGE_FUNCRTN SWPLUGIN_MANAGE_FUNC(SWPLUGIN_MANAGE_FUNCINPUT))
is called once per cycle (a cycle is each time the ShipWars Server
loops), so this function is called very frequently. In this function,
your plugin should perform routine tasks, such as checking it's reousrces
for changed and see if it's time to perform schedualed activities
(see the timming example for more information on
schedualing and timming). If this function returns 0, then it will be called
again on the next cycle. If this function returns -1, then the plugin
will be unloaded.
The
SWPLUGIN_SHUTDOWN_FUNCRTN SWPLUGIN_SHUTDOWN_FUNC(SWPLUGIN_SHUTDOWN_FUNCINPUT))
is called just before this plugin is unloaded. This is your last chance
to deallocate any resources (free memory) that this plugin has allocated.
This function returns void.
Timming in ShipWars is critically important, so here are some properties of timming you should be aware of:
Time compensation should be used in most physics calculations
to keep things in sync. If the server, your program, or the comptuer
starts to "lag" then common sense tells you that your time based calculations
will be inaccurate. For instance if you have a speed calculating code
speed = miles / time;, if things start to lag then the value
of speed may become inaccurate. So to compensate for this you will need to
calculate it as speed = miles / time * time_compensation;.
Lapsed time is the amount of time (in milliseconds) that was lost in the cycle due to lag. A theoretical cycle in the ShipWars system (without the interferance with lag) is 16 milliseconds. If there was no lag then the lapsed time would be 0 milliseconds, but in the real world it is not always the case so you should check the lapsed time if it is applicateable in your calculation.
Current time indicates the current time since midnight in milliseconds. This value is used very often, espessially in schedualing.
Schedualing specifies the next time to perform an action, this is used fairly often but difficult to explain at this point. There will be many examples demostrating schedualing below.
To introduce the beginner to timming smooth and gradually,
we have an example source called timming.c.
Look in the ShipWars Server source
in the directory xsw#.#/server/plugins/samples/timming.c.
This sample source demostrates how to obtain time values and
to perform schedualing. It prints a list of jokes and answers to each
joke at specified intervals. Read through the timming.c
source carefully and note how current time is fetched and checked against
schedualed `next' times. Also note the need to reset timmers when
the current time has cycled.
|
The ShipWars Server allows access to its global variables and functions
to your plugin by way of the plugin_data_ref_struct structure
(see xsw#.#/server/plugins.h for this prototype).
As of version 1.33d, the plugin_data_ref_struct structure
contains:
|
This structure contains a list of pointers to global variables and functions that your plugin is allow to access. See the timming example for some examples on how to obtain these pointers and their pointed to values.
A pointer to the plugin_data_ref_struct structure is passed to
every one of your Plugin function that the ShipWars Server calls.
Be forwarned that the address values in the
plugin_data_ref_struct
structure may change during execution, so your plugin functions should get the
latest values at the beginning of the init, manage, and shutdown functions.
Basic manipulation of objects is demostrated in
an example called moveobj.c (which is also
available from the ShipWars Server source at
xsw#.#/server/plugins/samples/moveobj.c).
This example demostrates how to obtain the pointer to the objects list for the currently loaded universe, check certain values of each object in the list and manipulate its values (move the object in this example).
|
All timming was done in units of milliseconds, positions of objects
were in Real units. If you are not sure about units in the
ShipWars system, please read the
building documents
or consult
someone familiar with universe building. Available XSW_OBJ_TYPE_*
values are defined in xsw#.#/include/objects.h.
Any question related to coding for ShipWars Server plugins can be asked on the ShipWars mailing list, there you can find many experianced coders who are intimatly familiar with coding such plugins.