genericobject.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. v3f rotation,
  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. writeV3F32(os, position);
  46. // velocity
  47. writeV3F32(os, velocity);
  48. // acceleration
  49. writeV3F32(os, acceleration);
  50. // rotation
  51. writeV3F32(os, rotation);
  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. writeF32(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. writeF32(os, framelength);
  82. writeU8(os, select_horiz_by_yawpitch);
  83. return os.str();
  84. }
  85. std::string gob_cmd_punched(u16 result_hp)
  86. {
  87. std::ostringstream os(std::ios::binary);
  88. // command
  89. writeU8(os, GENERIC_CMD_PUNCHED);
  90. // result_hp
  91. writeU16(os, result_hp);
  92. return os.str();
  93. }
  94. std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups)
  95. {
  96. std::ostringstream os(std::ios::binary);
  97. writeU8(os, GENERIC_CMD_UPDATE_ARMOR_GROUPS);
  98. writeU16(os, armor_groups.size());
  99. for (const auto &armor_group : armor_groups) {
  100. os<<serializeString(armor_group.first);
  101. writeS16(os, armor_group.second);
  102. }
  103. return os.str();
  104. }
  105. std::string gob_cmd_update_physics_override(float physics_override_speed, float physics_override_jump,
  106. float physics_override_gravity, bool sneak, bool sneak_glitch, bool new_move)
  107. {
  108. std::ostringstream os(std::ios::binary);
  109. // command
  110. writeU8(os, GENERIC_CMD_SET_PHYSICS_OVERRIDE);
  111. // parameters
  112. writeF32(os, physics_override_speed);
  113. writeF32(os, physics_override_jump);
  114. writeF32(os, physics_override_gravity);
  115. // these are sent inverted so we get true when the server sends nothing
  116. writeU8(os, !sneak);
  117. writeU8(os, !sneak_glitch);
  118. writeU8(os, !new_move);
  119. return os.str();
  120. }
  121. std::string gob_cmd_update_animation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
  122. {
  123. std::ostringstream os(std::ios::binary);
  124. // command
  125. writeU8(os, GENERIC_CMD_SET_ANIMATION);
  126. // parameters
  127. writeV2F32(os, frames);
  128. writeF32(os, frame_speed);
  129. writeF32(os, frame_blend);
  130. // these are sent inverted so we get true when the server sends nothing
  131. writeU8(os, !frame_loop);
  132. return os.str();
  133. }
  134. std::string gob_cmd_update_animation_speed(float frame_speed)
  135. {
  136. std::ostringstream os(std::ios::binary);
  137. // command
  138. writeU8(os, GENERIC_CMD_SET_ANIMATION_SPEED);
  139. // parameters
  140. writeF32(os, frame_speed);
  141. return os.str();
  142. }
  143. std::string gob_cmd_update_bone_position(const std::string &bone, v3f position,
  144. v3f rotation)
  145. {
  146. std::ostringstream os(std::ios::binary);
  147. // command
  148. writeU8(os, GENERIC_CMD_SET_BONE_POSITION);
  149. // parameters
  150. os<<serializeString(bone);
  151. writeV3F32(os, position);
  152. writeV3F32(os, rotation);
  153. return os.str();
  154. }
  155. std::string gob_cmd_update_attachment(int parent_id, const std::string &bone,
  156. v3f position, v3f rotation)
  157. {
  158. std::ostringstream os(std::ios::binary);
  159. // command
  160. writeU8(os, GENERIC_CMD_ATTACH_TO);
  161. // parameters
  162. writeS16(os, parent_id);
  163. os<<serializeString(bone);
  164. writeV3F32(os, position);
  165. writeV3F32(os, rotation);
  166. return os.str();
  167. }
  168. std::string gob_cmd_update_nametag_attributes(video::SColor color)
  169. {
  170. std::ostringstream os(std::ios::binary);
  171. // command
  172. writeU8(os, GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES);
  173. // parameters
  174. writeU8(os, 1); // version for forward compatibility
  175. writeARGB8(os, color);
  176. return os.str();
  177. }
  178. std::string gob_cmd_update_infant(u16 id, u8 type,
  179. const std::string &client_initialization_data)
  180. {
  181. std::ostringstream os(std::ios::binary);
  182. // command
  183. writeU8(os, GENERIC_CMD_SPAWN_INFANT);
  184. // parameters
  185. writeU16(os, id);
  186. writeU8(os, type);
  187. os<<serializeLongString(client_initialization_data);
  188. return os.str();
  189. }