]> git.lizzy.rs Git - dragonfireclient.git/blob - src/genericobject.cpp
Initial Commit
[dragonfireclient.git] / src / genericobject.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "genericobject.h"
21 #include <sstream>
22 #include "util/serialize.h"
23
24 std::string gob_cmd_set_properties(const ObjectProperties &prop)
25 {
26         std::ostringstream os(std::ios::binary);
27         writeU8(os, GENERIC_CMD_SET_PROPERTIES);
28         prop.serialize(os);
29         return os.str();
30 }
31
32 ObjectProperties gob_read_set_properties(std::istream &is)
33 {
34         ObjectProperties prop;
35         prop.deSerialize(is);
36         return prop;
37 }
38
39 std::string gob_cmd_update_position(
40         v3f position,
41         v3f velocity,
42         v3f acceleration,
43         v3f rotation,
44         bool do_interpolate,
45         bool is_movement_end,
46         f32 update_interval
47 ){
48         std::ostringstream os(std::ios::binary);
49         // command
50         writeU8(os, GENERIC_CMD_UPDATE_POSITION);
51         // pos
52         writeV3F32(os, position);
53         // velocity
54         writeV3F32(os, velocity);
55         // acceleration
56         writeV3F32(os, acceleration);
57         // rotation
58         writeV3F32(os, rotation);
59         // do_interpolate
60         writeU8(os, do_interpolate);
61         // is_end_position (for interpolation)
62         writeU8(os, is_movement_end);
63         // update_interval (for interpolation)
64         writeF32(os, update_interval);
65         return os.str();
66 }
67
68 std::string gob_cmd_set_texture_mod(const std::string &mod)
69 {
70         std::ostringstream os(std::ios::binary);
71         // command
72         writeU8(os, GENERIC_CMD_SET_TEXTURE_MOD);
73         // parameters
74         os<<serializeString(mod);
75         return os.str();
76 }
77
78 std::string gob_cmd_set_sprite(
79         v2s16 p,
80         u16 num_frames,
81         f32 framelength,
82         bool select_horiz_by_yawpitch
83 ){
84         std::ostringstream os(std::ios::binary);
85         // command
86         writeU8(os, GENERIC_CMD_SET_SPRITE);
87         // parameters
88         writeV2S16(os, p);
89         writeU16(os, num_frames);
90         writeF32(os, framelength);
91         writeU8(os, select_horiz_by_yawpitch);
92         return os.str();
93 }
94
95 std::string gob_cmd_punched(u16 result_hp)
96 {
97         std::ostringstream os(std::ios::binary);
98         // command
99         writeU8(os, GENERIC_CMD_PUNCHED);
100         // result_hp
101         writeU16(os, result_hp);
102         return os.str();
103 }
104
105 std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups)
106 {
107         std::ostringstream os(std::ios::binary);
108         writeU8(os, GENERIC_CMD_UPDATE_ARMOR_GROUPS);
109         writeU16(os, armor_groups.size());
110         for (const auto &armor_group : armor_groups) {
111                 os<<serializeString(armor_group.first);
112                 writeS16(os, armor_group.second);
113         }
114         return os.str();
115 }
116
117 std::string gob_cmd_update_physics_override(float physics_override_speed, float physics_override_jump,
118                 float physics_override_gravity, bool sneak, bool sneak_glitch, bool new_move)
119 {
120         std::ostringstream os(std::ios::binary);
121         // command
122         writeU8(os, GENERIC_CMD_SET_PHYSICS_OVERRIDE);
123         // parameters
124         writeF32(os, physics_override_speed);
125         writeF32(os, physics_override_jump);
126         writeF32(os, physics_override_gravity);
127         // these are sent inverted so we get true when the server sends nothing
128         writeU8(os, !sneak);
129         writeU8(os, !sneak_glitch);
130         writeU8(os, !new_move);
131         return os.str();
132 }
133
134 std::string gob_cmd_update_animation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
135 {
136         std::ostringstream os(std::ios::binary);
137         // command
138         writeU8(os, GENERIC_CMD_SET_ANIMATION);
139         // parameters
140         writeV2F32(os, frames);
141         writeF32(os, frame_speed);
142         writeF32(os, frame_blend);
143         // these are sent inverted so we get true when the server sends nothing
144         writeU8(os, !frame_loop);
145         return os.str();
146 }
147
148 std::string gob_cmd_update_animation_speed(float frame_speed)
149 {
150         std::ostringstream os(std::ios::binary);
151         // command
152         writeU8(os, GENERIC_CMD_SET_ANIMATION_SPEED);
153         // parameters
154         writeF32(os, frame_speed);
155         return os.str();
156 }
157
158 std::string gob_cmd_update_bone_position(const std::string &bone, v3f position,
159                 v3f rotation)
160 {
161         std::ostringstream os(std::ios::binary);
162         // command
163         writeU8(os, GENERIC_CMD_SET_BONE_POSITION);
164         // parameters
165         os<<serializeString(bone);
166         writeV3F32(os, position);
167         writeV3F32(os, rotation);
168         return os.str();
169 }
170
171 std::string gob_cmd_update_attachment(int parent_id, const std::string &bone,
172                 v3f position, v3f rotation)
173 {
174         std::ostringstream os(std::ios::binary);
175         // command
176         writeU8(os, GENERIC_CMD_ATTACH_TO);
177         // parameters
178         writeS16(os, parent_id);
179         os<<serializeString(bone);
180         writeV3F32(os, position);
181         writeV3F32(os, rotation);
182         return os.str();
183 }
184
185 std::string gob_cmd_update_nametag_attributes(video::SColor color)
186 {
187         std::ostringstream os(std::ios::binary);
188         // command
189         writeU8(os, GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES);
190         // parameters
191         writeU8(os, 1); // version for forward compatibility
192         writeARGB8(os, color);
193         return os.str();
194 }
195
196 std::string gob_cmd_update_infant(u16 id, u8 type,
197                 const std::string &client_initialization_data)
198 {
199         std::ostringstream os(std::ios::binary);
200         // command
201         writeU8(os, GENERIC_CMD_SPAWN_INFANT);
202         // parameters
203         writeU16(os, id);
204         writeU8(os, type);
205         os<<serializeLongString(client_initialization_data);
206         return os.str();
207 }