3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "serverobject.h"
22 #include "environment.h"
24 ServerActiveObject::ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos):
33 ServerActiveObject::~ServerActiveObject()
41 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
42 ServerActiveObject(env, id, pos),
48 void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
57 m_base_position.Y += dtime * BS * 2;
58 if(m_base_position.Y > 8*BS)
59 m_base_position.Y = 2*BS;
65 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
69 data += itos(0); // 0 = position
71 data += itos(m_base_position.X);
73 data += itos(m_base_position.Y);
75 data += itos(m_base_position.Z);
77 ActiveObjectMessage aom(getId(), false, data);
78 messages.push_back(aom);
91 object_set_base_position(self, x,y,z)
92 x,y,z = object_get_base_position(self)
93 object_add_message(self, data)
98 object_set_base_position(self, x, y, z)
100 static int lf_object_set_base_position(lua_State *L)
103 lua_Number z = lua_tonumber(L, -1);
106 lua_Number y = lua_tonumber(L, -1);
109 lua_Number x = lua_tonumber(L, -1);
112 LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
117 self->setBasePosition(v3f(x*BS,y*BS,z*BS));
119 return 0; // Number of return values
123 object_get_base_position(self)
125 static int lf_object_get_base_position(lua_State *L)
128 LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
133 v3f pos = self->getBasePosition();
135 lua_pushnumber(L, pos.X/BS);
136 lua_pushnumber(L, pos.Y/BS);
137 lua_pushnumber(L, pos.Z/BS);
138 return 3; // Number of return values
142 object_add_message(self, string data)
145 static int lf_object_add_message(lua_State *L)
149 const char *data_c = lua_tolstring(L, -1, &datalen);
152 LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
158 std::string data(data_c, datalen);
159 //dstream<<"object_add_message: data="<<data<<std::endl;
161 // Create message and add to queue
162 ActiveObjectMessage aom(self->getId());
164 aom.datastring = data;
165 self->m_message_queue.push_back(aom);
167 return 0; // Number of return values
171 object_get_node(x,y,z)
173 static int lf_object_get_node(lua_State *L)
176 lua_Number z = lua_tonumber(L, -1);
179 lua_Number y = lua_tonumber(L, -1);
182 lua_Number x = lua_tonumber(L, -1);
185 LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
190 v3s16 pos = floatToInt(v3f(x,y,z), 1.0);
192 /*dstream<<"Checking node from pos=("<<pos.X<<","<<pos.Y<<","<<pos.Z
196 MapNode n(CONTENT_IGNORE);
197 n = self->getEnv()->getMap().getNodeNoEx(pos);
199 // Create a table with some data about the node
201 lua_pushstring(L, "content");
202 lua_pushinteger(L, n.d);
204 lua_pushstring(L, "walkable");
205 lua_pushboolean(L, content_features(n.d).walkable);
215 static int lf_object_remove(lua_State *L)
218 LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
223 self->m_removed = true;
228 LuaSAO::LuaSAO(ServerEnvironment *env, u16 id, v3f pos):
229 ServerActiveObject(env, id, pos),
232 dstream<<"LuaSAO::LuaSAO(): id="<<id<<std::endl;
243 //lua_pushlightuserdata(L, this);
244 //lua_setglobal(L, "self");
246 // Register functions
247 lua_register(L, "object_set_base_position", lf_object_set_base_position);
248 lua_register(L, "object_get_base_position", lf_object_get_base_position);
249 lua_register(L, "object_add_message", lf_object_add_message);
250 lua_register(L, "object_get_node", lf_object_get_node);
251 lua_register(L, "object_remove", lf_object_remove);
259 std::string LuaSAO::getClientInitializationData()
262 Read client-side script from file
265 std::string relative_path;
266 relative_path += "luaobjects/";
267 relative_path += m_script_name;
268 relative_path += "/client.lua";
269 std::string full_path = porting::getDataPath(relative_path.c_str());
271 std::ifstream file(full_path.c_str(), std::ios::binary | std::ios::ate);
272 int size = file.tellg();
273 SharedBuffer<char> buf(size);
274 file.seekg(0, std::ios::beg);
275 file.read(&buf[0], size);
284 std::string script_string(&buf[0], buf.getSize());
285 data += serializeLongString(script_string);
288 Get data from server-side script for inclusion
290 std::string other_data;
294 const char *funcname = "get_client_init_data";
295 lua_getglobal(L, funcname);
296 if(!lua_isfunction(L,-1))
299 dstream<<"WARNING: LuaSAO: Function not found: "
300 <<funcname<<std::endl;
306 lua_pushlightuserdata(L, this);
308 // Call (1 parameters, 1 result)
309 if(lua_pcall(L, 1, 1, 0))
311 dstream<<"WARNING: LuaSAO: Error running function "
313 <<lua_tostring(L,-1)<<std::endl;
318 if(!lua_isstring(L,-1))
320 dstream<<"WARNING: LuaSAO: Function "<<funcname
321 <<" didn't return a string"<<std::endl;
326 const char *cs = lua_tolstring(L, -1, &cs_len);
329 other_data = std::string(cs, cs_len);
333 data += serializeLongString(other_data);
338 std::string LuaSAO::getServerInitializationData()
343 data.append(serializeString(m_script_name));
346 Get data from server-side script for inclusion
348 std::string other_data;
352 const char *funcname = "get_server_init_data";
353 lua_getglobal(L, funcname);
354 if(!lua_isfunction(L,-1))
357 dstream<<"WARNING: LuaSAO: Function not found: "
358 <<funcname<<std::endl;
364 lua_pushlightuserdata(L, this);
366 // Call (1 parameters, 1 result)
367 if(lua_pcall(L, 1, 1, 0))
369 dstream<<"WARNING: LuaSAO: Error running function "
371 <<lua_tostring(L,-1)<<std::endl;
376 if(!lua_isstring(L,-1))
378 dstream<<"WARNING: LuaSAO: Function "<<funcname
379 <<" didn't return a string"<<std::endl;
384 const char *cs = lua_tolstring(L, -1, &cs_len);
387 other_data = std::string(cs, cs_len);
391 data += serializeLongString(other_data);
396 void LuaSAO::initialize(const std::string &data)
398 std::istringstream is(data, std::ios::binary);
399 std::string script_name = deSerializeString(is);
400 std::string other = deSerializeLongString(is);
402 loadScripts(script_name);
405 Call initialize(self, data) in the script
408 const char *funcname = "initialize";
409 lua_getglobal(L, funcname);
410 if(!lua_isfunction(L,-1))
413 dstream<<"WARNING: LuaSAO: Function not found: "
414 <<funcname<<std::endl;
420 lua_pushlightuserdata(L, this);
422 lua_pushlstring(L, other.c_str(), other.size());
424 // Call (2 parameters, 0 result)
425 if(lua_pcall(L, 2, 0, 0))
427 dstream<<"WARNING: LuaSAO: Error running function "
429 <<lua_tostring(L,-1)<<std::endl;
434 void LuaSAO::loadScripts(const std::string &script_name)
436 m_script_name = script_name;
438 std::string relative_path;
439 relative_path += "luaobjects/";
440 relative_path += script_name;
441 std::string server_file = relative_path + "/server.lua";
442 std::string server_path = porting::getDataPath(server_file.c_str());
446 ret = luaL_loadfile(L, server_path.c_str());
449 const char *message = lua_tostring(L, -1);
451 dstream<<"LuaSAO::loadScript(): lua_loadfile failed: "
452 <<message<<std::endl;
456 ret = lua_pcall(L, 0, 0, 0);
459 const char *message = lua_tostring(L, -1);
461 dstream<<"LuaSAO::loadScript(): lua_pcall failed: "
462 <<message<<std::endl;
468 void LuaSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
470 lua_getglobal(L, "step");
471 if(!lua_isfunction(L,-1))
474 dstream<<"WARNING: LuaSAO::step(): step function not found"<<std::endl;
480 lua_pushlightuserdata(L, this);
482 lua_pushnumber(L, dtime);
484 // Call (2 parameters, 0 result)
485 if(lua_pcall(L, 2, 0, 0))
487 dstream<<"WARNING: LuaSAO::step(): Error running function step(): "
488 <<lua_tostring(L,-1)<<std::endl;
493 while(m_message_queue.size() != 0)
495 messages.push_back(m_message_queue.pop_front());