forked from mangos/MaNGOS
/
WaypointMovementGenerator.cpp
647 lines (560 loc) · 25.3 KB
/
WaypointMovementGenerator.cpp
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
creature_movement Table
alter table creature_movement add `textid1` int(11) NOT NULL default '0';
alter table creature_movement add `textid2` int(11) NOT NULL default '0';
alter table creature_movement add `textid3` int(11) NOT NULL default '0';
alter table creature_movement add `textid4` int(11) NOT NULL default '0';
alter table creature_movement add `textid5` int(11) NOT NULL default '0';
alter table creature_movement add `emote` int(10) unsigned default '0';
alter table creature_movement add `spell` int(5) unsigned default '0';
alter table creature_movement add `wpguid` int(11) default '0';
*/
#include <ctime>
#include "WaypointMovementGenerator.h"
#include "ObjectMgr.h"
#include "Creature.h"
#include "DestinationHolderImp.h"
#include "CreatureAI.h"
#include "WaypointManager.h"
#include "WorldPacket.h"
#include <cassert>
//-----------------------------------------------//
void WaypointMovementGenerator<Creature>::LoadPath(Creature &c)
{
sLog.outDetail("LoadPath: loading waypoint path for creature %d,%d", c.GetGUIDLow(), c.GetDBTableGUIDLow());
i_path = WaypointMgr.GetPath(c.GetDBTableGUIDLow());
if(!i_path)
{
sLog.outErrorDb("WaypointMovementGenerator::LoadPath: creature %s (Entry: %u GUID: %d) doesn't have waypoint path",
c.GetName(), c.GetEntry(), c.GetDBTableGUIDLow());
return;
}
uint32 node_count = i_path->size();
i_hasDone.resize(node_count);
for(uint32 i = 0; i < node_count-1; ++i)
i_hasDone[i] = false;
// to prevent a misbehavior inside "update"
// update is always called with the next wp - but the wpSys needs the current
// so when the routine is called the first time, wpSys gets the last waypoint
// and this prevents the system from performing text/emote, etc
i_hasDone[node_count - 1] = true;
}
void WaypointMovementGenerator<Creature>::ClearWaypoints()
{
i_path = NULL;
}
void WaypointMovementGenerator<Creature>::Initialize()
{
}
bool WaypointMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff)
{
if(!&creature)
return true;
// Waypoint movement can be switched on/off
// This is quite handy for escort quests and other stuff
if(creature.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_DISTRACTED))
return true;
// prevent a crash at empty waypoint path.
if(!i_path || i_path->empty())
return true;
// i_path was modified by chat commands for example
if(i_path->size() != i_hasDone.size())
i_hasDone.resize(i_path->size());
if(i_currentNode >= i_path->size())
i_currentNode = 0;
CreatureTraveller traveller(creature);
i_nextMoveTime.Update(diff);
i_destinationHolder.UpdateTraveller(traveller, diff, false, true);
// creature has been stopped in middle of the waypoint segment
if (!i_destinationHolder.HasArrived() && creature.IsStopped())
{
if( i_nextMoveTime.Passed()) // Timer has elapsed, meaning this part controlled it
{
SetStopedByPlayer(false);
// Now we re-set destination to same node and start travel
creature.addUnitState(UNIT_STAT_ROAMING);
if (creature.canFly())
creature.AddMonsterMoveFlag(MONSTER_MOVE_FLY);
const WaypointNode &node = i_path->at(i_currentNode);
i_destinationHolder.SetDestination(traveller, node.x, node.y, node.z);
i_nextMoveTime.Reset(i_destinationHolder.GetTotalTravelTime());
}
else // if( !i_nextMoveTime.Passed())
{ // unexpected end of timer && creature stopped && not at end of segment
if (!IsStopedByPlayer())
{ // Put 30 seconds delay
i_destinationHolder.IncreaseTravelTime(STOP_TIME_FOR_PLAYER);
i_nextMoveTime.Reset(STOP_TIME_FOR_PLAYER);
SetStopedByPlayer(true); // Mark we did it
}
}
return true; // Abort here this update
}
if( creature.IsStopped())
{
uint32 idx = i_currentNode > 0 ? i_currentNode-1 : i_path->size()-1;
if (!i_hasDone[idx])
{
if (i_path->at(idx).orientation !=100)
creature.SetOrientation(i_path->at(idx).orientation);
if(WaypointBehavior *behavior = i_path->at(idx).behavior)
{
if(behavior->emote != 0)
creature.SetUInt32Value(UNIT_NPC_EMOTESTATE,behavior->emote);
if(behavior->spell != 0)
creature.CastSpell(&creature,behavior->spell, false);
if(behavior->model1 != 0)
creature.SetDisplayId(behavior->model1);
if(behavior->textid[0])
{
// Not only one text is set
if( behavior->textid[1] )
{
// Select one from max 5 texts (0 and 1 laready checked)
int i = 2;
for( ; i < MAX_WAYPOINT_TEXT; ++i )
if( !behavior->textid[i] )
break;
creature.Say(behavior->textid[rand() % i], 0, 0);
}
else
creature.Say(behavior->textid[0], 0, 0);
}
i_hasDone[idx] = true;
MovementInform(creature);
} // wpBehaviour found
} // HasDone == false
} // i_creature.IsStopped()
if( i_nextMoveTime.Passed() ) // This is at the end of waypoint segment or has been stopped by player
{
if( creature.IsStopped() ) // If stopped then begin a new move segment
{
creature.addUnitState(UNIT_STAT_ROAMING);
if (creature.canFly())
creature.AddMonsterMoveFlag(MONSTER_MOVE_FLY);
const WaypointNode &node = i_path->at(i_currentNode);
i_destinationHolder.SetDestination(traveller, node.x, node.y, node.z);
i_nextMoveTime.Reset(i_destinationHolder.GetTotalTravelTime());
uint32 idx = i_currentNode > 0 ? i_currentNode-1 : i_path->size()-1;
if (i_path->at(idx).orientation !=100)
creature.SetOrientation(i_path->at(idx).orientation);
if(WaypointBehavior *behavior = i_path->at(idx).behavior )
{
i_hasDone[idx] = false;
if(behavior->model2 != 0)
creature.SetDisplayId(behavior->model2);
creature.SetUInt32Value(UNIT_NPC_EMOTESTATE, 0);
}
}
else // If not stopped then stop it and set the reset of TimeTracker to waittime
{
creature.StopMoving();
SetStopedByPlayer(false);
i_nextMoveTime.Reset(i_path->at(i_currentNode).delay);
++i_currentNode;
if( i_currentNode >= i_path->size() )
i_currentNode = 0;
}
}
return true;
}
void WaypointMovementGenerator<Creature>::MovementInform(Creature &unit)
{
if(unit.AI())
unit.AI()->MovementInform(WAYPOINT_MOTION_TYPE, i_currentNode);
}
//----------------------------------------------------//
void FlightPathMovementGenerator::LoadPath(Player &)
{
objmgr.GetTaxiPathNodes(i_pathId, i_path,i_mapIds);
}
uint32 FlightPathMovementGenerator::GetPathAtMapEnd() const
{
if(i_currentNode >= i_mapIds.size())
return i_mapIds.size();
uint32 curMapId = i_mapIds[i_currentNode];
for(uint32 i = i_currentNode; i < i_mapIds.size(); ++i)
{
if(i_mapIds[i] != curMapId)
return i;
}
return i_mapIds.size();
}
void FlightPathMovementGenerator::Initialize(Player &player)
{
player.getHostilRefManager().setOnlineOfflineState(false);
player.addUnitState(UNIT_STAT_IN_FLIGHT);
player.SetFlag(UNIT_FIELD_FLAGS,UNIT_FLAG_DISABLE_MOVE | UNIT_FLAG_TAXI_FLIGHT);
LoadPath(player);
Traveller<Player> traveller(player);
// do not send movement, it was sent already
i_destinationHolder.SetDestination(traveller, i_path[i_currentNode].x, i_path[i_currentNode].y, i_path[i_currentNode].z, false);
player.SendMonsterMoveByPath(GetPath(),GetCurrentNode(),GetPathAtMapEnd(),MONSTER_MOVE_SPLINE_FLY);
}
void FlightPathMovementGenerator::Finalize(Player & player)
{
// remove flag to prevent send object build movement packets for flight state and crash (movement generator already not at top of stack)
player.clearUnitState(UNIT_STAT_IN_FLIGHT);
float x, y, z;
i_destinationHolder.GetLocationNow(player.GetBaseMap(), x, y, z);
player.SetPosition(x, y, z, player.GetOrientation());
player.Unmount();
player.RemoveFlag(UNIT_FIELD_FLAGS,UNIT_FLAG_DISABLE_MOVE | UNIT_FLAG_TAXI_FLIGHT);
if(player.m_taxi.empty())
{
player.getHostilRefManager().setOnlineOfflineState(true);
if(player.pvpInfo.inHostileArea)
player.CastSpell(&player, 2479, true);
// update z position to ground and orientation for landing point
// this prevent cheating with landing point at lags
// when client side flight end early in comparison server side
player.StopMoving();
}
}
bool FlightPathMovementGenerator::Update(Player &player, const uint32 &diff)
{
if( MovementInProgress() )
{
Traveller<Player> traveller(player);
if( i_destinationHolder.UpdateTraveller(traveller, diff, false) )
{
i_destinationHolder.ResetUpdate(FLIGHT_TRAVEL_UPDATE);
if( i_destinationHolder.HasArrived() )
{
uint32 curMap = i_mapIds[i_currentNode];
++i_currentNode;
if( MovementInProgress() )
{
DEBUG_LOG("loading node %u for player %s", i_currentNode, player.GetName());
if(i_mapIds[i_currentNode]==curMap)
{
// do not send movement, it was sent already
i_destinationHolder.SetDestination(traveller, i_path[i_currentNode].x, i_path[i_currentNode].y, i_path[i_currentNode].z, false);
}
return true;
}
//else HasArrived()
}
else
return true;
}
else
return true;
}
// we have arrived at the end of the path
return false;
}
void FlightPathMovementGenerator::SetCurrentNodeAfterTeleport()
{
if(i_mapIds.empty())
return;
uint32 map0 = i_mapIds[0];
for(int i = 1; i < i_mapIds.size(); ++i)
{
if(i_mapIds[i]!=map0)
{
i_currentNode = i;
return;
}
}
}
//
// Unique1's ASTAR Pathfinding Code... For future use & reference...
//
#ifdef __PATHFINDING__
int GetFCost(int to, int num, int parentNum, float *gcost); // Below...
int ShortenASTARRoute(short int *pathlist, int number)
{ // Wrote this to make the routes a little smarter (shorter)... No point looping back to the same places... Unique1
short int temppathlist[MAX_PATHLIST_NODES];
int count = 0;
// int count2 = 0;
int temp, temp2;
int link;
int upto = 0;
for (temp = number; temp >= 0; temp--)
{
qboolean shortened = qfalse;
for (temp2 = 0; temp2 < temp; temp2++)
{
for (link = 0; link < nodes[pathlist[temp]].enodenum; link++)
{
if (nodes[pathlist[temp]].links[link].flags & PATH_BLOCKED)
continue;
//if ((bot->client->ps.eFlags & EF_TANK) && nodes[bot->current_node].links[link].flags & PATH_NOTANKS) //if this path is blocked, skip it
// continue;
//if (nodes[nodes[pathlist[temp]].links[link].targetNode].origin[2] > nodes[pathlist[temp]].origin[2] + 32)
// continue;
if (nodes[pathlist[temp]].links[link].targetNode == pathlist[temp2])
{ // Found a shorter route...
//if (OrgVisible(nodes[pathlist[temp2]].origin, nodes[pathlist[temp]].origin, -1))
{
temppathlist[count] = pathlist[temp2];
temp = temp2;
++count;
shortened = qtrue;
}
}
}
}
if (!shortened)
{
temppathlist[count] = pathlist[temp];
++count;
}
}
upto = count;
for (temp = 0; temp < count; temp++)
{
pathlist[temp] = temppathlist[upto];
--upto;
}
G_Printf("ShortenASTARRoute: Path size reduced from %i to %i nodes...n", number, count);
return count;
}
/*
===========================================================================
CreatePathAStar
This function uses the A* pathfinding algorithm to determine the
shortest path between any two nodes.
It's fairly complex, so I'm not really going to explain it much.
Look up A* and binary heaps for more info.
pathlist stores the ideal path between the nodes, in reverse order,
and the return value is the number of nodes in that path
===========================================================================
*/
int CreatePathAStar(gentity_t *bot, int from, int to, short int *pathlist)
{
//all the data we have to hold...since we can't do dynamic allocation, has to be MAX_NODES
//we can probably lower this later - eg, the open list should never have more than at most a few dozen items on it
short int openlist[MAX_NODES+1]; //add 1 because it's a binary heap, and they don't use 0 - 1 is the first used index
float gcost[MAX_NODES];
int fcost[MAX_NODES];
char list[MAX_NODES]; //0 is neither, 1 is open, 2 is closed - char because it's the smallest data type
short int parent[MAX_NODES];
short int numOpen = 0;
short int atNode, temp, newnode=-1;
qboolean found = qfalse;
int count = -1;
float gc;
int i, u, v, m;
vec3_t vec;
//clear out all the arrays
memset(openlist, 0, sizeof(short int)*(MAX_NODES+1));
memset(fcost, 0, sizeof(int)*MAX_NODES);
memset(list, 0, sizeof(char)*MAX_NODES);
memset(parent, 0, sizeof(short int)*MAX_NODES);
memset(gcost, -1, sizeof(float)*MAX_NODES);
//make sure we have valid data before calculating everything
if ((from == NODE_INVALID) || (to == NODE_INVALID) || (from >= MAX_NODES) || (to >= MAX_NODES) || (from == to))
return -1;
openlist[1] = from; //add the starting node to the open list
++numOpen;
gcost[from] = 0; //its f and g costs are obviously 0
fcost[from] = 0;
while (1)
{
if (numOpen != 0) //if there are still items in the open list
{
//pop the top item off of the list
atNode = openlist[1];
list[atNode] = 2; //put the node on the closed list so we don't check it again
--numOpen;
openlist[1] = openlist[numOpen+1]; //move the last item in the list to the top position
v = 1;
//this while loop reorders the list so that the new lowest fcost is at the top again
while (1)
{
u = v;
if ((2*u+1) < numOpen) //if both children exist
{
if (fcost[openlist[u]] >= fcost[openlist[2*u]])
v = 2*u;
if (fcost[openlist[v]] >= fcost[openlist[2*u+1]])
v = 2*u+1;
}
else
{
if ((2*u) < numOpen) //if only one child exists
{
if (fcost[openlist[u]] >= fcost[openlist[2*u]])
v = 2*u;
}
}
if (u != v) //if they're out of order, swap this item with its parent
{
temp = openlist[u];
openlist[u] = openlist[v];
openlist[v] = temp;
}
else
break;
}
for (i = 0; i < nodes[atNode].enodenum; ++i) //loop through all the links for this node
{
newnode = nodes[atNode].links[i].targetNode;
//if this path is blocked, skip it
if (nodes[atNode].links[i].flags & PATH_BLOCKED)
continue;
//if this path is blocked, skip it
if (bot->client && (bot->client->ps.eFlags & EF_TANK) && nodes[atNode].links[i].flags & PATH_NOTANKS)
continue;
//skip any unreachable nodes
if (bot->client && (nodes[newnode].type & NODE_ALLY_UNREACHABLE) && (bot->client->sess.sessionTeam == TEAM_ALLIES))
continue;
if (bot->client && (nodes[newnode].type & NODE_AXIS_UNREACHABLE) && (bot->client->sess.sessionTeam == TEAM_AXIS))
continue;
if (list[newnode] == 2) //if this node is on the closed list, skip it
continue;
if (list[newnode] != 1) //if this node is not already on the open list
{
openlist[++numOpen] = newnode; //add the new node to the open list
list[newnode] = 1;
parent[newnode] = atNode; //record the node's parent
if (newnode == to) //if we've found the goal, don't keep computing paths!
break; //this will break the 'for' and go all the way to 'if (list[to] == 1)'
//store it's f cost value
fcost[newnode] = GetFCost(to, newnode, parent[newnode], gcost);
//this loop re-orders the heap so that the lowest fcost is at the top
m = numOpen;
while (m != 1) //while this item isn't at the top of the heap already
{
//if it has a lower fcost than its parent
if (fcost[openlist[m]] <= fcost[openlist[m/2]])
{
temp = openlist[m/2];
openlist[m/2] = openlist[m];
openlist[m] = temp; //swap them
m /= 2;
}
else
break;
}
}
else //if this node is already on the open list
{
gc = gcost[atNode];
VectorSubtract(nodes[newnode].origin, nodes[atNode].origin, vec);
gc += VectorLength(vec); //calculate what the gcost would be if we reached this node along the current path
if (gc < gcost[newnode]) //if the new gcost is less (ie, this path is shorter than what we had before)
{
parent[newnode] = atNode; //set the new parent for this node
gcost[newnode] = gc; //and the new g cost
for (i = 1; i < numOpen; ++i) //loop through all the items on the open list
{
if (openlist[i] == newnode) //find this node in the list
{
//calculate the new fcost and store it
fcost[newnode] = GetFCost(to, newnode, parent[newnode], gcost);
//reorder the list again, with the lowest fcost item on top
m = i;
while (m != 1)
{
//if the item has a lower fcost than it's parent
if (fcost[openlist[m]] < fcost[openlist[m/2]])
{
temp = openlist[m/2];
openlist[m/2] = openlist[m];
openlist[m] = temp; //swap them
m /= 2;
}
else
break;
}
break; //exit the 'for' loop because we already changed this node
} //if
} //for
} //if (gc < gcost[newnode])
} //if (list[newnode] != 1) --> else
} //for (loop through links)
} //if (numOpen != 0)
else
{
found = qfalse; //there is no path between these nodes
break;
}
if (list[to] == 1) //if the destination node is on the open list, we're done
{
found = qtrue;
break;
}
} //while (1)
if (found == qtrue) //if we found a path
{
//G_Printf("%s - path found!n", bot->client->pers.netname);
count = 0;
temp = to; //start at the end point
while (temp != from) //travel along the path (backwards) until we reach the starting point
{
pathlist[count++] = temp; //add the node to the pathlist and increment the count
temp = parent[temp]; //move to the parent of this node to continue the path
}
pathlist[count++] = from; //add the beginning node to the end of the pathlist
#ifdef __BOT_SHORTEN_ROUTING__
count = ShortenASTARRoute(pathlist, count); // This isn't working... Dunno why.. Unique1
#endif //__BOT_SHORTEN_ROUTING__
}
else
{
//G_Printf("^1*** ^4BOT DEBUG^5: (CreatePathAStar) There is no route between node ^7%i^5 and node ^7%i^5.n", from, to);
count = CreateDumbRoute(from, to, pathlist);
if (count > 0)
{
#ifdef __BOT_SHORTEN_ROUTING__
count = ShortenASTARRoute(pathlist, count); // This isn't working... Dunno why.. Unique1
#endif //__BOT_SHORTEN_ROUTING__
return count;
}
}
return count; //return the number of nodes in the path, -1 if not found
}
/*
===========================================================================
GetFCost
Utility function used by A* pathfinding to calculate the
cost to move between nodes towards a goal. Using the A*
algorithm F = G + H, G here is the distance along the node
paths the bot must travel, and H is the straight-line distance
to the goal node.
Returned as an int because more precision is unnecessary and it
will slightly speed up heap access
===========================================================================
*/
int GetFCost(int to, int num, int parentNum, float *gcost)
{
float gc = 0;
float hc = 0;
vec3_t v;
if (gcost[num] == -1)
{
if (parentNum != -1)
{
gc = gcost[parentNum];
VectorSubtract(nodes[num].origin, nodes[parentNum].origin, v);
gc += VectorLength(v);
}
gcost[num] = gc;
}
else
gc = gcost[num];
VectorSubtract(nodes[to].origin, nodes[num].origin, v);
hc = VectorLength(v);
return (int)(gc + hc);
}
#endif //__PATHFINDING__