forked from Mirrorlandia_minetest/minetest
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
This commit is contained in:
parent
9778347c7f
commit
be32adc872
@ -1,154 +0,0 @@
|
||||
-- Server-side code of the test lua object
|
||||
|
||||
--
|
||||
-- Some helper functions and classes
|
||||
--
|
||||
|
||||
function vector_subtract(a, b)
|
||||
return {X=a.X-b.X, Y=a.Y-b.Y, Z=a.Z-b.Z}
|
||||
end
|
||||
|
||||
function vector_add(a, b)
|
||||
return {X=a.X+b.X, Y=a.Y+b.Y, Z=a.Z+b.Z}
|
||||
end
|
||||
|
||||
function vector_multiply(a, d)
|
||||
return {X=a.X*d, Y=a.Y*d, Z=a.Z*d}
|
||||
end
|
||||
|
||||
--
|
||||
-- Actual code
|
||||
--
|
||||
|
||||
counter = 0
|
||||
counter2 = 0
|
||||
counter3 = 0
|
||||
counter4 = 0
|
||||
death_counter = 0
|
||||
-- This is got in initialization from object_get_base_position(self)
|
||||
position = {X=0,Y=0,Z=0}
|
||||
rotation = {X=0, Y=math.random(0,360), Z=0}
|
||||
dir = 1
|
||||
temp1 = 0
|
||||
|
||||
function on_step(self, dtime)
|
||||
--[[if position.Y > 9.5 then
|
||||
position.Y = 6
|
||||
end
|
||||
if position.Y < 5.5 then
|
||||
position.Y = 9]]
|
||||
|
||||
-- Limit step to a sane value; it jumps a lot while the map generator
|
||||
-- is in action
|
||||
if dtime > 0.5 then
|
||||
dtime = 0.5
|
||||
end
|
||||
|
||||
-- Returned value has these fields:
|
||||
-- * int content
|
||||
-- * int param1
|
||||
-- * int param2
|
||||
p = {X=position.X, Y=position.Y-0.35, Z=position.Z}
|
||||
n = object_get_node(self, p)
|
||||
f = get_content_features(n.content)
|
||||
if f.walkable then
|
||||
dir = 1
|
||||
else
|
||||
dir = -1
|
||||
end
|
||||
-- Keep the object approximately at ground level
|
||||
position.Y = position.Y + dtime * 2.0 * dir
|
||||
|
||||
-- Move the object around
|
||||
position.X = position.X + math.cos(math.pi+rotation.Y/180*math.pi)
|
||||
* dtime * 2.0
|
||||
position.Z = position.Z + math.sin(math.pi+rotation.Y/180*math.pi)
|
||||
* dtime * 2.0
|
||||
|
||||
-- This value has to be set; it determines to which player the
|
||||
-- object is near to and such
|
||||
object_set_base_position(self, position)
|
||||
|
||||
counter4 = counter4 - dtime
|
||||
if counter4 < 0 then
|
||||
counter4 = counter4 + math.random(0.5,8)
|
||||
-- Mess around with the map
|
||||
np = vector_add(position, {X=0,Y=0,Z=0})
|
||||
object_place_node(self, np, {content=0})
|
||||
-- A node could be digged out with this:
|
||||
-- object_dig_node(self, np)
|
||||
end
|
||||
|
||||
counter3 = counter3 - dtime
|
||||
if counter3 < 0 then
|
||||
counter3 = counter3 + math.random(1,4)
|
||||
rotation.Y = rotation.Y + math.random(-180, 180)
|
||||
end
|
||||
|
||||
-- Send some custom messages at a custom interval
|
||||
|
||||
counter = counter - dtime
|
||||
if counter < 0 then
|
||||
counter = counter + 0.25
|
||||
if counter < 0 then
|
||||
counter = 0
|
||||
end
|
||||
|
||||
message = "pos " .. position.X .. " " .. position.Y .. " " .. position.Z
|
||||
object_add_message(self, message)
|
||||
|
||||
message = "rot " .. rotation.X .. " " .. rotation.Y .. " " .. rotation.Z
|
||||
object_add_message(self, message)
|
||||
end
|
||||
|
||||
-- Mess around with the map
|
||||
--[[counter2 = counter2 - dtime
|
||||
if counter2 < 0 then
|
||||
counter2 = counter2 + 3
|
||||
if temp1 == 0 then
|
||||
temp1 = 1
|
||||
object_dig_node(self, {X=0,Y=1,Z=0})
|
||||
else
|
||||
temp1 = 0
|
||||
n = {content=1}
|
||||
object_place_node(self, {X=0,Y=5,Z=0}, n)
|
||||
end
|
||||
end]]
|
||||
|
||||
-- Remove the object after some time
|
||||
death_counter = death_counter + dtime
|
||||
if death_counter > 30 then
|
||||
object_remove(self)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-- This stuff is passed to a newly created client-side counterpart of this object
|
||||
function on_get_client_init_data(self)
|
||||
-- Just return some data for testing
|
||||
return "result of get_client_init_data"
|
||||
end
|
||||
|
||||
-- This should return some data that mostly saves the state of this object
|
||||
-- Not completely implemented yet
|
||||
function on_get_server_init_data(self)
|
||||
-- Just return some data for testing
|
||||
return "result of get_server_init_data"
|
||||
end
|
||||
|
||||
-- When the object is loaded from scratch, this is called before the first
|
||||
-- on_step(). Data is an empty string or the output of an object spawner
|
||||
-- hook, but such things are not yet implemented.
|
||||
--
|
||||
-- At reload time, the last output of get_server_init_data is passed as data.
|
||||
--
|
||||
-- This should initialize the position of the object to the value returned
|
||||
-- by object_get_base_position(self)
|
||||
--
|
||||
-- Not completely implemented yet
|
||||
--
|
||||
function on_initialize(self, data)
|
||||
print("server object got initialization: " .. data)
|
||||
position = object_get_base_position(self)
|
||||
end
|
||||
|
@ -84,6 +84,8 @@ function SmoothTranslator:translate(dtime)
|
||||
if self.anim_time > 0.001 then
|
||||
moveratio = self.anim_time_counter / self.anim_time
|
||||
end
|
||||
-- Move a bit less than should, to avoid oscillation
|
||||
moveratio = moveratio * 0.8
|
||||
if moveratio > 1.5 then
|
||||
moveratio = 1.5
|
||||
end
|
||||
@ -124,10 +126,10 @@ function on_initialize(self, data)
|
||||
print("client object got initialization: " .. data)
|
||||
|
||||
corners = {
|
||||
{-1/2,-1/4, 0},
|
||||
{ 1/2,-1/4, 0},
|
||||
{ 1/2, 1/4, 0},
|
||||
{-1/2, 1/4, 0},
|
||||
{-1/2,-1/2, 0},
|
||||
{ 1/2,-1/2, 0},
|
||||
{ 1/2, 0, 0},
|
||||
{-1/2, 0, 0},
|
||||
}
|
||||
object_add_to_mesh(self, "rat.png", corners, false)
|
||||
|
322
data/scripts/objects/test/server.lua
Normal file
322
data/scripts/objects/test/server.lua
Normal file
@ -0,0 +1,322 @@
|
||||
-- Server-side code of the test lua object
|
||||
|
||||
--
|
||||
-- Some helper functions and classes
|
||||
--
|
||||
|
||||
-- For debugging
|
||||
function dump(o)
|
||||
if type(o) == 'table' then
|
||||
local s = '{ '
|
||||
for k,v in pairs(o) do
|
||||
if type(k) ~= 'number' then k = '"'..k..'"' end
|
||||
s = s .. '['..k..'] = ' .. dump(v) .. ','
|
||||
end
|
||||
return s .. '} '
|
||||
else
|
||||
return tostring(o)
|
||||
end
|
||||
end
|
||||
|
||||
function table.copy(t)
|
||||
local t2 = {}
|
||||
for k,v in pairs(t) do
|
||||
t2[k] = v
|
||||
end
|
||||
return t2
|
||||
end
|
||||
|
||||
function vector_zero()
|
||||
return {X=0,Y=0,Z=0}
|
||||
end
|
||||
|
||||
function vector_subtract(a, b)
|
||||
return {X=a.X-b.X, Y=a.Y-b.Y, Z=a.Z-b.Z}
|
||||
end
|
||||
|
||||
function vector_add(a, b)
|
||||
return {X=a.X+b.X, Y=a.Y+b.Y, Z=a.Z+b.Z}
|
||||
end
|
||||
|
||||
function vector_multiply(a, d)
|
||||
return {X=a.X*d, Y=a.Y*d, Z=a.Z*d}
|
||||
end
|
||||
|
||||
function vector_copy(a)
|
||||
return {X=a.X, Y=a.Y, Z=a.Z}
|
||||
end
|
||||
|
||||
function vector_length(a)
|
||||
return math.sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z)
|
||||
end
|
||||
|
||||
function vector_eq(a, b)
|
||||
return (a.X==b.X and a.Y==b.Y and a.Z==b.Z)
|
||||
end
|
||||
|
||||
function round(num, idp)
|
||||
local mult = 10^(idp or 0)
|
||||
return math.floor(num * mult + 0.5) / mult
|
||||
end
|
||||
|
||||
function vector_snap(a)
|
||||
return {X=round(a.X, 0), Y=round(a.Y, 0), Z=round(a.Z, 0)}
|
||||
end
|
||||
|
||||
--
|
||||
-- Actual code
|
||||
--
|
||||
|
||||
CONTENT_STONE = 0
|
||||
|
||||
is_digger = false
|
||||
counter = 0
|
||||
counter2 = 0
|
||||
counter3 = 0
|
||||
counter4 = 0
|
||||
counter_move = 0
|
||||
death_counter = 0
|
||||
-- This is set in on_initialize()
|
||||
position = {X=0,Y=0,Z=0}
|
||||
starting_position = {X=0,Y=0,Z=0}
|
||||
rotation = {X=0, Y=math.random(0,360), Z=0}
|
||||
y_dir = 1
|
||||
temp1 = 0
|
||||
speed = 1.5
|
||||
main_dir = {X=0,Y=0,Z=0}
|
||||
|
||||
function dir_goodness(env, pos, dir)
|
||||
if vector_eq(dir, vector_zero()) then
|
||||
return -1
|
||||
end
|
||||
p = vector_add(pos, dir)
|
||||
n = env_get_node(env, p)
|
||||
f = get_content_features(n.content)
|
||||
if f.walkable then
|
||||
p.Y = p.Y + 1
|
||||
n = env_get_node(env, p)
|
||||
f = get_content_features(n.content)
|
||||
if f.walkable then
|
||||
-- Too high
|
||||
return -1
|
||||
end
|
||||
-- Hill
|
||||
return 2
|
||||
end
|
||||
p.Y = p.Y - 1
|
||||
n = env_get_node(env, p)
|
||||
f = get_content_features(n.content)
|
||||
if f.walkable then
|
||||
-- Flat
|
||||
return 1
|
||||
end
|
||||
-- Drop
|
||||
return 0
|
||||
end
|
||||
|
||||
function on_step(self, dtime)
|
||||
-- Limit step to a sane value; it jumps a lot while the map generator
|
||||
-- is in action
|
||||
if dtime > 0.5 then
|
||||
dtime = 0.5
|
||||
end
|
||||
|
||||
env = object_get_environment(self)
|
||||
|
||||
--[[
|
||||
-- Returned value has these fields:
|
||||
-- * int content
|
||||
-- * int param1
|
||||
-- * int param2
|
||||
p = {X=position.X, Y=position.Y-0.35, Z=position.Z}
|
||||
n = env_get_node(env, p)
|
||||
f = get_content_features(n.content)
|
||||
if f.walkable then
|
||||
y_dir = 1
|
||||
else
|
||||
y_dir = -1
|
||||
end
|
||||
-- Keep the object approximately at ground level
|
||||
position.Y = position.Y + dtime * 2.0 * y_dir
|
||||
|
||||
-- Move the object around
|
||||
position.X = position.X + math.cos(math.pi+rotation.Y/180*math.pi)
|
||||
* dtime * speed
|
||||
position.Z = position.Z + math.sin(math.pi+rotation.Y/180*math.pi)
|
||||
* dtime * speed
|
||||
|
||||
-- Rotate the object if it is too far from the starting point
|
||||
counter3 = counter3 - dtime
|
||||
if counter3 < 0 then
|
||||
counter3 = counter3 + 1
|
||||
diff = vector_subtract(position, starting_position)
|
||||
d = vector_length(diff)
|
||||
--print("pos="..dump(position).." starting="..dump(starting_position))
|
||||
--print("diff=" .. dump(diff))
|
||||
--print("d=" .. d)
|
||||
if d > 3 then
|
||||
rotation.Y = rotation.Y + 90
|
||||
--rotation.Y = rotation.Y + math.random(-180, 180)
|
||||
end
|
||||
end
|
||||
|
||||
-- This value has to be set; it determines to which player the
|
||||
-- object is near to and such
|
||||
object_set_base_position(self, position)
|
||||
|
||||
counter4 = counter4 - dtime
|
||||
if counter4 < 0 then
|
||||
--counter4 = counter4 + math.random(0.5,8)
|
||||
counter4 = counter4 + 0.6/speed
|
||||
-- Mess around with the map
|
||||
if is_digger == true then
|
||||
np = vector_add(position, {X=0,Y=-0.6,Z=0})
|
||||
env_dig_node(env, np)
|
||||
else
|
||||
np = vector_add(position, {X=0,Y=0,Z=0})
|
||||
env_place_node(env, np, {content=0})
|
||||
end
|
||||
end
|
||||
--]]
|
||||
|
||||
counter_move = counter_move - dtime
|
||||
if counter_move < 0 then
|
||||
counter_move = counter_move + 1/speed
|
||||
if counter_move < 0 then counter_move = 0 end
|
||||
|
||||
old_position = vector_copy(position)
|
||||
|
||||
dirs = {
|
||||
{X=1, Y=0, Z=0},
|
||||
{X=-1, Y=0, Z=0},
|
||||
{X=0, Y=0, Z=1},
|
||||
{X=0, Y=0, Z=-1}
|
||||
}
|
||||
|
||||
best_dir = main_dir
|
||||
best_goodness = dir_goodness(env, position, main_dir)
|
||||
|
||||
for k,v in ipairs(dirs) do
|
||||
-- Don't go directly backwards
|
||||
if not vector_eq(vector_subtract(vector_zero(), v), main_dir) then
|
||||
goodness = dir_goodness(env, position, v)
|
||||
if goodness > best_goodness then
|
||||
best_dir = v
|
||||
goodness = best_goodness
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Place stone block when dir changed
|
||||
if not vector_eq(main_dir, best_dir) then
|
||||
np = vector_add(position, {X=0,Y=0,Z=0})
|
||||
env_place_node(env, np, {content=CONTENT_STONE})
|
||||
end
|
||||
|
||||
main_dir = best_dir
|
||||
|
||||
position = vector_add(position, main_dir)
|
||||
|
||||
pos_diff = vector_subtract(position, old_position)
|
||||
rotation.Y = math.atan2(pos_diff.Z, pos_diff.X)/math.pi*180-180
|
||||
|
||||
-- Returned value has these fields:
|
||||
-- * int content
|
||||
-- * int param1
|
||||
-- * int param2
|
||||
p = {X=position.X, Y=position.Y, Z=position.Z}
|
||||
n = env_get_node(env, p)
|
||||
f = get_content_features(n.content)
|
||||
if f.walkable then
|
||||
position.Y = position.Y + 1
|
||||
end
|
||||
p = {X=position.X, Y=position.Y-1, Z=position.Z}
|
||||
n = env_get_node(env, p)
|
||||
f = get_content_features(n.content)
|
||||
if not f.walkable then
|
||||
position.Y = position.Y - 1
|
||||
end
|
||||
|
||||
-- Center in the middle of the node
|
||||
position = vector_snap(position)
|
||||
end
|
||||
|
||||
-- This value has to be set; it determines to which player the
|
||||
-- object is near to and such
|
||||
object_set_base_position(self, position)
|
||||
|
||||
--[[
|
||||
counter4 = counter4 - dtime
|
||||
if counter4 < 0 then
|
||||
--counter4 = counter4 + math.random(0.5,8)
|
||||
counter4 = counter4 + 0.6/speed
|
||||
-- Mess around with the map
|
||||
if is_digger == true then
|
||||
np = vector_add(position, {X=0,Y=-0.6,Z=0})
|
||||
env_dig_node(env, np)
|
||||
else
|
||||
np = vector_add(position, {X=0,Y=0,Z=0})
|
||||
env_place_node(env, np, {content=0})
|
||||
end
|
||||
end
|
||||
--]]
|
||||
|
||||
---[[
|
||||
-- Send some custom messages at a custom interval
|
||||
|
||||
counter = counter - dtime
|
||||
if counter < 0 then
|
||||
counter = counter + 0.25
|
||||
if counter < 0 then
|
||||
counter = 0
|
||||
end
|
||||
|
||||
message = "pos " .. position.X .. " " .. position.Y .. " " .. position.Z
|
||||
object_add_message(self, message)
|
||||
|
||||
message = "rot " .. rotation.X .. " " .. rotation.Y .. " " .. rotation.Z
|
||||
object_add_message(self, message)
|
||||
end
|
||||
--]]
|
||||
|
||||
-- Remove the object after some time
|
||||
death_counter = death_counter + dtime
|
||||
if death_counter > 40 then
|
||||
object_remove(self)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-- This stuff is passed to a newly created client-side counterpart of this object
|
||||
function on_get_client_init_data(self)
|
||||
-- Just return some data for testing
|
||||
return "result of get_client_init_data"
|
||||
end
|
||||
|
||||
-- This should return some data that mostly saves the state of this object
|
||||
-- Not completely implemented yet
|
||||
function on_get_server_init_data(self)
|
||||
-- Just return some data for testing
|
||||
return "result of get_server_init_data"
|
||||
end
|
||||
|
||||
-- When the object is loaded from scratch, this is called before the first
|
||||
-- on_step(). Data is an empty string or the output of an object spawner
|
||||
-- hook, but such things are not yet implemented.
|
||||
--
|
||||
-- At reload time, the last output of get_server_init_data is passed as data.
|
||||
--
|
||||
-- This should initialize the position of the object to the value returned
|
||||
-- by object_get_base_position(self)
|
||||
--
|
||||
-- Not completely implemented yet
|
||||
--
|
||||
function on_initialize(self, data)
|
||||
print("server object got initialization: " .. data)
|
||||
position = object_get_base_position(self)
|
||||
starting_position = vector_copy(position);
|
||||
if math.random() < 0.5 then
|
||||
is_digger = true
|
||||
end
|
||||
end
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -96,6 +96,37 @@ extern "C"{
|
||||
on_initialize(self, data)
|
||||
*/
|
||||
|
||||
/*
|
||||
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=})
|
||||
*/
|
||||
@ -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,43 +391,77 @@ 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);
|
||||
v3f pos_f = v3f(x,y,z)*BS;
|
||||
|
||||
self->m_removed = true;
|
||||
Player *player = env->getNearestConnectedPlayer(pos_f);
|
||||
|
||||
return 0;
|
||||
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);
|
||||
Player *player = env->getPlayer(name);
|
||||
v3f pos(0,0,0);
|
||||
if(player)
|
||||
pos = player->getPosition();
|
||||
|
||||
ServerEnvironment *env = self->getEnv();
|
||||
env->
|
||||
v3f pos = ;
|
||||
lua_newtable(L);
|
||||
|
||||
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");
|
||||
@ -442,9 +475,16 @@ static int lf_object_remove(lua_State *L)
|
||||
lua_pushstring(L, "Z");
|
||||
lua_pushnumber(L, pos.Z/BS);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
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());
|
||||
|
Loading…
Reference in New Issue
Block a user