Skip to content

Commit 67049eb

Browse files
SmallJokerparamat
authored andcommittedDec 23, 2018
Fix entity rotation in existing worlds (#7989)
1 parent b8b33a6 commit 67049eb

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed
 

‎src/content_sao.cpp

+45-22
Original file line numberDiff line numberDiff line change
@@ -350,26 +350,43 @@ ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
350350
s16 hp = 1;
351351
v3f velocity;
352352
v3f rotation;
353-
if (!data.empty()) {
353+
354+
while (!data.empty()) { // breakable, run for one iteration
354355
std::istringstream is(data, std::ios::binary);
355-
// read version
356+
// 'version' does not allow to incrementally extend the parameter list thus
357+
// we need another variable to build on top of 'version=1'. Ugly hack but works™
358+
u8 version2 = 0;
356359
u8 version = readU8(is);
357-
// check if version is supported
358-
if(version == 0){
359-
name = deSerializeString(is);
360-
state = deSerializeLongString(is);
361-
}
362-
else if(version == 1){
363-
name = deSerializeString(is);
364-
state = deSerializeLongString(is);
365-
hp = readS16(is);
366-
velocity = readV3F1000(is);
367-
rotation = readV3F1000(is);
368-
}
360+
361+
name = deSerializeString(is);
362+
state = deSerializeLongString(is);
363+
364+
if (version < 1)
365+
break;
366+
367+
hp = readS16(is);
368+
velocity = readV3F1000(is);
369+
// yaw must be yaw to be backwards-compatible
370+
rotation.Y = readF1000(is);
371+
372+
if (is.good()) // EOF for old formats
373+
version2 = readU8(is);
374+
375+
if (version2 < 1) // PROTOCOL_VERSION < 37
376+
break;
377+
378+
// version2 >= 1
379+
rotation.X = readF1000(is);
380+
rotation.Z = readF1000(is);
381+
382+
// if (version2 < 2)
383+
// break;
384+
// <read new values>
385+
break;
369386
}
370387
// create object
371-
infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
372-
<<state<<"\")"<<std::endl;
388+
infostream << "LuaEntitySAO::create(name=\"" << name << "\" state=\""
389+
<< state << "\")" << std::endl;
373390
LuaEntitySAO *sao = new LuaEntitySAO(env, pos, name, state);
374391
sao->m_hp = hp;
375392
sao->m_velocity = velocity;
@@ -527,7 +544,7 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
527544
{
528545
std::ostringstream os(std::ios::binary);
529546

530-
// protocol >= 14
547+
// PROTOCOL_VERSION >= 37
531548
writeU8(os, 1); // version
532549
os << serializeString(""); // name
533550
writeU8(os, 0); // is_player
@@ -572,7 +589,7 @@ void LuaEntitySAO::getStaticData(std::string *result) const
572589
{
573590
verbosestream<<FUNCTION_NAME<<std::endl;
574591
std::ostringstream os(std::ios::binary);
575-
// version
592+
// version must be 1 to keep backwards-compatibility. See version2
576593
writeU8(os, 1);
577594
// name
578595
os<<serializeString(m_init_name);
@@ -584,12 +601,18 @@ void LuaEntitySAO::getStaticData(std::string *result) const
584601
} else {
585602
os<<serializeLongString(m_init_state);
586603
}
587-
// hp
588604
writeS16(os, m_hp);
589-
// velocity
590605
writeV3F1000(os, m_velocity);
591-
// rotation
592-
writeV3F1000(os, m_rotation);
606+
// yaw
607+
writeF1000(os, m_rotation.Y);
608+
609+
// version2. Increase this variable for new values
610+
writeU8(os, 1); // PROTOCOL_VERSION >= 37
611+
612+
writeF1000(os, m_rotation.X);
613+
writeF1000(os, m_rotation.Z);
614+
615+
// <write new values>
593616

594617
*result = os.str();
595618
}

0 commit comments

Comments
 (0)
Please sign in to comment.