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