lua bzfsAPI plugin

Questions, comments, and news on the server side plug-ins and it's API
Post Reply
trepan
Dev Wizard
Dev Wizard
Posts: 704
Joined: Fri Feb 21, 2003 7:50 pm

lua bzfsAPI plugin

Post by trepan »

For those that don't track SVN commits, I've added a lua plugin to the SVN sources.

There are several advantages to using scripting languages (lua, javascript, python, etc...),
for plugins instead of using the raw C++ interface.

1. no need to compile (or have bzfsAPI dev sources)
2. usually much easier to dev / debug
3. an extra layer of OS independence (works anywhere)
4. with recent bzfs changes, can be easily embedded in world files, ex (in test.bzw):

Code: Select all

options
  -loadplugin libbzfslua.so,dieHard,$HOME/bzfslua/bzfs.lua
end

# block comments
lua
  BZ.AttachMapObject('/*', '*/', function() end)
  BZ.AttachMapObject('-/*', '*/', function(name, data) return data end)
endlua

/*
woot,
a multiline comment
thanks to lua
*/

box
  size 10 10 10
end

lua
  local lines = {}
  for x = -100, 100, 10 do
    for y = -100, 100, 10 do
      table.insert(lines, 'box')
      table.insert(lines, 'pos " .. x .. ' ' .. y .. ' ' .. 40)
      table.insert(lines, 'size 3 3 31)
      table.insert(lines, 'end')
    end
  end
  return lines
endlua
NOTE: what isn't show here is that you can also add dynamic scripts in the bzw
file (ex: Airspawn and the like are very easy to build into the world file).

Using a scripting language also has its disadvantages:

1. probably isn't as fast as raw C++
2. might not have all of the C++ interface features implemented
3. might not be able to link to external libraries easily
(although the lua plugin can link to external dlls, but they should
be compiled against the bzflag lua sources)


Lua has a particular advantage in that it might also be used for client-side scripting
(I've got a large portion of the work already done, having started with the work that
I did for the Spring RTS project). If / when I commit, the post for that will have
cooler screenshots than this post ;-)
User avatar
optic delusion
Special Forces
Special Forces
Posts: 1052
Joined: Sat Sep 25, 2004 2:29 pm
Location: Planet MoFo
Contact:

Re: lua bzfsAPI plugin

Post by optic delusion »

This is the biggest thing to happen to bzw, since the invention of bzw.
Thanks for the super-simple example world, Trepan. That's exactly what we need. It's the first steps that are the hardest.
Take a look at my Defender game mode concept.

Thinking is not an automatic process. A man can choose to think or to let his mind stagnate, or he can choose actively to turn against his intelligence, to evade his knowledge, to subvert his reason. If he refuses to think, he courts disaster: he cannot with impunity reject his means of perceiving reality.
trepan
Dev Wizard
Dev Wizard
Posts: 704
Joined: Fri Feb 21, 2003 7:50 pm

Re: lua bzfsAPI plugin

Post by trepan »

For more examples of what lua plugins will look like compared to C++ ones,
take a look in the following directory:

http://bzflag.svn.sourceforge.net/viewv ... e/plugins/

Those plugins are "luafied" versions of C++ plugins which already exist. Instead of having
to load each plugin individually via a command line, I've continued what I started with Spring
and set it up so that a directory is scanned for lua module files, and then the code is loaded.
They will require the completion of 'modules.lua' before being useable by the general public.

Note that the LuaWorld and LuaUser client-side modules would look much the same.

As for this being the biggest thing to happen to bzw, I disagree. I consider the addition of
meshes and custom texturing to be more important for the reason that they allow changes
that would otherwise not be possible client-side. BZFS plugins are great for making server
modifications, but they could also be done by editting bzfs to suit your needs (or by
applying a patch).

And to give credit where credit is due:
- JeffM made bzfs plugins possible
- JeffM added bzfs plugin based custom map objects

P.S. Custom bzw objects are a headache for external editors, keep that in mind ;-)

P.P.S. The lua bzfs plugin could easily be migrated into the bzfs exec, and that is something
that I've considered (both for ease of use, and for execution speed). The fact that the lua
library is committed to bzflag's SVN make that much easier to do than to require an external
library dependency.
F687/s
Private First Class
Private First Class
Posts: 369
Joined: Sun Dec 31, 2006 8:30 pm

Re: lua bzfsAPI plugin

Post by F687/s »

Wow, very nice. I've heard about the inclusion of Lua into trunk, but haven't heard much about implementation. Hopefully this will create some new plugin developers (that are currently afraid of C++).

Some random questions (related to the plugin):

* You say that Lua can now be included into ordinary .bzw files. Do the BZW files need a preliminary declaration (such as a "loadplugin" or BZFS command line option), or can they be seamlessly integrated into the file? I don't know, the "-loadplugin libbzfslua.so,dieHard,$HOME/bzfslua/bzfs.lua" declaration looked kinda vague...

* I assume that, even though Lua is included into BZW, that still doesn't make the files dynamic? Like, you couldn't make a moving box or anything? (Minds well ask...)

* Finally, are the Lua parts in the .bzw file interpreted by the client or the server? Or: Is Lua basically acting as a .bzw preprocessor (plus more)?

Anyway, it's good to see that we're all one step closer to 3.0...
trepan
Dev Wizard
Dev Wizard
Posts: 704
Joined: Fri Feb 21, 2003 7:50 pm

Re: lua bzfsAPI plugin

Post by trepan »

BZFS plugins are entirely server-side. That should answer most of your questions.
trepan
Dev Wizard
Dev Wizard
Posts: 704
Joined: Fri Feb 21, 2003 7:50 pm

Re: lua bzfsAPI plugin

Post by trepan »

The server-side lua plugin was merged into the bzfs executable in SVN r19215.
That means that you can count on having server-side lua plugins regardless of
the build configuration (it also helps to speed up some of the callouts, and makes
it easier to share code with the client-side lua libraries).
Post Reply