Scripting WIP

master
Perttu Ahola 2011-11-12 10:39:44 +02:00
parent ea8d6d7abd
commit dcedfdacd1
12 changed files with 228 additions and 104 deletions

4
.gitignore vendored
View File

@ -21,3 +21,7 @@ src/jthread/libjthread.a
debug.txt debug.txt
bin/debug.txt bin/debug.txt
minetestmapper/map.png minetestmapper/map.png
locale/
src/lua/build/
src/lua/CMakeFiles/
*bak*

View File

@ -125,6 +125,10 @@ function dump(o, dumped)
return tostring(o) return tostring(o)
elseif type(o) == "function" then elseif type(o) == "function" then
return "<function>" return "<function>"
elseif type(o) == "userdata" then
return "<userdata>"
elseif type(o) == "nil" then
return "nil"
else else
error("cannot dump a " .. type(o)) error("cannot dump a " .. type(o))
return nil return nil
@ -139,7 +143,7 @@ local TNT = {
-- Maybe handle gravity and collision this way? dunno -- Maybe handle gravity and collision this way? dunno
physical = true, physical = true,
weight = 5, weight = 5,
boundingbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5}, collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
visual = "cube", visual = "cube",
textures = {"tnt_top.png","tnt_bottom.png","tnt_side.png","tnt_side.png","tnt_side.png","tnt_side.png"}, textures = {"tnt_top.png","tnt_bottom.png","tnt_side.png","tnt_side.png","tnt_side.png","tnt_side.png"},
-- Initial value for our timer -- Initial value for our timer
@ -161,10 +165,13 @@ end
-- Called when object is right-clicked -- Called when object is right-clicked
function TNT:on_rightclick(clicker) function TNT:on_rightclick(clicker)
print("TNT:on_rightclick()") print("TNT:on_rightclick()")
print("self: "..dump(self))
print("getmetatable(self): "..dump(getmetatable(self)))
print("getmetatable(getmetatable(self)): "..dump(getmetatable(getmetatable(self))))
pos = self.object:getpos() pos = self.object:getpos()
print("TNT:on_rightclick(): object position: "..dump(pos)) print("TNT:on_rightclick(): object position: "..dump(pos))
pos = {x=pos.x+0.5+1, y=pos.y+0.5, z=pos.z+0.5} pos = {x=pos.x+0.5+1, y=pos.y+0.5, z=pos.z+0.5}
minetest.env:add_node(pos, 0) --minetest.env:add_node(pos, 0)
end end
print("TNT dump: "..dump(TNT)) print("TNT dump: "..dump(TNT))

View File

@ -94,6 +94,7 @@ configure_file(
) )
set(common_SRCS set(common_SRCS
luaentity_common.cpp
scriptapi.cpp scriptapi.cpp
script.cpp script.cpp
log.cpp log.cpp

View File

@ -1268,6 +1268,8 @@ void MobV2CAO::setLooks(const std::string &looks)
LuaEntityCAO LuaEntityCAO
*/ */
#include "luaentity_common.h"
// Prototype // Prototype
LuaEntityCAO proto_LuaEntityCAO; LuaEntityCAO proto_LuaEntityCAO;
@ -1275,13 +1277,15 @@ LuaEntityCAO::LuaEntityCAO():
ClientActiveObject(0), ClientActiveObject(0),
m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.), m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
m_node(NULL), m_node(NULL),
m_position(v3f(0,10*BS,0)) m_position(v3f(0,10*BS,0)),
m_prop(new LuaEntityProperties)
{ {
ClientActiveObject::registerType(getType(), create); ClientActiveObject::registerType(getType(), create);
} }
LuaEntityCAO::~LuaEntityCAO() LuaEntityCAO::~LuaEntityCAO()
{ {
delete m_prop;
} }
ClientActiveObject* LuaEntityCAO::create() ClientActiveObject* LuaEntityCAO::create()
@ -1398,16 +1402,19 @@ void LuaEntityCAO::initialize(const std::string &data)
{ {
infostream<<"LuaEntityCAO: Got init data"<<std::endl; infostream<<"LuaEntityCAO: Got init data"<<std::endl;
{ std::istringstream is(data, std::ios::binary);
std::istringstream is(data, std::ios::binary); // version
// version u8 version = readU8(is);
u8 version = readU8(is); // check version
// check version if(version != 0)
if(version != 0) return;
return; // pos
// pos m_position = readV3F1000(is);
m_position = readV3F1000(is); // properties
} std::istringstream prop_is(deSerializeLongString(is), std::ios::binary);
m_prop->deSerialize(prop_is);
infostream<<"m_prop: "<<m_prop->dump()<<std::endl;
updateNodePos(); updateNodePos();
} }

View File

@ -380,6 +380,8 @@ private:
LuaEntityCAO LuaEntityCAO
*/ */
struct LuaEntityProperties;
class LuaEntityCAO : public ClientActiveObject class LuaEntityCAO : public ClientActiveObject
{ {
public: public:
@ -414,6 +416,7 @@ private:
core::aabbox3d<f32> m_selection_box; core::aabbox3d<f32> m_selection_box;
scene::IMeshSceneNode *m_node; scene::IMeshSceneNode *m_node;
v3f m_position; v3f m_position;
struct LuaEntityProperties *m_prop;
}; };

View File

@ -1495,6 +1495,7 @@ void MobV2SAO::doDamage(u16 d)
*/ */
#include "scriptapi.h" #include "scriptapi.h"
#include "luaentity_common.h"
// Prototype // Prototype
LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", ""); LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
@ -1527,14 +1528,13 @@ void LuaEntitySAO::addedToEnvironment(u16 id)
{ {
ServerActiveObject::addedToEnvironment(id); ServerActiveObject::addedToEnvironment(id);
// Create entity by name and state // Create entity from name and state
m_registered = true; m_registered = true;
lua_State *L = m_env->getLua(); lua_State *L = m_env->getLua();
scriptapi_luaentity_add(L, id, m_init_name.c_str(), m_init_state.c_str()); scriptapi_luaentity_add(L, id, m_init_name.c_str(), m_init_state.c_str());
// Get properties // Get properties
*m_prop = scriptapi_luaentity_get_properties(L, m_id); scriptapi_luaentity_get_properties(L, m_id, m_prop);
infostream<<"m_prop->visual="<<m_prop->visual<<std::endl;
} }
ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos, ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
@ -1571,6 +1571,11 @@ std::string LuaEntitySAO::getClientInitializationData()
writeU8(os, 0); writeU8(os, 0);
// pos // pos
writeV3F1000(os, m_base_position); writeV3F1000(os, m_base_position);
// properties
std::ostringstream prop_os(std::ios::binary);
m_prop->serialize(prop_os);
os<<serializeLongString(prop_os.str());
// return result
return os.str(); return os.str();
} }

83
src/luaentity_common.cpp Normal file
View File

@ -0,0 +1,83 @@
/*
Minetest-c55
Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "luaentity_common.h"
#include "utility.h"
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
LuaEntityProperties::LuaEntityProperties():
physical(true),
weight(5),
collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
visual("single_sprite")
{
textures.push_back("unknown_block.png");
}
std::string LuaEntityProperties::dump()
{
std::ostringstream os(std::ios::binary);
os<<"physical="<<physical;
os<<", weight="<<weight;
os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
os<<", visual="<<visual;
os<<", textures=[";
for(core::list<std::string>::Iterator i = textures.begin();
i != textures.end(); i++){
os<<"\""<<(*i)<<"\" ";
}
os<<"]";
return os.str();
}
void LuaEntityProperties::serialize(std::ostream &os)
{
writeU8(os, 0); // version
writeU8(os, physical);
writeF1000(os, weight);
writeV3F1000(os, collisionbox.MinEdge);
writeV3F1000(os, collisionbox.MaxEdge);
os<<serializeString(visual);
writeU16(os, textures.size());
for(core::list<std::string>::Iterator i = textures.begin();
i != textures.end(); i++){
os<<serializeString(*i);
}
}
void LuaEntityProperties::deSerialize(std::istream &is)
{
int version = readU8(is);
if(version != 0) throw SerializationError(
"unsupported LuaEntityProperties version");
physical = readU8(is);
weight = readF1000(is);
collisionbox.MinEdge = readV3F1000(is);
collisionbox.MaxEdge = readV3F1000(is);
visual = deSerializeString(is);
textures.clear();
int texture_count = readU16(is);
for(int i=0; i<texture_count; i++){
textures.push_back(deSerializeString(is));
}
}

42
src/luaentity_common.h Normal file
View File

@ -0,0 +1,42 @@
/*
Minetest-c55
Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef LUAENTITY_COMMON_HEADER
#define LUAENTITY_COMMON_HEADER
#include <string>
#include "irrlichttypes.h"
#include <iostream>
struct LuaEntityProperties
{
bool physical;
float weight;
core::aabbox3d<f32> collisionbox;
std::string visual;
core::list<std::string> textures;
LuaEntityProperties();
std::string dump();
void serialize(std::ostream &os);
void deSerialize(std::istream &is);
};
#endif

View File

@ -61,7 +61,7 @@ lua_State* script_init()
return L; return L;
} }
lua_State* script_deinit(lua_State *L) void script_deinit(lua_State *L)
{ {
lua_close(L); lua_close(L);
} }

View File

@ -24,7 +24,7 @@ typedef struct lua_State lua_State;
//#include <string> //#include <string>
lua_State* script_init(); lua_State* script_init();
lua_State* script_deinit(lua_State *L); void script_deinit(lua_State *L);
void script_error(lua_State *L, const char *fmt, ...); void script_error(lua_State *L, const char *fmt, ...);
bool script_load(lua_State *L, const char *path); bool script_load(lua_State *L, const char *path);

View File

@ -33,6 +33,22 @@ extern "C" {
#include "serverobject.h" #include "serverobject.h"
#include "script.h" #include "script.h"
//#include "luna.h" //#include "luna.h"
#include "luaentity_common.h"
/*
TODO:
- Global environment step function
- Random node triggers
- Object network and client-side stuff
- Named node types and dynamic id allocation
- LuaNodeMetadata
blockdef.has_metadata = true/false
- Stores an inventory and stuff in a Settings object
meta.inventory_add_list("main")
blockdef.on_inventory_modified
meta.set("owner", playername)
meta.get("owner")
*/
static void stackDump(lua_State *L, std::ostream &o) static void stackDump(lua_State *L, std::ostream &o)
{ {
@ -128,28 +144,8 @@ static int l_register_entity(lua_State *L)
return 0; /* number of results */ return 0; /* number of results */
} }
#if 0
static int l_new_entity(lua_State *L)
{
/* o = o or {}
setmetatable(o, self)
self.__index = self
return o */
if(lua_isnil(L, -1))
lua_newtable(L);
luaL_checktype(L, -1, LUA_TTABLE);
luaL_getmetatable(L, "minetest.entity");
lua_pushvalue(L, -1); // duplicate metatable
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
// return table
return 1;
}
#endif
static const struct luaL_Reg minetest_f [] = { static const struct luaL_Reg minetest_f [] = {
{"register_entity", l_register_entity}, {"register_entity", l_register_entity},
//{"new_entity", l_new_entity},
{NULL, NULL} {NULL, NULL}
}; };
@ -163,6 +159,30 @@ static const struct luaL_Reg minetest_entity_m [] = {
{NULL, NULL} {NULL, NULL}
}; };
static void objectref_get(lua_State *L, u16 id)
{
// Get minetest.object_refs[i]
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "object_refs");
luaL_checktype(L, -1, LUA_TTABLE);
lua_pushnumber(L, id);
lua_gettable(L, -2);
lua_remove(L, -2); // object_refs
lua_remove(L, -2); // minetest
}
static void luaentity_get(lua_State *L, u16 id)
{
// Get minetest.luaentities[i]
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "luaentities");
luaL_checktype(L, -1, LUA_TTABLE);
lua_pushnumber(L, id);
lua_gettable(L, -2);
lua_remove(L, -2); // luaentities
lua_remove(L, -2); // minetest
}
/* /*
Reference objects Reference objects
*/ */
@ -552,18 +572,6 @@ void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
lua_pop(L, 2); lua_pop(L, 2);
} }
static void objectref_get(lua_State *L, u16 id)
{
// Get minetest.object_refs[i]
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "object_refs");
luaL_checktype(L, -1, LUA_TTABLE);
lua_pushnumber(L, id);
lua_gettable(L, -2);
lua_remove(L, -2); // object_refs
lua_remove(L, -2); // minetest
}
/* /*
luaentity luaentity
*/ */
@ -646,18 +654,6 @@ void scriptapi_luaentity_rm(lua_State *L, u16 id)
lua_pop(L, 2); // pop luaentities, minetest lua_pop(L, 2); // pop luaentities, minetest
} }
static void luaentity_get(lua_State *L, u16 id)
{
// Get minetest.luaentities[i]
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "luaentities");
luaL_checktype(L, -1, LUA_TTABLE);
lua_pushnumber(L, id);
lua_gettable(L, -2);
lua_remove(L, -2); // luaentities
lua_remove(L, -2); // minetest
}
std::string scriptapi_luaentity_get_state(lua_State *L, u16 id) std::string scriptapi_luaentity_get_state(lua_State *L, u16 id)
{ {
realitycheck(L); realitycheck(L);
@ -667,74 +663,71 @@ std::string scriptapi_luaentity_get_state(lua_State *L, u16 id)
return ""; return "";
} }
LuaEntityProperties scriptapi_luaentity_get_properties(lua_State *L, u16 id) void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
LuaEntityProperties *prop)
{ {
realitycheck(L); realitycheck(L);
assert(lua_checkstack(L, 20)); assert(lua_checkstack(L, 20));
infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl; infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
StackUnroller stack_unroller(L); StackUnroller stack_unroller(L);
LuaEntityProperties prop;
// Get minetest.luaentities[id] // Get minetest.luaentities[id]
luaentity_get(L, id); luaentity_get(L, id);
//int object = lua_gettop(L); //int object = lua_gettop(L);
lua_getfield(L, -1, "physical"); lua_getfield(L, -1, "physical");
if(lua_isboolean(L, -1)) if(lua_isboolean(L, -1))
prop.physical = lua_toboolean(L, -1); prop->physical = lua_toboolean(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, -1, "weight"); lua_getfield(L, -1, "weight");
prop.weight = lua_tonumber(L, -1); prop->weight = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, -1, "boundingbox"); lua_getfield(L, -1, "collisionbox");
if(lua_istable(L, -1)){ if(lua_istable(L, -1)){
lua_rawgeti(L, -1, 1); lua_rawgeti(L, -1, 1);
prop.boundingbox.MinEdge.X = lua_tonumber(L, -1); prop->collisionbox.MinEdge.X = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_rawgeti(L, -1, 2); lua_rawgeti(L, -1, 2);
prop.boundingbox.MinEdge.Y = lua_tonumber(L, -1); prop->collisionbox.MinEdge.Y = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_rawgeti(L, -1, 3); lua_rawgeti(L, -1, 3);
prop.boundingbox.MinEdge.Z = lua_tonumber(L, -1); prop->collisionbox.MinEdge.Z = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_rawgeti(L, -1, 4); lua_rawgeti(L, -1, 4);
prop.boundingbox.MaxEdge.X = lua_tonumber(L, -1); prop->collisionbox.MaxEdge.X = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_rawgeti(L, -1, 5); lua_rawgeti(L, -1, 5);
prop.boundingbox.MaxEdge.Y = lua_tonumber(L, -1); prop->collisionbox.MaxEdge.Y = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_rawgeti(L, -1, 6); lua_rawgeti(L, -1, 6);
prop.boundingbox.MaxEdge.Z = lua_tonumber(L, -1); prop->collisionbox.MaxEdge.Z = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
} }
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, -1, "visual"); lua_getfield(L, -1, "visual");
if(lua_isstring(L, -1)) if(lua_isstring(L, -1))
prop.visual = lua_tostring(L, -1); prop->visual = lua_tostring(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, -1, "textures"); lua_getfield(L, -1, "textures");
if(lua_istable(L, -1)){ if(lua_istable(L, -1)){
prop.textures.clear(); prop->textures.clear();
int table = lua_gettop(L); int table = lua_gettop(L);
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, table) != 0){ while(lua_next(L, table) != 0){
// key at index -2 and value at index -1 // key at index -2 and value at index -1
if(lua_isstring(L, -1)) if(lua_isstring(L, -1))
prop.textures.push_back(lua_tostring(L, -1)); prop->textures.push_back(lua_tostring(L, -1));
else else
prop.textures.push_back(""); prop->textures.push_back("");
// removes value, keeps key for next iteration // removes value, keeps key for next iteration
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
lua_pop(L, 1); lua_pop(L, 1);
return prop;
} }
void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime) void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)

View File

@ -27,6 +27,7 @@ class Server;
class ServerEnvironment; class ServerEnvironment;
class ServerActiveObject; class ServerActiveObject;
typedef struct lua_State lua_State; typedef struct lua_State lua_State;
struct LuaEntityProperties;
void scriptapi_export(lua_State *L, Server *server); void scriptapi_export(lua_State *L, Server *server);
void scriptapi_add_environment(lua_State *L, ServerEnvironment *env); void scriptapi_add_environment(lua_State *L, ServerEnvironment *env);
@ -34,34 +35,12 @@ void scriptapi_add_environment(lua_State *L, ServerEnvironment *env);
void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj); void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj);
void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj); void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj);
struct LuaEntityProperties
{
bool physical;
float weight;
core::aabbox3d<f32> boundingbox;
std::string visual;
core::list<std::string> textures;
LuaEntityProperties():
physical(true),
weight(5),
boundingbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
visual("cube")
{
textures.push_back("unknown_block.png");
textures.push_back("unknown_block.png");
textures.push_back("unknown_block.png");
textures.push_back("unknown_block.png");
textures.push_back("unknown_block.png");
textures.push_back("unknown_block.png");
}
};
void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name, void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
const char *init_state); const char *init_state);
void scriptapi_luaentity_rm(lua_State *L, u16 id); void scriptapi_luaentity_rm(lua_State *L, u16 id);
std::string scriptapi_luaentity_get_state(lua_State *L, u16 id); std::string scriptapi_luaentity_get_state(lua_State *L, u16 id);
LuaEntityProperties scriptapi_luaentity_get_properties(lua_State *L, u16 id); void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
LuaEntityProperties *prop);
void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime); void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime);
void scriptapi_luaentity_rightclick_player(lua_State *L, u16 id, void scriptapi_luaentity_rightclick_player(lua_State *L, u16 id,
const char *playername); const char *playername);