87017c3907d4250f0c9f5ee6466e2af8f621bd14
[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                 bool send_recommended)
96 {
97         m_age += dtime;
98         if(m_age > 10)
99         {
100                 m_removed = true;
101                 return;
102         }
103
104         m_base_position.Y += dtime * BS * 2;
105         if(m_base_position.Y > 8*BS)
106                 m_base_position.Y = 2*BS;
107
108         if(send_recommended == false)
109                 return;
110
111         m_timer1 -= dtime;
112         if(m_timer1 < 0.0)
113         {
114                 m_timer1 += 0.125;
115                 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
116
117                 std::string data;
118
119                 data += itos(0); // 0 = position
120                 data += " ";
121                 data += itos(m_base_position.X);
122                 data += " ";
123                 data += itos(m_base_position.Y);
124                 data += " ";
125                 data += itos(m_base_position.Z);
126
127                 ActiveObjectMessage aom(getId(), false, data);
128                 messages.push_back(aom);
129         }
130 }
131
132
133 /*
134         ItemSAO
135 */
136
137 // Prototype
138 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
139
140 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
141                 const std::string inventorystring):
142         ServerActiveObject(env, id, pos),
143         m_inventorystring(inventorystring),
144         m_speed_f(0,0,0),
145         m_last_sent_position(0,0,0)
146 {
147         dstream<<"Server: ItemSAO created with inventorystring=\""
148                         <<m_inventorystring<<"\""<<std::endl;
149         ServerActiveObject::registerType(getType(), create);
150 }
151
152 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
153                 const std::string &data)
154 {
155         std::istringstream is(data, std::ios::binary);
156         char buf[1];
157         // read version
158         is.read(buf, 1);
159         u8 version = buf[0];
160         // check if version is supported
161         if(version != 0)
162                 return NULL;
163         std::string inventorystring = deSerializeString(is);
164         dstream<<"ItemSAO::create(): Creating item \""
165                         <<inventorystring<<"\""<<std::endl;
166         return new ItemSAO(env, id, pos, inventorystring);
167 }
168
169 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
170                 bool send_recommended)
171 {
172         assert(m_env);
173
174         const float interval = 0.2;
175         if(m_move_interval.step(dtime, interval)==false)
176                 return;
177         dtime = interval;
178         
179         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
180         collisionMoveResult moveresult;
181         // Apply gravity
182         m_speed_f += v3f(0, -dtime*9.81*BS, 0);
183         // Maximum movement without glitches
184         f32 pos_max_d = BS*0.25;
185         // Limit speed
186         if(m_speed_f.getLength()*dtime > pos_max_d)
187                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
188         v3f pos_f = getBasePosition();
189         v3f pos_f_old = pos_f;
190         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
191                         box, dtime, pos_f, m_speed_f);
192         
193         if(send_recommended == false)
194                 return;
195
196         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
197         {
198                 setBasePosition(pos_f);
199                 m_last_sent_position = pos_f;
200
201                 std::ostringstream os(std::ios::binary);
202                 char buf[6];
203                 // command (0 = update position)
204                 buf[0] = 0;
205                 os.write(buf, 1);
206                 // pos
207                 writeS32((u8*)buf, m_base_position.X*1000);
208                 os.write(buf, 4);
209                 writeS32((u8*)buf, m_base_position.Y*1000);
210                 os.write(buf, 4);
211                 writeS32((u8*)buf, m_base_position.Z*1000);
212                 os.write(buf, 4);
213                 // create message and add to list
214                 ActiveObjectMessage aom(getId(), false, os.str());
215                 messages.push_back(aom);
216         }
217 }
218
219 std::string ItemSAO::getClientInitializationData()
220 {
221         std::ostringstream os(std::ios::binary);
222         char buf[6];
223         // version
224         buf[0] = 0;
225         os.write(buf, 1);
226         // pos
227         writeS32((u8*)buf, m_base_position.X*1000);
228         os.write(buf, 4);
229         writeS32((u8*)buf, m_base_position.Y*1000);
230         os.write(buf, 4);
231         writeS32((u8*)buf, m_base_position.Z*1000);
232         os.write(buf, 4);
233         // inventorystring
234         os<<serializeString(m_inventorystring);
235         return os.str();
236 }
237
238 std::string ItemSAO::getStaticData()
239 {
240         dstream<<__FUNCTION_NAME<<std::endl;
241         std::ostringstream os(std::ios::binary);
242         char buf[1];
243         // version
244         buf[0] = 0;
245         os.write(buf, 1);
246         // inventorystring
247         os<<serializeString(m_inventorystring);
248         return os.str();
249 }
250
251 InventoryItem * ItemSAO::createInventoryItem()
252 {
253         try{
254                 std::istringstream is(m_inventorystring, std::ios_base::binary);
255                 InventoryItem *item = InventoryItem::deSerialize(is);
256                 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
257                                 <<m_inventorystring<<"\" -> item="<<item
258                                 <<std::endl;
259                 return item;
260         }
261         catch(SerializationError &e)
262         {
263                 dstream<<__FUNCTION_NAME<<": serialization error: "
264                                 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
265                 return NULL;
266         }
267 }
268
269
270 /*
271         RatSAO
272 */
273
274 // Prototype
275 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
276
277 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
278         ServerActiveObject(env, id, pos),
279         m_is_active(false),
280         m_speed_f(0,0,0)
281 {
282         //dstream<<"Server: RatSAO created"<<std::endl;
283         ServerActiveObject::registerType(getType(), create);
284
285         m_oldpos = v3f(0,0,0);
286         m_last_sent_position = v3f(0,0,0);
287         m_yaw = 0;
288         m_counter1 = 0;
289         m_counter2 = 0;
290         m_age = 0;
291         m_touching_ground = false;
292 }
293
294 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
295                 const std::string &data)
296 {
297         std::istringstream is(data, std::ios::binary);
298         char buf[1];
299         // read version
300         is.read(buf, 1);
301         u8 version = buf[0];
302         // check if version is supported
303         if(version != 0)
304                 return NULL;
305         return new RatSAO(env, id, pos);
306 }
307
308 void RatSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
309                 bool send_recommended)
310 {
311         assert(m_env);
312
313         if(m_is_active == false)
314         {
315                 if(m_inactive_interval.step(dtime, 0.5)==false)
316                         return;
317         }
318
319         /*
320                 The AI
321         */
322
323         /*m_age += dtime;
324         if(m_age > 60)
325         {
326                 // Die
327                 m_removed = true;
328                 return;
329         }*/
330
331         // Apply gravity
332         m_speed_f.Y -= dtime*9.81*BS;
333
334         /*
335                 Move around if some player is close
336         */
337         bool player_is_close = false;
338         // Check connected players
339         core::list<Player*> players = m_env->getPlayers(true);
340         core::list<Player*>::Iterator i;
341         for(i = players.begin();
342                         i != players.end(); i++)
343         {
344                 Player *player = *i;
345                 v3f playerpos = player->getPosition();
346                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
347                 {
348                         player_is_close = true;
349                         break;
350                 }
351         }
352
353         m_is_active = player_is_close;
354         
355         if(player_is_close == false)
356         {
357                 m_speed_f.X = 0;
358                 m_speed_f.Z = 0;
359         }
360         else
361         {
362                 // Move around
363                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
364                 f32 speed = 2*BS;
365                 m_speed_f.X = speed * dir.X;
366                 m_speed_f.Z = speed * dir.Z;
367
368                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
369                                 < dtime*speed/2)
370                 {
371                         m_counter1 -= dtime;
372                         if(m_counter1 < 0.0)
373                         {
374                                 m_counter1 += 1.0;
375                                 m_speed_f.Y = 5.0*BS;
376                         }
377                 }
378
379                 {
380                         m_counter2 -= dtime;
381                         if(m_counter2 < 0.0)
382                         {
383                                 m_counter2 += (float)(myrand()%100)/100*3.0;
384                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
385                                 m_yaw = wrapDegrees(m_yaw);
386                         }
387                 }
388         }
389         
390         m_oldpos = m_base_position;
391
392         /*
393                 Move it, with collision detection
394         */
395
396         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
397         collisionMoveResult moveresult;
398         // Maximum movement without glitches
399         f32 pos_max_d = BS*0.25;
400         // Limit speed
401         if(m_speed_f.getLength()*dtime > pos_max_d)
402                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
403         v3f pos_f = getBasePosition();
404         v3f pos_f_old = pos_f;
405         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
406                         box, dtime, pos_f, m_speed_f);
407         m_touching_ground = moveresult.touching_ground;
408         
409         setBasePosition(pos_f);
410
411         if(send_recommended == false)
412                 return;
413
414         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
415         {
416                 m_last_sent_position = pos_f;
417
418                 std::ostringstream os(std::ios::binary);
419                 // command (0 = update position)
420                 writeU8(os, 0);
421                 // pos
422                 writeV3F1000(os, m_base_position);
423                 // yaw
424                 writeF1000(os, m_yaw);
425                 // create message and add to list
426                 ActiveObjectMessage aom(getId(), false, os.str());
427                 messages.push_back(aom);
428         }
429 }
430
431 std::string RatSAO::getClientInitializationData()
432 {
433         std::ostringstream os(std::ios::binary);
434         // version
435         writeU8(os, 0);
436         // pos
437         writeV3F1000(os, m_base_position);
438         return os.str();
439 }
440
441 std::string RatSAO::getStaticData()
442 {
443         //dstream<<__FUNCTION_NAME<<std::endl;
444         std::ostringstream os(std::ios::binary);
445         // version
446         writeU8(os, 0);
447         return os.str();
448 }
449
450 InventoryItem* RatSAO::createPickedUpItem()
451 {
452         std::istringstream is("CraftItem rat 1", std::ios_base::binary);
453         InventoryItem *item = InventoryItem::deSerialize(is);
454         return item;
455 }
456
457