-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLetterMap.h
475 lines (353 loc) · 10.7 KB
/
LetterMap.h
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
//
// Created by flo on 10/28/2021.
//
#ifndef NOMPROJET_LETTERMAP_H
#define NOMPROJET_LETTERMAP_H
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <unordered_map>
#include <map>
#include <set>
#include <utility>
#include <iostream>
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
#include "Circularpairmap.h"
// For avoiding multiple declarations :
#ifndef LETTER_MAP
#define LETTER_MAP
using namespace sf;
using namespace std;
#define MAXN 50 // sqrt(max number of blocks)
#define MAXL 13 // max lato room
#define MINL 5 // min lato room
#define BORDER 2
#define ROOM_BORDER 1 // inside createRoom method
#define MAXRAY 10
#define MAXROOMS 15
#define p(a,b) make_pair(a,b)
#define ABS(a) (a<0)?-a:a
typedef pair<int,int> pi;
enum Cell{
Wall = 0,
Floor = 1,
Water = 2,
Box = 3,
Portal = 4,
Door = 5,
Spawn = 6,
User=7,
RoomRoof=8,
Unknown = -1
};
// should also add here a list of object that user can pass trough
// like creative game pass trough walls, or water walker jesus mode
/**
* Wall border around inclusive.
*/
struct Room{
int c;
int r;
int columns;
int rows;
bool exists = true;
pi center(){ // replace pi with custom struct plz
int midC = (c + (c+columns))/2;
int midR = (r + (r+rows))/2;
return p(midR,midC);
}
bool intersect(Room r1){
if (c > r1.c+r1.columns || r1.c > c+columns) return false;
if (r > r1.r+r1.rows || r1.r > r + rows) return false;
return true;
}
// return is_inside(r1.r,r1.c) ||is_inside(r1.r+r1.rows,r1.c) ||is_inside(r1.r,r1.c+r1.columns)
// || is_inside(r1.r+r1.rows,r1.c+r1.columns);
bool is_inside(int i,int j){
return (i>=r && i<=r+rows) && (j>=c && j<=c + columns);
}
};
struct Point {
int x;
int y;
};
struct queueNode {
Point pt;
int dist;
};
class LetterMap{
private:
//Cell cellOccupied;
string letters = "qwertyuiopasdfghjklzxcvbnm";
string accents = "òàèùì";
string numbers = "1234567890";
string metchars = "?!#@[]·-_/*<>";
string yo = "OY";
// player pos
int playerI=-1,playerJ=-1;
circularpairmap<bool> explored;
public:
char floor = '-';
int _rows = MAXN;
int _columns = MAXN;
//set<pi> explored = set<pi>();
Cell plane[MAXN+BORDER][MAXN+BORDER];
vector<Room> rooms;
int W,H,S;
/**
* W = width
* H = height
* S = letter size
* N = n blocks
*/
LetterMap(int W,int H,int S) {
this->W=W;
this->S=S;
this->H=H;
__bfs(MAXROOMS-1); // let -1
}
LetterMap() {
//does nothing
}
int operator() ( int x, int y ) { return plane[x][y]; }
int getNRows(){return this->_rows;}
int getNCols(){return this->_columns;}
// To optimize with cache
vector<vector<int>> getPlane(){
vector<vector<int>> plane_vectorized(MAXN+BORDER,vector<int>(MAXN+BORDER));
for(int i=0;i<MAXN+BORDER;i++)
for(int j=0;j<MAXN+BORDER;j++)
plane_vectorized[i][j] = plane[i][j]==Floor?0:1;
return plane_vectorized;
}
/**
* Greedy algorithm for rooms creations. Time O(2n)
*/
void __bfs(int nRooms){
clearMap();
// creating a room
Room from = createRoom();
rooms.push_back(from);
applyRoom(from,Floor);// Here before the material was Floor
while(nRooms--){
Room to = createRoom();
if(!to.exists) break; // means there's no more space
rooms.push_back(to);
applyRoom(to,Floor); // HereFloor before the material was
applyPath(connectRooms(from,to),Floor);
from = to;
}
}
/**
* Creates room if possible with MAXL MINL params. Time : O(1)
*/
Room createRoom(){
int randx,randy,latox,latoy;
int MAX_TRY = 50;
do{
randx = randomRange(0,MAXN-MINL);
randy = randomRange(0,MAXN-MINL);
latox = randomRange(MINL,min(MAXN-randx, MAXL));
latoy = randomRange(MINL,min(MAXN-randy, MAXL));
}while(!canBuildRoom(randx,randy,latox,latoy) && (MAX_TRY--)>0);
bool isOk = MAX_TRY>0;
Room r = {randx,randy,latox,latoy,isOk}; // if MAX_TRY>0 than exists
return r;
}
void getExplored(vector<pi> &cells){
for(auto x : explored.m)
cells.push_back(x.first);
// for(auto x : explored)
// cells.push_back(x.first);
}
void addExplored(vector<pi> justViewed){
for(pi x : justViewed){
pi tmp = make_pair(x.first, x.second);
if(explored.m.find(x)==explored.m.end())
explored.insert(tmp,true);
// if(explored.find(x)==explored.end())
// explored[tmp]=true;
//if(find(explored.begin(),explored.end(),tmp) == explored.end()) { // can remove it if u want!
}
//cout << "e oraaa : " << explored.size() << "\n";
}
/**
* Connects two separate rooms. Time : O(n)
* */
vector<pi> connectRooms(Room a,Room b){
pi from = a.center();
pi to = b.center();
int incI = from.second<to.second ? 1 : -1;
int incJ = from.first<to.first ? 1 : -1;
// Random 50% L direction
if(rand()%2){
}
vector<pi> points;
while(from.first != to.first) points.push_back(p(from.first,from.second)),from.first+=incJ;
while(from.second != to.second) points.push_back(p(from.first,from.second)),from.second+=incI;
return points;
}
/**
* Returns valid random spawn coords.
**/
pi getValidSpawn(){
pi spawn;
do{
spawn.first = randomRange(0,MAXN);
spawn.second = randomRange(0,MAXN);
}while(plane[spawn.first][spawn.second] != Floor);
//plane[spawn.first][spawn.second] = Spawn;
return spawn;
}
/**
* V.01 -> Time : O(n^2) bad complexity!
* V.02 -> Time : 0(n) ok!
*/
bool canBuildRoom(int randx,int randy,int latox,int latoy){
if(latox<0 || latoy<0) return false;
if(randx + latox > MAXN || randx < 0) return false;
if(randy + latoy > MAXN || randy < 0) return false;
Room r = {randx,randy,latox,latoy,false};
for(Room r1 : rooms)
if(r1.intersect(r))
return false;
return true;
}
/**
* Time : O((n-1)^2) that's ok!
* Maybe add border to Room of 1 block.
*/
void applyRoom(Room r,Cell material){
int b = ROOM_BORDER;
for(int i=r.r+b;i<r.r + r.rows-b;i++)
for(int j=r.c+b;j<r.c + r.columns-b;j++)
plane[i][j]=material;
}
/**
* Time : O(n) that's ok!
*/
void applyPath(vector<pi> tunnel,Cell material){
for(pi x : tunnel) plane[x.first][x.second] = material;
}
void clearMap(){
memset(plane,Wall,sizeof plane);
}
char factoryLetter(Cell cell){
switch(cell){
case Wall : return ((rand()%2)==0) ? 'Y' : 'O';
case Floor : return floor;
case User : return '@';
};
return 'Y';
}
char get(int i,int j){
if(!isLegit(i,j)) return 'E';
else return factoryLetter(plane[i][j]);
}
Cell getType(int i,int j){
if(!isLegit(i,j)) return Unknown;
return (Cell) plane[i][j];
}
bool isWall(int i,int j){
if(!isLegit(i,j)) return false;
return plane[i][j] == Wall;
}
// Attention : now u can go only on Floor and RoomRoof
bool canGo(int i,int j){
if(i<0 || j<0 || i>getNRows() || j>getNCols()) return false;
return plane[i][j] == Floor || plane[i][j] == RoomRoof || (playerI == i && playerJ == j);
}
// Attention : now u can go only on Floor and RoomRoof
bool isLegit(int i,int j){
return !(i<0 || j<0 || i>getNRows() || j>getNCols());
}
void __pr(){
for(int i=0;i<MAXN;i++){
for(int j=0;j<MAXN;j++)
cout << plane[i][j] << " ";
cout << "\n";
}
}
int getMAXN(){
return MAXN;
}
int randomRange(int min,int max){
return min + (rand() % (max-min));
}
void playerPos(int i,int j){
if(playerI!=-1 && playerJ!=-1){
plane[playerI][playerJ]=Floor;
}
playerI = i;
playerJ = j;
//cellOccupied = plane[playerI][playerJ];
plane[playerI][playerJ]=User;
}
int getPlayerRay(){
return MAXRAY;
}
int getPlayerRow(){
return playerI;
}
int getPlayerColumn(){
return playerJ;
}
bool isPlayerOnRoom(){
return plane[playerI][playerJ]=User;
}
///////////////////////////////////////////////////////////////////////////////////////////
bool isValid(int row, int col) {
return (row >= 0) && (row < getNRows()) && (col >= 0) && (col < getNCols());
}
int path_find(int c1,int r1,int r2,int c2,vector<pair<int,int>> &ans) {
Point src = {r1,c1};
Point dest = {r2,c2};
int rowNum[] = {-1, 0, 0, 1};
int colNum[] = {0, -1, 1, 0};
if (isWall(src.x,src.y) || isWall(dest.x,dest.y))
return -1;
bool visited[getNRows()][getNCols()];
memset(visited, false, sizeof visited);
visited[src.x][src.y] = true;
queue<queueNode> q;
queueNode s = {src, 0};
q.push(s);
queue<vector<pair<int,int>>> parents;
// path vector to store the current path
vector<pair<int,int>> path_tracer;
path_tracer.push_back({src.x,src.y});
parents.push(path_tracer);
while (!q.empty()) {
queueNode curr = q.front();
Point pt = curr.pt;
if (pt.x == dest.x && pt.y == dest.y){
ans = parents.front();
return ans.size();
}
q.pop();
path_tracer = parents.front();
parents.pop();
for (int i = 0; i < 4; i++) {
int row = pt.x + rowNum[i];
int col = pt.y + colNum[i];
if (isValid(row, col) && !isWall(row,col) && !visited[row][col]) {
visited[row][col] = true;
queueNode Adjcell = { {row, col}, curr.dist + 1 };
q.push(Adjcell);
vector< pair<int,int> > newpath(path_tracer);
newpath.push_back({row,col});
parents.push(newpath);
}
}
}
return -1;
}
};
#endif
#endif //NOMPROJET_LETTERMAP_H