From 58dc2559aad5abb3ae23f89155a4cdf37e0de885 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Wed, 29 Aug 2012 22:04:07 +0100 Subject: [PATCH] Implement retrieval of element properties --- src/cat/LuaScriptInterface.cpp | 78 +++++++++++++++++++++++++++-- src/cat/LuaScriptInterface.h | 1 + src/simulation/StructProperty.h | 2 +- src/simulation/elements/Element.cpp | 38 ++++++++++++++ src/simulation/elements/Element.h | 5 ++ 5 files changed, 119 insertions(+), 5 deletions(-) diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 50e27358e..8a01fc0c8 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -409,6 +409,7 @@ void LuaScriptInterface::initElementsAPI() //Methods struct luaL_reg elementsAPIMethods [] = { {"allocate", elements_allocate}, + {"element", elements_element}, {"free", elements_free}, {NULL, NULL} }; @@ -501,6 +502,66 @@ int LuaScriptInterface::elements_allocate(lua_State * l) return 1; } +int LuaScriptInterface::elements_element(lua_State * l) +{ + int args = lua_gettop(l); + int id; + luaL_checktype(l, 1, LUA_TNUMBER); + id = lua_tointeger(l, 1); + + if(id < 0 || id >= PT_NUM || !luacon_sim->elements[id].Enabled) + return luaL_error(l, "Invalid element"); + + if(args > 1) + { + //Write values from fields in the table + return 0; + } + else + { + std::vector properties = Element::GetProperties(); + //Write values from native data to a table + lua_newtable(l); + for(std::vector::iterator iter = properties.begin(), end = properties.end(); iter != end; ++iter) + { + intptr_t offset = (*iter).Offset; + switch((*iter).Type) + { + case StructProperty::ParticleType: + case StructProperty::Integer: + lua_pushinteger(l, *((int*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::UInteger: + lua_pushinteger(l, *((unsigned int*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::Float: + lua_pushnumber(l, *((float*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::Char: + lua_pushinteger(l, *((char*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::UChar: + lua_pushinteger(l, *((unsigned char*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::String: + lua_pushstring(l, *((char**)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::Colour: +#if PIXELSIZE == 4 + lua_pushinteger(l, *((unsigned int*)(((unsigned char*)&luacon_sim->elements[id])+offset))); +#else + lua_pushinteger(l, *((unsigned short*)(((unsigned char*)&luacon_sim->elements[id])+offset))); +#endif + break; + default: + lua_pushnil(l); + } + lua_setfield(l, -2, (*iter).Name.c_str()); + } + return 1; + } +} + int LuaScriptInterface::elements_free(lua_State * l) { int id; @@ -1417,10 +1478,19 @@ int luatpt_setconsole(lua_State* l) int luatpt_log(lua_State* l) { - if((*luacon_currentCommand) && !(*luacon_lastError).length()) - (*luacon_lastError) = luaL_optstring(l, 1, ""); - else - luacon_ci->Log(CommandInterface::LogNotice, luaL_optstring(l, 1, "")); + int args = lua_gettop(l); + for(int i = 1; i <= args; i++) + { + if((*luacon_currentCommand)) + { + if(!(*luacon_lastError).length()) + (*luacon_lastError) = luaL_optstring(l, i, ""); + else + (*luacon_lastError) += ", " + std::string(luaL_optstring(l, i, "")); + } + else + luacon_ci->Log(CommandInterface::LogNotice, luaL_optstring(l, i, "")); + } return 0; } diff --git a/src/cat/LuaScriptInterface.h b/src/cat/LuaScriptInterface.h index 9ae995a5a..e40ca2d56 100644 --- a/src/cat/LuaScriptInterface.h +++ b/src/cat/LuaScriptInterface.h @@ -49,6 +49,7 @@ class LuaScriptInterface: public CommandInterface { //Elements void initElementsAPI(); static int elements_allocate(lua_State * l); + static int elements_element(lua_State * l); static int elements_free(lua_State * l); public: lua_State *l; diff --git a/src/simulation/StructProperty.h b/src/simulation/StructProperty.h index 832e83be5..2458f41cb 100644 --- a/src/simulation/StructProperty.h +++ b/src/simulation/StructProperty.h @@ -13,7 +13,7 @@ struct StructProperty { - enum PropertyType { ParticleType, Colour, Integer, UInteger, Float, String }; + enum PropertyType { ParticleType, Colour, Integer, UInteger, Float, String, Char, UChar }; std::string Name; PropertyType Type; intptr_t Offset; diff --git a/src/simulation/elements/Element.cpp b/src/simulation/elements/Element.cpp index 8d4b997fb..9c1002af1 100644 --- a/src/simulation/elements/Element.cpp +++ b/src/simulation/elements/Element.cpp @@ -1,4 +1,5 @@ #include "simulation/Elements.h" +#include "simulation/StructProperty.h" Element::Element(): Identifier("DEFAULT_INVALID"), @@ -47,6 +48,43 @@ Element::Element(): { } +std::vector Element::GetProperties() +{ + std::vector properties; + properties.push_back(StructProperty("Name", StructProperty::String, offsetof(Element, Name))); + properties.push_back(StructProperty("Colour", StructProperty::Colour, offsetof(Element, Colour))); + properties.push_back(StructProperty("MenuVisible", StructProperty::Integer, offsetof(Element, MenuVisible))); + properties.push_back(StructProperty("MenuSection", StructProperty::Integer, offsetof(Element, MenuSection))); + properties.push_back(StructProperty("Advection", StructProperty::Float, offsetof(Element, Advection))); + properties.push_back(StructProperty("AirDrag", StructProperty::Float, offsetof(Element, AirDrag))); + properties.push_back(StructProperty("AirLoss", StructProperty::Float, offsetof(Element, AirLoss))); + properties.push_back(StructProperty("Loss", StructProperty::Float, offsetof(Element, Loss))); + properties.push_back(StructProperty("Collision", StructProperty::Float, offsetof(Element, Collision))); + properties.push_back(StructProperty("Gravity", StructProperty::Float, offsetof(Element, Gravity))); + properties.push_back(StructProperty("Diffusion", StructProperty::Float, offsetof(Element, Diffusion))); + properties.push_back(StructProperty("HotAir", StructProperty::Float, offsetof(Element, HotAir))); + properties.push_back(StructProperty("Falldown", StructProperty::Integer, offsetof(Element, Falldown))); + properties.push_back(StructProperty("Flammable", StructProperty::Integer, offsetof(Element, Flammable))); + properties.push_back(StructProperty("Explosive", StructProperty::Integer, offsetof(Element, Explosive))); + properties.push_back(StructProperty("Meltable", StructProperty::Integer, offsetof(Element, Meltable))); + properties.push_back(StructProperty("Hardness", StructProperty::Integer, offsetof(Element, Hardness))); + properties.push_back(StructProperty("Weight", StructProperty::Integer, offsetof(Element, Weight))); + properties.push_back(StructProperty("Temperature", StructProperty::Float, offsetof(Element, Temperature))); + properties.push_back(StructProperty("HeatConduct", StructProperty::UChar, offsetof(Element, HeatConduct))); + properties.push_back(StructProperty("Description", StructProperty::String, offsetof(Element, Description))); + properties.push_back(StructProperty("State", StructProperty::Char, offsetof(Element, State))); + properties.push_back(StructProperty("Properties", StructProperty::Integer, offsetof(Element, Properties))); + properties.push_back(StructProperty("LowPressure", StructProperty::Float, offsetof(Element, LowPressure))); + properties.push_back(StructProperty("LowPressureTransition", StructProperty::Integer, offsetof(Element, LowPressureTransition))); + properties.push_back(StructProperty("HighPressure", StructProperty::Float, offsetof(Element, HighPressure))); + properties.push_back(StructProperty("HighPressureTransition", StructProperty::Integer, offsetof(Element, HighPressureTransition))); + properties.push_back(StructProperty("LowTemperature", StructProperty::Float, offsetof(Element, LowTemperature))); + properties.push_back(StructProperty("LowTemperatureTransition", StructProperty::Integer, offsetof(Element, LowTemperatureTransition))); + properties.push_back(StructProperty("HighTemperature", StructProperty::Float, offsetof(Element, HighTemperature))); + properties.push_back(StructProperty("HighTemperatureTransition", StructProperty::Integer, offsetof(Element, HighTemperatureTransition))); + return properties; +} + int Element::legacyUpdate(UPDATE_FUNC_ARGS) { int r, rx, ry, rt; int t = parts[i].type; diff --git a/src/simulation/elements/Element.h b/src/simulation/elements/Element.h index 9df38d708..b51bddccd 100644 --- a/src/simulation/elements/Element.h +++ b/src/simulation/elements/Element.h @@ -4,6 +4,7 @@ #include "simulation/Simulation.h" #include "graphics/Renderer.h" #include "simulation/Elements.h" +#include "simulation/StructProperty.h" class Simulation; class Renderer; @@ -53,6 +54,10 @@ public: virtual ~Element() {} static int defaultGraphics(GRAPHICS_FUNC_ARGS); static int legacyUpdate(UPDATE_FUNC_ARGS); + + /** Returns a list of properties, their type and offset within the structure that can be changed + by higher-level processes refering to them by name such as Lua or the property tool **/ + static std::vector GetProperties(); }; #endif