-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChopper.cpp
More file actions
162 lines (135 loc) · 5.35 KB
/
Copy pathChopper.cpp
File metadata and controls
162 lines (135 loc) · 5.35 KB
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
#include "Chopper.h"
#include "Model.h"
#include "Truck.h"
#include "State_trooper.h"
void Chopper::update() {
// Handle pending manual commands
if (pending_position) {
beginPositionMode(pending_pos_speed);
setState("Moving to position");
pending_position = false;
}
if (pending_course) {
beginCourseMode(pending_course_speed);
setState("Moving to " + std::to_string(angle));
pending_course = false;
}
if (state == "Stopped") {
this->stop();
return;
}
// Mode 0: Move to a position, then stop
if (mode == 0) {
if (!destination) {
setState("Stopped");
en_route = false;
speed = 0.0;
}
else {
stepAlongHeading(speed / 100.0);
double remaining = Vehicle::computeDistance(_location->x, _location->y, destination->x, destination->y);
if (remaining < speed/100.0 ) {
_location->x = destination->x;
_location->y = destination->y;
setState("Stopped");
en_route = false;
speed = 0.0;
destination = nullptr;
} else {
setState("Moving to (" + std::to_string(destination->x) + "," + std::to_string(destination->y) + ")");
}
}
}
// Mode 1: Move on a fixed course indefinitely
if (mode == 1 && en_route) {
stepAlongHeading(speed / 100.0);
setState("Moving on course " + std::to_string(angle));
}
// Check if an attack is pending
if (pending_attack) {
// Find the target truck and police
const auto& vehicles = Model::getInstance().getVehicles();
std::shared_ptr<Vehicle> targetTruck = nullptr;
for (const auto& v : vehicles) {
if (v->getName() == pending_attack_target && dynamic_cast<Truck*>(v.get()) != nullptr) {
targetTruck = v;
break;
}
}
// Default result: fail
attack_success = false;
if (targetTruck) {
double truckDistance = Vehicle::computeDistance(_location->x, _location->y,
targetTruck->get_location()->x, targetTruck->get_location()->y);
if (truckDistance < attack_range) {
// Check for police
bool policeNearby = false;
for (const auto& v : vehicles) {
if (dynamic_cast<State_trooper*>(v.get()) != nullptr) {
double dist = Vehicle::computeDistance(targetTruck->get_location()->x,
targetTruck->get_location()->y,
v->get_location()->x,
v->get_location()->y);
if (dist <= 0.1) {
policeNearby = true;
break;
}
}
}
if (!policeNearby) {
attack_success = true;
targetTruckName = targetTruck->getName();
}
}
}
// Reset the pending flag
pending_attack = false;
mode = 2;
}
// Mode 2: Attack mode (try to attack a truck if target set)
if (mode == 2) {
// Attack logic is driven by Controller, so just respond to result
if (attack_success) {
// Success: disable the truck
const auto truck = dynamic_cast<Truck*>(Model::getInstance().findVehicleByName(targetTruckName).get());
truck->stop();
truck->setState("Off road");
truck->clearBoxes(); // zero boxes
truck->setSpeed(0.0); // stop the truck
// Increase attack range by 1, max 20
attack_range = std::min(attack_range + 0.01, 20.0);
std::cout << "Attack succeeded. New attack range: " << attack_range*100 << " km\n";
setState("Stopped");
this->en_route = false;
this->speed = 0.0;
mode = 0;
} else {
attack_range = std::max(0.01, attack_range - 0.01); // reduce attack range on failure
this->speed = 0.0;
this->en_route = false;
std::cout << "Attack failed. Attack range reduced to: " << attack_range*100 << " km\n";
setState("Stopped");
}
}
}
void Chopper::broadcast_current_state() {
std::cout << "Chopper " << getName()
<< " at (" << std::fixed << std::setprecision(2)
<< _location->x << ", " << _location->y << "), ";
if (mode == 1 && en_route) {
std::cout << "Moving on course " << std::fmod(angle, 360.0) << " deg, speed: " << speed << " km/h. "<<std::endl;
}
else if (mode == 0 && en_route) {
std::cout << "Moving to position (" << destination->x << ", " << destination->y << "), speed: " << speed << " km/h. "<<std::endl;
}
else {
std::cout << this->state << "." << std::endl;
}
}
void Chopper::attack(const std::string& truckName) {
// Schedule attack for the next update step
pending_attack = true;
pending_attack_target = truckName;
mode = 2; // attack mode
setState("Attacking " + truckName);
}