Skip to content

Commit c57b4ff

Browse files
author
sapier
committedJan 21, 2017
Add Entity get_texture_mod() to Lua API
Send texture modifier to clients connecting later too
1 parent 72535d3 commit c57b4ff

File tree

7 files changed

+53
-8
lines changed

7 files changed

+53
-8
lines changed
 

Diff for: ‎doc/lua_api.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,7 @@ This is basically a reference to a C++ `ServerActiveObject`
28482848
* `set_yaw(radians)`
28492849
* `get_yaw()`: returns number in radians
28502850
* `set_texture_mod(mod)`
2851+
* `get_texture_mod()` returns current texture modifier
28512852
* `set_sprite(p={x=0,y=0}, num_frames=1, framelength=0.2,
28522853
select_horiz_by_yawpitch=false)`
28532854
* Select sprite from spritesheet with optional animation and DM-style

Diff for: ‎src/content_cao.cpp

+20-6
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
574574
m_anim_framelength(0.2),
575575
m_anim_timer(0),
576576
m_reset_textures_timer(-1),
577+
m_previous_texture_modifier(""),
578+
m_current_texture_modifier(""),
577579
m_visuals_expired(false),
578580
m_step_distance_counter(0),
579581
m_last_light(255),
@@ -952,7 +954,10 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
952954
infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
953955
<<"\" not supported"<<std::endl;
954956
}
955-
updateTextures("");
957+
958+
/* don't update while punch texture modifier is active */
959+
if (m_reset_textures_timer < 0)
960+
updateTextures(m_current_texture_modifier);
956961

957962
scene::ISceneNode *node = getSceneNode();
958963
if (node && m_prop.nametag != "" && !m_is_local_player) {
@@ -1221,9 +1226,9 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
12211226
if(m_reset_textures_timer >= 0)
12221227
{
12231228
m_reset_textures_timer -= dtime;
1224-
if(m_reset_textures_timer <= 0){
1229+
if(m_reset_textures_timer <= 0) {
12251230
m_reset_textures_timer = -1;
1226-
updateTextures("");
1231+
updateTextures(m_previous_texture_modifier);
12271232
}
12281233
}
12291234
if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
@@ -1301,14 +1306,17 @@ void GenericCAO::updateTexturePos()
13011306
}
13021307
}
13031308

1304-
void GenericCAO::updateTextures(const std::string &mod)
1309+
void GenericCAO::updateTextures(const std::string mod)
13051310
{
13061311
ITextureSource *tsrc = m_client->tsrc();
13071312

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

1317+
m_previous_texture_modifier = m_current_texture_modifier;
1318+
m_current_texture_modifier = mod;
1319+
13121320
if(m_spritenode)
13131321
{
13141322
if(m_prop.visual == "sprite")
@@ -1611,6 +1619,12 @@ void GenericCAO::processMessage(const std::string &data)
16111619
updateNodePos();
16121620
} else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
16131621
std::string mod = deSerializeString(is);
1622+
1623+
// immediatly reset a engine issued texture modifier if a mod sends a different one
1624+
if (m_reset_textures_timer > 0) {
1625+
m_reset_textures_timer = -1;
1626+
updateTextures(m_previous_texture_modifier);
1627+
}
16141628
updateTextures(mod);
16151629
} else if (cmd == GENERIC_CMD_SET_SPRITE) {
16161630
v2s16 p = readV2S16(is);
@@ -1734,7 +1748,7 @@ void GenericCAO::processMessage(const std::string &data)
17341748
m_reset_textures_timer = 0.05;
17351749
if(damage >= 2)
17361750
m_reset_textures_timer += 0.05 * damage;
1737-
updateTextures("^[brighten");
1751+
updateTextures(m_current_texture_modifier + "^[brighten");
17381752
}
17391753
}
17401754
} else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
@@ -1802,7 +1816,7 @@ bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
18021816
m_reset_textures_timer = 0.05;
18031817
if(result.damage >= 2)
18041818
m_reset_textures_timer += 0.05 * result.damage;
1805-
updateTextures("^[brighten");
1819+
updateTextures(m_current_texture_modifier + "^[brighten");
18061820
}
18071821

18081822
return false;

Diff for: ‎src/content_cao.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class GenericCAO : public ClientActiveObject
102102
float m_anim_timer;
103103
ItemGroupList m_armor_groups;
104104
float m_reset_textures_timer;
105+
std::string m_previous_texture_modifier; // stores texture modifier before punch update
106+
std::string m_current_texture_modifier; // last applied texture modifier
105107
bool m_visuals_expired;
106108
float m_step_distance_counter;
107109
u8 m_last_light;
@@ -198,7 +200,7 @@ class GenericCAO : public ClientActiveObject
198200

199201
void updateTexturePos();
200202

201-
void updateTextures(const std::string &mod);
203+
void updateTextures(const std::string mod);
202204

203205
void updateAnimation();
204206

Diff for: ‎src/content_sao.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
257257
m_last_sent_position(0,0,0),
258258
m_last_sent_velocity(0,0,0),
259259
m_last_sent_position_timer(0),
260-
m_last_sent_move_precision(0)
260+
m_last_sent_move_precision(0),
261+
m_current_texture_modifier("")
261262
{
262263
// Only register type if no environment supplied
263264
if(env == NULL){
@@ -511,6 +512,9 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
511512
}
512513
}
513514

515+
msg_os << serializeLongString(gob_cmd_set_texture_mod(m_current_texture_modifier));
516+
message_count++;
517+
514518
writeU8(os, message_count);
515519
os.write(msg_os.str().c_str(), msg_os.str().size());
516520
}
@@ -687,11 +691,17 @@ v3f LuaEntitySAO::getAcceleration()
687691
void LuaEntitySAO::setTextureMod(const std::string &mod)
688692
{
689693
std::string str = gob_cmd_set_texture_mod(mod);
694+
m_current_texture_modifier = mod;
690695
// create message and add to list
691696
ActiveObjectMessage aom(getId(), true, str);
692697
m_messages_out.push(aom);
693698
}
694699

700+
std::string LuaEntitySAO::getTextureMod() const
701+
{
702+
return m_current_texture_modifier;
703+
}
704+
695705
void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
696706
bool select_horiz_by_yawpitch)
697707
{

Diff for: ‎src/content_sao.h

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class LuaEntitySAO : public UnitSAO
122122
v3f getAcceleration();
123123

124124
void setTextureMod(const std::string &mod);
125+
std::string getTextureMod() const;
125126
void setSprite(v2s16 p, int num_frames, float framelength,
126127
bool select_horiz_by_yawpitch);
127128
std::string getName();
@@ -143,6 +144,7 @@ class LuaEntitySAO : public UnitSAO
143144
v3f m_last_sent_velocity;
144145
float m_last_sent_position_timer;
145146
float m_last_sent_move_precision;
147+
std::string m_current_texture_modifier;
146148
};
147149

148150
/*

Diff for: ‎src/script/lua_api/l_object.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,19 @@ int ObjectRef::l_set_texture_mod(lua_State *L)
900900
return 0;
901901
}
902902

903+
// get_texture_mod(self)
904+
int ObjectRef::l_get_texture_mod(lua_State *L)
905+
{
906+
NO_MAP_LOCK_REQUIRED;
907+
ObjectRef *ref = checkobject(L, 1);
908+
LuaEntitySAO *co = getluaobject(ref);
909+
if (co == NULL) return 0;
910+
// Do it
911+
std::string mod = co->getTextureMod();
912+
lua_pushstring(L, mod.c_str());
913+
return 1;
914+
}
915+
903916
// set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
904917
// select_horiz_by_yawpitch=false)
905918
int ObjectRef::l_set_sprite(lua_State *L)

Diff for: ‎src/script/lua_api/l_object.h

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ class ObjectRef : public ModApiBase {
164164
// set_texture_mod(self, mod)
165165
static int l_set_texture_mod(lua_State *L);
166166

167+
// l_get_texture_mod(self)
168+
static int l_get_texture_mod(lua_State *L);
169+
167170
// set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
168171
// select_horiz_by_yawpitch=false)
169172
static int l_set_sprite(lua_State *L);

0 commit comments

Comments
 (0)