diff options
author | Perttu Ahola <celeron55@gmail.com> | 2011-02-23 11:10:09 +0200 |
---|---|---|
committer | Perttu Ahola <celeron55@gmail.com> | 2011-02-23 11:10:09 +0200 |
commit | be32adc872a085f6ced03000362b352b121fa103 (patch) | |
tree | 9d3dfd06322829759dcb364be2b19becccb34c79 /src | |
parent | 9778347c7f118f6b74153f1d2c8070920352eb68 (diff) | |
download | minetest-be32adc872a085f6ced03000362b352b121fa103.tar.gz minetest-be32adc872a085f6ced03000362b352b121fa103.tar.bz2 minetest-be32adc872a085f6ced03000362b352b121fa103.zip |
updated scripting api a bit
--HG--
rename : data/luaobjects/test/client.lua => data/scripts/objects/test/client.lua
rename : data/luaobjects/test/server.lua => data/scripts/objects/test/server.lua
Diffstat (limited to 'src')
-rw-r--r-- | src/clientobject.cpp | 2 | ||||
-rw-r--r-- | src/environment.cpp | 26 | ||||
-rw-r--r-- | src/environment.h | 1 | ||||
-rw-r--r-- | src/server.cpp | 2 | ||||
-rw-r--r-- | src/serverobject.cpp | 220 |
5 files changed, 157 insertions, 94 deletions
diff --git a/src/clientobject.cpp b/src/clientobject.cpp index 4319f1ef0..bbe108e15 100644 --- a/src/clientobject.cpp +++ b/src/clientobject.cpp @@ -649,7 +649,7 @@ void LuaCAO::updateNodePos() return; m_node->setPosition(m_position); - m_node->setRotation(-m_rotation); + m_node->setRotation(m_rotation); } void LuaCAO::setPosition(v3f pos) diff --git a/src/environment.cpp b/src/environment.cpp index ef973fb1e..2c9f4d68f 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -115,6 +115,26 @@ Player * Environment::getRandomConnectedPlayer() return NULL; } +Player * Environment::getNearestConnectedPlayer(v3f pos) +{ + core::list<Player*> connected_players = getPlayers(true); + f32 nearest_d = 0; + Player *nearest_player = NULL; + for(core::list<Player*>::Iterator + i = connected_players.begin(); + i != connected_players.end(); i++) + { + Player *player = *i; + f32 d = player->getPosition().getDistanceFrom(pos); + if(d < nearest_d || nearest_player == NULL) + { + nearest_d = d; + nearest_player = player; + } + } + return nearest_player; +} + core::list<Player*> Environment::getPlayers() { return m_players; @@ -480,9 +500,9 @@ void ServerEnvironment::step(float dtime) if(player) pos = player->getPosition(); pos += v3f( - myrand_range(-5,5)*BS, + myrand_range(-3,3)*BS, 0, - myrand_range(-5,5)*BS + myrand_range(-3,3)*BS ); /* @@ -494,7 +514,7 @@ void ServerEnvironment::step(float dtime) /* Select a random type for it */ - std::string objectdir = porting::getDataPath("luaobjects"); + std::string objectdir = porting::getDataPath("scripts/objects"); std::vector<fs::DirListNode> dirlist = fs::GetDirListing(objectdir); if(dirlist.size() > 0) { diff --git a/src/environment.h b/src/environment.h index a476230aa..b4159372a 100644 --- a/src/environment.h +++ b/src/environment.h @@ -58,6 +58,7 @@ public: Player * getPlayer(u16 peer_id); Player * getPlayer(const char *name); Player * getRandomConnectedPlayer(); + Player * getNearestConnectedPlayer(v3f pos); core::list<Player*> getPlayers(); core::list<Player*> getPlayers(bool ignore_disconnected); void printPlayers(std::ostream &o); diff --git a/src/server.cpp b/src/server.cpp index 76fa23a93..24f22c6b3 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -3080,8 +3080,6 @@ void Server::BroadcastChatMessage(const std::wstring &message) void Server::sendRemoveNode(v3s16 p, u16 ignore_id) { - JMutexAutoLock conlock(m_con_mutex); - // Create packet u32 replysize = 8; SharedBuffer<u8> reply(replysize); diff --git a/src/serverobject.cpp b/src/serverobject.cpp index 7266fc2b1..e62f1efd0 100644 --- a/src/serverobject.cpp +++ b/src/serverobject.cpp @@ -97,6 +97,37 @@ extern "C"{ */ /* + object_remove(x,y,z) +*/ +static int lf_object_remove(lua_State *L) +{ + // 1: self + LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1); + lua_pop(L, 1); + + assert(self); + + self->m_removed = true; + + return 0; +} + +/* + ServerEnvironment object_get_environment(self) +*/ +static int lf_object_get_environment(lua_State *L) +{ + // 1: self + LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1); + lua_pop(L, 1); + + assert(self); + + lua_pushlightuserdata(L, self->getEnv()); + return 1; +} + +/* object_set_base_position(self, {X=,Y=,Z=}) */ static int lf_object_set_base_position(lua_State *L) @@ -187,9 +218,9 @@ static int lf_object_add_message(lua_State *L) } /* - object_get_node(self, {X=,Y=,Z=}) + env_get_node(env, {X=,Y=,Z=}) */ -static int lf_object_get_node(lua_State *L) +static int lf_env_get_node(lua_State *L) { // 2: position assert(lua_istable(L, -1)); @@ -206,11 +237,11 @@ static int lf_object_get_node(lua_State *L) lua_Number z = lua_tonumber(L, -1); lua_pop(L, 1); lua_pop(L, 1); - // 1: self - LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1); + // 1: env + ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1); lua_pop(L, 1); - assert(self); + assert(env); v3s16 pos = floatToInt(v3f(x,y,z), 1.0); @@ -219,7 +250,7 @@ static int lf_object_get_node(lua_State *L) // Get the node MapNode n(CONTENT_IGNORE); - n = self->getEnv()->getMap().getNodeNoEx(pos); + n = env->getMap().getNodeNoEx(pos); // Create a table with some data about the node lua_newtable(L); @@ -237,36 +268,6 @@ static int lf_object_get_node(lua_State *L) return 1; } -#if 0 -/* - get_node_features(node) - node = {content=,param1=,param2=} -*/ -static int lf_get_node_features(lua_State *L) -{ - MapNode n; - - // 1: node - assert(lua_istable(L, -1)); - lua_pushstring(L, "content"); - lua_gettable(L, -2); - n.d = lua_tointeger(L, -1); - lua_pop(L, 1); - lua_pushstring(L, "param1"); - lua_gettable(L, -2); - n.param = lua_tointeger(L, -1); - lua_pop(L, 1); - lua_pushstring(L, "param2"); - lua_gettable(L, -2); - n.param2 = lua_tointeger(L, -1); - lua_pop(L, 1); - lua_pop(L, 1); - - ContentFeatures &f = content_features(n.d); - -} -#endif - /* get_content_features(content) */ @@ -296,10 +297,10 @@ static int lf_get_content_features(lua_State *L) } /* - bool object_dig_node(self, {X=,Y=,Z=}) + bool env_dig_node(env, {X=,Y=,Z=}) Return true on success */ -static int lf_object_dig_node(lua_State *L) +static int lf_env_dig_node(lua_State *L) { // 2: position assert(lua_istable(L, -1)); @@ -316,11 +317,10 @@ static int lf_object_dig_node(lua_State *L) lua_Number z = lua_tonumber(L, -1); lua_pop(L, 1); lua_pop(L, 1); - // 1: self - LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1); + // 1: env + ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1); lua_pop(L, 1); - - assert(self); + assert(env); v3s16 pos = floatToInt(v3f(x,y,z), 1.0); @@ -329,19 +329,19 @@ static int lf_object_dig_node(lua_State *L) This gets sent to the server by the map through the edit event system. */ - bool succeeded = self->getEnv()->getMap().removeNodeWithEvent(pos); + bool succeeded = env->getMap().removeNodeWithEvent(pos); lua_pushboolean(L, succeeded); return 1; } /* - bool object_place_node(self, {X=,Y=,Z=}, node) + bool env_place_node(env, {X=,Y=,Z=}, node) node={content=,param1=,param2=} param1 and param2 are optional Return true on success */ -static int lf_object_place_node(lua_State *L) +static int lf_env_place_node(lua_State *L) { // 3: node MapNode n(CONTENT_STONE); @@ -379,11 +379,10 @@ static int lf_object_place_node(lua_State *L) lua_Number z = lua_tonumber(L, -1); lua_pop(L, 1); lua_pop(L, 1); - // 1: self - LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1); + // 1: env + ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1); lua_pop(L, 1); - - assert(self); + assert(env); v3s16 pos = floatToInt(v3f(x,y,z), 1.0); @@ -392,59 +391,100 @@ static int lf_object_place_node(lua_State *L) This gets sent to the server by the map through the edit event system. */ - bool succeeded = self->getEnv()->getMap().addNodeWithEvent(pos, n); + bool succeeded = env->getMap().addNodeWithEvent(pos, n); lua_pushboolean(L, succeeded); return 1; } /* - object_remove(x,y,z) + string env_get_nearest_player_name(env, {X=,Y=,Z=}) */ -static int lf_object_remove(lua_State *L) +static int lf_env_get_nearest_player_name(lua_State *L) { - // 1: self - LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1); + // 2: position + assert(lua_istable(L, -1)); + lua_pushstring(L, "X"); + lua_gettable(L, -2); + lua_Number x = lua_tonumber(L, -1); lua_pop(L, 1); + lua_pushstring(L, "Y"); + lua_gettable(L, -2); + lua_Number y = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_pushstring(L, "Z"); + lua_gettable(L, -2); + lua_Number z = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_pop(L, 1); + // 1: env + ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1); + lua_pop(L, 1); + assert(env); - assert(self); - - self->m_removed = true; - - return 0; + v3f pos_f = v3f(x,y,z)*BS; + + Player *player = env->getNearestConnectedPlayer(pos_f); + + if(player) + lua_pushstring(L, player->getName()); + else + lua_pushstring(L, ""); + + return 1; // Number of return values } /* - {X=,Y=,Z=} object_get_nearest_player_position(self) + {exists=, pos={X=,Y=,Z=}, connected=} env_get_player_info(env, name) */ -/*static int lf_object_get_nearest_player_position(lua_State *L) +static int lf_env_get_player_info(lua_State *L) { - // 1: self - LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1); + // 2: name + const char *name = lua_tostring(L, -1); lua_pop(L, 1); + // 1: env + ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1); + lua_pop(L, 1); + assert(env); - assert(self); - - ServerEnvironment *env = self->getEnv(); - env-> - v3f pos = ; + Player *player = env->getPlayer(name); + v3f pos(0,0,0); + if(player) + pos = player->getPosition(); lua_newtable(L); - lua_pushstring(L, "X"); - lua_pushnumber(L, pos.X/BS); - lua_settable(L, -3); - - lua_pushstring(L, "Y"); - lua_pushnumber(L, pos.Y/BS); + lua_pushstring(L, "exists"); + lua_pushboolean(L, (player != NULL)); lua_settable(L, -3); + + if(player != NULL) + { + lua_pushstring(L, "pos"); + { + lua_newtable(L); + + lua_pushstring(L, "X"); + lua_pushnumber(L, pos.X/BS); + lua_settable(L, -3); + + lua_pushstring(L, "Y"); + lua_pushnumber(L, pos.Y/BS); + lua_settable(L, -3); + + lua_pushstring(L, "Z"); + lua_pushnumber(L, pos.Z/BS); + lua_settable(L, -3); + } + lua_settable(L, -3); - lua_pushstring(L, "Z"); - lua_pushnumber(L, pos.Z/BS); - lua_settable(L, -3); + lua_pushstring(L, "connected"); + lua_pushboolean(L, (player->peer_id != 0)); + lua_settable(L, -3); + } return 1; // Number of return values -}*/ +} LuaSAO::LuaSAO(ServerEnvironment *env, u16 id, v3f pos): ServerActiveObject(env, id, pos), @@ -465,14 +505,18 @@ LuaSAO::LuaSAO(ServerEnvironment *env, u16 id, v3f pos): //lua_setglobal(L, "self"); // Register functions - lua_register(L, "object_set_base_position", lf_object_set_base_position); - lua_register(L, "object_get_base_position", lf_object_get_base_position); - lua_register(L, "object_add_message", lf_object_add_message); - lua_register(L, "object_get_node", lf_object_get_node); - lua_register(L, "get_content_features", lf_get_content_features); - lua_register(L, "object_dig_node", lf_object_dig_node); - lua_register(L, "object_place_node", lf_object_place_node); - lua_register(L, "object_remove", lf_object_remove); +#define LUA_REGISTER_FUNC(L, x) lua_register(L, #x, lf_ ## x) + LUA_REGISTER_FUNC(L, object_remove); + LUA_REGISTER_FUNC(L, object_get_environment); + LUA_REGISTER_FUNC(L, object_set_base_position); + LUA_REGISTER_FUNC(L, object_get_base_position); + LUA_REGISTER_FUNC(L, object_add_message); + LUA_REGISTER_FUNC(L, env_get_node); + LUA_REGISTER_FUNC(L, get_content_features); + LUA_REGISTER_FUNC(L, env_dig_node); + LUA_REGISTER_FUNC(L, env_place_node); + LUA_REGISTER_FUNC(L, env_get_nearest_player_name); + LUA_REGISTER_FUNC(L, env_get_player_info); } LuaSAO::~LuaSAO() @@ -487,7 +531,7 @@ std::string LuaSAO::getClientInitializationData() */ std::string relative_path; - relative_path += "luaobjects/"; + relative_path += "scripts/objects/"; relative_path += m_script_name; relative_path += "/client.lua"; std::string full_path = porting::getDataPath(relative_path.c_str()); @@ -694,7 +738,7 @@ void LuaSAO::loadScripts(const std::string &script_name) m_script_name = script_name; std::string relative_path; - relative_path += "luaobjects/"; + relative_path += "scripts/objects/"; relative_path += script_name; std::string server_file = relative_path + "/server.lua"; std::string server_path = porting::getDataPath(server_file.c_str()); |