1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Monster.h"
#include "../Root.h"
#include "../Server.h"
#include "../ClientHandle.h"
#include "../World.h"
#include "../Player.h"
#include "../Defines.h"
#include "../MonsterConfig.h"
#include "../MersenneTwister.h"
#include "../Vector3f.h"
#include "../Vector3i.h"
#include "../Vector3d.h"
#include "../Tracer.h"
#include "../../iniFile/iniFile.h"
/*
#ifndef _WIN32
#include <unistd.h>
#endif
*/
cMonster::cMonster(void)
: super(etMob)
, m_Target(NULL)
, m_bMovingToDestination(false)
, m_DestinationTime( 0 )
, m_Gravity( -9.81f)
, m_bOnGround( false )
, m_DestroyTimer( 0 )
, m_Jump(0)
, m_MobType( 0 )
, m_SoundHurt( "" )
, m_SoundDeath( "" )
, m_EMState(IDLE)
, m_SightDistance(25)
, m_SeePlayerInterval (0)
, m_EMPersonality(AGGRESSIVE)
, m_AttackDamage(1.0f)
, m_AttackRange(5.0f)
, m_AttackInterval(0)
, m_AttackRate(3)
, idle_interval(0)
{
LOGD("cMonster::cMonster()");
LOGD("In state: %s", GetState());
m_bBurnable = true;
m_MetaData = NORMAL;
}
void cMonster::SpawnOn(cClientHandle & a_Client)
{
a_Client.SendSpawnMob(*this);
}
void cMonster::MoveToPosition( const Vector3f & a_Position )
{
m_bMovingToDestination = true;
m_Destination = a_Position;
}
bool cMonster::ReachedDestination()
{
Vector3f Distance = (m_Destination) - Vector3f( m_Pos );
if( Distance.SqrLength() < 2.f )
return true;
return false;
}
void cMonster::Tick(float a_Dt)
{
cPawn::Tick(a_Dt);
if( m_Health <= 0 )
{
m_DestroyTimer += a_Dt/1000;
if( m_DestroyTimer > 1 )
{
Destroy();
}
return;
}
a_Dt /= 1000;
if( m_bMovingToDestination )
{
Vector3f Pos( m_Pos );
Vector3f Distance = m_Destination - Pos;
if( !ReachedDestination() )
{
Distance.y = 0;
Distance.Normalize();
Distance *= 3;
m_Speed.x = Distance.x;
m_Speed.z = Distance.z;
if (m_EMState == ESCAPING)
{ //Runs Faster when escaping :D otherwise they just walk away
m_Speed.x *= 2.f;
m_Speed.z *= 2.f;
}
}
else
{
m_bMovingToDestination = false;
}
if( m_Speed.SqrLength() > 0.f )
{
if( m_bOnGround )
{
Vector3f NormSpeed = m_Speed.NormalizeCopy();
Vector3f NextBlock = Vector3f( m_Pos ) + NormSpeed;
double NextHeight = (double)GetWorld()->GetHeight( (int)NextBlock.x, (int)NextBlock.z );
if( NextHeight > m_Pos.y - 1.2 && NextHeight - m_Pos.y < 2.5 )
{
m_bOnGround = false;
m_Speed.y = 7.f; // Jump!!
}
}
}
}
HandlePhysics( a_Dt );
ReplicateMovement();
Vector3f Distance = m_Destination - Vector3f( m_Pos );
if (Distance.SqrLength() > 0.1f)
{
float Rotation, Pitch;
Distance.Normalize();
VectorToEuler( Distance.x, Distance.y, Distance.z, Rotation, Pitch );
SetRotation( Rotation );
SetPitch( Pitch );
}
if (m_EMState == IDLE)
{
// If enemy passive we ignore checks for player visibility
InStateIdle(a_Dt);
}
if (m_EMState == CHASING)
{
// If we do not see a player anymore skip chasing action
InStateChasing(a_Dt);
}
if (m_EMState == ESCAPING)
{
InStateEscaping(a_Dt);
}
}
void cMonster::ReplicateMovement()
{
if (m_bDirtyOrientation && !m_bDirtyPosition)
{
m_World->BroadcastEntLook(*this);
m_bDirtyOrientation = false;
}
if (m_bDirtyPosition)
{
float DiffX = (float)(GetPosX() - m_LastPosX);
float DiffY = (float)(GetPosY() - m_LastPosY);
float DiffZ = (float)(GetPosZ() - m_LastPosZ);
float SqrDist = DiffX * DiffX + DiffY * DiffY + DiffZ * DiffZ;
if (
(SqrDist > 4 * 4) // 4 blocks is max Relative Move
|| (m_World->GetWorldAge() - m_TimeLastTeleportPacket > 40) // Send an absolute position every 2 seconds
)
{
// LOGD("Teleported %f", sqrtf(SqrDist) );
m_World->BroadcastTeleportEntity(*this);
m_TimeLastTeleportPacket = m_World->GetWorldAge();
}
else
{
// Relative move sucks balls! It's always wrong wtf!
if (m_bDirtyOrientation)
{
m_World->BroadcastEntRelMoveLook(*this, (char)(DiffX * 32), (char)(DiffY * 32), (char)(DiffZ * 32));
m_bDirtyOrientation = false;
}
else
{
m_World->BroadcastEntRelMove(*this, (char)(DiffX * 32), (char)(DiffY * 32), (char)(DiffZ * 32));
}
}
m_LastPosX = GetPosX();
m_LastPosY = GetPosY();
m_LastPosZ = GetPosZ();
m_bDirtyPosition = false;
}
MoveToCorrectChunk();
}
void cMonster::HandlePhysics(float a_Dt)
{
if( m_bOnGround ) // check if it's still on the ground
{
cWorld* World = GetWorld();
if( World->GetBlock( (int)m_Pos.x, (int)m_Pos.y -1, (int)m_Pos.z ) == E_BLOCK_AIR )
{
m_bOnGround = false;
}
if( World->GetBlock( (int)m_Pos.x, (int)m_Pos.y, (int)m_Pos.z ) != E_BLOCK_AIR ) // If in ground itself, push it out
{
m_bOnGround = true;
m_Pos.y += 0.2;
m_bDirtyPosition = true;
}
m_Speed.x *= 0.7f/(1+a_Dt);
if( fabs(m_Speed.x) < 0.05 ) m_Speed.x = 0;
m_Speed.z *= 0.7f/(1+a_Dt);
if( fabs(m_Speed.z) < 0.05 ) m_Speed.z = 0;
}
if( !m_bOnGround )
{
float Gravity = -9.81f*a_Dt;
m_Speed.y += Gravity;
}
if( m_Speed.SqrLength() > 0.f )
{
cTracer Tracer( GetWorld() );
int Ret = Tracer.Trace( m_Pos, m_Speed, 2 );
if( Ret ) // Oh noez! we hit something
{
// Set to hit position
if( (Tracer.RealHit - Vector3f(m_Pos)).SqrLength() <= ( m_Speed * a_Dt ).SqrLength() )
{
if( Ret == 1 )
{
if( Tracer.HitNormal.x != 0.f ) m_Speed.x = 0.f;
if( Tracer.HitNormal.y != 0.f ) m_Speed.y = 0.f;
if( Tracer.HitNormal.z != 0.f ) m_Speed.z = 0.f;
if( Tracer.HitNormal.y > 0 ) // means on ground
{
m_bOnGround = true;
}
}
m_Pos = Tracer.RealHit;
m_Pos += Tracer.HitNormal * 0.2f;
}
else
m_Pos += m_Speed*a_Dt;
}
else
{ // We didn't hit anything, so move =]
m_Pos += m_Speed*a_Dt;
}
m_bDirtyPosition = true;
}
}
void cMonster::DoTakeDamage(TakeDamageInfo & a_TDI)
{
super::DoTakeDamage(a_TDI);
if((m_SoundHurt != "") && (m_Health > 0)) m_World->BroadcastSoundEffect(m_SoundHurt, (int)(m_Pos.x * 8), (int)(m_Pos.y * 8), (int)(m_Pos.z * 8), 1.0f, 0.8f);
if (a_TDI.Attacker != NULL)
{
m_Target = a_TDI.Attacker;
AddReference(m_Target);
}
}
void cMonster::KilledBy(cPawn * a_Killer)
{
super::KilledBy(a_Killer);
if(m_SoundHurt != "") m_World->BroadcastSoundEffect(m_SoundDeath, (int)(m_Pos.x * 8), (int)(m_Pos.y * 8), (int)(m_Pos.z * 8), 1.0f, 0.8f);
m_DestroyTimer = 0;
}
//----State Logic
const char *cMonster::GetState()
{
switch(m_EMState)
{
case IDLE: return "Idle";
case ATTACKING: return "Attacking";
case CHASING: return "Chasing";
default: return "Unknown";
}
}
// for debugging
void cMonster::SetState(const AString & a_State)
{
if (a_State.compare("Idle") == 0)
{
m_EMState = IDLE;
}
else if (a_State.compare("Attacking") == 0)
{
m_EMState = ATTACKING;
}
else if (a_State.compare("Chasing") == 0)
{
m_EMState = CHASING;
}
else
{
LOGD("cMonster::SetState(): Invalid state");
ASSERT(!"Invalid state");
}
}
//Checks to see if EventSeePlayer should be fired
//monster sez: Do I see the player
void cMonster::CheckEventSeePlayer()
{
cPlayer *Closest = FindClosestPlayer();
if (Closest)
{
EventSeePlayer(Closest);
}
}
void cMonster::CheckEventLostPlayer()
{
Vector3f pos;
cTracer LineOfSight(GetWorld());
if (m_Target != NULL)
{
pos = m_Target->GetPosition();
if ((pos - m_Pos).Length() > m_SightDistance || LineOfSight.Trace(m_Pos,(pos - m_Pos), (int)(pos - m_Pos).Length()))
{
EventLosePlayer();
}
}
else
{
EventLosePlayer();
}
}
// What to do if player is seen
// default to change state to chasing
void cMonster::EventSeePlayer(cEntity *a_SeenPlayer)
{
m_Target = a_SeenPlayer;
AddReference( m_Target );
}
void cMonster::EventLosePlayer()
{
Dereference(m_Target);
m_Target = 0;
m_EMState = IDLE;
}
//What to do if in Idle State
void cMonster::InStateIdle(float a_Dt)
{
idle_interval += a_Dt;
if (idle_interval > 1)
{
// at this interval the results are predictable
MTRand r1;
int rem = r1.randInt()%6 + 1;
// LOGD("Moving: int: %3.3f rem: %i",idle_interval,rem);
idle_interval -= 1; // So nothing gets dropped when the server hangs for a few seconds
Vector3f Dist;
Dist.x = (float)((r1.randInt()%11)-5);
Dist.z = (float)((r1.randInt()%11)-5);
if( Dist.SqrLength() > 2 && rem >= 3)
{
m_Destination.x = (float)(m_Pos.x + Dist.x);
m_Destination.z = (float)(m_Pos.z + Dist.z);
m_Destination.y = (float)GetWorld()->GetHeight( (int)m_Destination.x, (int)m_Destination.z ) + 1.2f;
MoveToPosition( m_Destination );
}
}
}
// What to do if in Chasing State
// This state should always be defined in each child class
void cMonster::InStateChasing(float a_Dt)
{
UNUSED(a_Dt);
}
// What to do if in Escaping State
void cMonster::InStateEscaping(float a_Dt)
{
(void)a_Dt;
if(m_Target)
{
Vector3d newloc = m_Pos;
newloc.x = (m_Target->GetPosition().x < newloc.x)? (newloc.x + m_SightDistance): (newloc.x - m_SightDistance);
newloc.z = (m_Target->GetPosition().z < newloc.z)? (newloc.z + m_SightDistance): (newloc.z - m_SightDistance);
MoveToPosition(newloc);
}
else
{
m_EMState = IDLE; //this shouldnt be required but just to be safe
}
}
// Do attack here
// a_Dt is passed so we can set attack rate
void cMonster::Attack(float a_Dt)
{
m_AttackInterval += a_Dt * m_AttackRate;
if ((m_Target != NULL) && (m_AttackInterval > 3.0))
{
// Setting this higher gives us more wiggle room for attackrate
m_AttackInterval = 0.0;
((cPawn *)m_Target)->TakeDamage(*this);
}
}
// Checks for Players close by and if they are visible return the closest
cPlayer * cMonster::FindClosestPlayer(void)
{
return m_World->FindClosestPlayer(m_Pos, m_SightDistance);
}
void cMonster::GetMonsterConfig(const char* pm_name)
{
cRoot::Get()->GetMonsterConfig()->AssignAttributes(this, pm_name);
}
void cMonster::SetAttackRate(int ar)
{
m_AttackRate = (float)ar;
}
void cMonster::SetAttackRange(float ar)
{
m_AttackRange = ar;
}
void cMonster::SetAttackDamage(float ad)
{
m_AttackDamage = ad;
}
void cMonster::SetSightDistance(float sd)
{
m_SightDistance = sd;
}
void cMonster::AddRandomDropItem(cItems & a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth)
{
MTRand r1;
int Count = r1.randInt() % (a_Max + 1 - a_Min) + a_Min;
if (Count > 0)
{
a_Drops.push_back(cItem(a_Item, Count, a_ItemHealth));
}
}
|