[suggestion] Community LUA Library

Take command of air and naval assets from post-WW2 to the near future in tactical and operational scale, complete with historical and hypothetical scenarios and an integrated scenario editor.

Moderator: MOD_Command

Post Reply
User avatar
snowburn
Posts: 188
Joined: Mon Sep 23, 2013 9:10 pm
Location: Bovril, Argentina

[suggestion] Community LUA Library

Post by snowburn »

Hello!!

i've downloaded v1.06 and its a giant step forward, congratulation to all the devs :)
i think its time for us (the players) to create a lua library which the most used functions to simplify scenario creation.

for example:

How to assign a possibly dead unit to a mission.
Author: Baloogan
function AddToMission(side_name,unit_name,mission_name)
local unit = ScenEdit_GetUnit({side=side_name,name=unit_name})
if unit ~= nil then
unit.mission = mission_name
end
end



This is a library for making using GetKeyValue and SetKeyValue easier. It allows you to use persisted values through a simple tablelike interface
Author: ckfinite
KeyStore = {
keys = {
},
demarshallers = {
prim_str = function(strval) return strval end,
prim_num = function(strval) return tonumber(strval) end,
prim_nil = function(strval) return nil end,
prim_bool= function(strval) return strval=="true" end,
prim_func= function(strval) return nil end,
prim_tab = function(strval) return nil end
}
}

--metatable
local persister = {}

local function contains(t, e)
for i = 1,#t do
if t == e then return true end
end
return false
end

function persister.__index(table, key)
local result = ScenEdit_GetKeyValue(key)

if result == nil then
return nil
end

if (string.sub(result,1,1) == ":") then
--unmarshalled
return string.sub(result,2)
else
local slen,val = string.match(result,"([0-9]+):(.*)")
local nlen = tonumber(slen)
local demarshal_name = string.sub(val,1,nlen)
local demarshaller = table.demarshallers[demarshal_name]
return demarshaller(string.sub(val,nlen+1))
end
end

function persister.__newindex(_table,key,value)
function marshal(value)
local mt = getmetatable(value)
local name,marshalled = nil,nil
if mt == nil or (mt ~= nil and mt.__marshal == nil) then
if type(value) == "string" then
name,marshalled = "prim_str",value
elseif type(value) == "number" then
name,marshalled = "prim_num",tostring(value)
elseif type(value) == "nil" then
name,marshalled = "prim_nil",""
elseif type(value) == "boolean" then
name,marshalled = "prim_bool",tostring(value)
elseif type(value) == "function" then
name,marshalled = "prim_func","" --TODO
elseif type(value) == "table" then
name,marshalled = "prim_tab","" --TODO
else
name,marshalled = "prim_str",tostring(value) --default to str
end
end

if mt ~= nil and mt.__marshal ~= nil then
name,marshalled = mt.__marshal(value)
name = "mrsh_" .. name
end
return string.len(name) .. ":" .. name .. marshalled
end

if contains(_table.keys,key) then
ScenEdit_SetKeyValue(key,marshal(value))
else
table.insert(_table.keys, key)
ScenEdit_SetKeyValue(key,marshal(value))
end
end

function persister.__call(table, key, demarshaller)
table.demarshallers["mrsh_" .. key] = demarshaller
end

setmetatable(KeyStore, persister)
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: [suggestion] Community LUA Library

Post by ckfinite »

There's moves afoot to make a package manager-like system. For now, though, I'll be posting my work on Baloogan's forum and on a to-be-created Github repository, which would be a better way to connect to it, especially because that library code has a nasty bug in it.
User avatar
snowburn
Posts: 188
Joined: Mon Sep 23, 2013 9:10 pm
Location: Bovril, Argentina

RE: [suggestion] Community LUA Library

Post by snowburn »

a Github repository is an excellent idea
Post Reply

Return to “Command: Modern Operations series”