content_cao.cpp 48 KB

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