clientenvironment.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 "mtevent.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 "shader.h"
  32. #include "content_cao.h"
  33. #include <algorithm>
  34. #include "client/renderingengine.h"
  35. /*
  36. CAOShaderConstantSetter
  37. */
  38. //! Shader constant setter for passing material emissive color to the CAO object_shader
  39. class CAOShaderConstantSetter : public IShaderConstantSetter
  40. {
  41. public:
  42. CAOShaderConstantSetter():
  43. m_emissive_color_setting("emissiveColor")
  44. {}
  45. ~CAOShaderConstantSetter() override = default;
  46. void onSetConstants(video::IMaterialRendererServices *services) override
  47. {
  48. // Ambient color
  49. video::SColorf emissive_color(m_emissive_color);
  50. float as_array[4] = {
  51. emissive_color.r,
  52. emissive_color.g,
  53. emissive_color.b,
  54. emissive_color.a,
  55. };
  56. m_emissive_color_setting.set(as_array, services);
  57. }
  58. void onSetMaterial(const video::SMaterial& material) override
  59. {
  60. m_emissive_color = material.EmissiveColor;
  61. }
  62. private:
  63. video::SColor m_emissive_color;
  64. CachedPixelShaderSetting<float, 4> m_emissive_color_setting;
  65. };
  66. class CAOShaderConstantSetterFactory : public IShaderConstantSetterFactory
  67. {
  68. public:
  69. CAOShaderConstantSetterFactory()
  70. {}
  71. virtual IShaderConstantSetter* create()
  72. {
  73. return new CAOShaderConstantSetter();
  74. }
  75. };
  76. /*
  77. ClientEnvironment
  78. */
  79. ClientEnvironment::ClientEnvironment(ClientMap *map,
  80. ITextureSource *texturesource, Client *client):
  81. Environment(client),
  82. m_map(map),
  83. m_texturesource(texturesource),
  84. m_client(client)
  85. {
  86. auto *shdrsrc = m_client->getShaderSource();
  87. shdrsrc->addShaderConstantSetterFactory(new CAOShaderConstantSetterFactory());
  88. }
  89. ClientEnvironment::~ClientEnvironment()
  90. {
  91. m_ao_manager.clear();
  92. for (auto &simple_object : m_simple_objects) {
  93. delete simple_object;
  94. }
  95. // Drop/delete map
  96. m_map->drop();
  97. delete m_local_player;
  98. }
  99. Map & ClientEnvironment::getMap()
  100. {
  101. return *m_map;
  102. }
  103. ClientMap & ClientEnvironment::getClientMap()
  104. {
  105. return *m_map;
  106. }
  107. void ClientEnvironment::setLocalPlayer(LocalPlayer *player)
  108. {
  109. /*
  110. It is a failure if already is a local player
  111. */
  112. FATAL_ERROR_IF(m_local_player != NULL,
  113. "Local player already allocated");
  114. m_local_player = player;
  115. }
  116. void ClientEnvironment::step(float dtime)
  117. {
  118. /* Step time of day */
  119. stepTimeOfDay(dtime);
  120. // Get some settings
  121. bool fly_allowed = m_client->checkLocalPrivilege("fly");
  122. bool free_move = fly_allowed && g_settings->getBool("free_move");
  123. // Get local player
  124. LocalPlayer *lplayer = getLocalPlayer();
  125. assert(lplayer);
  126. // collision info queue
  127. std::vector<CollisionInfo> player_collisions;
  128. /*
  129. Get the speed the player is going
  130. */
  131. bool is_climbing = lplayer->is_climbing;
  132. f32 player_speed = lplayer->getSpeed().getLength();
  133. /*
  134. Maximum position increment
  135. */
  136. //f32 position_max_increment = 0.05*BS;
  137. f32 position_max_increment = 0.1*BS;
  138. // Maximum time increment (for collision detection etc)
  139. // time = distance / speed
  140. f32 dtime_max_increment = 1;
  141. if(player_speed > 0.001)
  142. dtime_max_increment = position_max_increment / player_speed;
  143. // Maximum time increment is 10ms or lower
  144. if(dtime_max_increment > 0.01)
  145. dtime_max_increment = 0.01;
  146. // Don't allow overly huge dtime
  147. if(dtime > 0.5)
  148. dtime = 0.5;
  149. /*
  150. Stuff that has a maximum time increment
  151. */
  152. u32 steps = ceil(dtime / dtime_max_increment);
  153. f32 dtime_part = dtime / steps;
  154. for (; steps > 0; --steps) {
  155. /*
  156. Local player handling
  157. */
  158. // Control local player
  159. lplayer->applyControl(dtime_part, this);
  160. // Apply physics
  161. if (!free_move) {
  162. // Gravity
  163. v3f speed = lplayer->getSpeed();
  164. if (!is_climbing && !lplayer->in_liquid)
  165. speed.Y -= lplayer->movement_gravity *
  166. lplayer->physics_override_gravity * dtime_part * 2.0f;
  167. // Liquid floating / sinking
  168. if (!is_climbing && lplayer->in_liquid &&
  169. !lplayer->swimming_vertical &&
  170. !lplayer->swimming_pitch)
  171. speed.Y -= lplayer->movement_liquid_sink * dtime_part * 2.0f;
  172. // Movement resistance
  173. if (lplayer->move_resistance > 0) {
  174. // How much the node's move_resistance blocks movement, ranges
  175. // between 0 and 1. Should match the scale at which liquid_viscosity
  176. // increase affects other liquid attributes.
  177. static const f32 resistance_factor = 0.3f;
  178. v3f d_wanted;
  179. bool in_liquid_stable = lplayer->in_liquid_stable || lplayer->in_liquid;
  180. if (in_liquid_stable) {
  181. d_wanted = -speed / lplayer->movement_liquid_fluidity;
  182. } else {
  183. d_wanted = -speed / BS;
  184. }
  185. f32 dl = d_wanted.getLength();
  186. if (in_liquid_stable) {
  187. if (dl > lplayer->movement_liquid_fluidity_smooth)
  188. dl = lplayer->movement_liquid_fluidity_smooth;
  189. }
  190. dl *= (lplayer->move_resistance * resistance_factor) +
  191. (1 - resistance_factor);
  192. v3f d = d_wanted.normalize() * (dl * dtime_part * 100.0f);
  193. speed += d;
  194. }
  195. lplayer->setSpeed(speed);
  196. }
  197. /*
  198. Move the lplayer.
  199. This also does collision detection.
  200. */
  201. lplayer->move(dtime_part, this, position_max_increment,
  202. &player_collisions);
  203. }
  204. bool player_immortal = false;
  205. f32 player_fall_factor = 1.0f;
  206. GenericCAO *playercao = lplayer->getCAO();
  207. if (playercao) {
  208. player_immortal = playercao->isImmortal();
  209. int addp_p = itemgroup_get(playercao->getGroups(),
  210. "fall_damage_add_percent");
  211. // convert armor group into an usable fall damage factor
  212. player_fall_factor = 1.0f + (float)addp_p / 100.0f;
  213. }
  214. for (const CollisionInfo &info : player_collisions) {
  215. v3f speed_diff = info.new_speed - info.old_speed;;
  216. // Handle only fall damage
  217. // (because otherwise walking against something in fast_move kills you)
  218. if (speed_diff.Y < 0 || info.old_speed.Y >= 0)
  219. continue;
  220. // Get rid of other components
  221. speed_diff.X = 0;
  222. speed_diff.Z = 0;
  223. f32 pre_factor = 1; // 1 hp per node/s
  224. f32 tolerance = BS*14; // 5 without damage
  225. if (info.type == COLLISION_NODE) {
  226. const ContentFeatures &f = m_client->ndef()->
  227. get(m_map->getNode(info.node_p));
  228. // Determine fall damage modifier
  229. int addp_n = itemgroup_get(f.groups, "fall_damage_add_percent");
  230. // convert node group to an usable fall damage factor
  231. f32 node_fall_factor = 1.0f + (float)addp_n / 100.0f;
  232. // combine both player fall damage modifiers
  233. pre_factor = node_fall_factor * player_fall_factor;
  234. }
  235. float speed = pre_factor * speed_diff.getLength();
  236. if (speed > tolerance && !player_immortal && pre_factor > 0.0f) {
  237. f32 damage_f = (speed - tolerance) / BS;
  238. u16 damage = (u16)MYMIN(damage_f + 0.5, U16_MAX);
  239. if (damage != 0) {
  240. damageLocalPlayer(damage, true);
  241. m_client->getEventManager()->put(
  242. new SimpleTriggerEvent(MtEvent::PLAYER_FALLING_DAMAGE));
  243. }
  244. }
  245. }
  246. if (m_client->modsLoaded())
  247. m_script->environment_step(dtime);
  248. // Update lighting on local player (used for wield item)
  249. u32 day_night_ratio = getDayNightRatio();
  250. {
  251. // Get node at head
  252. // On InvalidPositionException, use this as default
  253. // (day: LIGHT_SUN, night: 0)
  254. MapNode node_at_lplayer(CONTENT_AIR, 0x0f, 0);
  255. v3s16 p = lplayer->getLightPosition();
  256. node_at_lplayer = m_map->getNode(p);
  257. u16 light = getInteriorLight(node_at_lplayer, 0, m_client->ndef());
  258. lplayer->light_color = encode_light(light, 0); // this transfers light.alpha
  259. final_color_blend(&lplayer->light_color, light, day_night_ratio);
  260. }
  261. /*
  262. Step active objects and update lighting of them
  263. */
  264. bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
  265. auto cb_state = [this, dtime, update_lighting, day_night_ratio] (ClientActiveObject *cao) {
  266. // Step object
  267. cao->step(dtime, this);
  268. if (update_lighting)
  269. cao->updateLight(day_night_ratio);
  270. };
  271. m_ao_manager.step(dtime, cb_state);
  272. /*
  273. Step and handle simple objects
  274. */
  275. g_profiler->avg("ClientEnv: CSO count [#]", m_simple_objects.size());
  276. for (auto i = m_simple_objects.begin(); i != m_simple_objects.end();) {
  277. ClientSimpleObject *simple = *i;
  278. simple->step(dtime);
  279. if(simple->m_to_be_removed) {
  280. delete simple;
  281. i = m_simple_objects.erase(i);
  282. }
  283. else {
  284. ++i;
  285. }
  286. }
  287. }
  288. void ClientEnvironment::addSimpleObject(ClientSimpleObject *simple)
  289. {
  290. m_simple_objects.push_back(simple);
  291. }
  292. GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
  293. {
  294. ClientActiveObject *obj = getActiveObject(id);
  295. if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC)
  296. return (GenericCAO*) obj;
  297. return NULL;
  298. }
  299. u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
  300. {
  301. // Register object. If failed return zero id
  302. if (!m_ao_manager.registerObject(object))
  303. return 0;
  304. object->addToScene(m_texturesource, m_client->getSceneManager());
  305. // Update lighting immediately
  306. object->updateLight(getDayNightRatio());
  307. return object->getId();
  308. }
  309. void ClientEnvironment::addActiveObject(u16 id, u8 type,
  310. const std::string &init_data)
  311. {
  312. ClientActiveObject* obj =
  313. ClientActiveObject::create((ActiveObjectType) type, m_client, this);
  314. if(obj == NULL)
  315. {
  316. infostream<<"ClientEnvironment::addActiveObject(): "
  317. <<"id="<<id<<" type="<<type<<": Couldn't create object"
  318. <<std::endl;
  319. return;
  320. }
  321. obj->setId(id);
  322. try
  323. {
  324. obj->initialize(init_data);
  325. }
  326. catch(SerializationError &e)
  327. {
  328. errorstream<<"ClientEnvironment::addActiveObject():"
  329. <<" id="<<id<<" type="<<type
  330. <<": SerializationError in initialize(): "
  331. <<e.what()
  332. <<": init_data="<<serializeJsonString(init_data)
  333. <<std::endl;
  334. }
  335. u16 new_id = addActiveObject(obj);
  336. // Object initialized:
  337. if ((obj = getActiveObject(new_id))) {
  338. // Final step is to update all children which are already known
  339. // Data provided by AO_CMD_SPAWN_INFANT
  340. const auto &children = obj->getAttachmentChildIds();
  341. for (auto c_id : children) {
  342. if (auto *o = getActiveObject(c_id))
  343. o->updateAttachments();
  344. }
  345. }
  346. }
  347. void ClientEnvironment::removeActiveObject(u16 id)
  348. {
  349. // Get current attachment childs to detach them visually
  350. std::unordered_set<int> attachment_childs;
  351. if (auto *obj = getActiveObject(id))
  352. attachment_childs = obj->getAttachmentChildIds();
  353. m_ao_manager.removeObject(id);
  354. // Perform a proper detach in Irrlicht
  355. for (auto c_id : attachment_childs) {
  356. if (ClientActiveObject *child = getActiveObject(c_id))
  357. child->updateAttachments();
  358. }
  359. }
  360. void ClientEnvironment::processActiveObjectMessage(u16 id, const std::string &data)
  361. {
  362. ClientActiveObject *obj = getActiveObject(id);
  363. if (obj == NULL) {
  364. infostream << "ClientEnvironment::processActiveObjectMessage():"
  365. << " got message for id=" << id << ", which doesn't exist."
  366. << std::endl;
  367. return;
  368. }
  369. try {
  370. obj->processMessage(data);
  371. } catch (SerializationError &e) {
  372. errorstream<<"ClientEnvironment::processActiveObjectMessage():"
  373. << " id=" << id << " type=" << obj->getType()
  374. << " SerializationError in processMessage(): " << e.what()
  375. << std::endl;
  376. }
  377. }
  378. /*
  379. Callbacks for activeobjects
  380. */
  381. void ClientEnvironment::damageLocalPlayer(u16 damage, bool handle_hp)
  382. {
  383. LocalPlayer *lplayer = getLocalPlayer();
  384. assert(lplayer);
  385. if (handle_hp) {
  386. if (lplayer->hp > damage)
  387. lplayer->hp -= damage;
  388. else
  389. lplayer->hp = 0;
  390. }
  391. ClientEnvEvent event;
  392. event.type = CEE_PLAYER_DAMAGE;
  393. event.player_damage.amount = damage;
  394. event.player_damage.send_to_server = handle_hp;
  395. m_client_event_queue.push(event);
  396. }
  397. /*
  398. Client likes to call these
  399. */
  400. ClientEnvEvent ClientEnvironment::getClientEnvEvent()
  401. {
  402. FATAL_ERROR_IF(m_client_event_queue.empty(),
  403. "ClientEnvironment::getClientEnvEvent(): queue is empty");
  404. ClientEnvEvent event = m_client_event_queue.front();
  405. m_client_event_queue.pop();
  406. return event;
  407. }
  408. void ClientEnvironment::getSelectedActiveObjects(
  409. const core::line3d<f32> &shootline_on_map,
  410. std::vector<PointedThing> &objects)
  411. {
  412. std::vector<DistanceSortedActiveObject> allObjects;
  413. getActiveObjects(shootline_on_map.start,
  414. shootline_on_map.getLength() + 10.0f, allObjects);
  415. const v3f line_vector = shootline_on_map.getVector();
  416. for (const auto &allObject : allObjects) {
  417. ClientActiveObject *obj = allObject.obj;
  418. aabb3f selection_box;
  419. if (!obj->getSelectionBox(&selection_box))
  420. continue;
  421. const v3f &pos = obj->getPosition();
  422. aabb3f offsetted_box(selection_box.MinEdge + pos,
  423. selection_box.MaxEdge + pos);
  424. v3f current_intersection;
  425. v3s16 current_normal;
  426. if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector,
  427. &current_intersection, &current_normal)) {
  428. objects.emplace_back((s16) obj->getId(), current_intersection, current_normal,
  429. (current_intersection - shootline_on_map.start).getLengthSQ());
  430. }
  431. }
  432. }