content_cao.cpp 45 KB

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