Compare commits

...

5 Commits

Author SHA1 Message Date
Perttu Ahola 25cf3757b2 Add nodedef.on_blast() to lua_api.txt in order to support chained explosions of any explosives 2012-09-09 23:24:48 +03:00
Perttu Ahola 72ef6f637e Statically store always_collect field of __builtin:item 2012-09-09 23:14:41 +03:00
Perttu Ahola 9cadaf824b Add dtime_s to entity activation 2012-09-09 17:12:29 +03:00
Perttu Ahola 1cc1b93e65 Fix ServerActiveObject stuff 2012-09-09 16:11:05 +03:00
Perttu Ahola c4f400acd0 Fix wielditem entity drawtype brightness control 2012-09-09 13:26:52 +03:00
10 changed files with 82 additions and 40 deletions

View File

@ -57,11 +57,23 @@ minetest.register_entity("__builtin:item", {
end, end,
get_staticdata = function(self) get_staticdata = function(self)
return self.itemstring --return self.itemstring
return minetest.serialize({
itemstring = self.itemstring,
always_collect = self.always_collect,
})
end, end,
on_activate = function(self, staticdata) on_activate = function(self, staticdata)
self.itemstring = staticdata if string.sub(staticdata, 1, string.len("return")) == "return" then
local data = minetest.deserialize(staticdata)
if data and type(data) == "table" then
self.itemstring = data.itemstring
self.always_collect = data.always_collect
end
else
self.itemstring = staticdata
end
self.object:set_armor_groups({immortal=1}) self.object:set_armor_groups({immortal=1})
self.object:setvelocity({x=0, y=2, z=0}) self.object:setvelocity({x=0, y=2, z=0})
self.object:setacceleration({x=0, y=-10, z=0}) self.object:setacceleration({x=0, y=-10, z=0})

View File

@ -1237,7 +1237,7 @@ Entity definition (register_entity)
initial_properties = <initial object properties>, initial_properties = <initial object properties>,
on_activate = function(self, staticdata), on_activate = function(self, staticdata, dtime_s),
on_step = function(self, dtime), on_step = function(self, dtime),
on_punch = function(self, hitter), on_punch = function(self, hitter),
on_rightclick = function(self, clicker), on_rightclick = function(self, clicker),
@ -1421,6 +1421,11 @@ Node definition (register_node)
on_metadata_inventory_take = func(pos, listname, index, stack, player), on_metadata_inventory_take = func(pos, listname, index, stack, player),
^ Called after the actual action has happened, according to what was allowed. ^ Called after the actual action has happened, according to what was allowed.
^ No return value ^ No return value
on_blast = func(pos, intensity),
^ intensity: 1.0 = mid range of regular TNT
^ If defined, called when an explosion touches the node, instead of
removing the node
} }
Recipe for register_craft: (shaped) Recipe for register_craft: (shaped)

View File

@ -40,6 +40,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/serialize.h" #include "util/serialize.h"
#include "util/mathconstants.h" #include "util/mathconstants.h"
#include "map.h" #include "map.h"
#include <IMeshManipulator.h>
class Settings; class Settings;
struct ToolCapabilities; struct ToolCapabilities;
@ -797,8 +798,15 @@ public:
infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl; infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
IItemDefManager *idef = m_gamedef->idef(); IItemDefManager *idef = m_gamedef->idef();
ItemStack item(m_prop.textures[0], 1, 0, "", idef); ItemStack item(m_prop.textures[0], 1, 0, "", idef);
scene::IMesh *mesh = item.getDefinition(idef).wield_mesh; scene::IMesh *item_mesh = item.getDefinition(idef).wield_mesh;
// Copy mesh to be able to set unique vertex colors
scene::IMeshManipulator *manip =
irr->getVideoDriver()->getMeshManipulator();
scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
m_meshnode = smgr->addMeshSceneNode(mesh, NULL); m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
mesh->drop();
m_meshnode->setScale(v3f(m_prop.visual_size.X/2, m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
m_prop.visual_size.Y/2, m_prop.visual_size.Y/2,
@ -838,15 +846,17 @@ public:
{ {
bool is_visible = (m_hp != 0); bool is_visible = (m_hp != 0);
u8 li = decode_light(light_at_pos); u8 li = decode_light(light_at_pos);
m_last_light = li; if(li != m_last_light){
video::SColor color(255,li,li,li); m_last_light = li;
if(m_meshnode){ video::SColor color(255,li,li,li);
setMeshColor(m_meshnode->getMesh(), color); if(m_meshnode){
m_meshnode->setVisible(is_visible); setMeshColor(m_meshnode->getMesh(), color);
} m_meshnode->setVisible(is_visible);
if(m_spritenode){ }
m_spritenode->setColor(color); if(m_spritenode){
m_spritenode->setVisible(is_visible); m_spritenode->setColor(color);
m_spritenode->setVisible(is_visible);
}
} }
} }

View File

@ -376,9 +376,9 @@ LuaEntitySAO::~LuaEntitySAO()
} }
} }
void LuaEntitySAO::addedToEnvironment() void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
{ {
ServerActiveObject::addedToEnvironment(); ServerActiveObject::addedToEnvironment(dtime_s);
// Create entity from name // Create entity from name
lua_State *L = m_env->getLua(); lua_State *L = m_env->getLua();
@ -390,7 +390,7 @@ void LuaEntitySAO::addedToEnvironment()
// Initialize HP from properties // Initialize HP from properties
m_hp = m_prop.hp_max; m_hp = m_prop.hp_max;
// Activate entity, supplying serialized state // Activate entity, supplying serialized state
scriptapi_luaentity_activate(L, m_id, m_init_state.c_str()); scriptapi_luaentity_activate(L, m_id, m_init_state.c_str(), dtime_s);
} }
} }
@ -805,9 +805,9 @@ std::string PlayerSAO::getDescription()
} }
// Called after id has been set and has been inserted in environment // Called after id has been set and has been inserted in environment
void PlayerSAO::addedToEnvironment() void PlayerSAO::addedToEnvironment(u32 dtime_s)
{ {
ServerActiveObject::addedToEnvironment(); ServerActiveObject::addedToEnvironment(dtime_s);
ServerActiveObject::setBasePosition(m_player->getPosition()); ServerActiveObject::setBasePosition(m_player->getPosition());
m_player->setPlayerSAO(this); m_player->setPlayerSAO(this);
m_player->peer_id = m_peer_id; m_player->peer_id = m_peer_id;

View File

@ -43,7 +43,7 @@ public:
{ return ACTIVEOBJECT_TYPE_LUAENTITY; } { return ACTIVEOBJECT_TYPE_LUAENTITY; }
u8 getSendType() const u8 getSendType() const
{ return ACTIVEOBJECT_TYPE_GENERIC; } { return ACTIVEOBJECT_TYPE_GENERIC; }
virtual void addedToEnvironment(); virtual void addedToEnvironment(u32 dtime_s);
static ServerActiveObject* create(ServerEnvironment *env, v3f pos, static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
const std::string &data); const std::string &data);
void step(float dtime, bool send_recommended); void step(float dtime, bool send_recommended);
@ -118,7 +118,7 @@ public:
Active object <-> environment interface Active object <-> environment interface
*/ */
void addedToEnvironment(); void addedToEnvironment(u32 dtime_s);
void removingFromEnvironment(); void removingFromEnvironment();
bool isStaticAllowed() const; bool isStaticAllowed() const;
bool unlimitedTransferDistance() const; bool unlimitedTransferDistance() const;

View File

@ -743,19 +743,25 @@ neighbor_found:
u32 active_object_count = block->m_static_objects.m_active.size(); u32 active_object_count = block->m_static_objects.m_active.size();
// Find out how many objects this and all the neighbors contain // Find out how many objects this and all the neighbors contain
u32 active_object_count_wider = 0; u32 active_object_count_wider = 0;
u32 wider_unknown_count = 0;
for(s16 x=-1; x<=1; x++) for(s16 x=-1; x<=1; x++)
for(s16 y=-1; y<=1; y++) for(s16 y=-1; y<=1; y++)
for(s16 z=-1; z<=1; z++) for(s16 z=-1; z<=1; z++)
{ {
MapBlock *block2 = map->getBlockNoCreateNoEx( MapBlock *block2 = map->getBlockNoCreateNoEx(
block->getPos() + v3s16(x,y,z)); block->getPos() + v3s16(x,y,z));
if(block2==NULL) if(block2==NULL){
wider_unknown_count = 0;
continue; continue;
}
active_object_count_wider += active_object_count_wider +=
block2->m_static_objects.m_active.size() block2->m_static_objects.m_active.size()
+ block2->m_static_objects.m_stored.size(); + block2->m_static_objects.m_stored.size();
} }
// Extrapolate
u32 wider_known_count = 3*3*3 - wider_unknown_count;
active_object_count_wider += wider_unknown_count * active_object_count_wider / wider_known_count;
// Call all the trigger variations // Call all the trigger variations
i->abm->trigger(m_env, p, n); i->abm->trigger(m_env, p, n);
i->abm->trigger(m_env, p, n, i->abm->trigger(m_env, p, n,
@ -784,7 +790,7 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime)
<<dtime_s<<" seconds old."<<std::endl;*/ <<dtime_s<<" seconds old."<<std::endl;*/
// Activate stored objects // Activate stored objects
activateObjects(block); activateObjects(block, dtime_s);
// Run node timers // Run node timers
std::map<v3s16, NodeTimer> elapsed_timers = std::map<v3s16, NodeTimer> elapsed_timers =
@ -1243,7 +1249,7 @@ u16 getFreeServerActiveObjectId(
u16 ServerEnvironment::addActiveObject(ServerActiveObject *object) u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
{ {
assert(object); assert(object);
u16 id = addActiveObjectRaw(object, true); u16 id = addActiveObjectRaw(object, true, 0);
return id; return id;
} }
@ -1402,7 +1408,7 @@ ActiveObjectMessage ServerEnvironment::getActiveObjectMessage()
*/ */
u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
bool set_changed) bool set_changed, u32 dtime_s)
{ {
assert(object); assert(object);
if(object->getId() == 0){ if(object->getId() == 0){
@ -1442,7 +1448,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
// Register reference in scripting api (must be done before post-init) // Register reference in scripting api (must be done before post-init)
scriptapi_add_object_reference(m_lua, object); scriptapi_add_object_reference(m_lua, object);
// Post-initialize object // Post-initialize object
object->addedToEnvironment(); object->addedToEnvironment(dtime_s);
// Add static data to block // Add static data to block
if(object->isStaticAllowed()) if(object->isStaticAllowed())
@ -1465,9 +1471,10 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
"addActiveObjectRaw"); "addActiveObjectRaw");
} }
else{ else{
v3s16 p = floatToInt(objectpos, BS);
errorstream<<"ServerEnvironment::addActiveObjectRaw(): " errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
<<"could not find block for storing id="<<object->getId() <<"could not find block for storing id="<<object->getId()
<<" statically"<<std::endl; <<" statically (pos="<<PP(p)<<")"<<std::endl;
} }
} }
@ -1578,7 +1585,7 @@ static void print_hexdump(std::ostream &o, const std::string &data)
/* /*
Convert stored objects from blocks near the players to active. Convert stored objects from blocks near the players to active.
*/ */
void ServerEnvironment::activateObjects(MapBlock *block) void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
{ {
if(block==NULL) if(block==NULL)
return; return;
@ -1602,7 +1609,7 @@ void ServerEnvironment::activateObjects(MapBlock *block)
"large amount of objects"); "large amount of objects");
return; return;
} }
// A list for objects that couldn't be converted to static for some // A list for objects that couldn't be converted to active for some
// reason. They will be stored back. // reason. They will be stored back.
core::list<StaticObject> new_stored; core::list<StaticObject> new_stored;
// Loop through stored static objects // Loop through stored static objects
@ -1632,7 +1639,7 @@ void ServerEnvironment::activateObjects(MapBlock *block)
<<"activated static object pos="<<PP(s_obj.pos/BS) <<"activated static object pos="<<PP(s_obj.pos/BS)
<<" type="<<(int)s_obj.type<<std::endl; <<" type="<<(int)s_obj.type<<std::endl;
// This will also add the object to the active static list // This will also add the object to the active static list
addActiveObjectRaw(obj, false); addActiveObjectRaw(obj, false, dtime_s);
} }
// Clear stored list // Clear stored list
block->m_static_objects.m_stored.clear(); block->m_static_objects.m_stored.clear();
@ -1756,7 +1763,12 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
// Add to the block where the object is located in // Add to the block where the object is located in
v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS)); v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
// Get or generate the block // Get or generate the block
MapBlock *block = m_map->emergeBlock(blockpos); MapBlock *block = NULL;
try{
block = m_map->emergeBlock(blockpos);
} catch(InvalidPositionException &e){
// Handled via NULL pointer
}
if(block) if(block)
{ {
@ -1793,9 +1805,10 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
} }
else{ else{
if(!force_delete){ if(!force_delete){
v3s16 p = floatToInt(objectpos, BS);
errorstream<<"ServerEnv: Could not find or generate " errorstream<<"ServerEnv: Could not find or generate "
<<"a block for storing id="<<obj->getId() <<"a block for storing id="<<obj->getId()
<<" statically"<<std::endl; <<" statically (pos="<<PP(p)<<")"<<std::endl;
continue; continue;
} }
} }
@ -2129,6 +2142,7 @@ void ClientEnvironment::step(float dtime)
Step active objects and update lighting of them Step active objects and update lighting of them
*/ */
bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
for(core::map<u16, ClientActiveObject*>::Iterator for(core::map<u16, ClientActiveObject*>::Iterator
i = m_active_objects.getIterator(); i = m_active_objects.getIterator();
i.atEnd()==false; i++) i.atEnd()==false; i++)
@ -2137,7 +2151,7 @@ void ClientEnvironment::step(float dtime)
// Step object // Step object
obj->step(dtime, this); obj->step(dtime, this);
if(m_active_object_light_update_interval.step(dtime, 0.21)) if(update_lighting)
{ {
// Update lighting // Update lighting
u8 light = 0; u8 light = 0;

View File

@ -312,7 +312,7 @@ private:
Returns the id of the object. Returns the id of the object.
Returns 0 if not added and thus deleted. Returns 0 if not added and thus deleted.
*/ */
u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed); u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
/* /*
Remove all objects that satisfy (m_removed && m_known_by_count==0) Remove all objects that satisfy (m_removed && m_known_by_count==0)
@ -322,7 +322,7 @@ private:
/* /*
Convert stored objects from block to active Convert stored objects from block to active
*/ */
void activateObjects(MapBlock *block); void activateObjects(MapBlock *block, u32 dtime_s);
/* /*
Convert objects that are not in active blocks to static. Convert objects that are not in active blocks to static.

View File

@ -6483,7 +6483,7 @@ bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name)
} }
void scriptapi_luaentity_activate(lua_State *L, u16 id, void scriptapi_luaentity_activate(lua_State *L, u16 id,
const std::string &staticdata) const std::string &staticdata, u32 dtime_s)
{ {
realitycheck(L); realitycheck(L);
assert(lua_checkstack(L, 20)); assert(lua_checkstack(L, 20));
@ -6501,8 +6501,9 @@ void scriptapi_luaentity_activate(lua_State *L, u16 id,
luaL_checktype(L, -1, LUA_TFUNCTION); luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self lua_pushvalue(L, object); // self
lua_pushlstring(L, staticdata.c_str(), staticdata.size()); lua_pushlstring(L, staticdata.c_str(), staticdata.size());
// Call with 2 arguments, 0 results lua_pushinteger(L, dtime_s);
if(lua_pcall(L, 2, 0, 0)) // Call with 3 arguments, 0 results
if(lua_pcall(L, 3, 0, 0))
script_error(L, "error running function on_activate: %s\n", script_error(L, "error running function on_activate: %s\n",
lua_tostring(L, -1)); lua_tostring(L, -1));
} }

View File

@ -167,7 +167,7 @@ void scriptapi_detached_inventory_on_take(lua_State *L,
// Returns true if succesfully added into Lua; false otherwise. // Returns true if succesfully added into Lua; false otherwise.
bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name); bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name);
void scriptapi_luaentity_activate(lua_State *L, u16 id, void scriptapi_luaentity_activate(lua_State *L, u16 id,
const std::string &staticdata); const std::string &staticdata, u32 dtime_s);
void scriptapi_luaentity_rm(lua_State *L, u16 id); void scriptapi_luaentity_rm(lua_State *L, u16 id);
std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id); std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id);
void scriptapi_luaentity_get_properties(lua_State *L, u16 id, void scriptapi_luaentity_get_properties(lua_State *L, u16 id,

View File

@ -62,7 +62,7 @@ public:
{ return getType(); } { return getType(); }
// Called after id has been set and has been inserted in environment // Called after id has been set and has been inserted in environment
virtual void addedToEnvironment(){}; virtual void addedToEnvironment(u32 dtime_s){};
// Called before removing from environment // Called before removing from environment
virtual void removingFromEnvironment(){}; virtual void removingFromEnvironment(){};
// Returns true if object's deletion is the job of the // Returns true if object's deletion is the job of the