-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationMgr.cpp
More file actions
281 lines (236 loc) · 7.75 KB
/
Copy pathSimulationMgr.cpp
File metadata and controls
281 lines (236 loc) · 7.75 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
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
/*
* SimulationMgr.cpp
*
* Created on: Apr 20, 2023
* Author: süleyman yağız başaran
* id: 22103782
* sec: 3
* hw: 3
*/
#include <string.h>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#include "Creature.h"
#include "MaxHeap.h"
#include "MinHeap.h"
int main(int argc, char* argv[]) { //int argc, char **argv
Creature creatures[30];
MaxHeap maxxHeap;
//MinHeap minnHeap;
MinHeap minnHeap;
int numOfCreatures = 0;
int creatureCountFromFile = 0;
int lineCount = 0;
fstream file (argv[1], ios::in);
if(file.is_open())
{
bool isEntrance = true;
bool isCreature = false;
int tempID;
double tempx;
double tempy;
int tempHealth;
bool isFood = false;
int tempQuality;
int tempSpawnTime;
int lineStoreVal = 0;
std::string line;
//File Reading Part
while(getline(file,line))
{
std::stringstream linestream(line);
std::string value;
while(getline(linestream,value,','))
{
if(isEntrance){
numOfCreatures = stoi(value);
isEntrance = false;
}
else if(isCreature){
if(lineStoreVal == 0){
tempID = stoi(value);
}
else if(lineStoreVal == 1){
tempx = std::stod(value);
}
else if(lineStoreVal == 2){
tempy = std::stod(value);
}
else if(lineStoreVal == 3){
tempHealth = stoi(value);
}
}
else if(isFood){
if(lineStoreVal == 0){
tempID = stoi(value);
}
else if(lineStoreVal == 1){
tempx = std::stod(value);
}
else if(lineStoreVal == 2){
tempy = std::stod(value);
}
else if(lineStoreVal == 3){
tempQuality = stoi(value);
}
else if(lineStoreVal == 4){
tempSpawnTime = stoi(value);
}
}
lineStoreVal++;
}
lineStoreVal = 0;
if(isCreature){
Creature cr1(tempID,tempx,tempy,tempHealth);
creatures[creatureCountFromFile] = cr1;
creatureCountFromFile++;
}
if(isFood){
Food f1(tempID,tempx,tempy,tempQuality,tempSpawnTime);
minnHeap.heapInsert(f1);
}
lineCount++;
if(lineCount == 1){
isCreature = true;
}
if(lineCount > numOfCreatures){
isCreature = false;
isFood = true;
}
}
}
else{
}
//End of file read
//Sort method for creatures array (selection sort)
for(int i=0; i<numOfCreatures; i++){
int minID = creatures[i].ID;
int minElement = i;
for(int j=i+1; j<numOfCreatures; j++){
if(creatures[j].ID < minID){
minID = creatures[j].ID;
minElement = j;
}
}
Creature tempCreature(creatures[i].ID, creatures[i].x, creatures[i].y, creatures[i].health);
creatures[i] = creatures[minElement];
creatures[minElement] = tempCreature;
}
//end of sort
int timeTick = 0;
//int numOfCreatures = 4;
int numOfAliveCreatures = numOfCreatures;
/*
Creature ct1 = Creature(2,10,2,15);
Creature ct2 = Creature(1,1,1,15);
Creature ct3 = Creature(3,4,5,15);
Creature ct4 = Creature(4,3,5.5,15);
Creature creatures[numOfCreatures];
creatures[0] = ct2;
creatures[1] = ct1;
creatures[2] = ct3;
creatures[3] = ct4;*/
/*
Food f1 = Food(1,7,11,1,0);
Food f2 = Food(2,2,2,5,13);
Food f3 = Food(3,1,3,11,0);
Food f4 = Food(4,8,2,2,0);
Food f5 = Food(5,2,12,10,4);
minnHeap.heapInsert(f1);
minnHeap.heapInsert(f2);
minnHeap.heapInsert(f3);
minnHeap.heapInsert(f4);
minnHeap.heapInsert(f5);*/
cout << setprecision(2) << fixed;
while( numOfAliveCreatures > 0){
for(int i = 0; i < numOfCreatures; i++)
cout << "Creature " << creatures[i].ID << ": " << creatures[i].x << ", "<< creatures[i].y << endl;
if(!minnHeap.heapIsEmpty()){
bool exit = false; //?
while(!exit) { // Adding foods to the game
Food tempFood = minnHeap.peek();
if(minnHeap.peek().spawnTime == timeTick && maxxHeap.idCheck(tempFood.ID) == false){//?
minnHeap.heapDelete(tempFood);
maxxHeap.heapInsert(tempFood);
}
else exit = true;
}
}
for(int i = 0; i < numOfCreatures; i++)
{ // FIGHT
for(int j = 0; j < numOfCreatures; j++)
{
if(i != j)
{
if(creatures[i].checkFight(creatures[j]))
{
creatures[j].health = 0;
}
}
}
}
for(int i = 0; i < numOfCreatures; i++)
{ // EATING
if(!maxxHeap.heapIsEmpty())
{
if(creatures[i].ifRadiusEatFood(maxxHeap.peek()))
{
Food tempFood2 = maxxHeap.peek();
creatures[i].health += maxxHeap.peek().quality;
maxxHeap.heapDelete(tempFood2);
}
}
}
for(int i = 0; i < numOfCreatures; i++)
{ // MOVE
if(!maxxHeap.heapIsEmpty() && creatures[i].health > 0){
double temp1 = creatures[i].x - maxxHeap.peek().x;
double temp11 = pow(temp1,2);
double temp2 = creatures[i].y - maxxHeap.peek().y;
double temp22 = pow(temp2,2);
double lastDistance = sqrt(temp11 + temp22);
if(creatures[i].health <= 10 )
{
double xMoveAmount = (maxxHeap.peek().x - creatures[i].x)/lastDistance;
double yMoveAmount = (maxxHeap.peek().y - creatures[i].y)/lastDistance;
creatures[i].x += xMoveAmount;
creatures[i].y += yMoveAmount;
}
else if(creatures[i].health > 10)
{
double xMoveAmount = ((maxxHeap.peek().x - creatures[i].x)/lastDistance) * ((10.00/creatures[i].health));
double yMoveAmount = ((maxxHeap.peek().y - creatures[i].y)/lastDistance) * ((10.00/creatures[i].health));
creatures[i].x += xMoveAmount;
creatures[i].y += yMoveAmount;
}
}
}
for(int i = 0; i < numOfCreatures; i++)
{ //DECREASE HEALTH
if(creatures[i].health > 0)
creatures[i].health--;
}
int count = 0;
for(int i = 0; i < numOfCreatures; i++)
{
if(creatures[i].health == 0)
count++;
}
/*
cout << "Time Tick: " << timeTick << endl;
cout << "Num Of Alive Creatures: " << numOfAliveCreatures << endl;
for(int i = 0; i<numOfCreatures; i++){
if(creatures[i].health > 0)
cout << "Alive cre: " <<creatures[i].ID << " Health: " << creatures[i].health << endl;
}
cout << endl;*/
numOfAliveCreatures = numOfCreatures - count;
timeTick++; //TIME TICK
}
return 0;
}