Lua modding prototype

I’ve completed a very barebone prototype that demonstrates how lua could be used to define iuse’s.

https://github.com/CIB/Cataclysm-DDA/commit/5c2ff27dadf92d05403a1bcbff203c667319ae9c

Example:

data/luamods/core/main.lua

-- Main mod file that will be executed by the game
-- at initialization time.

function hiccup(player, item, active)
    game.add_msg("You hiccup.")
end

game.register_iuse("HICCUP", hiccup)

data/raw/items/tools.json

  { "id":"lighter",
    "type": "TOOL",
    "symbol": ",",
    "color": "blue",
    "name": "lighter",
    "description": "A lighter must be carried to use various drugs, like cigarettes, or to light things like molotov cocktails.  You can also use a lighter to light nearby items on fire.",
    "rarity": 60,
    "price": 35,
    "material": ["plastic","iron"],
    "weight": 0,
    "volume": 0,
    "bashing": 0,
    "cutting": 0,
    "to_hit": 0,
    "max_charges": 100,
    "initial_charges": 100,
    "charges_per_use": 1,
    "turns_per_charge": 0,
    "ammo": "none",
    "revert_to": "null",
    "use_action": "HICCUP"
  },

Now the lighter prints a hiccup message.


This is, as I said, still very bare-bones. There’s no way to access anything but game.add_msg(). Even the arguments to the iuse functions are currently nil, as I haven’t wrapped the player or item class in a lua object yet.

I’m not using any automatic binding generator. I’ve looked at tolua++, luabind and luna-gen, but all of them seemed too heavy weight to me or otherwise didn’t compile. All the bindings at the moment are hand-grafted, which doesn’t work too badly, though I suppose when it comes to wrapping accessors like item.name it will get annoying fast.

On the upside extra dependencies are thus minimal. On unix-like systems, lua 5.1 is easy to install. On other platforms, all that’s needed is downloading the lua source distribution and compiling it(lua is extremely portable, as it’s pure ANSI C).

Feedback welcome.

http://www.youtube.com/watch?v=PiMRb4-_Umw Some footage.

This is very, very cool.

It would allow us to expand the scope of the worldbuilder a bit and allow far more customization if people could define their own item actions, even if it’s only to a limited extent. I will be following this eagerly.

I’ve wrapped just a bit more player functionality and added a “tick” callback that will be called on every turn.

local oxygen = 100 -- Will be called every game tick. function game.tick() -- Use up oxygen oxygen = oxygen - 3
-- Breathe
if player.underwater then
    -- Do nothing, can't breathe
elseif player:has_disease("smoke") then
    if game.rng(1, 10) < 3 then
        oxygen = oxygen + 3
    end
else
    oxygen = oxygen + 4
    if oxygen > 100 then
        oxygen = 100
    end
end

-- If we're low on oxygen alert the player.
if oxygen < 10 then
    if game.rng(1, 10) == 1 then
        game.add_msg("You're choking to death!")
    end
    player.pain = player.pain + 5
elseif oxygen < 50 then
    if game.rng(1, 10) == 1 then
        game.add_msg("There's no air, you're going to suffocate!")
    end
elseif oxygen < 90 then
    if game.rng(1, 10) == 10 then
        game.add_msg("You can hardly breathe!")
    end
end

end

Just to demonstrate that you can do rather complex things with this. Of course, storing the oxygen in a local is bad style, as it won’t be saved. I plan to have a “catajson player::json_data” variable, which will provide a way to easily add variables in lua to the player that will be saved automatically, while still remaining accessible to C++.

1 Like

Supporting this wholeheartedly.

Awesome. I’ve been waiting for something like this.

this is why i wanna learn programming languages C++, LUA, and Ruby are on the top of my list :stuck_out_tongue: tho LUA and Ruby are easier to read xD

Some more progress lately, and (maybe) something actually useful:

Still all WIP, but definitely getting to the point now where I can add useful things with lua.