Skip to content

Commit

Permalink
Add Entity get_texture_mod() to Lua API
Browse files Browse the repository at this point in the history
Send texture modifier to clients connecting later too
  • Loading branch information
sapier committed Jan 21, 2017
1 parent 72535d3 commit c57b4ff
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/lua_api.txt
Expand Up @@ -2848,6 +2848,7 @@ This is basically a reference to a C++ `ServerActiveObject`
* `set_yaw(radians)`
* `get_yaw()`: returns number in radians
* `set_texture_mod(mod)`
* `get_texture_mod()` returns current texture modifier
* `set_sprite(p={x=0,y=0}, num_frames=1, framelength=0.2,
select_horiz_by_yawpitch=false)`
* Select sprite from spritesheet with optional animation and DM-style
Expand Down
26 changes: 20 additions & 6 deletions src/content_cao.cpp
Expand Up @@ -574,6 +574,8 @@ GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
m_anim_framelength(0.2),
m_anim_timer(0),
m_reset_textures_timer(-1),
m_previous_texture_modifier(""),
m_current_texture_modifier(""),
m_visuals_expired(false),
m_step_distance_counter(0),
m_last_light(255),
Expand Down Expand Up @@ -952,7 +954,10 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
<<"\" not supported"<<std::endl;
}
updateTextures("");

/* don't update while punch texture modifier is active */
if (m_reset_textures_timer < 0)
updateTextures(m_current_texture_modifier);

scene::ISceneNode *node = getSceneNode();
if (node && m_prop.nametag != "" && !m_is_local_player) {
Expand Down Expand Up @@ -1221,9 +1226,9 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
if(m_reset_textures_timer >= 0)
{
m_reset_textures_timer -= dtime;
if(m_reset_textures_timer <= 0){
if(m_reset_textures_timer <= 0) {
m_reset_textures_timer = -1;
updateTextures("");
updateTextures(m_previous_texture_modifier);
}
}
if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
Expand Down Expand Up @@ -1301,14 +1306,17 @@ void GenericCAO::updateTexturePos()
}
}

void GenericCAO::updateTextures(const std::string &mod)
void GenericCAO::updateTextures(const std::string mod)
{
ITextureSource *tsrc = m_client->tsrc();

bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");

m_previous_texture_modifier = m_current_texture_modifier;
m_current_texture_modifier = mod;

if(m_spritenode)
{
if(m_prop.visual == "sprite")
Expand Down Expand Up @@ -1611,6 +1619,12 @@ void GenericCAO::processMessage(const std::string &data)
updateNodePos();
} else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
std::string mod = deSerializeString(is);

// immediatly reset a engine issued texture modifier if a mod sends a different one
if (m_reset_textures_timer > 0) {
m_reset_textures_timer = -1;
updateTextures(m_previous_texture_modifier);
}
updateTextures(mod);
} else if (cmd == GENERIC_CMD_SET_SPRITE) {
v2s16 p = readV2S16(is);
Expand Down Expand Up @@ -1734,7 +1748,7 @@ void GenericCAO::processMessage(const std::string &data)
m_reset_textures_timer = 0.05;
if(damage >= 2)
m_reset_textures_timer += 0.05 * damage;
updateTextures("^[brighten");
updateTextures(m_current_texture_modifier + "^[brighten");
}
}
} else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
Expand Down Expand Up @@ -1802,7 +1816,7 @@ bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
m_reset_textures_timer = 0.05;
if(result.damage >= 2)
m_reset_textures_timer += 0.05 * result.damage;
updateTextures("^[brighten");
updateTextures(m_current_texture_modifier + "^[brighten");
}

return false;
Expand Down
4 changes: 3 additions & 1 deletion src/content_cao.h
Expand Up @@ -102,6 +102,8 @@ class GenericCAO : public ClientActiveObject
float m_anim_timer;
ItemGroupList m_armor_groups;
float m_reset_textures_timer;
std::string m_previous_texture_modifier; // stores texture modifier before punch update
std::string m_current_texture_modifier; // last applied texture modifier
bool m_visuals_expired;
float m_step_distance_counter;
u8 m_last_light;
Expand Down Expand Up @@ -198,7 +200,7 @@ class GenericCAO : public ClientActiveObject

void updateTexturePos();

void updateTextures(const std::string &mod);
void updateTextures(const std::string mod);

void updateAnimation();

Expand Down
12 changes: 11 additions & 1 deletion src/content_sao.cpp
Expand Up @@ -257,7 +257,8 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
m_last_sent_position(0,0,0),
m_last_sent_velocity(0,0,0),
m_last_sent_position_timer(0),
m_last_sent_move_precision(0)
m_last_sent_move_precision(0),
m_current_texture_modifier("")
{
// Only register type if no environment supplied
if(env == NULL){
Expand Down Expand Up @@ -511,6 +512,9 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
}
}

msg_os << serializeLongString(gob_cmd_set_texture_mod(m_current_texture_modifier));
message_count++;

writeU8(os, message_count);
os.write(msg_os.str().c_str(), msg_os.str().size());
}
Expand Down Expand Up @@ -687,11 +691,17 @@ v3f LuaEntitySAO::getAcceleration()
void LuaEntitySAO::setTextureMod(const std::string &mod)
{
std::string str = gob_cmd_set_texture_mod(mod);
m_current_texture_modifier = mod;
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push(aom);
}

std::string LuaEntitySAO::getTextureMod() const
{
return m_current_texture_modifier;
}

void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
bool select_horiz_by_yawpitch)
{
Expand Down
2 changes: 2 additions & 0 deletions src/content_sao.h
Expand Up @@ -122,6 +122,7 @@ class LuaEntitySAO : public UnitSAO
v3f getAcceleration();

void setTextureMod(const std::string &mod);
std::string getTextureMod() const;
void setSprite(v2s16 p, int num_frames, float framelength,
bool select_horiz_by_yawpitch);
std::string getName();
Expand All @@ -143,6 +144,7 @@ class LuaEntitySAO : public UnitSAO
v3f m_last_sent_velocity;
float m_last_sent_position_timer;
float m_last_sent_move_precision;
std::string m_current_texture_modifier;
};

/*
Expand Down
13 changes: 13 additions & 0 deletions src/script/lua_api/l_object.cpp
Expand Up @@ -900,6 +900,19 @@ int ObjectRef::l_set_texture_mod(lua_State *L)
return 0;
}

// get_texture_mod(self)
int ObjectRef::l_get_texture_mod(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if (co == NULL) return 0;
// Do it
std::string mod = co->getTextureMod();
lua_pushstring(L, mod.c_str());
return 1;
}

// set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
// select_horiz_by_yawpitch=false)
int ObjectRef::l_set_sprite(lua_State *L)
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_object.h
Expand Up @@ -164,6 +164,9 @@ class ObjectRef : public ModApiBase {
// set_texture_mod(self, mod)
static int l_set_texture_mod(lua_State *L);

// l_get_texture_mod(self)
static int l_get_texture_mod(lua_State *L);

// set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
// select_horiz_by_yawpitch=false)
static int l_set_sprite(lua_State *L);
Expand Down

0 comments on commit c57b4ff

Please sign in to comment.