]> git.lizzy.rs Git - dragonfireclient.git/blob - src/serverobject.cpp
merged an old head into main branch
[dragonfireclient.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_pending_deactivation(false),
33         m_static_exists(false),
34         m_static_block(1337,1337,1337),
35         m_env(env),
36         m_base_position(pos)
37 {
38 }
39
40 ServerActiveObject::~ServerActiveObject()
41 {
42 }
43
44 ServerActiveObject* ServerActiveObject::create(u8 type,
45                 ServerEnvironment *env, u16 id, v3f pos,
46                 const std::string &data)
47 {
48         // Find factory function
49         core::map<u16, Factory>::Node *n;
50         n = m_types.find(type);
51         if(n == NULL)
52         {
53                 // If factory is not found, just return.
54                 dstream<<"WARNING: ServerActiveObject: No factory for type="
55                                 <<type<<std::endl;
56                 return NULL;
57         }
58
59         Factory f = n->getValue();
60         ServerActiveObject *object = (*f)(env, id, pos, data);
61         return object;
62 }
63
64 void ServerActiveObject::registerType(u16 type, Factory f)
65 {
66         core::map<u16, Factory>::Node *n;
67         n = m_types.find(type);
68         if(n)
69                 return;
70         m_types.insert(type, f);
71 }
72
73
74 /*
75         TestSAO
76 */
77
78 // Prototype
79 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
80
81 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
82         ServerActiveObject(env, id, pos),
83         m_timer1(0),
84         m_age(0)
85 {
86         ServerActiveObject::registerType(getType(), create);
87 }
88
89 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
90                 const std::string &data)
91 {
92         return new TestSAO(env, id, pos);
93 }
94
95 void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
96                 bool send_recommended)
97 {
98         m_age += dtime;
99         if(m_age > 10)
100         {
101                 m_removed = true;
102                 return;
103         }
104
105         m_base_position.Y += dtime * BS * 2;
106         if(m_base_position.Y > 8*BS)
107                 m_base_position.Y = 2*BS;
108
109         if(send_recommended == false)
110                 return;
111
112         m_timer1 -= dtime;
113         if(m_timer1 < 0.0)
114         {
115                 m_timer1 += 0.125;
116                 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
117
118                 std::string data;
119
120                 data += itos(0); // 0 = position
121                 data += " ";
122                 data += itos(m_base_position.X);
123                 data += " ";
124                 data += itos(m_base_position.Y);
125                 data += " ";
126                 data += itos(m_base_position.Z);
127
128                 ActiveObjectMessage aom(getId(), false, data);
129                 messages.push_back(aom);
130         }
131 }
132
133
134 /*
135         ItemSAO
136 */
137
138 // Prototype
139 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
140
141 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
142                 const std::string inventorystring):
143         ServerActiveObject(env, id, pos),
144         m_inventorystring(inventorystring),
145         m_speed_f(0,0,0),
146         m_last_sent_position(0,0,0)
147 {
148         ServerActiveObject::registerType(getType(), create);
149 }
150
151 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
152                 const std::string &data)
153 {
154         std::istringstream is(data, std::ios::binary);
155         char buf[1];
156         // read version
157         is.read(buf, 1);
158         u8 version = buf[0];
159         // check if version is supported
160         if(version != 0)
161                 return NULL;
162         std::string inventorystring = deSerializeString(is);
163         dstream<<"ItemSAO::create(): Creating item \""
164                         <<inventorystring<<"\""<<std::endl;
165         return new ItemSAO(env, id, pos, inventorystring);
166 }
167
168 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
169                 bool send_recommended)
170 {
171         assert(m_env);
172
173         const float interval = 0.2;
174         if(m_move_interval.step(dtime, interval)==false)
175                 return;
176         dtime = interval;
177         
178         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
179         collisionMoveResult moveresult;
180         // Apply gravity
181         m_speed_f += v3f(0, -dtime*9.81*BS, 0);
182         // Maximum movement without glitches
183         f32 pos_max_d = BS*0.25;
184         // Limit speed
185         if(m_speed_f.getLength()*dtime > pos_max_d)
186                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
187         v3f pos_f = getBasePosition();
188         v3f pos_f_old = pos_f;
189         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
190                         box, dtime, pos_f, m_speed_f);
191         
192         if(send_recommended == false)
193                 return;
194
195         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
196         {
197                 setBasePosition(pos_f);
198                 m_last_sent_position = pos_f;
199
200                 std::ostringstream os(std::ios::binary);
201                 char buf[6];
202                 // command (0 = update position)
203                 buf[0] = 0;
204                 os.write(buf, 1);
205                 // pos
206                 writeS32((u8*)buf, m_base_position.X*1000);
207                 os.write(buf, 4);
208                 writeS32((u8*)buf, m_base_position.Y*1000);
209                 os.write(buf, 4);
210                 writeS32((u8*)buf, m_base_position.Z*1000);
211                 os.write(buf, 4);
212                 // create message and add to list
213                 ActiveObjectMessage aom(getId(), false, os.str());
214                 messages.push_back(aom);
215         }
216 }
217
218 std::string ItemSAO::getClientInitializationData()
219 {
220         std::ostringstream os(std::ios::binary);
221         char buf[6];
222         // version
223         buf[0] = 0;
224         os.write(buf, 1);
225         // pos
226         writeS32((u8*)buf, m_base_position.X*1000);
227         os.write(buf, 4);
228         writeS32((u8*)buf, m_base_position.Y*1000);
229         os.write(buf, 4);
230         writeS32((u8*)buf, m_base_position.Z*1000);
231         os.write(buf, 4);
232         // inventorystring
233         os<<serializeString(m_inventorystring);
234         return os.str();
235 }
236
237 std::string ItemSAO::getStaticData()
238 {
239         dstream<<__FUNCTION_NAME<<std::endl;
240         std::ostringstream os(std::ios::binary);
241         char buf[1];
242         // version
243         buf[0] = 0;
244         os.write(buf, 1);
245         // inventorystring
246         os<<serializeString(m_inventorystring);
247         return os.str();
248 }
249
250 InventoryItem * ItemSAO::createInventoryItem()
251 {
252         try{
253                 std::istringstream is(m_inventorystring, std::ios_base::binary);
254                 InventoryItem *item = InventoryItem::deSerialize(is);
255                 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
256                                 <<m_inventorystring<<"\" -> item="<<item
257                                 <<std::endl;
258                 return item;
259         }
260         catch(SerializationError &e)
261         {
262                 dstream<<__FUNCTION_NAME<<": serialization error: "
263                                 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
264                 return NULL;
265         }
266 }
267
268
269 /*
270         RatSAO
271 */
272
273 // Prototype
274 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
275
276 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
277         ServerActiveObject(env, id, pos),
278         m_is_active(false),
279         m_speed_f(0,0,0)
280 {
281         ServerActiveObject::registerType(getType(), create);
282
283         m_oldpos = v3f(0,0,0);
284         m_last_sent_position = v3f(0,0,0);
285         m_yaw = 0;
286         m_counter1 = 0;
287         m_counter2 = 0;
288         m_age = 0;
289         m_touching_ground = false;
290 }
291
292 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
293                 const std::string &data)
294 {
295         std::istringstream is(data, std::ios::binary);
296         char buf[1];
297         // read version
298         is.read(buf, 1);
299         u8 version = buf[0];
300         // check if version is supported
301         if(version != 0)
302                 return NULL;
303         return new RatSAO(env, id, pos);
304 }
305
306 void RatSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
307                 bool send_recommended)
308 {
309         assert(m_env);
310
311         if(m_is_active == false)
312         {
313                 if(m_inactive_interval.step(dtime, 0.5)==false)
314                         return;
315         }
316
317         /*
318                 The AI
319         */
320
321         /*m_age += dtime;
322         if(m_age > 60)
323         {
324                 // Die
325                 m_removed = true;
326                 return;
327         }*/
328
329         // Apply gravity
330         m_speed_f.Y -= dtime*9.81*BS;
331
332         /*
333                 Move around if some player is close
334         */
335         bool player_is_close = false;
336         // Check connected players
337         core::list<Player*> players = m_env->getPlayers(true);
338         core::list<Player*>::Iterator i;
339         for(i = players.begin();
340                         i != players.end(); i++)
341         {
342                 Player *player = *i;
343                 v3f playerpos = player->getPosition();
344                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
345                 {
346                         player_is_close = true;
347                         break;
348                 }
349         }
350
351         m_is_active = player_is_close;
352         
353         if(player_is_close == false)
354         {
355                 m_speed_f.X = 0;
356                 m_speed_f.Z = 0;
357         }
358         else
359         {
360                 // Move around
361                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
362                 f32 speed = 2*BS;
363                 m_speed_f.X = speed * dir.X;
364                 m_speed_f.Z = speed * dir.Z;
365
366                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
367                                 < dtime*speed/2)
368                 {
369                         m_counter1 -= dtime;
370                         if(m_counter1 < 0.0)
371                         {
372                                 m_counter1 += 1.0;
373                                 m_speed_f.Y = 5.0*BS;
374                         }
375                 }
376
377                 {
378                         m_counter2 -= dtime;
379                         if(m_counter2 < 0.0)
380                         {
381                                 m_counter2 += (float)(myrand()%100)/100*3.0;
382                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
383                                 m_yaw = wrapDegrees(m_yaw);
384                         }
385                 }
386         }
387         
388         m_oldpos = m_base_position;
389
390         /*
391                 Move it, with collision detection
392         */
393
394         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
395         collisionMoveResult moveresult;
396         // Maximum movement without glitches
397         f32 pos_max_d = BS*0.25;
398         // Limit speed
399         if(m_speed_f.getLength()*dtime > pos_max_d)
400                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
401         v3f pos_f = getBasePosition();
402         v3f pos_f_old = pos_f;
403         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
404                         box, dtime, pos_f, m_speed_f);
405         m_touching_ground = moveresult.touching_ground;
406         
407         setBasePosition(pos_f);
408
409         if(send_recommended == false)
410                 return;
411
412         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
413         {
414                 m_last_sent_position = pos_f;
415
416                 std::ostringstream os(std::ios::binary);
417                 // command (0 = update position)
418                 writeU8(os, 0);
419                 // pos
420                 writeV3F1000(os, m_base_position);
421                 // yaw
422                 writeF1000(os, m_yaw);
423                 // create message and add to list
424                 ActiveObjectMessage aom(getId(), false, os.str());
425                 messages.push_back(aom);
426         }
427 }
428
429 std::string RatSAO::getClientInitializationData()
430 {
431         std::ostringstream os(std::ios::binary);
432         // version
433         writeU8(os, 0);
434         // pos
435         writeV3F1000(os, m_base_position);
436         return os.str();
437 }
438
439 std::string RatSAO::getStaticData()
440 {
441         //dstream<<__FUNCTION_NAME<<std::endl;
442         std::ostringstream os(std::ios::binary);
443         // version
444         writeU8(os, 0);
445         return os.str();
446 }
447
448 InventoryItem* RatSAO::createPickedUpItem()
449 {
450         std::istringstream is("CraftItem rat 1", std::ios_base::binary);
451         InventoryItem *item = InventoryItem::deSerialize(is);
452         return item;
453 }
454
455 /*
456         Oerkki1SAO
457 */
458
459 // Prototype
460 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
461
462 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
463         ServerActiveObject(env, id, pos),
464         m_is_active(false),
465         m_speed_f(0,0,0)
466 {
467         ServerActiveObject::registerType(getType(), create);
468
469         m_oldpos = v3f(0,0,0);
470         m_last_sent_position = v3f(0,0,0);
471         m_yaw = 0;
472         m_counter1 = 0;
473         m_counter2 = 0;
474         m_age = 0;
475         m_touching_ground = false;
476         m_hp = 20;
477 }
478
479 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
480                 const std::string &data)
481 {
482         std::istringstream is(data, std::ios::binary);
483         // read version
484         u8 version = readU8(is);
485         // read hp
486         u8 hp = readU8(is);
487         // check if version is supported
488         if(version != 0)
489                 return NULL;
490         Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
491         o->m_hp = hp;
492         return o;
493 }
494
495 void Oerkki1SAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
496                 bool send_recommended)
497 {
498         assert(m_env);
499
500         if(m_is_active == false)
501         {
502                 if(m_inactive_interval.step(dtime, 0.5)==false)
503                         return;
504         }
505
506         /*
507                 The AI
508         */
509
510         m_age += dtime;
511         if(m_age > 120)
512         {
513                 // Die
514                 m_removed = true;
515                 return;
516         }
517
518         // Apply gravity
519         m_speed_f.Y -= dtime*9.81*BS;
520
521         /*
522                 Move around if some player is close
523         */
524         bool player_is_close = false;
525         v3f near_player_pos;
526         // Check connected players
527         core::list<Player*> players = m_env->getPlayers(true);
528         core::list<Player*>::Iterator i;
529         for(i = players.begin();
530                         i != players.end(); i++)
531         {
532                 Player *player = *i;
533                 v3f playerpos = player->getPosition();
534                 if(m_base_position.getDistanceFrom(playerpos) < BS*15.0)
535                 {
536                         player_is_close = true;
537                         near_player_pos = playerpos;
538                         break;
539                 }
540         }
541
542         m_is_active = player_is_close;
543         
544         if(player_is_close == false)
545         {
546                 m_speed_f.X = 0;
547                 m_speed_f.Z = 0;
548         }
549         else
550         {
551                 // Move around
552
553                 v3f ndir = near_player_pos - m_base_position;
554                 ndir.Y = 0;
555                 ndir /= ndir.getLength();
556                 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
557                 if(nyaw < m_yaw - 180)
558                         nyaw += 360;
559                 else if(nyaw > m_yaw + 180)
560                         nyaw -= 360;
561                 m_yaw = 0.95*m_yaw + 0.05*nyaw;
562                 m_yaw = wrapDegrees(m_yaw);
563
564                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
565                 f32 speed = 2*BS;
566                 m_speed_f.X = speed * dir.X;
567                 m_speed_f.Z = speed * dir.Z;
568
569                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
570                                 < dtime*speed/2)
571                 {
572                         m_counter1 -= dtime;
573                         if(m_counter1 < 0.0)
574                         {
575                                 m_counter1 += 1.0;
576                                 // Jump
577                                 m_speed_f.Y = 5.0*BS;
578                         }
579                 }
580
581                 {
582                         m_counter2 -= dtime;
583                         if(m_counter2 < 0.0)
584                         {
585                                 m_counter2 += (float)(myrand()%100)/100*3.0;
586                                 //m_yaw += ((float)(myrand()%200)-100)/100*180;
587                                 m_yaw += ((float)(myrand()%200)-100)/100*90;
588                                 m_yaw = wrapDegrees(m_yaw);
589                         }
590                 }
591         }
592         
593         m_oldpos = m_base_position;
594
595         /*
596                 Move it, with collision detection
597         */
598
599         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
600         collisionMoveResult moveresult;
601         // Maximum movement without glitches
602         f32 pos_max_d = BS*0.25;
603         // Limit speed
604         if(m_speed_f.getLength()*dtime > pos_max_d)
605                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
606         v3f pos_f = getBasePosition();
607         v3f pos_f_old = pos_f;
608         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
609                         box, dtime, pos_f, m_speed_f);
610         m_touching_ground = moveresult.touching_ground;
611         
612         setBasePosition(pos_f);
613
614         if(send_recommended == false)
615                 return;
616
617         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
618         {
619                 m_last_sent_position = pos_f;
620
621                 std::ostringstream os(std::ios::binary);
622                 // command (0 = update position)
623                 writeU8(os, 0);
624                 // pos
625                 writeV3F1000(os, m_base_position);
626                 // yaw
627                 writeF1000(os, m_yaw);
628                 // create message and add to list
629                 ActiveObjectMessage aom(getId(), false, os.str());
630                 messages.push_back(aom);
631         }
632 }
633
634 std::string Oerkki1SAO::getClientInitializationData()
635 {
636         std::ostringstream os(std::ios::binary);
637         // version
638         writeU8(os, 0);
639         // pos
640         writeV3F1000(os, m_base_position);
641         return os.str();
642 }
643
644 std::string Oerkki1SAO::getStaticData()
645 {
646         //dstream<<__FUNCTION_NAME<<std::endl;
647         std::ostringstream os(std::ios::binary);
648         // version
649         writeU8(os, 0);
650         // hp
651         writeU8(os, m_hp);
652         return os.str();
653 }
654
655 u16 Oerkki1SAO::punch(const std::string &toolname)
656 {
657         u16 amount = 5;
658         if(amount < m_hp)
659         {
660                 m_hp -= amount;
661         }
662         else
663         {
664                 // Die
665                 m_removed = true;
666         }
667         return 65536/100;
668 }
669
670