blob: 850efb95d24c0d551116e6c867e4bc1b5f8f5658 (
plain)
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
|
/*
base/patrol.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include "core/gameserver.h"
#include "base/patrol.h"
#include "base/game.h"
namespace game {
/* --- WayPoint atrol ------------------------------------------------------ */
Patrol::WayPoint::WayPoint() {
waypoint_target = 0;
waypoint_cargo = 0;
waypoint_dock = false;
}
Patrol::WayPoint::~WayPoint() {
waypoint_target = 0;
waypoint_cargo = 0;
}
void Patrol::WayPoint::set_target_label(const std::string &label)
{
waypoint_target_label.assign(label);
}
void Patrol::WayPoint::set_target(core::Entity *entity)
{
waypoint_target = entity;
}
void Patrol::WayPoint::set_dock(const bool dock)
{
waypoint_dock = dock;
}
void Patrol::WayPoint::set_cargo(Cargo *cargo)
{
waypoint_cargo = cargo;
}
/* --- Patrol ------------------------------------------------------ */
Patrol::Patrol() : core::Entity()
{
// this is a server-side only entity
set_serverside(true);
unset_flag(core::Entity::ShowOnMap);
set_flag(core::Entity::NonSolid);
entity_moduletypeid = patrol_enttype;
set_label("patrol");
set_radius(1.0f);
patrol_leader = 0;
patrol_faction = 0;
patrol_profile = NPC::ProfilePatrol;
patrol_waypoint_current == patrol_waypoints.end();
patrol_launch_timeout = 0;
}
Patrol::~Patrol()
{
// delete waypoints
for (WayPoints::iterator it = patrol_waypoints.begin(); it != patrol_waypoints.end(); ++it) {
delete (*it);
(*it) = 0;
}
patrol_waypoints.clear();
// detach and delete remaining members
for (Members::iterator it = patrol_members.begin(); it != patrol_members.end(); ++it) {
NPC *member = (*it);
member->set_patrol(0);
member->die();
(*it) = 0;
}
patrol_members.clear();
}
void Patrol::set_profile(const NPC::Profile profile)
{
patrol_profile = profile;
}
void Patrol::set_faction(Faction *faction)
{
patrol_faction = faction;
if (patrol_faction) {
patrol_faction->apply(this);
}
}
void Patrol::validate()
{
int waypoint_counter = 1;
for (WayPoints::iterator it = patrol_waypoints.begin(); it != patrol_waypoints.end(); ++it) {
WayPoint *waypoint = (*it);
if (waypoint->target_label().size() == 0) {
con_warn << " Patrol '" << label() << "' WayPoint " << waypoint_counter << " has no target" << "'\n";
break;
}
std::string entitylabel;
std::string zonelabel;
core::Zone *targetzone = 0;
size_t pos = waypoint->target_label().find(':');
if ((pos == std::string::npos) || (pos < 1) || (pos >= (waypoint->target_label().size() - 1))) {
targetzone = zone();
zonelabel.assign(zone()->label());
entitylabel.assign(waypoint->target_label());
} else {
zonelabel.assign(waypoint->target_label().substr(0, pos));
entitylabel.assign(waypoint->target_label().substr(pos + 1, waypoint->target_label().size() - pos));
targetzone = core::Zone::find(zonelabel);
if (!targetzone) {
con_warn << " Patrol '" << label() << "' waypoint " << waypoint_counter << " has invalid target zone '" << zonelabel << "'\n";
break;
}
}
core::Entity *targetentity = targetzone->find_entity(entitylabel);
if (!targetentity) {
con_warn << " Patrol '" << label() << "' waypoint " << waypoint_counter << " has unknown target '" << entitylabel << "' in zone '" << zonelabel << "'\n";
break;
}
if (waypoint->dock() && !targetentity->has_flag(core::Entity::Dockable)) {
con_warn << " Patrol '" << label() << "' waypoint " << waypoint_counter << " set to dock at non-dockable target '" << entitylabel << "' in zone '" << zonelabel << "'\n";
break;
}
waypoint->set_target(targetentity);
}
// remove invalid waypoints
for (WayPoints::iterator it = patrol_waypoints.begin(); it != patrol_waypoints.end(); ) {
WayPoint *waypoint = (*it);
if (!waypoint->target()) {
delete waypoint;
(*it) = 0;
patrol_waypoints.erase(it++);
} else {
++it;
}
}
size_t nbships = 0;
for (core::Inventory::Items::const_iterator it = inventory()->items().begin(); it != inventory()->items().end(); it++) {
core::Item *item = (*it);
if (item->info()->type() == ShipModel::infotype()) {
nbships++;
}
}
if (patrol_waypoints.size() == 0) {
con_warn << " Patrol '" << label() << "' without waypoints" << "\n";
die();
} else if (nbships == 0) {
con_warn << " Patrol '" << label() << "' without ship types" << "\n";
die();
} else if (!(*patrol_waypoints.begin())->target()->has_flag(core::Entity::Dockable)) {
con_warn << " Patrol '" << label() << "' spawn set to non-dockable target" << "\n";
die();
} else {
// spawn waypoint is dockable
(*patrol_waypoints.begin())->set_dock(true);
// spawn waypoint is current
patrol_waypoint_current = patrol_waypoints.begin();
con_debug << " " << label() << " patrol " << patrol_waypoints.size() << " " << aux::plural("waypoint", patrol_waypoints.size()) << " "
<< nbships << " ship " << aux::plural("type", nbships) << std::endl;
}
}
Patrol::WayPoint *Patrol::add_waypoint()
{
WayPoint *waypoint = new WayPoint();
patrol_waypoints.push_back(waypoint);
return waypoint;
}
void Patrol::add_member(NPC *npc)
{
npc->set_patrol(this);
patrol_members.push_back(npc);
}
void Patrol::erase_member(NPC *npc)
{
Members::iterator it = patrol_members.begin();
while (it != patrol_members.end()) {
if ((*it) == npc) {
(*it)->set_patrol(0);
(*it) = 0;
patrol_members.erase(it);
it = patrol_members.end();
} else {
++it;
}
}
set_leader();
}
void Patrol::set_leader()
{
patrol_leader = ( (patrol_members.size() > 0 ) ? (*patrol_members.begin()) : 0 );
for (Members::iterator it = patrol_members.begin(); it != patrol_members.end(); ++it) {
if ((*it) == patrol_leader) {
(*it)->set_leader(0);
} else {
(*it)->set_leader(patrol_leader);
}
}
}
// return true is any of the zones of any waypoint in this patrol has players in it
bool Patrol::route_alive() const
{
if (zone()->keepalive_run()) {
return true;
}
for (WayPoints::const_iterator it = patrol_waypoints.begin(); it != patrol_waypoints.end(); ++it) {
if ((*it)->target()->zone()->keepalive_run()) {
return true;
}
}
return false;
}
void Patrol::frame(const unsigned long elapsed)
{
if (patrol_waypoints.size() < 2) {
return;
}
if (patrol_members.size() == 0) {
// there are no members, verify if the spawn zone has players in it and create a new patrol
if (patrol_waypoint_current == patrol_waypoints.end() || !(*patrol_waypoint_current)->dock()) {
patrol_waypoint_current = patrol_waypoints.begin();
}
if (route_alive()) {
core::Entity *spawn = waypoint()->target();
if (spawn->has_flag(core::Entity::Dockable)) {
// create NPC members for every ship in the patrol's inventory
for (core::Inventory::Items::const_iterator it = inventory()->items().begin(); it != inventory()->items().end(); it++) {
core::Item *item = (*it);
if (item->info()->type() != ShipModel::infotype()) {
continue;
}
// find shipmodel
ShipModel *shipmodel = ShipModel::find(item->info()->label());
if (!shipmodel) {
continue;
}
const size_t nbships = 1 + math::randomi(item->amount());
for (size_t i = 0; i < nbships; i++) {
// add NPC
NPC *npc = new NPC(patrol_profile, shipmodel);
// set NPC name
if (shipmodel->npc_name().size()) {
npc->set_name(shipmodel->npc_name());
}
// set NPC color
if (faction()) {
faction()->apply(npc);
}
// patrol ships are not dockable
if (npc->has_flag(core::Entity::Dockable)) {
unset_flag(core::Entity::Dockable);
// delete menus
for (Menus::iterator mit = npc->menus().begin(); mit != npc->menus().end(); mit++) {
delete (*mit);
(*mit) = 0;
}
npc->menus().clear();
}
// dock npc at spawn
npc->set_zone(spawn->zone());
npc->set_dock(spawn);
// add NPC to patrol
add_member(npc);
}
}
set_leader();
patrol_launch_timeout = core::server()->timestamp() + 15000 + (unsigned long) math::randomi(30000);
}
}
} else if (patrol_leader) {
if (patrol_leader->state() == core::Entity::Docked) {
if (patrol_leader->dock() == waypoint()->target()) {
if (route_alive()) {
// verify everyone in the patrol is docked
bool group_docked = true;
for (Members::iterator it = patrol_members.begin(); it != patrol_members.end(); ++it) {
if ((*it)->state() != core::Entity::Docked) {
group_docked = false;
}
}
// next waypoint
if (group_docked) {
if (patrol_launch_timeout > 0 ) {
// patrol was created, wait for it to launch
if (core::server()->timestamp() > patrol_launch_timeout) {
// launch everyone
for (Members::iterator mit = patrol_members.begin(); mit != patrol_members.end(); ++mit) {
NPC *member = (*mit);
// buy cargo if requested
if (waypoint()->dock() && waypoint()->cargo() && member->inventory()) {
// erase all cargo from inventory
for (core::Inventory::Items::iterator iit = member->inventory()->items().begin(); iit != member->inventory()->items().end(); ) {
if ((*iit)->info()->type() == Cargo::infotype()) {
member->inventory()->items().erase(iit++);
} else {
++iit;
}
}
member->inventory()->recalculate();
core::Item *item = new core::Item(waypoint()->cargo());
item->set_amount( (long)(member->inventory()->capacity_available() / waypoint()->cargo()->volume()));
member->inventory()->add(item);
member->inventory()->recalculate();
}
// launch member
member->launch();
}
patrol_waypoint_current++;
if (patrol_waypoint_current == patrol_waypoints.end()) {
patrol_waypoint_current = patrol_waypoints.begin();
}
patrol_leader->set_autopilot_target(waypoint()->target());
patrol_launch_timeout = 0;
}
} else {
// reset patrol
for (Members::iterator mit = patrol_members.begin(); mit != patrol_members.end(); ++mit) {
NPC *member = (*mit);
member->die();
}
}
} else {
patrol_launch_timeout = 0;
}
}
} else {
patrol_leader->launch();
}
} else if ((patrol_leader->state() == core::Entity::Normal) || (patrol_leader->state() == core::Entity::ImpulseInitiate) || (patrol_leader->state() == core::Entity::Impulse)) {
if (waypoint()->target()->zone() == patrol_leader->zone()) {
patrol_leader->set_autopilot_target(waypoint()->target());
patrol_leader->set_autopilot_flag(Ship::AutoPilotEnabled);
patrol_leader->unset_autopilot_flag(Ship::AutoPilotFormation);
if ( (waypoint()->dock()) && (waypoint()->target()->has_flag(core::Entity::Dockable)) ) {
patrol_leader->set_autopilot_flag(Ship::AutoPilotDock);
} else {
patrol_leader->unset_autopilot_flag(Ship::AutoPilotDock);
if ( math::distance(patrol_leader->location(), waypoint()->target()->location()) < PLANET_SAFE_DISTANCE + patrol_leader->radius() + waypoint()->target()->radius() ) {
// check if other patrol memebers are near
bool group_present = true;
for (Members::iterator it = patrol_members.begin(); it != patrol_members.end(); ++it) {
if ((*it)->zone() != patrol_leader->zone()) {
group_present = false;
} else if ( math::distance(patrol_leader->location(), (*it)->location()) > 4.0f * (patrol_leader->radius() + (*it)->radius()) ) {
group_present = false;
}
}
// next waypoint
if (group_present) {
patrol_waypoint_current++;
if (patrol_waypoint_current == patrol_waypoints.end()) {
patrol_waypoint_current = patrol_waypoints.begin();
}
patrol_leader->set_autopilot_target(waypoint()->target());
} else {
patrol_leader->set_autopilot_target(0);
}
}
}
} else {
// current waypoint is in different zone
// check if a jumpgate got use here
JumpGate *jumpgate = (waypoint()->target()->moduletype() == jumpgate_enttype ? static_cast<JumpGate *>(waypoint()->target()) : 0 );
if (jumpgate && jumpgate->target()->zone() == patrol_leader->zone()) {
// check if other patrol memebers are near
bool group_present = true;
for (Members::iterator it = patrol_members.begin(); it != patrol_members.end(); ++it) {
if ((*it)->zone() != patrol_leader->zone()) {
group_present = false;
} else if ( math::distance(patrol_leader->location(), (*it)->location()) > 4.0f * (patrol_leader->radius() + (*it)->radius()) ) {
group_present = false;
}
}
// next waypoint
if (group_present) {
patrol_waypoint_current++;
if (patrol_waypoint_current == patrol_waypoints.end()) {
patrol_waypoint_current = patrol_waypoints.begin();
}
patrol_leader->set_autopilot_target(waypoint()->target());
} else {
// cruise away from the jumpgate until patrol memebers arrive
patrol_leader->set_autopilot_target(jumpgate->target());
patrol_leader->unset_autopilot_flag(Ship::AutoPilotEnabled);
patrol_leader->set_target_thrust(0.25f);
}
} else {
// we're lost: delete patrol
for (Members::iterator it = patrol_members.begin(); it != patrol_members.end(); ++it) {
(*it)->die();
}
}
}
} else {
patrol_leader->set_autopilot_target(0);
}
}
}
}
|