-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph_Simulation.cpp
executable file
·291 lines (211 loc) · 6.71 KB
/
graph_Simulation.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
#include <QTimer>
#include "Settings.h"
#include "w_MainWindow.h"
#include "Individual.h"
#include "h_Simulation.h"
#include "h_World.h"
#include "h_Robot.h"
#include "h_EatableItem.h"
#include "graph_Scene.h"
#include "graph_Robot.h"
#include "graph_EatableItem.h"
#include "graph_Simulation.h"
graph_Simulation::graph_Simulation(Settings* settings, h_Simulation* aSimulation, w_MainWindow *aMainWindow)
: s(settings),
mainWindow(aMainWindow),
timeOutInterval(s->sim.timeOutInterval),
numIteration(0)
{
// Update the simulation
changeSimulation(aSimulation);
timer = new QTimer;
scene = new graph_Scene(s, simulation->getWorld());
addAllItemsToScene();
QObject::connect(timer, SIGNAL(timeout()), simulation, SLOT(run()));
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(iterate()));
}
graph_Simulation::~graph_Simulation()
{
deleteGraph_Items();
delete timer;
delete scene;
}
void graph_Simulation::deleteGraph_Items()
{
foreach (graph_Robot* graph_robot, graph_robots)
delete graph_robot;
foreach (graph_EatableItem* graph_food, graph_foods)
delete graph_food;
foreach (graph_EatableItem* graph_poison, graph_poisons)
delete graph_poison;
}
void graph_Simulation::changeSimulation(h_Simulation* newSimulation)
{
// If graphical items already exist, delete them
deleteGraph_Items();
simulation = newSimulation;
// Creation of the food and poison items
createGraphEatableItems();
// Creation of the robots
for (int i = 0; i < simulation->getNumberRobots(); ++i)
{
h_Robot* curRobot = simulation->getRobots()[i];
graph_robots.append(s->createGraphRobot(s, this, curRobot));
}
}
void graph_Simulation::createGraphEatableItems()
{
// Creation of the food items
for (int i = 0; i < simulation->getNumberFoods(); ++i)
{
h_EatableItem* curFood = simulation->getFoods()[i];
graph_foods.append(new graph_Food(s, this, curFood));
}
// Creation of the poison items
for (int i = 0; i < simulation->getNumberPoisons(); ++i)
{
h_EatableItem* curPoison = simulation->getPoisons()[i];
graph_poisons.append(new graph_Poison(s, this, curPoison));
}
}
void graph_Simulation::clearAllItems()
{
// Clear food items
clearItems(&graph_foods);
// Clear Poison items
clearItems(&graph_poisons);
}
void graph_Simulation::clearItems(QList<graph_EatableItem *>* pListItems)
{
int numberItems = pListItems->size();
for(int i = 0; i < numberItems ; ++i)
{
graph_EatableItem* curItem = pListItems->last();
scene->removeItem(curItem);
simulation->removeItem(curItem->getItem());
pListItems->removeLast();
delete curItem;
}
}
void graph_Simulation::resetAllItems()
{
foreach (graph_Robot* graph_robot, graph_robots)
resetGraphRobot(graph_robot);
foreach (graph_EatableItem* graph_food, graph_foods)
resetGraphFood(graph_food);
foreach (graph_EatableItem* graph_poison, graph_poisons)
resetGraphPoison(graph_poison);
}
void graph_Simulation::resetGraphRobot(graph_Robot *graph_robot)
{
simulation->resetRobot(graph_robot->getRobot());
}
void graph_Simulation::resetGraphFood(graph_EatableItem* graph_food)
{
simulation->resetFood(graph_food->getItem());
}
void graph_Simulation::resetGraphPoison(graph_EatableItem* graph_poison)
{
simulation->resetFood(graph_poison->getItem());
}
void graph_Simulation::addAllItemsToScene()
{
foreach (graph_Robot* graph_robot, graph_robots)
scene->addItem(graph_robot);
addEatableItemsToScene();
}
void graph_Simulation::addEatableItemsToScene()
{
foreach (graph_EatableItem* graph_food, graph_foods)
scene->addItem(graph_food);
foreach (graph_EatableItem* graph_poison, graph_poisons)
scene->addItem(graph_poison);
}
void graph_Simulation::startSim()
{
timer->start(timeOutInterval);
}
void graph_Simulation::pauseSim()
{
timer->stop();
}
void graph_Simulation::resumeSim()
{
timer->start(timeOutInterval);
}
/*slot*/ void graph_Simulation::iterate()
{
numIteration++;
emit sendIteration(numIteration);
}
void graph_Simulation::setTimeOutInterval(qreal time)
{
timeOutInterval = time;
timer->setInterval(timeOutInterval);
}
qreal graph_Simulation::getTimeOutInterval() const
{
//return timer->interval();
return timeOutInterval;
}
void graph_Simulation::removeItem(graph_EatableItem* graph_eatableItem)
{
simulation->removeItem(graph_eatableItem->getItem());
if(graph_eatableItem->isGraphFood())
graph_foods.removeOne(graph_eatableItem);
else if(graph_eatableItem->isGraphPoison())
graph_poisons.removeOne(graph_eatableItem);
delete graph_eatableItem;
}
void graph_Simulation::reInitializeAllItems()
{
// Remove all graphical items
clearAllItems();
// Remove then re-create all items
simulation->reInitAllEatableItems();
// Recreate corresponding graphical items
createGraphEatableItems();
// Add new graphical items to scene
addEatableItemsToScene();
}
void graph_Simulation::reIniRobotsPos()
{
foreach(graph_Robot* graph_robot, graph_robots)
resetGraphRobot(graph_robot);
}
void graph_Simulation::addFood(QPointF pt)
{
// Conversion (the basic vectors and angle norms are different)
QPointF npt = convertCoordinates(pt);
h_EatableItem* food = simulation->addFood(&npt);
graph_foods.append(new graph_Food(s, this, food));
scene->addItem(graph_foods.last());
}
void graph_Simulation::addPoison(QPointF pt)
{
// Conversion (the basic vectors and angle norms are different)
QPointF npt = convertCoordinates(pt);
h_EatableItem* poison = simulation->addPoison(&npt);
graph_poisons.append(new graph_Poison(s, this, poison));
scene->addItem(graph_poisons.last());
}
void graph_Simulation::addGraphicsItem(QGraphicsItem* item)
{
scene->addItem(item);
}
void graph_Simulation::removeGraphicsItem(QGraphicsItem* item)
{
scene->removeItem(item);
}
QPointF graph_Simulation::convertCoordinates(QPointF pt)
{
int wWidth = simulation->getWorld()->getWidth();
int wHeigh = simulation->getWorld()->getHeigh();
return QPointF(pt.x() + wWidth/2,
wHeigh/2 - pt.y());
}
void graph_Simulation::changeRobotsPathLengths(int newPath)
{
foreach (graph_Robot* graph_robot, graph_robots)
graph_robot->setNumberLastPositions(newPath);
}