92a0b83f4b10bdfc6c729a4ff7c04b1670e94ab8
[oweals/minetest.git] / src / serverobject.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "serverobject.h"
21 #include <fstream>
22 #include "environment.h"
23 #include "inventory.h"
24 #include "collision.h"
25
26 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
27
28 ServerActiveObject::ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos):
29         ActiveObject(id),
30         m_known_by_count(0),
31         m_removed(false),
32         m_static_exists(false),
33         m_static_block(1337,1337,1337),
34         m_env(env),
35         m_base_position(pos)
36 {
37 }
38
39 ServerActiveObject::~ServerActiveObject()
40 {
41 }
42
43 ServerActiveObject* ServerActiveObject::create(u8 type,
44                 ServerEnvironment *env, u16 id, v3f pos,
45                 const std::string &data)
46 {
47         // Find factory function
48         core::map<u16, Factory>::Node *n;
49         n = m_types.find(type);
50         if(n == NULL)
51         {
52                 // If factory is not found, just return.
53                 dstream<<"WARNING: ServerActiveObject: No factory for type="
54                                 <<type<<std::endl;
55                 return NULL;
56         }
57
58         Factory f = n->getValue();
59         ServerActiveObject *object = (*f)(env, id, pos, data);
60         return object;
61 }
62
63 void ServerActiveObject::registerType(u16 type, Factory f)
64 {
65         core::map<u16, Factory>::Node *n;
66         n = m_types.find(type);
67         if(n)
68                 return;
69         m_types.insert(type, f);
70 }
71
72
73 /*
74         TestSAO
75 */
76
77 // Prototype
78 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
79
80 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
81         ServerActiveObject(env, id, pos),
82         m_timer1(0),
83         m_age(0)
84 {
85         ServerActiveObject::registerType(getType(), create);
86 }
87
88 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
89                 const std::string &data)
90 {
91         return new TestSAO(env, id, pos);
92 }
93
94 void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
95 {
96         m_age += dtime;
97         if(m_age > 10)
98         {
99                 m_removed = true;
100                 return;
101         }
102
103         m_base_position.Y += dtime * BS * 2;
104         if(m_base_position.Y > 8*BS)
105                 m_base_position.Y = 2*BS;
106
107         m_timer1 -= dtime;
108         if(m_timer1 < 0.0)
109         {
110                 m_timer1 += 0.125;
111                 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
112
113                 std::string data;
114
115                 data += itos(0); // 0 = position
116                 data += " ";
117                 data += itos(m_base_position.X);
118                 data += " ";
119                 data += itos(m_base_position.Y);
120                 data += " ";
121                 data += itos(m_base_position.Z);
122
123                 ActiveObjectMessage aom(getId(), false, data);
124                 messages.push_back(aom);
125         }
126 }
127
128
129 /*
130         ItemSAO
131 */
132
133 // Prototype
134 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
135
136 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
137                 const std::string inventorystring):
138         ServerActiveObject(env, id, pos),
139         m_inventorystring(inventorystring),
140         m_speed_f(0,0,0)
141 {
142         dstream<<"Server: ItemSAO created with inventorystring=\""
143                         <<m_inventorystring<<"\""<<std::endl;
144         ServerActiveObject::registerType(getType(), create);
145 }
146
147 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
148                 const std::string &data)
149 {
150         std::istringstream is(data, std::ios::binary);
151         char buf[1];
152         // read version
153         is.read(buf, 1);
154         u8 version = buf[0];
155         // check if version is supported
156         if(version != 0)
157                 return NULL;
158         std::string inventorystring = deSerializeString(is);
159         dstream<<"ItemSAO::create(): Creating item \""
160                         <<inventorystring<<"\""<<std::endl;
161         return new ItemSAO(env, id, pos, inventorystring);
162 }
163
164 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
165 {
166         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
167         collisionMoveResult moveresult;
168         // Apply gravity
169         m_speed_f += v3f(0, -dtime*9.81*BS, 0);
170         // Maximum movement without glitches
171         f32 pos_max_d = BS*0.25;
172         // Limit speed
173         if(m_speed_f.getLength()*dtime > pos_max_d)
174                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
175         v3f pos_f = getBasePosition();
176         v3f pos_f_old = pos_f;
177         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
178                         box, dtime, pos_f, m_speed_f);
179         
180         if(pos_f.getDistanceFrom(pos_f_old) > 0.01*BS)
181         {
182                 setBasePosition(pos_f);
183
184                 std::ostringstream os(std::ios::binary);
185                 char buf[6];
186                 // command (0 = update position)
187                 buf[0] = 0;
188                 os.write(buf, 1);
189                 // pos
190                 writeS32((u8*)buf, m_base_position.X*1000);
191                 os.write(buf, 4);
192                 writeS32((u8*)buf, m_base_position.Y*1000);
193                 os.write(buf, 4);
194                 writeS32((u8*)buf, m_base_position.Z*1000);
195                 os.write(buf, 4);
196                 // create message and add to list
197                 ActiveObjectMessage aom(getId(), false, os.str());
198                 messages.push_back(aom);
199         }
200 }
201
202 std::string ItemSAO::getClientInitializationData()
203 {
204         std::ostringstream os(std::ios::binary);
205         char buf[6];
206         // version
207         buf[0] = 0;
208         os.write(buf, 1);
209         // pos
210         writeS32((u8*)buf, m_base_position.X*1000);
211         os.write(buf, 4);
212         writeS32((u8*)buf, m_base_position.Y*1000);
213         os.write(buf, 4);
214         writeS32((u8*)buf, m_base_position.Z*1000);
215         os.write(buf, 4);
216         // inventorystring
217         os<<serializeString(m_inventorystring);
218         return os.str();
219 }
220
221 std::string ItemSAO::getStaticData()
222 {
223         dstream<<__FUNCTION_NAME<<std::endl;
224         std::ostringstream os(std::ios::binary);
225         char buf[1];
226         // version
227         buf[0] = 0;
228         os.write(buf, 1);
229         // inventorystring
230         os<<serializeString(m_inventorystring);
231         return os.str();
232 }
233
234 InventoryItem * ItemSAO::createInventoryItem()
235 {
236         try{
237                 std::istringstream is(m_inventorystring, std::ios_base::binary);
238                 InventoryItem *item = InventoryItem::deSerialize(is);
239                 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
240                                 <<m_inventorystring<<"\" -> item="<<item
241                                 <<std::endl;
242                 return item;
243         }
244         catch(SerializationError &e)
245         {
246                 dstream<<__FUNCTION_NAME<<": serialization error: "
247                                 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
248                 return NULL;
249         }
250 }
251
252
253