Lua Based Missions

Moderators: GSH, VSMIT, Red Devil, Commando

Post Reply
User avatar
Nielk1
Flying Mauler
Posts: 2991
Joined: Fri Feb 18, 2011 10:35 pm
Contact:

Lua Based Missions

Post by Nielk1 »

I worked on a script about 1.5 years ago to run LUA for mission scripts.

I recently started working on this again. Past 2 days. It now "works". Note that the word "works" is in scare quotes for a reason. The load code still needs complex type enumeration re-alignment and I've got to test every function and look for crashes.

I need some people who are willing to try to test this. There is a lot of work to do, and it's buggy as hell. It can be finished, but it needs lots of testing.

Work remaining:
  1. Test all functions
  2. Design scope/module system (current implementation is just workable)
  3. Redo hook system to handel return values better. Right now it has Call or CallAll where All preserves all returns and Call stops working after the first. This was good for a hook system laying ontop of a centralized script, but I've used this as part of the core, so it doesn't work outright.
  4. Fix Save/Load
Example of a LUA mission script:

Code: Select all

local _G = _G;
local hook = hook;

_G.PrintConsoleMessage("Loading GameMode.InstantAction");
module( "InstantAction" )

_G.MissionData.Text = "STRING";

hook.Add( "Update", "Chilli_Custom_Update", function( GameTurn )
  if ( _G.MissionData.TargetTest == nil
     or not _G.MissionData.TargetTest:IsAround() ) then
    SpawnBox();
  end

  if GameTurn % 10 == 0 then
    if _G.MissionData.TargetTestIsObjectified then
      _G.MissionData.TargetTest:SetObjectiveOff();
    else
      _G.MissionData.TargetTest:SetObjectiveOn();
    end
    _G.MissionData.TargetTestIsObjectified = not _G.MissionData.TargetTestIsObjectified;
  end
end )

function SpawnBox()
  local player = _G.GetPlayerHandle(1);
  local PlayerLocation = player:GetPositionV();
  local vectorLoc = _G.SetCircularPos(PlayerLocation, _G.GetRandomFloat(10)+20, _G.GetRandomFloat(6.28318530718));
  _G.MissionData.TargetTest = _G.BuildObject("ibcrat00", 2, vectorLoc);
  _G.MissionData.TargetTestIsObjectified = false;
end

hook.Add( "InitialSetup", "Chilli_Custom_InitialSetup", function()
  SpawnBox();
end )

_G.PrintConsoleMessage("Loadinged GameMode.InstantAction");
This script simply spawns a crate and toggles it's objective on and off. This crate will instantly respawn if it is removed.

Image

So, I need testers and people willing to help code this.
The entire code base is here: https://github.com/Nielk1/BZ2-LuaMission
The code _api.lua is over 5200 lines.

I can generate some documentation once I've got some people willing to help. I really need people who are willing to learn the LUA side or who know enough about the C side to help me resolve some of the bigger issues. I do all my coding for work in C# so my C is very rusty.
User avatar
Red Devil
Recycler
Posts: 4398
Joined: Fri Feb 18, 2011 5:10 pm
Location: High in the Rocky Mountains

Re: Lua Based Missions

Post by Red Devil »

i'll see what i can do. just got home from trip last night and tired, plus i need to put up 80 feet of shelving, plus a bunch of other house and bz2 stuff.
If given the truth, the people can be depended upon to meet any national crisis. The great point is to bring them the real facts - and beer.
Abraham Lincoln

Battlestrat, FE, G66, In The Shadows, Starfleet, Uler, & ZTV

Lifetime member of JBS and NRA
User avatar
Nielk1
Flying Mauler
Posts: 2991
Joined: Fri Feb 18, 2011 10:35 pm
Contact:

Re: Lua Based Missions

Post by Nielk1 »

I know the save code is currently broken for complex types because the enumeration isn't re-mapped when the type array is loaded. I plan to fix that later though as first I need to track down a few random AVs I've been getting that appear to be connected either to the C side or the luajit FFI bindings.


The current todo list as I see it:
  1. Test functions
  2. Implement any missing functions based on latest public patch
  3. Resolve AVs from C side or luajit FFI code
  4. Fix complex type load
  5. Finalize scoping and hooking model
  6. Replace LUA side read/write functions for saving/loading with C side buffed functions to reduce SAV size due to BZ2's padding
  7. Investigate use of compression in SAV data, determine if MP handles compression itself or if gzipping the data stream would reduce footprint in MP
Also, I'm curious if my instructions on using the project are clear enough so let me know.

The hope is, of course, that using the system to write scripts should expose the flaws in the system.
Commando
Flying Mauler
Posts: 2176
Joined: Fri Feb 18, 2011 6:41 pm

Re: Lua Based Missions

Post by Commando »

How much work would it take to create a fully working MPI port of the dll?
User avatar
Nielk1
Flying Mauler
Posts: 2991
Joined: Fri Feb 18, 2011 10:35 pm
Contact:

Re: Lua Based Missions

Post by Nielk1 »

Commando wrote:How much work would it take to create a fully working MPI port of the dll?
Still a lot.

I can however try to do a port to use as a method to discover issues in the underlying framework.
Commando
Flying Mauler
Posts: 2176
Joined: Fri Feb 18, 2011 6:41 pm

Re: Lua Based Missions

Post by Commando »

That was my line of thinking.
User avatar
Red Devil
Recycler
Posts: 4398
Joined: Fri Feb 18, 2011 5:10 pm
Location: High in the Rocky Mountains

Re: Lua Based Missions

Post by Red Devil »

i'm thinking that a test might not have to actually do anything except run all the functions and spit out responses to the console log with expected results and results because not all the dll's use all the callbacks.
If given the truth, the people can be depended upon to meet any national crisis. The great point is to bring them the real facts - and beer.
Abraham Lincoln

Battlestrat, FE, G66, In The Shadows, Starfleet, Uler, & ZTV

Lifetime member of JBS and NRA
User avatar
Nielk1
Flying Mauler
Posts: 2991
Joined: Fri Feb 18, 2011 10:35 pm
Contact:

Re: Lua Based Missions

Post by Nielk1 »

Red Devil wrote:i'm thinking that a test might not have to actually do anything except run all the functions and spit out responses to the console log with expected results and results because not all the dll's use all the callbacks.
One thing I badly need is the functions validated, yes.

The design I went for was full OOP. Since so few people code BZ2 missions there wasn't a big need to match BZ2's C++ style. For this reason you will see a lot of concepts of objects. Like you tell a GameObject to do something, you don't tell the world to have a GameObject do something.

I am still doing a lot of changes to the code now, I am looking at trying to wrap each part into it's own module to improve maintainability and scoping.
Commando
Flying Mauler
Posts: 2176
Joined: Fri Feb 18, 2011 6:41 pm

Re: Lua Based Missions

Post by Commando »

Will the dll be able to execute code within an aip? Basically i think it would be useful to be able to use SetLabel in build plans and then reference labels using GetLabel in attacker, defender, and base build plans.
User avatar
Nielk1
Flying Mauler
Posts: 2991
Joined: Fri Feb 18, 2011 10:35 pm
Contact:

Re: Lua Based Missions

Post by Nielk1 »

Commando wrote:Will the dll be able to execute code within an aip? Basically i think it would be useful to be able to use SetLabel in build plans and then reference labels using GetLabel in attacker, defender, and base build plans.
Sadly no, aside from the fact I used a different type of LUA than GSH did (though he could switch his customized fastcall lua51 dll out for a cdecl luajit dll, if not for the whole fastcall customization it could be swapped out with just the different DLL), GSH would have to create some sort of interconnect.

I pursued a lot of different methods to doing this and ended up using LuaJIT. As a result there are no ScriptUtils bindings for GSH to simply drop into the system to give the API lua access to ScriptUtils either. If he was to use LuaJit for the AIPs then the ffi block of my _api.lua file could be used to provide these bindings. (I would also no longer need to worry about loading the luajit dll or custom compiling it to change it's name/signiture so it doesn't conflict with stock's lua51.dll.) I am currently working on modularizing the _api.lua into multiple files, so the ffi bindings will probably get their own module.

I am personally of the feeling that the best path might be to give full AIP control over to the mission DLL in some form rather than trying to merge the DLL's ScriptUtils with the AIPs, but one is obviously far easier to do than the other.

TLDR: If GSH was to switch from his custom fastcall lua51 to stock cdecl luajit then I can isolate the FFI section of my _api.lua for use with AIP lua. I don't think he's sandboxed the functions at all, I know I haven't with LUAJIT. My implementation is actually scary powerful in that you can load external DLLs, but it's no more dangerous than C++ aside from the fact it might be easier to use in some respects. This is why I decided against making scripts download when you join a server, even though that would have been theoretically possible.
Post Reply