-
Notifications
You must be signed in to change notification settings - Fork 0
/
person.c
263 lines (229 loc) · 11.6 KB
/
person.c
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <SDL2/SDL.h>
#include "game.h"
extern tileset room;
extern person player;
extern person *managers[MANAGERS_COUNT];
extern Uint64 prevMoveTime;
extern uint64_t safetyDist;
extern uint64_t codingProgress;
extern uint64_t mentalHealth;
// bool calcCollision(person *p, float x2, float y2);
// float calcPersonCollSize(person *p, int dir);
// bool calcPersonMovement(person *p);
void randomManagerPosition(person *m) {
switch (rand()%4) {
case 0:
m->posX = (rand() % (FIELDS_X -1)) * FIELD_SIZE;
m->posY = 0;
break;
case 1:
m->posX = 0;
m->posY = (rand() % (FIELDS_Y -1)) * FIELD_SIZE;
break;
case 2:
m->posX = (rand() % (FIELDS_X -1)) * FIELD_SIZE;
m->posY = (WINDOW_HEIGHT/FIELD_SIZE - 1) * FIELD_SIZE;
break;
case 3:
m->posX = (WINDOW_WIDTH/FIELD_SIZE - 1) * FIELD_SIZE;
m->posY = (rand() % (FIELDS_Y -1)) * FIELD_SIZE;
break;
}
}
bool calcPPCollision(person *p1, person *p2) {
if(p1->posX + FIELD_SIZE-1 < p2->posX || p1->posX > p2->posX + FIELD_SIZE-1) return false;
if(p1->posY + FIELD_SIZE-1 < p2->posY || p1->posY > p2->posY + FIELD_SIZE-1) return false;
return true;
}
// float calcPersonCollSize(person *p, int dir) {
// return (float)FIELD_SIZE/2 * (1.0 + (float)dir * p->collisionRatio);
// }
bool calcPersonMovement(person *p) {
if (p->dirX) p->dirX /= fabs(p->dirX);
if (p->dirY) p->dirY /= fabs(p->dirY);
float personDirRatio = (p->dirX && p->dirY) ? 0.7 : 1.0 ;
float pCollRatio = p->collisionRatio;
if (pCollRatio > 1.0) pCollRatio = 1.0;
float pCollFix = ((1-pCollRatio) * (float)FIELD_SIZE)/2;
// float oCollRatio = OBJECT_COLLISION_RATIO;
// if (oCollRatio > 1.0) oCollRatio = 1.0;
// float oCollFix = ((1-oCollRatio) * (float)FIELD_SIZE)/2;
float prevRectPosX = p->posX;
float prevRectPosY = p->posY;
float newRectPosX = (float)p->posX + (float)p->dirX * personDirRatio * (float)p->speed;
float newRectPosY = (float)p->posY + (float)p->dirY * personDirRatio * (float)p->speed;
int prevX1 = (int)(prevRectPosX + pCollFix) / FIELD_SIZE;
int prevX2 = (int)(prevRectPosX + FIELD_SIZE-1 - pCollFix) / FIELD_SIZE;
int prevY1 = (int)(prevRectPosY + pCollFix) / FIELD_SIZE;
int prevY2 = (int)(prevRectPosY + FIELD_SIZE-1 - pCollFix) / FIELD_SIZE;
int newX1 = (int)(newRectPosX + pCollFix) / FIELD_SIZE;
int newX2 = (int)(newRectPosX + FIELD_SIZE-1 - pCollFix) / FIELD_SIZE;
int newY1 = (int)(newRectPosY + pCollFix) / FIELD_SIZE;
int newY2 = (int)(newRectPosY + FIELD_SIZE-1 - pCollFix) / FIELD_SIZE;
int p1Coll = getFieldTypeByCoord(newRectPosX + pCollFix, newRectPosY + pCollFix) & (FIELD_TYPE_DESK);
int p2Coll = getFieldTypeByCoord(newRectPosX + FIELD_SIZE-1 - pCollFix, newRectPosY + pCollFix) & (FIELD_TYPE_DESK);
int p3Coll = getFieldTypeByCoord(newRectPosX + pCollFix, newRectPosY + FIELD_SIZE-1 - pCollFix) & (FIELD_TYPE_DESK);
int p4Coll = getFieldTypeByCoord(newRectPosX + FIELD_SIZE-1 - pCollFix, newRectPosY + FIELD_SIZE-1 - pCollFix) & (FIELD_TYPE_DESK);
if (p1Coll) {
if (getFieldTypeByID(prevX1, prevY1-1) & (FIELD_TYPE_DESK) && (newY1 < prevY1)) {
newRectPosY = (float)prevY1 * FIELD_SIZE - pCollFix;
}
if (getFieldTypeByID(prevX1-1, prevY1) & (FIELD_TYPE_DESK) && (newX1 < prevX1)) {
newRectPosX = (float)prevX1 * FIELD_SIZE - pCollFix;
}
if (getFieldTypeByID(prevX1-1, prevY1-1) & (FIELD_TYPE_DESK) && (newX1 < prevX1) && (newY1 < prevY1)) {
newRectPosX = (float)prevX1 * FIELD_SIZE - pCollFix;
newRectPosY = (float)prevY1 * FIELD_SIZE - pCollFix;
}
if (getFieldTypeByID(prevX1-1, prevY1+1) & (FIELD_TYPE_DESK) && (newX1 < prevX1) && (newY1 > prevY1)) {
newRectPosX = (float)(prevX1) * FIELD_SIZE - pCollFix;
newRectPosY = (float)(prevY1 + 1) * FIELD_SIZE - pCollFix;
}
if (getFieldTypeByID(prevX1+1, prevY1-1) & (FIELD_TYPE_DESK) && (newX1 > prevX1) && (newY1 < prevY1)) {
newRectPosX = (float)(prevX1 + 1) * FIELD_SIZE - pCollFix;
newRectPosY = (float)(prevY1) * FIELD_SIZE - pCollFix;
}
}
if (p2Coll) {
if (getFieldTypeByID(prevX2, prevY1-1) & (FIELD_TYPE_DESK) && (newY1 < prevY1)) {
newRectPosY = (float)prevY1 * FIELD_SIZE - pCollFix;
}
if (getFieldTypeByID(prevX2+1, prevY1) & (FIELD_TYPE_DESK) && (newX2 > prevX2)) {
newRectPosX = (float)prevX2 * FIELD_SIZE + pCollFix;
}
if (getFieldTypeByID(prevX2+1, prevY1-1) & (FIELD_TYPE_DESK) && (newX2 > prevX2) && (newY1 < prevY1)) {
newRectPosX = (float)prevX2 * FIELD_SIZE + pCollFix;
newRectPosY = (float)prevY1 * FIELD_SIZE - pCollFix;
}
if (getFieldTypeByID(prevX2-1, prevY1-1) & (FIELD_TYPE_DESK) && (newX2 < prevX2) && (newY1 < prevY1)) {
newRectPosX = (float)(prevX2-1) * FIELD_SIZE + pCollFix;
newRectPosY = (float)prevY1 * FIELD_SIZE - pCollFix;
}
if (getFieldTypeByID(prevX2+1, prevY1+1) & (FIELD_TYPE_DESK) && (newX2 > prevX2) && (newY1 > prevY1)) {
newRectPosX = (float)prevX2 * FIELD_SIZE + pCollFix;
newRectPosY = (float)(prevY1+1) * FIELD_SIZE - pCollFix;
}
}
if (p3Coll) {
if (getFieldTypeByID(prevX1, prevY2+1) & (FIELD_TYPE_DESK) && (newY2 > prevY2)) {
newRectPosY = (float)prevY2 * FIELD_SIZE + pCollFix;
}
if (getFieldTypeByID(prevX1-1, prevY2) & (FIELD_TYPE_DESK) && (newX1 < prevX1)) {
newRectPosX = (float)prevX1 * FIELD_SIZE - pCollFix;
}
if (getFieldTypeByID(prevX1-1, prevY2+1) & (FIELD_TYPE_DESK) && (newX1 < prevX1) && (newY2 > prevY2)) {
newRectPosX = (float)prevX1 * FIELD_SIZE - pCollFix;
newRectPosY = (float)prevY2 * FIELD_SIZE + pCollFix;
}
if (getFieldTypeByID(prevX1+1, prevY2+1) & (FIELD_TYPE_DESK) && (newX1 > prevX1) && (newY2 > prevY2)) {
newRectPosX = (float)(prevX1+1) * FIELD_SIZE - pCollFix;
newRectPosY = (float)prevY2 * FIELD_SIZE + pCollFix;
}
if (getFieldTypeByID(prevX1-1, prevY2-1) & (FIELD_TYPE_DESK) && (newX1 < prevX1) && (newY2 < prevY2)) {
newRectPosX = (float)prevX1 * FIELD_SIZE - pCollFix;
newRectPosY = (float)(prevY2-1) * FIELD_SIZE + pCollFix;
}
}
if (p4Coll) {
if (getFieldTypeByID(prevX2, prevY2+1) & (FIELD_TYPE_DESK) && (newY2 > prevY2)) {
newRectPosY = (float)prevY2 * FIELD_SIZE + pCollFix;
}
if (getFieldTypeByID(prevX2+1, prevY2) & (FIELD_TYPE_DESK) && (newX2 > prevX2)) {
newRectPosX = (float)prevX2 * FIELD_SIZE + pCollFix;
}
if (getFieldTypeByID(prevX2+1, prevY2+1) & (FIELD_TYPE_DESK) && (newX2 > prevX2) && (newY2 > prevY2)) {
newRectPosX = (float)prevX2 * FIELD_SIZE + pCollFix;
newRectPosY = (float)prevY2 * FIELD_SIZE + pCollFix;
}
if (getFieldTypeByID(prevX2-1, prevY2+1) & (FIELD_TYPE_DESK) && (newX2 < prevX2) && (newY2 > prevY2)) {
newRectPosX = (float)(prevX2-1) * FIELD_SIZE + pCollFix;
newRectPosY = (float)prevY2 * FIELD_SIZE + pCollFix;
}
if (getFieldTypeByID(prevX2+1, prevY2-1) & (FIELD_TYPE_DESK) && (newX2 > prevX2) && (newY2 < prevY2)) {
newRectPosX = (float)prevX2 * FIELD_SIZE + pCollFix;
newRectPosY = (float)(prevY2-1) * FIELD_SIZE + pCollFix;
}
}
if (newRectPosX + pCollFix < 0) newRectPosX = - pCollFix;
if (newRectPosX > WINDOW_WIDTH - FIELD_SIZE + pCollFix) newRectPosX = WINDOW_WIDTH - FIELD_SIZE + pCollFix;
if (newRectPosY + pCollFix < 0) newRectPosY = - pCollFix;
if (newRectPosY > WINDOW_HEIGHT - FIELD_SIZE + pCollFix) newRectPosY = WINDOW_HEIGHT - FIELD_SIZE + pCollFix;
p->posX = newRectPosX;
p->posY = newRectPosY;
return newRectPosX == prevRectPosX && newRectPosY == prevRectPosY;
}
int calcMovement(void) {
/* calc movement */
calcPersonMovement(&player);
Uint64 currentTime = SDL_GetTicks64();
if ((currentTime - prevMoveTime) >= MS_PER_MOVE) {
prevMoveTime = currentTime;
calcPathRoutes();
player.moveCounter++;
if (player.moveCounter % MANAGERS_APPEAR_PERIOD == 0) {
initNewManager();
}
float fSafetyDist2 = WINDOW_WIDTH*WINDOW_WIDTH + WINDOW_HEIGHT*WINDOW_WIDTH;
for (int i = 0; i < MANAGERS_COUNT; ++i) {
if (managers[i] != NULL) {
if (calcPPCollision(&player,managers[i])) {
managers[i]->dirX = 0;
managers[i]->dirY = 0;
} else {
float curX = managers[i]->posX;
float curY = managers[i]->posY;
int curFieldX1 = (int)(curX) / FIELD_SIZE;
int curFieldX2 = (int)(curX + FIELD_SIZE-1) / FIELD_SIZE;
int curFieldY1 = (int)(curY) / FIELD_SIZE;
int curFieldY2 = (int)(curY + FIELD_SIZE-1) / FIELD_SIZE;
int curFieldXCentered = (managers[i]->posX + (FIELD_SIZE-1)/2) / FIELD_SIZE;
int curFieldYCentered = (managers[i]->posY + (FIELD_SIZE-1)/2) / FIELD_SIZE;
if (managers[i]->dirX == 0 && managers[i]->dirY == 0) {
managers[i]->dirX = room.fields[curFieldYCentered * FIELDS_X + curFieldXCentered].prevX - curFieldXCentered;
managers[i]->dirY = room.fields[curFieldYCentered * FIELDS_X + curFieldXCentered].prevY - curFieldYCentered;
}
if (managers[i]->dirX < 0) {
managers[i]->dirX = room.fields[curFieldYCentered * FIELDS_X + curFieldX2].prevX - curFieldX2;
} else if (managers[i]->dirX > 0) {
managers[i]->dirX = room.fields[curFieldYCentered * FIELDS_X + curFieldX1].prevX - curFieldX1;
} else if (managers[i]->dirY < 0) {
managers[i]->dirY = room.fields[curFieldY2 * FIELDS_X + curFieldXCentered].prevY - curFieldY2;
} else if (managers[i]->dirY > 0) {
managers[i]->dirY = room.fields[curFieldY1 * FIELDS_X + curFieldXCentered].prevY - curFieldY1;
}
calcPersonMovement(managers[i]);
}
float d2 = (managers[i]->posX - player.posX)*(managers[i]->posX - player.posX) + (managers[i]->posY - player.posY)*(managers[i]->posY - player.posY);
if (d2 < fSafetyDist2)
fSafetyDist2 = d2;
managers[i]->moveCounter++;
if (managers[i]->moveCounter % MANAGER_SPEED_INCREASE_PERIOD == 0)
managers[i]->speed += MANAGER_SPEED_INCREASE_RATIO;
}
}
safetyDist = (int)sqrt(fSafetyDist2);
if (!player.dirX && !player.dirY) {
// бар с процессом кодинга (зависит от близости)
int sF;
if ((sF = safetyDist/FIELD_SIZE) < 4) {
codingProgress += 10/4 * sF;
} else {
codingProgress += 10;
if (mentalHealth < 100)
mentalHealth++;
}
}
if (safetyDist < FIELD_SIZE*1.5) {
// бар с энергией (уменьшается при касании, со временем восстанавливаться)
if (mentalHealth > 0)
mentalHealth--;
}
}
return 0;
}