-
Notifications
You must be signed in to change notification settings - Fork 160
/
Fleet.cpp
1417 lines (1204 loc) · 59.8 KB
/
Fleet.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Fleet.h"
#include "Universe.h"
#include "Ship.h"
#include "System.h"
#include "Predicates.h"
#include "../util/i18n.h"
#include "../util/Logger.h"
#include "../util/ScopedTimer.h"
#include "../Empire/Empire.h"
#include "../Empire/EmpireManager.h"
#include "../universe/ShipDesign.h"
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/timer.hpp>
#include <cmath>
namespace {
const double MAX_SHIP_SPEED = 500.0; // max allowed speed of ship movement
const double FLEET_MOVEMENT_EPSILON = 0.1; // how close a fleet needs to be to a system to have arrived in the system
bool SystemHasNoVisibleStarlanes(int system_id, int empire_id)
{ return !GetUniverse().SystemHasVisibleStarlanes(system_id, empire_id); }
template<typename IT>
double PathLength (const IT& begin, const IT& end) {
if (begin == end) {
return 0;
} else {
double distance = 0.0;
for (std::list<int>::const_iterator it = begin; it != end; ++it) {
std::list<int>::const_iterator next_it = it; ++next_it;
//DebugLogger() << "Fleet::SetRoute() new route has system id " << *it;
if (next_it == end)
break; // current system is the last on the route, so don't need to add any additional distance.
TemporaryPtr<const System> cur_sys = GetSystem(*it);
if (!cur_sys) {
ErrorLogger() << "Fleet::SetRoute() couldn't get system with id " << *it;
return distance;
}
TemporaryPtr<const System> next_sys = GetSystem(*next_it);
if (!next_sys) {
ErrorLogger() << "Fleet::SetRoute() couldn't get system with id " << *next_it;
return distance;
}
double dist_x = next_sys->X() - cur_sys->X();
double dist_y = next_sys->Y() - cur_sys->Y();
distance += std::sqrt(dist_x*dist_x + dist_y*dist_y);
}
return distance;
}
}
void MoveFleetWithShips(TemporaryPtr<Fleet>& fleet, double x, double y){
fleet->MoveTo(x, y);
std::vector<TemporaryPtr<Ship> > ships = Objects().FindObjects<Ship>(fleet->ShipIDs());
for (std::vector<TemporaryPtr<Ship> >::iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<Ship> ship = *ship_it;
ship->MoveTo(x, y);
}
}
void InsertFleetWithShips(TemporaryPtr<Fleet>& fleet, TemporaryPtr<System>& system){
system->Insert(fleet);
std::vector<TemporaryPtr<Ship> > ships = Objects().FindObjects<Ship>(fleet->ShipIDs());
for (std::vector<TemporaryPtr<Ship> >::iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<Ship> ship = *ship_it;
system->Insert(ship);
}
}
}
// static(s)
const int Fleet::ETA_UNKNOWN = (1 << 30);
const int Fleet::ETA_OUT_OF_RANGE = (1 << 30) - 1;
const int Fleet::ETA_NEVER = (1 << 30) - 2;
Fleet::Fleet(const std::string& name, double x, double y, int owner) :
UniverseObject(name, x, y),
m_prev_system(INVALID_OBJECT_ID),
m_next_system(INVALID_OBJECT_ID),
m_aggressive(true),
m_ordered_given_to_empire_id(ALL_EMPIRES),
m_travel_distance(0.0),
m_arrived_this_turn(false),
m_arrival_starlane(INVALID_OBJECT_ID)
{
UniverseObject::Init();
SetOwner(owner);
}
Fleet* Fleet::Clone(int empire_id) const {
Visibility vis = GetUniverse().GetObjectVisibilityByEmpire(this->ID(), empire_id);
if (!(vis >= VIS_BASIC_VISIBILITY && vis <= VIS_FULL_VISIBILITY))
return 0;
Fleet* retval = new Fleet();
retval->Copy(TemporaryFromThis(), empire_id);
return retval;
}
void Fleet::Copy(TemporaryPtr<const UniverseObject> copied_object, int empire_id) {
if (copied_object == this)
return;
TemporaryPtr<const Fleet> copied_fleet = boost::dynamic_pointer_cast<const Fleet>(copied_object);
if (!copied_fleet) {
ErrorLogger() << "Fleet::Copy passed an object that wasn't a Fleet";
return;
}
int copied_object_id = copied_object->ID();
Visibility vis = GetUniverse().GetObjectVisibilityByEmpire(copied_object_id, empire_id);
std::set<std::string> visible_specials = GetUniverse().GetObjectVisibleSpecialsByEmpire(copied_object_id, empire_id);
UniverseObject::Copy(copied_object, vis, visible_specials);
if (vis >= VIS_BASIC_VISIBILITY) {
this->m_ships = copied_fleet->VisibleContainedObjectIDs(empire_id);
this->m_next_system = copied_fleet->m_next_system;
this->m_prev_system = copied_fleet->m_prev_system;
this->m_arrived_this_turn = copied_fleet->m_arrived_this_turn;
this->m_arrival_starlane = copied_fleet->m_arrival_starlane;
Meter* stealth_meter = this->GetMeter(METER_STEALTH);
const Meter* copied_stealth_meter = copied_fleet->GetMeter(METER_STEALTH);
stealth_meter->Set(copied_stealth_meter->Current(), copied_stealth_meter->Initial());
if (vis >= VIS_PARTIAL_VISIBILITY) {
this->m_aggressive = copied_fleet->m_aggressive;
if (this->Unowned())
this->m_name = copied_fleet->m_name;
if (vis >= VIS_FULL_VISIBILITY) {
this->m_travel_route = copied_fleet->m_travel_route;
this->m_travel_distance = copied_fleet->m_travel_distance;
this->m_ordered_given_to_empire_id =copied_fleet->m_ordered_given_to_empire_id;
} else {
int moving_to = copied_fleet->m_next_system;
std::list<int> travel_route;
double travel_distance = copied_fleet->m_travel_distance;
const std::list<int>& copied_fleet_route = copied_fleet->m_travel_route;
if (m_travel_route.empty() && !copied_fleet->m_travel_route.empty())
m_travel_route.push_back(moving_to);
ShortenRouteToEndAtSystem(travel_route, moving_to);
if (!travel_route.empty() && travel_route.front() != 0 && travel_route.size() != copied_fleet_route.size()) {
try {
travel_distance -= GetUniverse().ShortestPath(travel_route.back(),
copied_fleet_route.back()).second;
} catch (...) {
DebugLogger() << "Fleet::Copy couldn't find route to system(s):"
<< " travel route back: " << travel_route.back()
<< " or copied fleet route back: " << copied_fleet_route.back();
}
}
this->m_travel_route = travel_route;
this->m_travel_distance = travel_distance;
}
}
}
}
UniverseObjectType Fleet::ObjectType() const
{ return OBJ_FLEET; }
std::string Fleet::Dump() const {
std::stringstream os;
os << UniverseObject::Dump();
os << ( m_aggressive ? " agressive" : " passive")
<< " cur system: " << SystemID()
<< " moving to: " << FinalDestinationID()
<< " prev system: " << m_prev_system
<< " next system: " << m_next_system
<< " arrival lane: " << m_arrival_starlane
<< " ships: ";
for (std::set<int>::const_iterator it = m_ships.begin(); it != m_ships.end();) {
int ship_id = *it;
++it;
os << ship_id << (it == m_ships.end() ? "" : ", ");
}
return os.str();
}
int Fleet::ContainerObjectID() const
{ return this->SystemID(); }
const std::set<int>& Fleet::ContainedObjectIDs() const
{ return m_ships; }
bool Fleet::Contains(int object_id) const
{ return object_id != INVALID_OBJECT_ID && m_ships.find(object_id) != m_ships.end(); }
bool Fleet::ContainedBy(int object_id) const
{ return object_id != INVALID_OBJECT_ID && this->SystemID() == object_id; }
const std::string& Fleet::PublicName(int empire_id) const {
// Disclose real fleet name only to fleet owners.
if (GetUniverse().AllObjectsVisible() || empire_id == ALL_EMPIRES || OwnedBy(empire_id))
return Name();
else if (!Unowned())
return UserString("FW_FOREIGN_FLEET");
else if (Unowned() && HasMonsters())
return UserString("MONSTERS");
else if (Unowned() && GetVisibility(empire_id) > VIS_NO_VISIBILITY)
return UserString("FW_ROGUE_FLEET");
else
return UserString("OBJ_FLEET");
}
const std::list<int>& Fleet::TravelRoute() const {
return m_travel_route;
}
std::list<MovePathNode> Fleet::MovePath(bool flag_blockades /*= false*/) const
{ return MovePath(TravelRoute(), flag_blockades); }
std::list<MovePathNode> Fleet::MovePath(const std::list<int>& route, bool flag_blockades /*= false*/) const {
std::list<MovePathNode> retval;
if (route.empty())
return retval; // nowhere to go => empty path
// if (route.size() == 1) do nothing special. this fleet is probably on the starlane leading to
// its final destination. normal looping to read destination should work fine
if (route.size() == 2 && route.front() == route.back())
return retval; // nowhere to go => empty path
if (this->Speed() < FLEET_MOVEMENT_EPSILON) {
retval.push_back(MovePathNode(this->X(), this->Y(), true, ETA_NEVER, this->SystemID(), INVALID_OBJECT_ID, INVALID_OBJECT_ID));
return retval; // can't move => path is just this system with explanatory ETA
}
float fuel = Fuel();
float max_fuel = MaxFuel();
//DebugLogger() << "Fleet " << this->Name() << " movePath fuel: " << fuel << " sys id: " << this->SystemID();
// determine all systems where fleet(s) can be resupplied if fuel runs out
int owner = this->Owner();
const Empire* empire = GetEmpire(owner);
std::set<int> fleet_supplied_systems;
std::set<int> unobstructed_systems;
if (empire) {
fleet_supplied_systems = empire->FleetSupplyableSystemIDs();
unobstructed_systems = empire->SupplyUnobstructedSystems();
}
// determine if, given fuel available and supplyable systems, fleet will ever be able to move
if (fuel < 1.0f &&
this->SystemID() != INVALID_OBJECT_ID &&
fleet_supplied_systems.find(this->SystemID()) == fleet_supplied_systems.end())
{
MovePathNode node(this->X(), this->Y(), true, ETA_OUT_OF_RANGE,
this->SystemID(),
INVALID_OBJECT_ID,
INVALID_OBJECT_ID);
retval.push_back(node);
return retval; // can't move => path is just this system with explanatory ETA
}
// blockade debug logging
//DebugLogger() << "Fleet::MovePath for fleet " << this->Name() << " ID(" << this->ID() <<") and route:";
//for (std::list<int>::const_iterator route_it = route.begin(); route_it != route.end(); route_it++)
// DebugLogger() << "Fleet::MovePath ... " << *route_it;
//DebugLogger() << "Fleet::MovePath END of Route ";
// get iterator pointing to TemporaryPtr<System> on route that is the first after where this fleet is currently.
// if this fleet is in a system, the iterator will point to the system after the current in the route
// if this fleet is not in a system, the iterator will point to the first system in the route
std::list<int>::const_iterator route_it = route.begin();
if (*route_it == SystemID())
++route_it; // first system in route is current system of this fleet. skip to the next system
if (route_it == route.end())
return retval; // current system of this fleet is the *only* system in the route. path is empty.
// get current, previous and next systems of fleet
TemporaryPtr<const System> cur_system = GetSystem(this->SystemID()); // may be 0
TemporaryPtr<const System> prev_system = GetSystem(this->PreviousSystemID());// may be 0 if this fleet is not moving or ordered to move
TemporaryPtr<const System> next_system = GetSystem(*route_it); // can't use this->NextSystemID() because this fleet may not be moving and may not have a next system. this might occur when a fleet is in a system, not ordered to move or ordered to move to a system, but a projected fleet move line is being calculated to a different system
if (!next_system) {
ErrorLogger() << "Fleet::MovePath couldn't get next system with id " << *route_it << " for this fleet " << this->Name();
return retval;
}
//DebugLogger() << "initial cur system: " << (cur_system ? cur_system->Name() : "(none)") <<
// " prev system: " << (prev_system ? prev_system->Name() : "(none)") <<
// " next system: " << (next_system ? next_system->Name() : "(none)");
bool isPostBlockade = false;
if (cur_system) {
//DebugLogger() << "Fleet::MovePath starting in system "<< SystemID();
if (flag_blockades && next_system->ID() != m_arrival_starlane &&
(unobstructed_systems.find(cur_system->ID()) == unobstructed_systems.end()))
{
//DebugLogger() << "Fleet::MovePath checking blockade from "<< cur_system->ID() << " to "<< next_system->ID();
if (BlockadedAtSystem(cur_system->ID(), next_system->ID())){
// blockade debug logging
//DebugLogger() << "Fleet::MovePath finds system " <<cur_system->Name() << " (" <<cur_system->ID() <<
// ") blockaded for fleet " << this->Name();
isPostBlockade = true;
} else {
// blockade debug logging
//DebugLogger() << "Fleet::MovePath finds system " << cur_system->Name() << " (" << cur_system->ID() <<
// ") NOT blockaded for fleet " << this->Name();
}
}
}
// place initial position MovePathNode
MovePathNode initial_pos(this->X(), this->Y(), false /* not an end of turn node */, 0 /* turns taken to reach position of node */,
(cur_system ? cur_system->ID() : INVALID_OBJECT_ID),
(prev_system ? prev_system->ID() : INVALID_OBJECT_ID),
(next_system ? next_system->ID() : INVALID_OBJECT_ID));
retval.push_back(initial_pos);
const int TOO_LONG = 100; // limit on turns to simulate. 99 turns max keeps ETA to two digits, making UI work better
int turns_taken = 1;
double turn_dist_remaining = this->Speed(); // additional distance that can be travelled in current turn of fleet movement being simulated
double cur_x = this->X();
double cur_y = this->Y();
double next_x = next_system->X();
double next_y = next_system->Y();
// simulate fleet movement given known speed, starting position, fuel limit and systems on route
// need to populate retval with MovePathNodes that indicate the correct position, whether this
// fleet will end a turn at the node, the turns it will take to reach the node, and (when applicable)
// the current (if at a system), previous and next system IDs at which the fleet will be. the
// previous and next system ids are needed to know what starlane a given node is located on, if any.
// nodes at systems don't need previous system ids to be valid, but should have next system ids
// valid so that when rendering starlanes using the returned move path, lines departing a system
// can be drawn on the correct side of the system icon
while (turns_taken < TOO_LONG) {
// each loop iteration moves the current position to the next location of interest along the move
// path, and then adds a node at that position.
//DebugLogger() << " starting iteration";
//if (cur_system)
// DebugLogger() << " at system " << cur_system->Name() << " with id " << cur_system->ID();
//else
// DebugLogger() << " at (" << cur_x << ", " << cur_y << ")";
// Make sure that there actually still is a starlane between the two systems
// we are between
TemporaryPtr<const System> prev_or_cur;
if (cur_system) {
prev_or_cur = cur_system;
} else if (prev_system) {
prev_or_cur = prev_system;
} else {
ErrorLogger() << "Fleet::MovePath: No previous or current system!?";
}
if (prev_or_cur) {
if (!prev_or_cur->HasStarlaneTo(next_system->ID())) {
DebugLogger() << "Fleet::MovePath: No starlane connection between systems " << prev_or_cur->ID() << " and " << next_system->ID()
<< ". Abandoning the rest of the route.";
return retval;
}
}
// check if fuel limits movement or current system refuels passing fleet
if (cur_system) {
// check if current system has fuel supply available
if (fleet_supplied_systems.find(cur_system->ID()) != fleet_supplied_systems.end()) {
// current system has fuel supply. replenish fleet's supply and don't restrict movement
fuel = max_fuel;
//DebugLogger() << " ... at system with fuel supply. replenishing and continuing movement";
} else {
// current system has no fuel supply. require fuel to proceed
if (fuel >= 1.0) {
//DebugLogger() << " ... at system without fuel supply. consuming unit of fuel to proceed";
fuel -= 1.0;
} else {
//DebugLogger() << " ... at system without fuel supply. have insufficient fuel to continue moving";
turns_taken = ETA_OUT_OF_RANGE;
break;
}
}
}
// find distance to next system along path from current position
double dist_to_next_system = std::sqrt((next_x - cur_x)*(next_x - cur_x) + (next_y - cur_y)*(next_y - cur_y));
//DebugLogger() << " ... dist to next system: " << dist_to_next_system;
// move ship as far as it can go this turn, or to next system, whichever is closer, and deduct
// distance travelled from distance travellable this turn
if (turn_dist_remaining >= FLEET_MOVEMENT_EPSILON) {
double dist_travelled_this_step = std::min(turn_dist_remaining, dist_to_next_system);
//DebugLogger() << " ... fleet moving " << dist_travelled_this_step << " this iteration. dist to next system: " << dist_to_next_system << " and turn_dist_remaining: " << turn_dist_remaining;
double x_dist = next_x - cur_x;
double y_dist = next_y - cur_y;
// dist_to_next_system = std::sqrt(x_dist * x_dist + y_dist * y_dist); // should already equal this distance, so don't need to recalculate
double unit_vec_x = x_dist / dist_to_next_system;
double unit_vec_y = y_dist / dist_to_next_system;
cur_x += unit_vec_x*dist_travelled_this_step;
cur_y += unit_vec_y*dist_travelled_this_step;
turn_dist_remaining -= dist_travelled_this_step;
dist_to_next_system -= dist_travelled_this_step;
// if moved away any distance from a system, are no longer in that system
if (cur_system && dist_travelled_this_step >= FLEET_MOVEMENT_EPSILON) {
prev_system = cur_system;
cur_system = TemporaryPtr<System>();
}
}
bool end_turn_at_cur_position = false;
// check if fleet can move any further this turn
if (turn_dist_remaining < FLEET_MOVEMENT_EPSILON) {
//DebugLogger() << " ... fleet can't move further this turn.";
turn_dist_remaining = 0.0; // to prevent any possible precision-related errors
end_turn_at_cur_position = true;
}
// check if current position is close enough to next system on route to qualify as at that system.
if (dist_to_next_system < FLEET_MOVEMENT_EPSILON) {
// close enough to be consider to be at next system.
// set current position to be exactly at next system to avoid rounding issues
cur_system = next_system;
cur_x = cur_system->X(); // update positions to ensure no round-off-errors
cur_y = cur_system->Y();
//DebugLogger() << " ... arrived at system: " << cur_system->Name();
bool clear_exit = cur_system->ID() == m_arrival_starlane; //just part of the test for the moment
// attempt to get next system on route, to update next system. if new current
// system is the end of the route, abort.
++route_it;
if (route_it != route.end()) {
// update next system on route and distance to it from current position
next_system = GetEmpireKnownSystem(*route_it, owner);
if (next_system) {
//DebugLogger() << "Fleet::MovePath checking unrestriced lane travel";
clear_exit = clear_exit || (next_system && next_system->ID() == m_arrival_starlane) ||
(empire && empire->UnrestrictedLaneTravel(cur_system->ID(), next_system->ID()));
}
}
if (flag_blockades && !clear_exit) {
//DebugLogger() << "Fleet::MovePath checking blockades at system "<<cur_system->Name() << " ("<<cur_system->ID() <<
// ") for fleet " << this->Name() <<" travelling to system "<< (*route_it);
if (BlockadedAtSystem(cur_system->ID(), next_system->ID())) {
// blockade debug logging
//DebugLogger() << "Fleet::MovePath finds system "<<cur_system->Name() << " ("<<cur_system->ID() <<
// ") blockaded for fleet " << this->Name();
isPostBlockade = true;
} else {
//DebugLogger() << "Fleet::MovePath finds system "<<cur_system->Name() << " ("<<cur_system->ID() <<
// ") NOT blockaded for fleet " << this->Name();
}
}
if (route_it == route.end())
break;
if (!next_system) {
ErrorLogger() << "Fleet::MovePath couldn't get system with id " << *route_it;
break;
}
next_x = next_system->X();
next_y = next_system->Y();
}
// if new position is an obstructed system, must end turn here
// on client side, if have stale info on cur_system it may appear blockaded even if not actually obstructed,
// and so will force a stop in that situation
if (cur_system && (unobstructed_systems.find(cur_system->ID()) == unobstructed_systems.end())) {
turn_dist_remaining = 0.0;
end_turn_at_cur_position = true;
}
// if turn done and turns taken is enough, abort simulation
if (end_turn_at_cur_position && (turns_taken + 1 >= TOO_LONG)) {
// exit loop before placing current node to simplify post-loop processing: now all cases require a post-loop node to be added
++turns_taken;
break;
}
// blockade debug logging
//DebugLogger() << "Fleet::MovePath for fleet " << this->Name() << " id " << this->ID() << " adding node at sysID " <<
// (cur_system ? cur_system->ID() : INVALID_OBJECT_ID) << " with post blockade status " << isPostBlockade <<
// " and ETA " << turns_taken;
// add MovePathNode for current position (end of turn position and/or system location)
MovePathNode cur_pos(cur_x, cur_y, end_turn_at_cur_position, turns_taken,
(cur_system ? cur_system->ID() : INVALID_OBJECT_ID),
(prev_system ? prev_system->ID() : INVALID_OBJECT_ID),
(next_system ? next_system->ID() : INVALID_OBJECT_ID), isPostBlockade);
retval.push_back(cur_pos);
// if the turn ended at this position, increment the turns taken and
// reset the distance remaining to be travelled during the current (now
// next) turn for the next loop iteration
if (end_turn_at_cur_position) {
//DebugLogger() << " ... end of simulated turn " << turns_taken;
++turns_taken;
turn_dist_remaining = this->Speed();
}
}
// done looping. may have exited due to reaching end of path, lack of fuel, or turns taken getting too big
if (turns_taken == TOO_LONG)
turns_taken = ETA_NEVER;
// blockade debug logging
//DebugLogger() << "Fleet::MovePath for fleet " << this->Name()<<" id "<<this->ID()<<" adding node at sysID "<<
// (cur_system ? cur_system->ID() : INVALID_OBJECT_ID) << " with post blockade status " << isPostBlockade <<
// " and ETA " << turns_taken;
MovePathNode final_pos(cur_x, cur_y, true, turns_taken,
(cur_system ? cur_system->ID() : INVALID_OBJECT_ID),
(prev_system ? prev_system->ID() : INVALID_OBJECT_ID),
(next_system ? next_system->ID() : INVALID_OBJECT_ID), isPostBlockade);
retval.push_back(final_pos);
//DebugLogger() << "Fleet::MovePath for fleet " << this->Name()<<" id "<<this->ID()<<" is complete";
return retval;
}
std::pair<int, int> Fleet::ETA() const
{ return ETA(MovePath()); }
std::pair<int, int> Fleet::ETA(const std::list<MovePathNode>& move_path) const {
// check that path exists. if empty, there was no valid route or some other problem prevented pathing
if (move_path.empty())
return std::make_pair(ETA_UNKNOWN, ETA_UNKNOWN);
// check for single node in path. return the single node's eta as both .first and .second (likely indicates that fleet couldn't move)
if (move_path.size() == 1) {
const MovePathNode& node = *move_path.begin();
return std::make_pair(node.eta, node.eta);
}
// general case: there is a multi-node path. return the ETA of the first object node, and the ETA of the last node
int last_stop_eta = move_path.rbegin()->eta;
int first_stop_eta = last_stop_eta;
for (std::list<MovePathNode>::const_iterator it = ++(move_path.begin()); it != move_path.end(); ++it) {
const MovePathNode& node = *it;
if (node.object_id != INVALID_OBJECT_ID) {
first_stop_eta = node.eta;
break;
}
}
return std::make_pair(last_stop_eta, first_stop_eta);
}
float Fleet::Fuel() const {
if (NumShips() < 1)
return 0.0f;
// determine fuel available to fleet (fuel of the ship that has the least fuel in the fleet)
float fuel = Meter::LARGE_VALUE;
bool is_fleet_scrapped = true;
std::vector<TemporaryPtr<const Ship> > ships = Objects().FindObjects<const Ship>(m_ships);
for (std::vector<TemporaryPtr<const Ship> >::const_iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<const Ship> ship = *ship_it;
const Meter* meter = ship->UniverseObject::GetMeter(METER_FUEL);
if (!meter) {
ErrorLogger() << "Fleet::Fuel skipping ship with no fuel meter";
continue;
}
if (!ship->OrderedScrapped()) {
fuel = std::min(fuel, meter->Current());
is_fleet_scrapped = false;
}
}
if (is_fleet_scrapped) {
fuel = 0.0f;
}
return fuel;
}
float Fleet::MaxFuel() const {
if (NumShips() < 1)
return 0.0f;
// determine the maximum amount of fuel that can be stored by the ship in the fleet that
// can store the least amount of fuel
float max_fuel = Meter::LARGE_VALUE;
bool is_fleet_scrapped = true;
std::vector<TemporaryPtr<const Ship> > ships = Objects().FindObjects<const Ship>(m_ships);
for (std::vector<TemporaryPtr<const Ship> >::const_iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<const Ship> ship = *ship_it;
const Meter* meter = ship->UniverseObject::GetMeter(METER_MAX_FUEL);
if (!meter) {
ErrorLogger() << "Fleet::MaxFuel skipping ship with no max fuel meter";
continue;
}
if (!ship->OrderedScrapped()) {
max_fuel = std::min(max_fuel, meter->Current());
is_fleet_scrapped = false;
}
}
if (is_fleet_scrapped) {
max_fuel = 0.0f;
}
return max_fuel;
}
int Fleet::FinalDestinationID() const {
if(m_travel_route.empty()){
return INVALID_OBJECT_ID;
} else {
return m_travel_route.back();
}
}
bool Fleet::HasMonsters() const {
std::vector<TemporaryPtr<const Ship> > ships = Objects().FindObjects<const Ship>(m_ships);
for (std::vector<TemporaryPtr<const Ship> >::const_iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<const Ship> ship = *ship_it;
if (ship->IsMonster())
return true;
}
return false;
}
bool Fleet::HasArmedShips() const {
std::vector<TemporaryPtr<const Ship> > ships = Objects().FindObjects<const Ship>(m_ships);
for (std::vector<TemporaryPtr<const Ship> >::const_iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<const Ship> ship = *ship_it;
if (ship->IsArmed())
return true;
}
return false;
}
bool Fleet::HasColonyShips() const {
std::vector<TemporaryPtr<const Ship> > ships = Objects().FindObjects<const Ship>(m_ships);
for (std::vector<TemporaryPtr<const Ship> >::const_iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<const Ship> ship = *ship_it;
if (ship->CanColonize())
if (const ShipDesign* design = ship->Design())
if (design->ColonyCapacity() > 0.0)
return true;
}
return false;
}
bool Fleet::HasOutpostShips() const {
std::vector<TemporaryPtr<const Ship> > ships = Objects().FindObjects<const Ship>(m_ships);
for (std::vector<TemporaryPtr<const Ship> >::const_iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<const Ship> ship = *ship_it;
if (ship->CanColonize())
if (const ShipDesign* design = ship->Design())
if (design->ColonyCapacity() == 0.0)
return true;
}
return false;
}
bool Fleet::HasTroopShips() const {
std::vector<TemporaryPtr<const Ship> > ships = Objects().FindObjects<const Ship>(m_ships);
for (std::vector<TemporaryPtr<const Ship> >::const_iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<const Ship> ship = *ship_it;
if (ship->HasTroops())
return true;
}
return false;
}
bool Fleet::HasShipsOrderedScrapped() const {
std::vector<TemporaryPtr<Ship> > ships = Objects().FindObjects<Ship>(m_ships);
for (std::vector<TemporaryPtr<Ship> >::iterator it = ships.begin();
it != ships.end(); ++it)
{
TemporaryPtr<Ship> ship = *it;
if (ship->OrderedScrapped())
return true;
}
return false;
}
bool Fleet::HasShipsWithoutScrapOrders() const {
std::vector<TemporaryPtr<Ship> > ships = Objects().FindObjects<Ship>(m_ships);
for (std::vector<TemporaryPtr<Ship> >::iterator it = ships.begin();
it != ships.end(); ++it)
{
TemporaryPtr<Ship> ship = *it;
if (!ship->OrderedScrapped())
return true;
}
return false;
}
bool Fleet::UnknownRoute() const {
return m_travel_route.size() == 1 && m_travel_route.front() == INVALID_OBJECT_ID;
}
TemporaryPtr<UniverseObject> Fleet::Accept(const UniverseObjectVisitor& visitor) const
{ return visitor.Visit(boost::const_pointer_cast<Fleet>(boost::static_pointer_cast<const Fleet>(TemporaryFromThis()))); }
void Fleet::SetRoute(const std::list<int>& route) {
//DebugLogger() << "Fleet::SetRoute() ";
if (UnknownRoute())
throw std::invalid_argument("Fleet::SetRoute() : Attempted to set an unknown route.");
if (m_prev_system != SystemID() && m_prev_system == route.front() && !CanChangeDirectionEnRoute())
throw std::invalid_argument("Fleet::SetRoute() : Illegally attempted to change a fleet's direction while it was in transit.");
m_travel_route = route;
// Moving to where we are is not moving at all
if (m_travel_route.size() == 1 && this->SystemID() == m_travel_route.front()) {
m_travel_route.clear();
m_next_system = INVALID_OBJECT_ID;
}
// calculate length of line segments between systems on route, and sum up to determine length of route between
// systems on route. (Might later add distance from fleet to first system on route to this to get the total
// route length, or this may itself be the total route length if the fleet is at the first system on the route).
m_travel_distance = PathLength(m_travel_route.begin(), m_travel_route.end());
if (!m_travel_route.empty()) {
// if we're already moving, add in the distance from where we are to the first system in the route
if (SystemID() != route.front()) {
TemporaryPtr<const System> starting_system = GetSystem(route.front());
if (!starting_system) {
ErrorLogger() << "Fleet::SetRoute couldn't get system with id " << route.front();
return;
}
double dist_x = starting_system->X() - this->X();
double dist_y = starting_system->Y() - this->Y();
m_travel_distance += std::sqrt(dist_x*dist_x + dist_y*dist_y);
}
if (m_prev_system != SystemID() && m_prev_system == m_travel_route.front()) {
m_prev_system = m_next_system; // if already in transit and turning around, swap prev and next
} else if (SystemID() == route.front()) {
m_prev_system = SystemID();
}
std::list<int>::const_iterator it = m_travel_route.begin();
m_next_system = m_prev_system == SystemID() ? (*++it) : (*it);
}
StateChangedSignal();
}
void Fleet::SetAggressive(bool aggressive/* = true*/) {
if (aggressive == m_aggressive)
return;
m_aggressive = aggressive;
StateChangedSignal();
}
void Fleet::AddShip(int ship_id) {
std::vector<int> ship_ids;
ship_ids.push_back(ship_id);
AddShips(ship_ids);
}
void Fleet::AddShips(const std::vector<int>& ship_ids) {
size_t old_ships_size = m_ships.size();
std::copy(ship_ids.begin(), ship_ids.end(), std::inserter(m_ships, m_ships.end()));
if (old_ships_size != m_ships.size())
StateChangedSignal();
}
void Fleet::RemoveShip(int ship_id) {
std::vector<int> ship_ids;
ship_ids.push_back(ship_id);
RemoveShips(ship_ids);
}
void Fleet::RemoveShips(const std::vector<int>& ship_ids) {
size_t old_ships_size = m_ships.size();
for (std::vector<int>::const_iterator it = ship_ids.begin(); it != ship_ids.end(); ++it)
m_ships.erase(*it);
if (old_ships_size != m_ships.size())
StateChangedSignal();
}
void Fleet::SetNextAndPreviousSystems(int next, int prev) {
m_prev_system = prev;
m_next_system = next;
m_arrival_starlane = prev; // see comment for ArrivalStarlane()
}
void Fleet::MovementPhase() {
//DebugLogger() << "Fleet::MovementPhase this: " << this->Name() << " id: " << this->ID();
TemporaryPtr<Fleet> fleet = boost::dynamic_pointer_cast<Fleet>(TemporaryFromThis());
if (fleet != this) {
ErrorLogger() << "Fleet::MovementPhase was passed a TemporaryPtr different from itself.";
return;
}
Empire* empire = GetEmpire(fleet->Owner());
std::set<int> supply_unobstructed_systems;
if (empire)
supply_unobstructed_systems.insert(empire->SupplyUnobstructedSystems().begin(),
empire->SupplyUnobstructedSystems().end());
std::vector<TemporaryPtr<Ship> > ships = Objects().FindObjects<Ship>(m_ships);
// if owner of fleet can resupply ships at the location of this fleet, then
// resupply all ships in this fleet
if (empire && empire->SystemHasFleetSupply(fleet->SystemID())) {
for (std::vector<TemporaryPtr<Ship> >::iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<Ship> ship = *ship_it;
ship->Resupply();
if (Meter* fuel_meter = ship->UniverseObject::GetMeter(METER_FUEL))
fuel_meter->BackPropegate();
}
}
TemporaryPtr<System> current_system = GetSystem(fleet->SystemID());
TemporaryPtr<const System> initial_system = current_system;
std::list<MovePathNode> move_path = fleet->MovePath();
// If the move path cannot lead to our destination,
// make our route take us as far as it can
if (!move_path.empty() && !m_travel_route.empty() &&
move_path.back().object_id != m_travel_route.back())
{
std::list<int> shortened_route = fleet->TravelRoute();
fleet->ShortenRouteToEndAtSystem(shortened_route, move_path.back().object_id);
fleet->SetRoute(shortened_route);
move_path = fleet->MovePath();
}
std::list<MovePathNode>::const_iterator it = move_path.begin();
std::list<MovePathNode>::const_iterator next_it = it;
if (next_it != move_path.end())
++next_it;
// is the fleet stuck in a system for a whole turn?
if (current_system) {
///update m_arrival_starlane if no blockade, if needed
if (supply_unobstructed_systems.find(fleet->SystemID()) != supply_unobstructed_systems.end()) {
fleet->m_arrival_starlane = fleet->SystemID();//allows departure via any starlane
}
// in a system. if there is no system after the current one in the
// path, or the current and next nodes have the same system id, that
// is an actual system, or if blockaded from intended path, then won't
// be moving this turn.
bool stopped = false;
if (next_it == move_path.end()) {
stopped = true;
} else if (it->object_id != INVALID_OBJECT_ID && it->object_id == next_it->object_id) {
stopped = true;
} else if (fleet->m_arrival_starlane != fleet->SystemID()) {
int next_sys_id;
if (next_it->object_id != INVALID_OBJECT_ID) {
next_sys_id = next_it->object_id;
} else {
next_sys_id = next_it->lane_end_id;
}
stopped = fleet->BlockadedAtSystem(fleet->SystemID(), next_sys_id);
}
if (stopped) {
// fuel regeneration for ships in stationary fleet
if (fleet->FinalDestinationID() == INVALID_OBJECT_ID ||
fleet->FinalDestinationID() == fleet->SystemID())
{
for (std::vector<TemporaryPtr<Ship> >::const_iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<Ship> ship = *ship_it;
if (Meter* fuel_meter = ship->UniverseObject::GetMeter(METER_FUEL)) {
fuel_meter->AddToCurrent(0.1001f); // .0001 to prevent rounding down
fuel_meter->BackPropegate();
}
}
}
return;
} else {
// record previous system on fleet's path, and the starlane along
// which it will arrive at the next system (for blockading purposes)
fleet->m_arrival_starlane = fleet->SystemID();
fleet->m_prev_system = fleet->SystemID();
// remove fleet and ships from system they are departing
current_system->Remove(fleet->ID());
fleet->SetSystem(INVALID_OBJECT_ID);
for (std::vector<TemporaryPtr<Ship> >::iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<Ship> ship = *ship_it;
current_system->Remove(ship->ID());
ship->SetSystem(INVALID_OBJECT_ID);
}
}
}
// if fleet not moving, nothing more to do.
if (move_path.empty() || move_path.size() == 1) {
return;
}
// move fleet in sequence to MovePathNodes it can reach this turn
float fuel_consumed = 0.0f;
for (it = move_path.begin(); it != move_path.end(); ++it) {
next_it = it; ++next_it;
TemporaryPtr<System> system = GetSystem(it->object_id);
// is this system the last node reached this turn? either it's an end of turn node,
// or there are no more nodes after this one on path
bool node_is_next_stop = (it->turn_end || next_it == move_path.end());
if (system) {
// node is a system. explore system for all owners of this fleet
if (empire) {
empire->AddExploredSystem(it->object_id);
empire->RecordPendingLaneUpdate(it->object_id, fleet->m_prev_system); // specifies the lane from it->object_id back to m_prev_system is available
}
fleet->m_prev_system = system->ID(); // passing a system, so update previous system of this fleet
// reached a system, so remove it from the route
if (fleet->m_travel_route.front() == system->ID()) {
m_travel_route.erase(m_travel_route.begin());
} else {
ErrorLogger() << "Fleet::MovementPahse: Encountered a system not on route.";
// TODO: Notify the suer with a sitrep?
}
bool resupply_here = empire ? empire->SystemHasFleetSupply(system->ID()) : false;
// if this system can provide supplies, reset consumed fuel and refuel ships
if (resupply_here) {
//DebugLogger() << " ... node has fuel supply. consumed fuel for movement reset to 0 and fleet resupplied";
fuel_consumed = 0.0f;
for (std::vector<TemporaryPtr<Ship> >::iterator ship_it = ships.begin();
ship_it != ships.end(); ++ship_it)
{
TemporaryPtr<Ship> ship = *ship_it;
ship->Resupply();
}
}
// is system the last node reached this turn?
if (node_is_next_stop) {
// fleet ends turn at this node. insert fleet and ships into system
InsertFleetWithShips(fleet, system);
current_system = system;
if (supply_unobstructed_systems.find(fleet->SystemID()) != supply_unobstructed_systems.end()) {
fleet->m_arrival_starlane = fleet->SystemID();//allows departure via any starlane
}
break;