content_cao.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <ICameraSceneNode.h>
  17. #include <ITextSceneNode.h>
  18. #include <IBillboardSceneNode.h>
  19. #include <IMeshManipulator.h>
  20. #include <IAnimatedMeshSceneNode.h>
  21. #include "content_cao.h"
  22. #include "util/numeric.h" // For IntervalLimiter
  23. #include "util/serialize.h"
  24. #include "util/basic_macros.h"
  25. #include "client/sound.h"
  26. #include "client/tile.h"
  27. #include "environment.h"
  28. #include "collision.h"
  29. #include "settings.h"
  30. #include "serialization.h" // For decompressZlib
  31. #include "clientobject.h"
  32. #include "mesh.h"
  33. #include "itemdef.h"
  34. #include "tool.h"
  35. #include "content_cso.h"
  36. #include "sound.h"
  37. #include "nodedef.h"
  38. #include "localplayer.h"
  39. #include "map.h"
  40. #include "camera.h" // CameraModes
  41. #include "client.h"
  42. #include "wieldmesh.h"
  43. #include <algorithm>
  44. #include "client/renderingengine.h"
  45. class Settings;
  46. struct ToolCapabilities;
  47. std::unordered_map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
  48. void SmoothTranslator::init(v3f vect)
  49. {
  50. vect_old = vect;
  51. vect_show = vect;
  52. vect_aim = vect;
  53. anim_counter = 0;
  54. anim_time = 0;
  55. anim_time_counter = 0;
  56. aim_is_end = true;
  57. }
  58. void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
  59. {
  60. aim_is_end = is_end_position;
  61. vect_old = vect_show;
  62. vect_aim = vect_new;
  63. if(update_interval > 0)
  64. {
  65. anim_time = update_interval;
  66. } else {
  67. if(anim_time < 0.001 || anim_time > 1.0)
  68. anim_time = anim_time_counter;
  69. else
  70. anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
  71. }
  72. anim_time_counter = 0;
  73. anim_counter = 0;
  74. }
  75. void SmoothTranslator::translate(f32 dtime)
  76. {
  77. anim_time_counter = anim_time_counter + dtime;
  78. anim_counter = anim_counter + dtime;
  79. v3f vect_move = vect_aim - vect_old;
  80. f32 moveratio = 1.0;
  81. if(anim_time > 0.001)
  82. moveratio = anim_time_counter / anim_time;
  83. // Move a bit less than should, to avoid oscillation
  84. moveratio = moveratio * 0.8;
  85. float move_end = 1.5;
  86. if(aim_is_end)
  87. move_end = 1.0;
  88. if(moveratio > move_end)
  89. moveratio = move_end;
  90. vect_show = vect_old + vect_move * moveratio;
  91. }
  92. /*
  93. Other stuff
  94. */
  95. static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
  96. float txs, float tys, int col, int row)
  97. {
  98. video::SMaterial& material = bill->getMaterial(0);
  99. core::matrix4& matrix = material.getTextureMatrix(0);
  100. matrix.setTextureTranslate(txs*col, tys*row);
  101. matrix.setTextureScale(txs, tys);
  102. }
  103. /*
  104. TestCAO
  105. */
  106. class TestCAO : public ClientActiveObject
  107. {
  108. public:
  109. TestCAO(Client *client, ClientEnvironment *env);
  110. virtual ~TestCAO() = default;
  111. ActiveObjectType getType() const
  112. {
  113. return ACTIVEOBJECT_TYPE_TEST;
  114. }
  115. static ClientActiveObject* create(Client *client, ClientEnvironment *env);
  116. void addToScene(ITextureSource *tsrc);
  117. void removeFromScene(bool permanent);
  118. void updateLight(u8 light_at_pos);
  119. v3s16 getLightPosition();
  120. void updateNodePos();
  121. void step(float dtime, ClientEnvironment *env);
  122. void processMessage(const std::string &data);
  123. bool getCollisionBox(aabb3f *toset) const { return false; }
  124. private:
  125. scene::IMeshSceneNode *m_node;
  126. v3f m_position;
  127. };
  128. // Prototype
  129. TestCAO proto_TestCAO(NULL, NULL);
  130. TestCAO::TestCAO(Client *client, ClientEnvironment *env):
  131. ClientActiveObject(0, client, env),
  132. m_node(NULL),
  133. m_position(v3f(0,10*BS,0))
  134. {
  135. ClientActiveObject::registerType(getType(), create);
  136. }
  137. ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
  138. {
  139. return new TestCAO(client, env);
  140. }
  141. void TestCAO::addToScene(ITextureSource *tsrc)
  142. {
  143. if(m_node != NULL)
  144. return;
  145. //video::IVideoDriver* driver = smgr->getVideoDriver();
  146. scene::SMesh *mesh = new scene::SMesh();
  147. scene::IMeshBuffer *buf = new scene::SMeshBuffer();
  148. video::SColor c(255,255,255,255);
  149. video::S3DVertex vertices[4] =
  150. {
  151. video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
  152. video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
  153. video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
  154. video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
  155. };
  156. u16 indices[] = {0,1,2,2,3,0};
  157. buf->append(vertices, 4, indices, 6);
  158. // Set material
  159. buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
  160. buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
  161. buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
  162. buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
  163. buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
  164. buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  165. // Add to mesh
  166. mesh->addMeshBuffer(buf);
  167. buf->drop();
  168. m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
  169. mesh->drop();
  170. updateNodePos();
  171. }
  172. void TestCAO::removeFromScene(bool permanent)
  173. {
  174. if (!m_node)
  175. return;
  176. m_node->remove();
  177. m_node = NULL;
  178. }
  179. void TestCAO::updateLight(u8 light_at_pos)
  180. {
  181. }
  182. v3s16 TestCAO::getLightPosition()
  183. {
  184. return floatToInt(m_position, BS);
  185. }
  186. void TestCAO::updateNodePos()
  187. {
  188. if (!m_node)
  189. return;
  190. m_node->setPosition(m_position);
  191. //m_node->setRotation(v3f(0, 45, 0));
  192. }
  193. void TestCAO::step(float dtime, ClientEnvironment *env)
  194. {
  195. if(m_node)
  196. {
  197. v3f rot = m_node->getRotation();
  198. //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
  199. rot.Y += dtime * 180;
  200. m_node->setRotation(rot);
  201. }
  202. }
  203. void TestCAO::processMessage(const std::string &data)
  204. {
  205. infostream<<"TestCAO: Got data: "<<data<<std::endl;
  206. std::istringstream is(data, std::ios::binary);
  207. u16 cmd;
  208. is>>cmd;
  209. if(cmd == 0)
  210. {
  211. v3f newpos;
  212. is>>newpos.X;
  213. is>>newpos.Y;
  214. is>>newpos.Z;
  215. m_position = newpos;
  216. updateNodePos();
  217. }
  218. }
  219. /*
  220. GenericCAO
  221. */
  222. #include "genericobject.h"
  223. GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
  224. ClientActiveObject(0, client, env)
  225. {
  226. if (client == NULL) {
  227. ClientActiveObject::registerType(getType(), create);
  228. } else {
  229. m_client = client;
  230. }
  231. }
  232. bool GenericCAO::getCollisionBox(aabb3f *toset) const
  233. {
  234. if (m_prop.physical)
  235. {
  236. //update collision box
  237. toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
  238. toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
  239. toset->MinEdge += m_position;
  240. toset->MaxEdge += m_position;
  241. return true;
  242. }
  243. return false;
  244. }
  245. bool GenericCAO::collideWithObjects() const
  246. {
  247. return m_prop.collideWithObjects;
  248. }
  249. void GenericCAO::initialize(const std::string &data)
  250. {
  251. infostream<<"GenericCAO: Got init data"<<std::endl;
  252. processInitData(data);
  253. if (m_is_player) {
  254. // Check if it's the current player
  255. LocalPlayer *player = m_env->getLocalPlayer();
  256. if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
  257. m_is_local_player = true;
  258. m_is_visible = false;
  259. player->setCAO(this);
  260. }
  261. }
  262. }
  263. void GenericCAO::processInitData(const std::string &data)
  264. {
  265. std::istringstream is(data, std::ios::binary);
  266. int num_messages = 0;
  267. // version
  268. u8 version = readU8(is);
  269. // check version
  270. if (version == 1) { // In PROTOCOL_VERSION 14
  271. m_name = deSerializeString(is);
  272. m_is_player = readU8(is);
  273. m_id = readU16(is);
  274. m_position = readV3F1000(is);
  275. m_yaw = readF1000(is);
  276. m_hp = readS16(is);
  277. num_messages = readU8(is);
  278. } else {
  279. errorstream<<"GenericCAO: Unsupported init data version"
  280. <<std::endl;
  281. return;
  282. }
  283. for (int i = 0; i < num_messages; i++) {
  284. std::string message = deSerializeLongString(is);
  285. processMessage(message);
  286. }
  287. pos_translator.init(m_position);
  288. updateNodePos();
  289. }
  290. GenericCAO::~GenericCAO()
  291. {
  292. removeFromScene(true);
  293. }
  294. bool GenericCAO::getSelectionBox(aabb3f *toset) const
  295. {
  296. if (!m_prop.is_visible || !m_is_visible || m_is_local_player
  297. || !m_prop.pointable || getParent() != NULL) {
  298. return false;
  299. }
  300. *toset = m_selection_box;
  301. return true;
  302. }
  303. v3f GenericCAO::getPosition()
  304. {
  305. if (getParent() != NULL) {
  306. scene::ISceneNode *node = getSceneNode();
  307. if (node)
  308. return node->getAbsolutePosition();
  309. return m_position;
  310. }
  311. return pos_translator.vect_show;
  312. }
  313. const bool GenericCAO::isImmortal()
  314. {
  315. return itemgroup_get(getGroups(), "immortal");
  316. }
  317. scene::ISceneNode* GenericCAO::getSceneNode()
  318. {
  319. if (m_meshnode) {
  320. return m_meshnode;
  321. }
  322. if (m_animated_meshnode) {
  323. return m_animated_meshnode;
  324. }
  325. if (m_wield_meshnode) {
  326. return m_wield_meshnode;
  327. }
  328. if (m_spritenode) {
  329. return m_spritenode;
  330. }
  331. return NULL;
  332. }
  333. scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
  334. {
  335. return m_animated_meshnode;
  336. }
  337. void GenericCAO::setChildrenVisible(bool toset)
  338. {
  339. for (u16 cao_id : m_children) {
  340. GenericCAO *obj = m_env->getGenericCAO(cao_id);
  341. if (obj) {
  342. obj->setVisible(toset);
  343. }
  344. }
  345. }
  346. void GenericCAO::setAttachments()
  347. {
  348. updateAttachments();
  349. }
  350. ClientActiveObject* GenericCAO::getParent() const
  351. {
  352. ClientActiveObject *obj = NULL;
  353. u16 attached_id = m_env->attachement_parent_ids[getId()];
  354. if ((attached_id != 0) &&
  355. (attached_id != getId())) {
  356. obj = m_env->getActiveObject(attached_id);
  357. }
  358. return obj;
  359. }
  360. void GenericCAO::removeFromScene(bool permanent)
  361. {
  362. // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
  363. if((m_env != NULL) && (permanent))
  364. {
  365. for (u16 ci : m_children) {
  366. if (m_env->attachement_parent_ids[ci] == getId()) {
  367. m_env->attachement_parent_ids[ci] = 0;
  368. }
  369. }
  370. m_env->attachement_parent_ids[getId()] = 0;
  371. LocalPlayer* player = m_env->getLocalPlayer();
  372. if (this == player->parent) {
  373. player->parent = NULL;
  374. player->isAttached = false;
  375. }
  376. }
  377. if (m_meshnode) {
  378. m_meshnode->remove();
  379. m_meshnode->drop();
  380. m_meshnode = NULL;
  381. } else if (m_animated_meshnode) {
  382. m_animated_meshnode->remove();
  383. m_animated_meshnode->drop();
  384. m_animated_meshnode = NULL;
  385. } else if (m_wield_meshnode) {
  386. m_wield_meshnode->remove();
  387. m_wield_meshnode->drop();
  388. m_wield_meshnode = NULL;
  389. } else if (m_spritenode) {
  390. m_spritenode->remove();
  391. m_spritenode->drop();
  392. m_spritenode = NULL;
  393. }
  394. if (m_nametag) {
  395. m_client->getCamera()->removeNametag(m_nametag);
  396. m_nametag = NULL;
  397. }
  398. }
  399. void GenericCAO::addToScene(ITextureSource *tsrc)
  400. {
  401. m_smgr = RenderingEngine::get_scene_manager();
  402. if (getSceneNode() != NULL) {
  403. return;
  404. }
  405. m_visuals_expired = false;
  406. if (!m_prop.is_visible) {
  407. return;
  408. }
  409. if (m_prop.visual == "sprite") {
  410. infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
  411. m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
  412. NULL, v2f(1, 1), v3f(0,0,0), -1);
  413. m_spritenode->grab();
  414. m_spritenode->setMaterialTexture(0,
  415. tsrc->getTextureForMesh("unknown_node.png"));
  416. m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
  417. m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
  418. m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
  419. m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
  420. u8 li = m_last_light;
  421. m_spritenode->setColor(video::SColor(255,li,li,li));
  422. m_spritenode->setSize(m_prop.visual_size*BS);
  423. {
  424. const float txs = 1.0 / 1;
  425. const float tys = 1.0 / 1;
  426. setBillboardTextureMatrix(m_spritenode,
  427. txs, tys, 0, 0);
  428. }
  429. } else if (m_prop.visual == "upright_sprite") {
  430. scene::SMesh *mesh = new scene::SMesh();
  431. double dx = BS * m_prop.visual_size.X / 2;
  432. double dy = BS * m_prop.visual_size.Y / 2;
  433. u8 li = m_last_light;
  434. video::SColor c(255, li, li, li);
  435. { // Front
  436. scene::IMeshBuffer *buf = new scene::SMeshBuffer();
  437. video::S3DVertex vertices[4] = {
  438. video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
  439. video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
  440. video::S3DVertex( dx, dy, 0, 0,0,0, c, 0,0),
  441. video::S3DVertex(-dx, dy, 0, 0,0,0, c, 1,0),
  442. };
  443. u16 indices[] = {0,1,2,2,3,0};
  444. buf->append(vertices, 4, indices, 6);
  445. // Set material
  446. buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
  447. buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
  448. buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
  449. buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  450. // Add to mesh
  451. mesh->addMeshBuffer(buf);
  452. buf->drop();
  453. }
  454. { // Back
  455. scene::IMeshBuffer *buf = new scene::SMeshBuffer();
  456. video::S3DVertex vertices[4] = {
  457. video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
  458. video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
  459. video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
  460. video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
  461. };
  462. u16 indices[] = {0,1,2,2,3,0};
  463. buf->append(vertices, 4, indices, 6);
  464. // Set material
  465. buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
  466. buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
  467. buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
  468. buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
  469. // Add to mesh
  470. mesh->addMeshBuffer(buf);
  471. buf->drop();
  472. }
  473. m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
  474. m_meshnode->grab();
  475. mesh->drop();
  476. // Set it to use the materials of the meshbuffers directly.
  477. // This is needed for changing the texture in the future
  478. m_meshnode->setReadOnlyMaterials(true);
  479. }
  480. else if(m_prop.visual == "cube") {
  481. infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
  482. scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
  483. m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
  484. m_meshnode->grab();
  485. mesh->drop();
  486. m_meshnode->setScale(v3f(m_prop.visual_size.X,
  487. m_prop.visual_size.Y,
  488. m_prop.visual_size.X));
  489. u8 li = m_last_light;
  490. setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
  491. m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
  492. m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
  493. m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
  494. m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
  495. }
  496. else if(m_prop.visual == "mesh") {
  497. infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
  498. scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
  499. if(mesh)
  500. {
  501. m_animated_meshnode = RenderingEngine::get_scene_manager()->
  502. addAnimatedMeshSceneNode(mesh, NULL);
  503. m_animated_meshnode->grab();
  504. mesh->drop(); // The scene node took hold of it
  505. m_animated_meshnode->animateJoints(); // Needed for some animations
  506. m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
  507. m_prop.visual_size.Y,
  508. m_prop.visual_size.X));
  509. u8 li = m_last_light;
  510. // set vertex colors to ensure alpha is set
  511. setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
  512. setAnimatedMeshColor(m_animated_meshnode, video::SColor(255,li,li,li));
  513. bool backface_culling = m_prop.backface_culling;
  514. if (m_is_player)
  515. backface_culling = false;
  516. m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, true);
  517. m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
  518. m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
  519. m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
  520. m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
  521. }
  522. else
  523. errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
  524. } else if (m_prop.visual == "wielditem") {
  525. ItemStack item;
  526. infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
  527. if (m_prop.wield_item.empty()) {
  528. // Old format, only textures are specified.
  529. infostream << "textures: " << m_prop.textures.size() << std::endl;
  530. if (!m_prop.textures.empty()) {
  531. infostream << "textures[0]: " << m_prop.textures[0]
  532. << std::endl;
  533. IItemDefManager *idef = m_client->idef();
  534. item = ItemStack(m_prop.textures[0], 1, 0, idef);
  535. }
  536. } else {
  537. infostream << "serialized form: " << m_prop.wield_item << std::endl;
  538. item.deSerialize(m_prop.wield_item, m_client->idef());
  539. }
  540. m_wield_meshnode = new WieldMeshSceneNode(
  541. RenderingEngine::get_scene_manager(), -1);
  542. m_wield_meshnode->setItem(item, m_client);
  543. m_wield_meshnode->setScale(
  544. v3f(m_prop.visual_size.X / 2, m_prop.visual_size.Y / 2,
  545. m_prop.visual_size.X / 2));
  546. u8 li = m_last_light;
  547. m_wield_meshnode->setColor(video::SColor(255, li, li, li));
  548. } else {
  549. infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
  550. <<"\" not supported"<<std::endl;
  551. }
  552. /* don't update while punch texture modifier is active */
  553. if (m_reset_textures_timer < 0)
  554. updateTextures(m_current_texture_modifier);
  555. scene::ISceneNode *node = getSceneNode();
  556. if (node && !m_prop.nametag.empty() && !m_is_local_player) {
  557. // Add nametag
  558. v3f pos;
  559. pos.Y = m_prop.selectionbox.MaxEdge.Y + 0.3f;
  560. m_nametag = m_client->getCamera()->addNametag(node,
  561. m_prop.nametag, m_prop.nametag_color,
  562. pos);
  563. }
  564. updateNodePos();
  565. updateAnimation();
  566. updateBonePosition();
  567. updateAttachments();
  568. }
  569. void GenericCAO::updateLight(u8 light_at_pos)
  570. {
  571. // Don't update light of attached one
  572. if (getParent() != NULL) {
  573. return;
  574. }
  575. updateLightNoCheck(light_at_pos);
  576. // Update light of all children
  577. for (u16 i : m_children) {
  578. ClientActiveObject *obj = m_env->getActiveObject(i);
  579. if (obj) {
  580. obj->updateLightNoCheck(light_at_pos);
  581. }
  582. }
  583. }
  584. void GenericCAO::updateLightNoCheck(u8 light_at_pos)
  585. {
  586. if (m_glow < 0)
  587. return;
  588. u8 li = decode_light(light_at_pos + m_glow);
  589. if (li != m_last_light) {
  590. m_last_light = li;
  591. video::SColor color(255,li,li,li);
  592. if (m_meshnode) {
  593. setMeshColor(m_meshnode->getMesh(), color);
  594. } else if (m_animated_meshnode) {
  595. setAnimatedMeshColor(m_animated_meshnode, color);
  596. } else if (m_wield_meshnode) {
  597. m_wield_meshnode->setColor(color);
  598. } else if (m_spritenode) {
  599. m_spritenode->setColor(color);
  600. }
  601. }
  602. }
  603. v3s16 GenericCAO::getLightPosition()
  604. {
  605. if (m_is_player)
  606. return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
  607. return floatToInt(m_position, BS);
  608. }
  609. void GenericCAO::updateNodePos()
  610. {
  611. if (getParent() != NULL)
  612. return;
  613. scene::ISceneNode *node = getSceneNode();
  614. if (node) {
  615. v3s16 camera_offset = m_env->getCameraOffset();
  616. node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
  617. if (node != m_spritenode) { // rotate if not a sprite
  618. v3f rot = node->getRotation();
  619. rot.Y = -m_yaw;
  620. node->setRotation(rot);
  621. }
  622. }
  623. }
  624. void GenericCAO::step(float dtime, ClientEnvironment *env)
  625. {
  626. // Handel model of local player instantly to prevent lags
  627. if (m_is_local_player) {
  628. LocalPlayer *player = m_env->getLocalPlayer();
  629. if (m_is_visible) {
  630. int old_anim = player->last_animation;
  631. float old_anim_speed = player->last_animation_speed;
  632. m_position = player->getPosition();
  633. m_velocity = v3f(0,0,0);
  634. m_acceleration = v3f(0,0,0);
  635. pos_translator.vect_show = m_position;
  636. m_yaw = player->getYaw();
  637. const PlayerControl &controls = player->getPlayerControl();
  638. bool walking = false;
  639. if (controls.up || controls.down || controls.left || controls.right ||
  640. controls.forw_move_joystick_axis != 0.f ||
  641. controls.sidew_move_joystick_axis != 0.f)
  642. walking = true;
  643. f32 new_speed = player->local_animation_speed;
  644. v2s32 new_anim = v2s32(0,0);
  645. bool allow_update = false;
  646. // increase speed if using fast or flying fast
  647. if((g_settings->getBool("fast_move") &&
  648. m_client->checkLocalPrivilege("fast")) &&
  649. (controls.aux1 ||
  650. (!player->touching_ground &&
  651. g_settings->getBool("free_move") &&
  652. m_client->checkLocalPrivilege("fly"))))
  653. new_speed *= 1.5;
  654. // slowdown speed if sneeking
  655. if (controls.sneak && walking)
  656. new_speed /= 2;
  657. if (walking && (controls.LMB || controls.RMB)) {
  658. new_anim = player->local_animations[3];
  659. player->last_animation = WD_ANIM;
  660. } else if(walking) {
  661. new_anim = player->local_animations[1];
  662. player->last_animation = WALK_ANIM;
  663. } else if(controls.LMB || controls.RMB) {
  664. new_anim = player->local_animations[2];
  665. player->last_animation = DIG_ANIM;
  666. }
  667. // Apply animations if input detected and not attached
  668. // or set idle animation
  669. if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
  670. allow_update = true;
  671. m_animation_range = new_anim;
  672. m_animation_speed = new_speed;
  673. player->last_animation_speed = m_animation_speed;
  674. } else {
  675. player->last_animation = NO_ANIM;
  676. if (old_anim != NO_ANIM) {
  677. m_animation_range = player->local_animations[0];
  678. updateAnimation();
  679. }
  680. }
  681. // Update local player animations
  682. if ((player->last_animation != old_anim ||
  683. m_animation_speed != old_anim_speed) &&
  684. player->last_animation != NO_ANIM && allow_update)
  685. updateAnimation();
  686. }
  687. }
  688. if (m_visuals_expired && m_smgr) {
  689. m_visuals_expired = false;
  690. // Attachments, part 1: All attached objects must be unparented first,
  691. // or Irrlicht causes a segmentation fault
  692. for (auto ci = m_children.begin(); ci != m_children.end();) {
  693. if (m_env->attachement_parent_ids[*ci] != getId()) {
  694. ci = m_children.erase(ci);
  695. continue;
  696. }
  697. ClientActiveObject *obj = m_env->getActiveObject(*ci);
  698. if (obj) {
  699. scene::ISceneNode *child_node = obj->getSceneNode();
  700. if (child_node)
  701. child_node->setParent(m_smgr->getRootSceneNode());
  702. }
  703. ++ci;
  704. }
  705. removeFromScene(false);
  706. addToScene(m_client->tsrc());
  707. // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
  708. for (u16 cao_id : m_children) {
  709. // Get the object of the child
  710. ClientActiveObject *obj = m_env->getActiveObject(cao_id);
  711. if (obj)
  712. obj->setAttachments();
  713. }
  714. }
  715. // Make sure m_is_visible is always applied
  716. scene::ISceneNode *node = getSceneNode();
  717. if (node)
  718. node->setVisible(m_is_visible);
  719. if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
  720. {
  721. // Set these for later
  722. m_position = getPosition();
  723. m_velocity = v3f(0,0,0);
  724. m_acceleration = v3f(0,0,0);
  725. pos_translator.vect_show = m_position;
  726. if(m_is_local_player) // Update local player attachment position
  727. {
  728. LocalPlayer *player = m_env->getLocalPlayer();
  729. player->overridePosition = getParent()->getPosition();
  730. m_env->getLocalPlayer()->parent = getParent();
  731. }
  732. } else {
  733. v3f lastpos = pos_translator.vect_show;
  734. if(m_prop.physical)
  735. {
  736. aabb3f box = m_prop.collisionbox;
  737. box.MinEdge *= BS;
  738. box.MaxEdge *= BS;
  739. collisionMoveResult moveresult;
  740. f32 pos_max_d = BS*0.125; // Distance per iteration
  741. v3f p_pos = m_position;
  742. v3f p_velocity = m_velocity;
  743. moveresult = collisionMoveSimple(env,env->getGameDef(),
  744. pos_max_d, box, m_prop.stepheight, dtime,
  745. &p_pos, &p_velocity, m_acceleration,
  746. this, m_prop.collideWithObjects);
  747. // Apply results
  748. m_position = p_pos;
  749. m_velocity = p_velocity;
  750. bool is_end_position = moveresult.collides;
  751. pos_translator.update(m_position, is_end_position, dtime);
  752. pos_translator.translate(dtime);
  753. updateNodePos();
  754. } else {
  755. m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
  756. m_velocity += dtime * m_acceleration;
  757. pos_translator.update(m_position, pos_translator.aim_is_end,
  758. pos_translator.anim_time);
  759. pos_translator.translate(dtime);
  760. updateNodePos();
  761. }
  762. float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
  763. m_step_distance_counter += moved;
  764. if (m_step_distance_counter > 1.5f * BS) {
  765. m_step_distance_counter = 0.0f;
  766. if (!m_is_local_player && m_prop.makes_footstep_sound) {
  767. const NodeDefManager *ndef = m_client->ndef();
  768. v3s16 p = floatToInt(getPosition() +
  769. v3f(0.0f, (m_prop.collisionbox.MinEdge.Y - 0.5f) * BS, 0.0f), BS);
  770. MapNode n = m_env->getMap().getNodeNoEx(p);
  771. SimpleSoundSpec spec = ndef->get(n).sound_footstep;
  772. // Reduce footstep gain, as non-local-player footsteps are
  773. // somehow louder.
  774. spec.gain *= 0.6f;
  775. m_client->sound()->playSoundAt(spec, false, getPosition());
  776. }
  777. }
  778. }
  779. m_anim_timer += dtime;
  780. if(m_anim_timer >= m_anim_framelength)
  781. {
  782. m_anim_timer -= m_anim_framelength;
  783. m_anim_frame++;
  784. if(m_anim_frame >= m_anim_num_frames)
  785. m_anim_frame = 0;
  786. }
  787. updateTexturePos();
  788. if(m_reset_textures_timer >= 0)
  789. {
  790. m_reset_textures_timer -= dtime;
  791. if(m_reset_textures_timer <= 0) {
  792. m_reset_textures_timer = -1;
  793. updateTextures(m_previous_texture_modifier);
  794. }
  795. }
  796. if(!getParent() && fabs(m_prop.automatic_rotate) > 0.001)
  797. {
  798. m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
  799. updateNodePos();
  800. }
  801. if (!getParent() && m_prop.automatic_face_movement_dir &&
  802. (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
  803. float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
  804. + m_prop.automatic_face_movement_dir_offset;
  805. float max_rotation_delta =
  806. dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
  807. float delta = wrapDegrees_0_360(target_yaw - m_yaw);
  808. if (delta > max_rotation_delta && 360 - delta > max_rotation_delta) {
  809. m_yaw += (delta < 180) ? max_rotation_delta : -max_rotation_delta;
  810. m_yaw = wrapDegrees_0_360(m_yaw);
  811. } else {
  812. m_yaw = target_yaw;
  813. }
  814. updateNodePos();
  815. }
  816. }
  817. void GenericCAO::updateTexturePos()
  818. {
  819. if(m_spritenode)
  820. {
  821. scene::ICameraSceneNode* camera =
  822. m_spritenode->getSceneManager()->getActiveCamera();
  823. if(!camera)
  824. return;
  825. v3f cam_to_entity = m_spritenode->getAbsolutePosition()
  826. - camera->getAbsolutePosition();
  827. cam_to_entity.normalize();
  828. int row = m_tx_basepos.Y;
  829. int col = m_tx_basepos.X;
  830. if(m_tx_select_horiz_by_yawpitch)
  831. {
  832. if(cam_to_entity.Y > 0.75)
  833. col += 5;
  834. else if(cam_to_entity.Y < -0.75)
  835. col += 4;
  836. else{
  837. float mob_dir =
  838. atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
  839. float dir = mob_dir - m_yaw;
  840. dir = wrapDegrees_180(dir);
  841. //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
  842. if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
  843. col += 2;
  844. else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
  845. col += 3;
  846. else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
  847. col += 0;
  848. else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
  849. col += 1;
  850. else
  851. col += 4;
  852. }
  853. }
  854. // Animation goes downwards
  855. row += m_anim_frame;
  856. float txs = m_tx_size.X;
  857. float tys = m_tx_size.Y;
  858. setBillboardTextureMatrix(m_spritenode,
  859. txs, tys, col, row);
  860. }
  861. }
  862. void GenericCAO::updateTextures(std::string mod)
  863. {
  864. ITextureSource *tsrc = m_client->tsrc();
  865. bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
  866. bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
  867. bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
  868. m_previous_texture_modifier = m_current_texture_modifier;
  869. m_current_texture_modifier = mod;
  870. m_glow = m_prop.glow;
  871. if (m_spritenode) {
  872. if (m_prop.visual == "sprite") {
  873. std::string texturestring = "unknown_node.png";
  874. if (!m_prop.textures.empty())
  875. texturestring = m_prop.textures[0];
  876. texturestring += mod;
  877. m_spritenode->setMaterialTexture(0,
  878. tsrc->getTextureForMesh(texturestring));
  879. // This allows setting per-material colors. However, until a real lighting
  880. // system is added, the code below will have no effect. Once MineTest
  881. // has directional lighting, it should work automatically.
  882. if (!m_prop.colors.empty()) {
  883. m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
  884. m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
  885. m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
  886. }
  887. m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
  888. m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
  889. m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
  890. }
  891. }
  892. if (m_animated_meshnode) {
  893. if (m_prop.visual == "mesh") {
  894. for (u32 i = 0; i < m_prop.textures.size() &&
  895. i < m_animated_meshnode->getMaterialCount(); ++i) {
  896. std::string texturestring = m_prop.textures[i];
  897. if (texturestring.empty())
  898. continue; // Empty texture string means don't modify that material
  899. texturestring += mod;
  900. video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
  901. if (!texture) {
  902. errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
  903. continue;
  904. }
  905. // Set material flags and texture
  906. video::SMaterial& material = m_animated_meshnode->getMaterial(i);
  907. material.TextureLayer[0].Texture = texture;
  908. material.setFlag(video::EMF_LIGHTING, true);
  909. material.setFlag(video::EMF_BILINEAR_FILTER, false);
  910. // don't filter low-res textures, makes them look blurry
  911. // player models have a res of 64
  912. const core::dimension2d<u32> &size = texture->getOriginalSize();
  913. const u32 res = std::min(size.Height, size.Width);
  914. use_trilinear_filter &= res > 64;
  915. use_bilinear_filter &= res > 64;
  916. m_animated_meshnode->getMaterial(i)
  917. .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
  918. m_animated_meshnode->getMaterial(i)
  919. .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
  920. m_animated_meshnode->getMaterial(i)
  921. .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
  922. }
  923. for (u32 i = 0; i < m_prop.colors.size() &&
  924. i < m_animated_meshnode->getMaterialCount(); ++i)
  925. {
  926. // This allows setting per-material colors. However, until a real lighting
  927. // system is added, the code below will have no effect. Once MineTest
  928. // has directional lighting, it should work automatically.
  929. m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
  930. m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
  931. m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
  932. }
  933. }
  934. }
  935. if(m_meshnode)
  936. {
  937. if(m_prop.visual == "cube")
  938. {
  939. for (u32 i = 0; i < 6; ++i)
  940. {
  941. std::string texturestring = "unknown_node.png";
  942. if(m_prop.textures.size() > i)
  943. texturestring = m_prop.textures[i];
  944. texturestring += mod;
  945. // Set material flags and texture
  946. video::SMaterial& material = m_meshnode->getMaterial(i);
  947. material.setFlag(video::EMF_LIGHTING, false);
  948. material.setFlag(video::EMF_BILINEAR_FILTER, false);
  949. material.setTexture(0,
  950. tsrc->getTextureForMesh(texturestring));
  951. material.getTextureMatrix(0).makeIdentity();
  952. // This allows setting per-material colors. However, until a real lighting
  953. // system is added, the code below will have no effect. Once MineTest
  954. // has directional lighting, it should work automatically.
  955. if(m_prop.colors.size() > i)
  956. {
  957. m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
  958. m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
  959. m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
  960. }
  961. m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
  962. m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
  963. m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
  964. }
  965. } else if (m_prop.visual == "upright_sprite") {
  966. scene::IMesh *mesh = m_meshnode->getMesh();
  967. {
  968. std::string tname = "unknown_object.png";
  969. if (!m_prop.textures.empty())
  970. tname = m_prop.textures[0];
  971. tname += mod;
  972. scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
  973. buf->getMaterial().setTexture(0,
  974. tsrc->getTextureForMesh(tname));
  975. // This allows setting per-material colors. However, until a real lighting
  976. // system is added, the code below will have no effect. Once MineTest
  977. // has directional lighting, it should work automatically.
  978. if(!m_prop.colors.empty()) {
  979. buf->getMaterial().AmbientColor = m_prop.colors[0];
  980. buf->getMaterial().DiffuseColor = m_prop.colors[0];
  981. buf->getMaterial().SpecularColor = m_prop.colors[0];
  982. }
  983. buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
  984. buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
  985. buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
  986. }
  987. {
  988. std::string tname = "unknown_object.png";
  989. if (m_prop.textures.size() >= 2)
  990. tname = m_prop.textures[1];
  991. else if (!m_prop.textures.empty())
  992. tname = m_prop.textures[0];
  993. tname += mod;
  994. scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
  995. buf->getMaterial().setTexture(0,
  996. tsrc->getTextureForMesh(tname));
  997. // This allows setting per-material colors. However, until a real lighting
  998. // system is added, the code below will have no effect. Once MineTest
  999. // has directional lighting, it should work automatically.
  1000. if (m_prop.colors.size() >= 2) {
  1001. buf->getMaterial().AmbientColor = m_prop.colors[1];
  1002. buf->getMaterial().DiffuseColor = m_prop.colors[1];
  1003. buf->getMaterial().SpecularColor = m_prop.colors[1];
  1004. setMeshColor(mesh, m_prop.colors[1]);
  1005. } else if (!m_prop.colors.empty()) {
  1006. buf->getMaterial().AmbientColor = m_prop.colors[0];
  1007. buf->getMaterial().DiffuseColor = m_prop.colors[0];
  1008. buf->getMaterial().SpecularColor = m_prop.colors[0];
  1009. setMeshColor(mesh, m_prop.colors[0]);
  1010. }
  1011. buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
  1012. buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
  1013. buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
  1014. }
  1015. }
  1016. }
  1017. }
  1018. void GenericCAO::updateAnimation()
  1019. {
  1020. if (!m_animated_meshnode)
  1021. return;
  1022. if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
  1023. m_animated_meshnode->getEndFrame() != m_animation_range.Y)
  1024. m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
  1025. if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
  1026. m_animated_meshnode->setAnimationSpeed(m_animation_speed);
  1027. m_animated_meshnode->setTransitionTime(m_animation_blend);
  1028. // Requires Irrlicht 1.8 or greater
  1029. #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
  1030. if (m_animated_meshnode->getLoopMode() != m_animation_loop)
  1031. m_animated_meshnode->setLoopMode(m_animation_loop);
  1032. #endif
  1033. }
  1034. void GenericCAO::updateAnimationSpeed()
  1035. {
  1036. if (!m_animated_meshnode)
  1037. return;
  1038. m_animated_meshnode->setAnimationSpeed(m_animation_speed);
  1039. }
  1040. void GenericCAO::updateBonePosition()
  1041. {
  1042. if (m_bone_position.empty() || !m_animated_meshnode)
  1043. return;
  1044. m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
  1045. for(std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
  1046. ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
  1047. std::string bone_name = (*ii).first;
  1048. v3f bone_pos = (*ii).second.X;
  1049. v3f bone_rot = (*ii).second.Y;
  1050. irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
  1051. if(bone)
  1052. {
  1053. bone->setPosition(bone_pos);
  1054. bone->setRotation(bone_rot);
  1055. }
  1056. }
  1057. }
  1058. void GenericCAO::updateAttachments()
  1059. {
  1060. if (!getParent()) { // Detach or don't attach
  1061. scene::ISceneNode *node = getSceneNode();
  1062. if (node) {
  1063. v3f old_position = node->getAbsolutePosition();
  1064. v3f old_rotation = node->getRotation();
  1065. node->setParent(m_smgr->getRootSceneNode());
  1066. node->setPosition(old_position);
  1067. node->setRotation(old_rotation);
  1068. node->updateAbsolutePosition();
  1069. }
  1070. if (m_is_local_player) {
  1071. LocalPlayer *player = m_env->getLocalPlayer();
  1072. player->isAttached = false;
  1073. }
  1074. }
  1075. else // Attach
  1076. {
  1077. scene::ISceneNode *my_node = getSceneNode();
  1078. scene::ISceneNode *parent_node = getParent()->getSceneNode();
  1079. scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
  1080. getParent()->getAnimatedMeshSceneNode();
  1081. if (parent_animated_mesh_node && !m_attachment_bone.empty()) {
  1082. parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
  1083. }
  1084. if (my_node && parent_node) {
  1085. my_node->setParent(parent_node);
  1086. my_node->setPosition(m_attachment_position);
  1087. my_node->setRotation(m_attachment_rotation);
  1088. my_node->updateAbsolutePosition();
  1089. }
  1090. if (m_is_local_player) {
  1091. LocalPlayer *player = m_env->getLocalPlayer();
  1092. player->isAttached = true;
  1093. }
  1094. }
  1095. }
  1096. void GenericCAO::processMessage(const std::string &data)
  1097. {
  1098. //infostream<<"GenericCAO: Got message"<<std::endl;
  1099. std::istringstream is(data, std::ios::binary);
  1100. // command
  1101. u8 cmd = readU8(is);
  1102. if (cmd == GENERIC_CMD_SET_PROPERTIES) {
  1103. m_prop = gob_read_set_properties(is);
  1104. m_selection_box = m_prop.selectionbox;
  1105. m_selection_box.MinEdge *= BS;
  1106. m_selection_box.MaxEdge *= BS;
  1107. m_tx_size.X = 1.0 / m_prop.spritediv.X;
  1108. m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
  1109. if(!m_initial_tx_basepos_set){
  1110. m_initial_tx_basepos_set = true;
  1111. m_tx_basepos = m_prop.initial_sprite_basepos;
  1112. }
  1113. if (m_is_local_player) {
  1114. LocalPlayer *player = m_env->getLocalPlayer();
  1115. player->makes_footstep_sound = m_prop.makes_footstep_sound;
  1116. aabb3f collision_box = m_prop.collisionbox;
  1117. collision_box.MinEdge *= BS;
  1118. collision_box.MaxEdge *= BS;
  1119. player->setCollisionbox(collision_box);
  1120. player->setEyeHeight(m_prop.eye_height);
  1121. player->setZoomFOV(m_prop.zoom_fov);
  1122. }
  1123. if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
  1124. m_prop.nametag = m_name;
  1125. expireVisuals();
  1126. } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
  1127. // Not sent by the server if this object is an attachment.
  1128. // We might however get here if the server notices the object being detached before the client.
  1129. m_position = readV3F1000(is);
  1130. m_velocity = readV3F1000(is);
  1131. m_acceleration = readV3F1000(is);
  1132. if(fabs(m_prop.automatic_rotate) < 0.001)
  1133. m_yaw = readF1000(is);
  1134. else
  1135. readF1000(is);
  1136. bool do_interpolate = readU8(is);
  1137. bool is_end_position = readU8(is);
  1138. float update_interval = readF1000(is);
  1139. // Place us a bit higher if we're physical, to not sink into
  1140. // the ground due to sucky collision detection...
  1141. if(m_prop.physical)
  1142. m_position += v3f(0,0.002,0);
  1143. if(getParent() != NULL) // Just in case
  1144. return;
  1145. if(do_interpolate)
  1146. {
  1147. if(!m_prop.physical)
  1148. pos_translator.update(m_position, is_end_position, update_interval);
  1149. } else {
  1150. pos_translator.init(m_position);
  1151. }
  1152. updateNodePos();
  1153. } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
  1154. std::string mod = deSerializeString(is);
  1155. // immediatly reset a engine issued texture modifier if a mod sends a different one
  1156. if (m_reset_textures_timer > 0) {
  1157. m_reset_textures_timer = -1;
  1158. updateTextures(m_previous_texture_modifier);
  1159. }
  1160. updateTextures(mod);
  1161. } else if (cmd == GENERIC_CMD_SET_SPRITE) {
  1162. v2s16 p = readV2S16(is);
  1163. int num_frames = readU16(is);
  1164. float framelength = readF1000(is);
  1165. bool select_horiz_by_yawpitch = readU8(is);
  1166. m_tx_basepos = p;
  1167. m_anim_num_frames = num_frames;
  1168. m_anim_framelength = framelength;
  1169. m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
  1170. updateTexturePos();
  1171. } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
  1172. float override_speed = readF1000(is);
  1173. float override_jump = readF1000(is);
  1174. float override_gravity = readF1000(is);
  1175. // these are sent inverted so we get true when the server sends nothing
  1176. bool sneak = !readU8(is);
  1177. bool sneak_glitch = !readU8(is);
  1178. bool new_move = !readU8(is);
  1179. if(m_is_local_player)
  1180. {
  1181. LocalPlayer *player = m_env->getLocalPlayer();
  1182. player->physics_override_speed = override_speed;
  1183. player->physics_override_jump = override_jump;
  1184. player->physics_override_gravity = override_gravity;
  1185. player->physics_override_sneak = sneak;
  1186. player->physics_override_sneak_glitch = sneak_glitch;
  1187. player->physics_override_new_move = new_move;
  1188. }
  1189. } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
  1190. // TODO: change frames send as v2s32 value
  1191. v2f range = readV2F1000(is);
  1192. if (!m_is_local_player) {
  1193. m_animation_range = v2s32((s32)range.X, (s32)range.Y);
  1194. m_animation_speed = readF1000(is);
  1195. m_animation_blend = readF1000(is);
  1196. // these are sent inverted so we get true when the server sends nothing
  1197. m_animation_loop = !readU8(is);
  1198. updateAnimation();
  1199. } else {
  1200. LocalPlayer *player = m_env->getLocalPlayer();
  1201. if(player->last_animation == NO_ANIM)
  1202. {
  1203. m_animation_range = v2s32((s32)range.X, (s32)range.Y);
  1204. m_animation_speed = readF1000(is);
  1205. m_animation_blend = readF1000(is);
  1206. // these are sent inverted so we get true when the server sends nothing
  1207. m_animation_loop = !readU8(is);
  1208. }
  1209. // update animation only if local animations present
  1210. // and received animation is unknown (except idle animation)
  1211. bool is_known = false;
  1212. for (int i = 1;i<4;i++)
  1213. {
  1214. if(m_animation_range.Y == player->local_animations[i].Y)
  1215. is_known = true;
  1216. }
  1217. if(!is_known ||
  1218. (player->local_animations[1].Y + player->local_animations[2].Y < 1))
  1219. {
  1220. updateAnimation();
  1221. }
  1222. }
  1223. } else if (cmd == GENERIC_CMD_SET_ANIMATION_SPEED) {
  1224. m_animation_speed = readF1000(is);
  1225. updateAnimationSpeed();
  1226. } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
  1227. std::string bone = deSerializeString(is);
  1228. v3f position = readV3F1000(is);
  1229. v3f rotation = readV3F1000(is);
  1230. m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
  1231. updateBonePosition();
  1232. } else if (cmd == GENERIC_CMD_ATTACH_TO) {
  1233. u16 parentID = readS16(is);
  1234. u16 oldparent = m_env->attachement_parent_ids[getId()];
  1235. if (oldparent) {
  1236. m_children.erase(std::remove(m_children.begin(), m_children.end(),
  1237. getId()), m_children.end());
  1238. }
  1239. m_env->attachement_parent_ids[getId()] = parentID;
  1240. GenericCAO *parentobj = m_env->getGenericCAO(parentID);
  1241. if (parentobj) {
  1242. parentobj->m_children.push_back(getId());
  1243. }
  1244. m_attachment_bone = deSerializeString(is);
  1245. m_attachment_position = readV3F1000(is);
  1246. m_attachment_rotation = readV3F1000(is);
  1247. // localplayer itself can't be attached to localplayer
  1248. if (!m_is_local_player) {
  1249. m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
  1250. // Objects attached to the local player should be hidden by default
  1251. m_is_visible = !m_attached_to_local;
  1252. }
  1253. updateAttachments();
  1254. } else if (cmd == GENERIC_CMD_PUNCHED) {
  1255. /*s16 damage =*/ readS16(is);
  1256. s16 result_hp = readS16(is);
  1257. // Use this instead of the send damage to not interfere with prediction
  1258. s16 damage = m_hp - result_hp;
  1259. m_hp = result_hp;
  1260. if (damage > 0)
  1261. {
  1262. if (m_hp <= 0)
  1263. {
  1264. // TODO: Execute defined fast response
  1265. // As there is no definition, make a smoke puff
  1266. ClientSimpleObject *simple = createSmokePuff(
  1267. m_smgr, m_env, m_position,
  1268. m_prop.visual_size * BS);
  1269. m_env->addSimpleObject(simple);
  1270. } else if (m_reset_textures_timer < 0) {
  1271. // TODO: Execute defined fast response
  1272. // Flashing shall suffice as there is no definition
  1273. m_reset_textures_timer = 0.05;
  1274. if(damage >= 2)
  1275. m_reset_textures_timer += 0.05 * damage;
  1276. updateTextures(m_current_texture_modifier + "^[brighten");
  1277. }
  1278. }
  1279. } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
  1280. m_armor_groups.clear();
  1281. int armor_groups_size = readU16(is);
  1282. for(int i=0; i<armor_groups_size; i++)
  1283. {
  1284. std::string name = deSerializeString(is);
  1285. int rating = readS16(is);
  1286. m_armor_groups[name] = rating;
  1287. }
  1288. } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
  1289. // Deprecated, for backwards compatibility only.
  1290. readU8(is); // version
  1291. m_prop.nametag_color = readARGB8(is);
  1292. if (m_nametag != NULL) {
  1293. m_nametag->nametag_color = m_prop.nametag_color;
  1294. v3f pos;
  1295. pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
  1296. m_nametag->nametag_pos = pos;
  1297. }
  1298. } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
  1299. u16 child_id = readU16(is);
  1300. u8 type = readU8(is);
  1301. if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
  1302. childobj->processInitData(deSerializeLongString(is));
  1303. } else {
  1304. m_env->addActiveObject(child_id, type, deSerializeLongString(is));
  1305. }
  1306. } else {
  1307. warningstream << FUNCTION_NAME
  1308. << ": unknown command or outdated client \""
  1309. << +cmd << "\"" << std::endl;
  1310. }
  1311. }
  1312. /* \pre punchitem != NULL
  1313. */
  1314. bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
  1315. float time_from_last_punch)
  1316. {
  1317. assert(punchitem); // pre-condition
  1318. const ToolCapabilities *toolcap =
  1319. &punchitem->getToolCapabilities(m_client->idef());
  1320. PunchDamageResult result = getPunchDamage(
  1321. m_armor_groups,
  1322. toolcap,
  1323. punchitem,
  1324. time_from_last_punch);
  1325. if(result.did_punch && result.damage != 0)
  1326. {
  1327. if(result.damage < m_hp)
  1328. {
  1329. m_hp -= result.damage;
  1330. } else {
  1331. m_hp = 0;
  1332. // TODO: Execute defined fast response
  1333. // As there is no definition, make a smoke puff
  1334. ClientSimpleObject *simple = createSmokePuff(
  1335. m_smgr, m_env, m_position,
  1336. m_prop.visual_size * BS);
  1337. m_env->addSimpleObject(simple);
  1338. }
  1339. // TODO: Execute defined fast response
  1340. // Flashing shall suffice as there is no definition
  1341. if (m_reset_textures_timer < 0) {
  1342. m_reset_textures_timer = 0.05;
  1343. if (result.damage >= 2)
  1344. m_reset_textures_timer += 0.05 * result.damage;
  1345. updateTextures(m_current_texture_modifier + "^[brighten");
  1346. }
  1347. }
  1348. return false;
  1349. }
  1350. std::string GenericCAO::debugInfoText()
  1351. {
  1352. std::ostringstream os(std::ios::binary);
  1353. os<<"GenericCAO hp="<<m_hp<<"\n";
  1354. os<<"armor={";
  1355. for(ItemGroupList::const_iterator i = m_armor_groups.begin();
  1356. i != m_armor_groups.end(); ++i)
  1357. {
  1358. os<<i->first<<"="<<i->second<<", ";
  1359. }
  1360. os<<"}";
  1361. return os.str();
  1362. }
  1363. // Prototype
  1364. GenericCAO proto_GenericCAO(NULL, NULL);