clientenvironment.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2017 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 "util/serialize.h"
  17. #include "util/pointedthing.h"
  18. #include "client.h"
  19. #include "clientenvironment.h"
  20. #include "clientsimpleobject.h"
  21. #include "clientmap.h"
  22. #include "scripting_client.h"
  23. #include "mapblock_mesh.h"
  24. #include "event.h"
  25. #include "collision.h"
  26. #include "nodedef.h"
  27. #include "profiler.h"
  28. #include "raycast.h"
  29. #include "voxelalgorithms.h"
  30. #include "settings.h"
  31. #include "content_cao.h"
  32. #include <algorithm>
  33. #include "client/renderingengine.h"
  34. /*
  35. ClientEnvironment
  36. */
  37. ClientEnvironment::ClientEnvironment(ClientMap *map,
  38. ITextureSource *texturesource, Client *client):
  39. Environment(client),
  40. m_map(map),
  41. m_texturesource(texturesource),
  42. m_client(client)
  43. {
  44. char zero = 0;
  45. memset(attachement_parent_ids, zero, sizeof(attachement_parent_ids));
  46. }
  47. ClientEnvironment::~ClientEnvironment()
  48. {
  49. // delete active objects
  50. for (auto &active_object : m_active_objects) {
  51. delete active_object.second;
  52. }
  53. for (auto &simple_object : m_simple_objects) {
  54. delete simple_object;
  55. }
  56. // Drop/delete map
  57. m_map->drop();
  58. delete m_local_player;
  59. }
  60. Map & ClientEnvironment::getMap()
  61. {
  62. return *m_map;
  63. }
  64. ClientMap & ClientEnvironment::getClientMap()
  65. {
  66. return *m_map;
  67. }
  68. void ClientEnvironment::setLocalPlayer(LocalPlayer *player)
  69. {
  70. /*
  71. It is a failure if already is a local player
  72. */
  73. FATAL_ERROR_IF(m_local_player != NULL,
  74. "Local player already allocated");
  75. m_local_player = player;
  76. }
  77. void ClientEnvironment::step(float dtime)
  78. {
  79. /* Step time of day */
  80. stepTimeOfDay(dtime);
  81. // Get some settings
  82. bool fly_allowed = m_client->checkLocalPrivilege("fly");
  83. bool free_move = fly_allowed && g_settings->getBool("free_move");
  84. // Get local player
  85. LocalPlayer *lplayer = getLocalPlayer();
  86. assert(lplayer);
  87. // collision info queue
  88. std::vector<CollisionInfo> player_collisions;
  89. /*
  90. Get the speed the player is going
  91. */
  92. bool is_climbing = lplayer->is_climbing;
  93. f32 player_speed = lplayer->getSpeed().getLength();
  94. /*
  95. Maximum position increment
  96. */
  97. //f32 position_max_increment = 0.05*BS;
  98. f32 position_max_increment = 0.1*BS;
  99. // Maximum time increment (for collision detection etc)
  100. // time = distance / speed
  101. f32 dtime_max_increment = 1;
  102. if(player_speed > 0.001)
  103. dtime_max_increment = position_max_increment / player_speed;
  104. // Maximum time increment is 10ms or lower
  105. if(dtime_max_increment > 0.01)
  106. dtime_max_increment = 0.01;
  107. // Don't allow overly huge dtime
  108. if(dtime > 0.5)
  109. dtime = 0.5;
  110. f32 dtime_downcount = dtime;
  111. /*
  112. Stuff that has a maximum time increment
  113. */
  114. u32 loopcount = 0;
  115. do
  116. {
  117. loopcount++;
  118. f32 dtime_part;
  119. if(dtime_downcount > dtime_max_increment)
  120. {
  121. dtime_part = dtime_max_increment;
  122. dtime_downcount -= dtime_part;
  123. }
  124. else
  125. {
  126. dtime_part = dtime_downcount;
  127. /*
  128. Setting this to 0 (no -=dtime_part) disables an infinite loop
  129. when dtime_part is so small that dtime_downcount -= dtime_part
  130. does nothing
  131. */
  132. dtime_downcount = 0;
  133. }
  134. /*
  135. Handle local player
  136. */
  137. {
  138. // Apply physics
  139. if(!free_move && !is_climbing)
  140. {
  141. // Gravity
  142. v3f speed = lplayer->getSpeed();
  143. if(!lplayer->in_liquid)
  144. speed.Y -= lplayer->movement_gravity * lplayer->physics_override_gravity * dtime_part * 2;
  145. // Liquid floating / sinking
  146. if(lplayer->in_liquid && !lplayer->swimming_vertical)
  147. speed.Y -= lplayer->movement_liquid_sink * dtime_part * 2;
  148. // Liquid resistance
  149. if(lplayer->in_liquid_stable || lplayer->in_liquid)
  150. {
  151. // How much the node's viscosity blocks movement, ranges between 0 and 1
  152. // Should match the scale at which viscosity increase affects other liquid attributes
  153. const f32 viscosity_factor = 0.3;
  154. v3f d_wanted = -speed / lplayer->movement_liquid_fluidity;
  155. f32 dl = d_wanted.getLength();
  156. if(dl > lplayer->movement_liquid_fluidity_smooth)
  157. dl = lplayer->movement_liquid_fluidity_smooth;
  158. dl *= (lplayer->liquid_viscosity * viscosity_factor) + (1 - viscosity_factor);
  159. v3f d = d_wanted.normalize() * dl;
  160. speed += d;
  161. }
  162. lplayer->setSpeed(speed);
  163. }
  164. /*
  165. Move the lplayer.
  166. This also does collision detection.
  167. */
  168. lplayer->move(dtime_part, this, position_max_increment,
  169. &player_collisions);
  170. }
  171. }
  172. while(dtime_downcount > 0.001);
  173. //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
  174. bool player_immortal = lplayer->getCAO() && lplayer->getCAO()->isImmortal();
  175. for (const CollisionInfo &info : player_collisions) {
  176. v3f speed_diff = info.new_speed - info.old_speed;;
  177. // Handle only fall damage
  178. // (because otherwise walking against something in fast_move kills you)
  179. if (speed_diff.Y < 0 || info.old_speed.Y >= 0)
  180. continue;
  181. // Get rid of other components
  182. speed_diff.X = 0;
  183. speed_diff.Z = 0;
  184. f32 pre_factor = 1; // 1 hp per node/s
  185. f32 tolerance = BS*14; // 5 without damage
  186. f32 post_factor = 1; // 1 hp per node/s
  187. if (info.type == COLLISION_NODE) {
  188. const ContentFeatures &f = m_client->ndef()->
  189. get(m_map->getNodeNoEx(info.node_p));
  190. // Determine fall damage multiplier
  191. int addp = itemgroup_get(f.groups, "fall_damage_add_percent");
  192. pre_factor = 1.0f + (float)addp / 100.0f;
  193. }
  194. float speed = pre_factor * speed_diff.getLength();
  195. if (speed > tolerance && !player_immortal) {
  196. f32 damage_f = (speed - tolerance) / BS * post_factor;
  197. u8 damage = (u8)MYMIN(damage_f + 0.5, 255);
  198. if (damage != 0) {
  199. damageLocalPlayer(damage, true);
  200. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_FALLING_DAMAGE));
  201. }
  202. }
  203. }
  204. if (m_client->moddingEnabled()) {
  205. m_script->environment_step(dtime);
  206. }
  207. // Update lighting on local player (used for wield item)
  208. u32 day_night_ratio = getDayNightRatio();
  209. {
  210. // Get node at head
  211. // On InvalidPositionException, use this as default
  212. // (day: LIGHT_SUN, night: 0)
  213. MapNode node_at_lplayer(CONTENT_AIR, 0x0f, 0);
  214. v3s16 p = lplayer->getLightPosition();
  215. node_at_lplayer = m_map->getNodeNoEx(p);
  216. u16 light = getInteriorLight(node_at_lplayer, 0, m_client->ndef());
  217. final_color_blend(&lplayer->light_color, light, day_night_ratio);
  218. }
  219. /*
  220. Step active objects and update lighting of them
  221. */
  222. g_profiler->avg("CEnv: num of objects", m_active_objects.size());
  223. bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
  224. for (auto &ao_it : m_active_objects) {
  225. ClientActiveObject* obj = ao_it.second;
  226. // Step object
  227. obj->step(dtime, this);
  228. if (update_lighting) {
  229. // Update lighting
  230. u8 light = 0;
  231. bool pos_ok;
  232. // Get node at head
  233. v3s16 p = obj->getLightPosition();
  234. MapNode n = m_map->getNodeNoEx(p, &pos_ok);
  235. if (pos_ok)
  236. light = n.getLightBlend(day_night_ratio, m_client->ndef());
  237. else
  238. light = blend_light(day_night_ratio, LIGHT_SUN, 0);
  239. obj->updateLight(light);
  240. }
  241. }
  242. /*
  243. Step and handle simple objects
  244. */
  245. g_profiler->avg("CEnv: num of simple objects", m_simple_objects.size());
  246. for (auto i = m_simple_objects.begin(); i != m_simple_objects.end();) {
  247. auto cur = i;
  248. ClientSimpleObject *simple = *cur;
  249. simple->step(dtime);
  250. if(simple->m_to_be_removed) {
  251. delete simple;
  252. i = m_simple_objects.erase(cur);
  253. }
  254. else {
  255. ++i;
  256. }
  257. }
  258. }
  259. void ClientEnvironment::addSimpleObject(ClientSimpleObject *simple)
  260. {
  261. m_simple_objects.push_back(simple);
  262. }
  263. GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
  264. {
  265. ClientActiveObject *obj = getActiveObject(id);
  266. if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC)
  267. return (GenericCAO*) obj;
  268. return NULL;
  269. }
  270. ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
  271. {
  272. auto n = m_active_objects.find(id);
  273. if (n == m_active_objects.end())
  274. return NULL;
  275. return n->second;
  276. }
  277. bool isFreeClientActiveObjectId(const u16 id,
  278. ClientActiveObjectMap &objects)
  279. {
  280. return id != 0 && objects.find(id) == objects.end();
  281. }
  282. u16 getFreeClientActiveObjectId(ClientActiveObjectMap &objects)
  283. {
  284. //try to reuse id's as late as possible
  285. static u16 last_used_id = 0;
  286. u16 startid = last_used_id;
  287. for(;;) {
  288. last_used_id ++;
  289. if (isFreeClientActiveObjectId(last_used_id, objects))
  290. return last_used_id;
  291. if (last_used_id == startid)
  292. return 0;
  293. }
  294. }
  295. u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
  296. {
  297. assert(object); // Pre-condition
  298. if(object->getId() == 0)
  299. {
  300. u16 new_id = getFreeClientActiveObjectId(m_active_objects);
  301. if(new_id == 0)
  302. {
  303. infostream<<"ClientEnvironment::addActiveObject(): "
  304. <<"no free ids available"<<std::endl;
  305. delete object;
  306. return 0;
  307. }
  308. object->setId(new_id);
  309. }
  310. if (!isFreeClientActiveObjectId(object->getId(), m_active_objects)) {
  311. infostream<<"ClientEnvironment::addActiveObject(): "
  312. <<"id is not free ("<<object->getId()<<")"<<std::endl;
  313. delete object;
  314. return 0;
  315. }
  316. infostream<<"ClientEnvironment::addActiveObject(): "
  317. <<"added (id="<<object->getId()<<")"<<std::endl;
  318. m_active_objects[object->getId()] = object;
  319. object->addToScene(m_texturesource);
  320. { // Update lighting immediately
  321. u8 light = 0;
  322. bool pos_ok;
  323. // Get node at head
  324. v3s16 p = object->getLightPosition();
  325. MapNode n = m_map->getNodeNoEx(p, &pos_ok);
  326. if (pos_ok)
  327. light = n.getLightBlend(getDayNightRatio(), m_client->ndef());
  328. else
  329. light = blend_light(getDayNightRatio(), LIGHT_SUN, 0);
  330. object->updateLight(light);
  331. }
  332. return object->getId();
  333. }
  334. void ClientEnvironment::addActiveObject(u16 id, u8 type,
  335. const std::string &init_data)
  336. {
  337. ClientActiveObject* obj =
  338. ClientActiveObject::create((ActiveObjectType) type, m_client, this);
  339. if(obj == NULL)
  340. {
  341. infostream<<"ClientEnvironment::addActiveObject(): "
  342. <<"id="<<id<<" type="<<type<<": Couldn't create object"
  343. <<std::endl;
  344. return;
  345. }
  346. obj->setId(id);
  347. try
  348. {
  349. obj->initialize(init_data);
  350. }
  351. catch(SerializationError &e)
  352. {
  353. errorstream<<"ClientEnvironment::addActiveObject():"
  354. <<" id="<<id<<" type="<<type
  355. <<": SerializationError in initialize(): "
  356. <<e.what()
  357. <<": init_data="<<serializeJsonString(init_data)
  358. <<std::endl;
  359. }
  360. addActiveObject(obj);
  361. }
  362. void ClientEnvironment::removeActiveObject(u16 id)
  363. {
  364. verbosestream<<"ClientEnvironment::removeActiveObject(): "
  365. <<"id="<<id<<std::endl;
  366. ClientActiveObject* obj = getActiveObject(id);
  367. if (obj == NULL) {
  368. infostream<<"ClientEnvironment::removeActiveObject(): "
  369. <<"id="<<id<<" not found"<<std::endl;
  370. return;
  371. }
  372. obj->removeFromScene(true);
  373. delete obj;
  374. m_active_objects.erase(id);
  375. }
  376. void ClientEnvironment::processActiveObjectMessage(u16 id, const std::string &data)
  377. {
  378. ClientActiveObject *obj = getActiveObject(id);
  379. if (obj == NULL) {
  380. infostream << "ClientEnvironment::processActiveObjectMessage():"
  381. << " got message for id=" << id << ", which doesn't exist."
  382. << std::endl;
  383. return;
  384. }
  385. try {
  386. obj->processMessage(data);
  387. } catch (SerializationError &e) {
  388. errorstream<<"ClientEnvironment::processActiveObjectMessage():"
  389. << " id=" << id << " type=" << obj->getType()
  390. << " SerializationError in processMessage(): " << e.what()
  391. << std::endl;
  392. }
  393. }
  394. /*
  395. Callbacks for activeobjects
  396. */
  397. void ClientEnvironment::damageLocalPlayer(u8 damage, bool handle_hp)
  398. {
  399. LocalPlayer *lplayer = getLocalPlayer();
  400. assert(lplayer);
  401. if (handle_hp) {
  402. if (lplayer->hp > damage)
  403. lplayer->hp -= damage;
  404. else
  405. lplayer->hp = 0;
  406. }
  407. ClientEnvEvent event;
  408. event.type = CEE_PLAYER_DAMAGE;
  409. event.player_damage.amount = damage;
  410. event.player_damage.send_to_server = handle_hp;
  411. m_client_event_queue.push(event);
  412. }
  413. void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
  414. {
  415. ClientEnvEvent event;
  416. event.type = CEE_PLAYER_BREATH;
  417. event.player_breath.amount = breath;
  418. m_client_event_queue.push(event);
  419. }
  420. /*
  421. Client likes to call these
  422. */
  423. void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
  424. std::vector<DistanceSortedActiveObject> &dest)
  425. {
  426. for (auto &ao_it : m_active_objects) {
  427. ClientActiveObject* obj = ao_it.second;
  428. f32 d = (obj->getPosition() - origin).getLength();
  429. if (d > max_d)
  430. continue;
  431. dest.emplace_back(obj, d);
  432. }
  433. }
  434. ClientEnvEvent ClientEnvironment::getClientEnvEvent()
  435. {
  436. FATAL_ERROR_IF(m_client_event_queue.empty(),
  437. "ClientEnvironment::getClientEnvEvent(): queue is empty");
  438. ClientEnvEvent event = m_client_event_queue.front();
  439. m_client_event_queue.pop();
  440. return event;
  441. }
  442. void ClientEnvironment::getSelectedActiveObjects(
  443. const core::line3d<f32> &shootline_on_map,
  444. std::vector<PointedThing> &objects)
  445. {
  446. std::vector<DistanceSortedActiveObject> allObjects;
  447. getActiveObjects(shootline_on_map.start,
  448. shootline_on_map.getLength() + 10.0f, allObjects);
  449. const v3f line_vector = shootline_on_map.getVector();
  450. for (const auto &allObject : allObjects) {
  451. ClientActiveObject *obj = allObject.obj;
  452. aabb3f selection_box;
  453. if (!obj->getSelectionBox(&selection_box))
  454. continue;
  455. const v3f &pos = obj->getPosition();
  456. aabb3f offsetted_box(selection_box.MinEdge + pos,
  457. selection_box.MaxEdge + pos);
  458. v3f current_intersection;
  459. v3s16 current_normal;
  460. if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector,
  461. &current_intersection, &current_normal)) {
  462. objects.emplace_back((s16) obj->getId(), current_intersection, current_normal,
  463. (current_intersection - shootline_on_map.start).getLengthSQ());
  464. }
  465. }
  466. }