genericobject.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Minetest
  3. Copyright (C) 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 "genericobject.h"
  17. #include <sstream>
  18. #include "util/serialize.h"
  19. std::string gob_cmd_set_properties(const ObjectProperties &prop)
  20. {
  21. std::ostringstream os(std::ios::binary);
  22. writeU8(os, GENERIC_CMD_SET_PROPERTIES);
  23. prop.serialize(os);
  24. return os.str();
  25. }
  26. ObjectProperties gob_read_set_properties(std::istream &is)
  27. {
  28. ObjectProperties prop;
  29. prop.deSerialize(is);
  30. return prop;
  31. }
  32. std::string gob_cmd_update_position(
  33. v3f position,
  34. v3f velocity,
  35. v3f acceleration,
  36. f32 yaw,
  37. bool do_interpolate,
  38. bool is_movement_end,
  39. f32 update_interval
  40. ){
  41. std::ostringstream os(std::ios::binary);
  42. // command
  43. writeU8(os, GENERIC_CMD_UPDATE_POSITION);
  44. // pos
  45. writeV3F1000(os, position);
  46. // velocity
  47. writeV3F1000(os, velocity);
  48. // acceleration
  49. writeV3F1000(os, acceleration);
  50. // yaw
  51. writeF1000(os, yaw);
  52. // do_interpolate
  53. writeU8(os, do_interpolate);
  54. // is_end_position (for interpolation)
  55. writeU8(os, is_movement_end);
  56. // update_interval (for interpolation)
  57. writeF1000(os, update_interval);
  58. return os.str();
  59. }
  60. std::string gob_cmd_set_texture_mod(const std::string &mod)
  61. {
  62. std::ostringstream os(std::ios::binary);
  63. // command
  64. writeU8(os, GENERIC_CMD_SET_TEXTURE_MOD);
  65. // parameters
  66. os<<serializeString(mod);
  67. return os.str();
  68. }
  69. std::string gob_cmd_set_sprite(
  70. v2s16 p,
  71. u16 num_frames,
  72. f32 framelength,
  73. bool select_horiz_by_yawpitch
  74. ){
  75. std::ostringstream os(std::ios::binary);
  76. // command
  77. writeU8(os, GENERIC_CMD_SET_SPRITE);
  78. // parameters
  79. writeV2S16(os, p);
  80. writeU16(os, num_frames);
  81. writeF1000(os, framelength);
  82. writeU8(os, select_horiz_by_yawpitch);
  83. return os.str();
  84. }
  85. std::string gob_cmd_punched(s16 damage, s16 result_hp)
  86. {
  87. std::ostringstream os(std::ios::binary);
  88. // command
  89. writeU8(os, GENERIC_CMD_PUNCHED);
  90. // damage
  91. writeS16(os, damage);
  92. // result_hp
  93. writeS16(os, result_hp);
  94. return os.str();
  95. }
  96. std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups)
  97. {
  98. std::ostringstream os(std::ios::binary);
  99. writeU8(os, GENERIC_CMD_UPDATE_ARMOR_GROUPS);
  100. writeU16(os, armor_groups.size());
  101. for(ItemGroupList::const_iterator i = armor_groups.begin();
  102. i != armor_groups.end(); i++){
  103. os<<serializeString(i->first);
  104. writeS16(os, i->second);
  105. }
  106. return os.str();
  107. }
  108. std::string gob_cmd_update_physics_override(float physics_override_speed, float physics_override_jump,
  109. float physics_override_gravity, bool sneak, bool sneak_glitch)
  110. {
  111. std::ostringstream os(std::ios::binary);
  112. // command
  113. writeU8(os, GENERIC_CMD_SET_PHYSICS_OVERRIDE);
  114. // parameters
  115. writeF1000(os, physics_override_speed);
  116. writeF1000(os, physics_override_jump);
  117. writeF1000(os, physics_override_gravity);
  118. // these are sent inverted so we get true when the server sends nothing
  119. writeU8(os, !sneak);
  120. writeU8(os, !sneak_glitch);
  121. return os.str();
  122. }
  123. std::string gob_cmd_update_animation(v2f frames, float frame_speed, float frame_blend)
  124. {
  125. std::ostringstream os(std::ios::binary);
  126. // command
  127. writeU8(os, GENERIC_CMD_SET_ANIMATION);
  128. // parameters
  129. writeV2F1000(os, frames);
  130. writeF1000(os, frame_speed);
  131. writeF1000(os, frame_blend);
  132. return os.str();
  133. }
  134. std::string gob_cmd_update_bone_position(std::string bone, v3f position, v3f rotation)
  135. {
  136. std::ostringstream os(std::ios::binary);
  137. // command
  138. writeU8(os, GENERIC_CMD_SET_BONE_POSITION);
  139. // parameters
  140. os<<serializeString(bone);
  141. writeV3F1000(os, position);
  142. writeV3F1000(os, rotation);
  143. return os.str();
  144. }
  145. std::string gob_cmd_update_attachment(int parent_id, std::string bone, v3f position, v3f rotation)
  146. {
  147. std::ostringstream os(std::ios::binary);
  148. // command
  149. writeU8(os, GENERIC_CMD_SET_ATTACHMENT);
  150. // parameters
  151. writeS16(os, parent_id);
  152. os<<serializeString(bone);
  153. writeV3F1000(os, position);
  154. writeV3F1000(os, rotation);
  155. return os.str();
  156. }