Lua for unit scripting

Lua for unit scripting

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Lua for unit scripting

Post by imbaczek »

Disclaimer: I'm a noob at scripting anything in spring.

I've downloaded the svn trunk and compiled it fine on XP (one small glitch with streflop.a - needed to rename it to libstreflop.a so ld could see it), but can't find any information about call-lua except for its presence in the source. I wanted to play around with inter-unit communication and figured that instead of messing with C++ source, I could try to do it in Lua.

My plan was to make MAX_MESSAGES sets of unit ids, each set consisting of observer ids to be notified. Then when some message is sent, it's passed to it's destination (or broadcast) only to units which are in the corresponding set. Retrieval of messages would happen in an additional cob thread, eg. like this:

Code: Select all

MessageLoop() {
    while(TRUE) {
        while (get MESSAGES_AVAILIBLE) {
            get MESSAGE_ID
            get MESSAGE_PARAM
            // process messages
        }
        sleep 50;
    }
}
Hooking up to the message would happen by calling e.g.

Code: Select all

get HOOK_MESSAGE (msg_id)
To send a message, something like this would be required:

Code: Select all

get SEND_MESSAGE (msg_id, dest, param1, param2)
where dest == -1 would mean broadcast.

I looked at scriptor and its .cfg is made of fail, if it wasn't, I'd rather do something like

Code: Select all

hook-message msg_id;
unhook-message msg_id;
send-message msg_id, destination, param1, param2;
wait-for-message msg_id;
wait-for-any-message;
and this remembered me that if there's a global Lua namespace and a unit Lua namespace, and if there are ways to pass data from Lua to cob code (and there seem to be), then there would be no need to write any C++ (which is good, since C++ sucks ;p) What scares me is what I have to do to make call-lua work - compiler.cfg really turns me off.

So, finally, the question is - is it feasible to make messages and/or public vars in Lua, or should I start hacking away at Sim/Units/Cob/? (Hope that somebody wants it, because I don't plan to make a mod, just to hack on something interesting :))
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

It is possible, take a look at this thread:
http://spring.clan-sy.com/phpbb/viewtopic.php?t=9329

If there are a lot of messages being passed
around, that it would be better to create a C++ call.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Post by imbaczek »

One problem with your examples - apparently print is a noop under windows - nothing shows up in the console, infolog or anywhere else. Had to do emit-sfx to find out that lua actually works, but I'd like to have a function that prints to infolog. If there's such a thing, please point it out somewhere :)
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

print() can work on Windows with the SVN execs, but you have to
run it through a 'flushing pipe' for it to become visible immediately:

http://www.geocities.com/patchnpuki/tips/index.htm#w5

You can also use Spring.Echo() (which sends to the game console
for all lua scripts, except the start scripts).

P.S. I've cross-compiled the winpipe executable, here it is:
http://trepan.homelinux.net/spring/winpipe.zip
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Post by imbaczek »

Cool.

Here's something I baked after I got hold of some lua and some bos:

http://neoteric.pl/~imbaczek/spring/messages_lua.avi

It's simple - the sheep has its StartMoving() augmented with a

Code: Select all

call-script lua_BroadcastMessage(MSG_GLOW_HAND);
and the Lord has an additional cob thread:

Code: Select all

#include "luamessage.h"

MessageLoop()
{
        var msgid, reacting;

        while (TRUE) {
                reacting = FALSE;
                call-script lua_PeekMessage();
                while (get LUA1) {
                        call-script lua_GetMessage();
                        msgid = get LUA1;
                        if (msgid == MSG_GLOW_HAND && !reacting) {
                                emit-sfx LORD_BUILDER_FX from sprayer;
                                reacting = TRUE;
                        }
        
                        call-script lua_PeekMessage();
                }
                sleep 50;
        }
}
Seems to work fine but someone will have to see if this approach scales (ie. find a use for this ;p) I'll post the lua source after I clean it up, but it's not rocket science.

PS forgot that the lord has to register that it accepts the message - and has to unregister that upon death:

Code: Select all

Create() {
    ...
    call-script lua_HookMessage(MSG_GLOW_HAND);
}

Killed() {
    ....
    call-script lua_UnhookMessage(MSG_GLOW_HAND);
}
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Post by imbaczek »

Here's the source, could use some documentation but I believe it's usable. Should work with the nightly builds.

http://neoteric.pl/~imbaczek/spring/messages_lua.zip

Edit: new version, has fixed message parameters and a demo which uses them (not for much, but still...)

Edit2: almost forgot that it also has public variables included. Anyone interested is invited to check how well they perform.
User avatar
Dragon45
Posts: 2883
Joined: 16 Aug 2004, 04:36

Post by Dragon45 »

Youtube the vid please, I can't view AVIs on these macs here and cant dl the codec for it either :
User avatar
Peet
Malcontent
Posts: 4384
Joined: 27 Feb 2006, 22:04

Post by Peet »

Get VLC >_>
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

Ironically, I have problems watching videos on YouTube. It requires
the flash player, which is not yet supported on AMD64/linux. At least
google video allows one to download the raw video file, haven't found
the same option on YouTube.

P.S. I should probably try the nspluginwrapper trick for flash... or
perhaps take a look at the alternative OSS gnash player
CautionToTheWind
Posts: 272
Joined: 30 May 2006, 17:06

Post by CautionToTheWind »

trepan wrote:I have problems watching videos on YouTube.
(...)
At least google video allows one to download the raw video file
(...)
There are firefox extensions with that capability for youtube.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

Thanks, I picked up the "video downloader" plugin
(and mplayer has no problem playing the flv files)
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Post by imbaczek »

Youtube can take several hours for the video to show up; by that time most of the WIP you see here is gone missing or changed dramatically ^^ When I'll finish my tech demos I'll post on youtube, for now try VLC as someone suggested: http://www.videolan.org/vlc/download-macosx.html - it *should* know how to play back anything you throw at it.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Post by zwzsg »

Youtube & google.video low resolution can severly butcher the prettyness of a video. And it ain't easy to see youtube video fullscreen. So as long as you use a fairly standard codec and have your own hosting, I prefer being linked to an avi than to youtube.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

most videos wotn laod on youtube in fullscreen, I have to close the tab and reload the original page and watch in normal size.
Post Reply

Return to “Engine”