-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapGeneratorEngine.cpp
More file actions
176 lines (153 loc) · 7.66 KB
/
MapGeneratorEngine.cpp
File metadata and controls
176 lines (153 loc) · 7.66 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
#include <random>
#include "Field.h"
#include <set>
#include <SFML/Graphics.hpp>
#include <vector>
#include <map>
// --- Function for map random generating ---
auto MapGenerator (std::vector<Field>& MapFieldsByType) -> void {
// Randomizer engine
auto rd = std::random_device();
auto gen = std::mt19937(rd());
auto dis = std::uniform_int_distribution<>();
// Fields in safe zone (near spawns)
auto ExcludedForAllFields = std::set<int>{11,23,71,83,24,36,37,48,35,47,46,59};
// Resetting to default
MapFieldsByType = std::vector<Field>(84,Field::Standard);
// Generating one Petrol Station field
dis = std::uniform_int_distribution<>(0, 31); // Petrol Station, Ammo Drop and Workshop can only be on one of certain 32 fields
auto PetrolIndex = dis(gen); // Obviously they can not share their field (Only one of each on one field)
// Setting Petrol Station Field accordingly
if(PetrolIndex<8) PetrolIndex+=2;
else if(PetrolIndex<16) PetrolIndex+=6;
else if(PetrolIndex<24) PetrolIndex+=46;
else PetrolIndex+=50;
MapFieldsByType[PetrolIndex] = Field::PetrolFull;
// Generating one Ammo Drop field
auto AmmoDropIndex = dis(gen);
if(AmmoDropIndex<8) AmmoDropIndex+=2;
else if(AmmoDropIndex<16) AmmoDropIndex+=6;
else if(AmmoDropIndex<24) AmmoDropIndex+=46;
else AmmoDropIndex+=50;
while(MapFieldsByType[AmmoDropIndex]!=Field::Standard){
AmmoDropIndex = dis(gen);
if(AmmoDropIndex<8) AmmoDropIndex+=2;
else if(AmmoDropIndex<16) AmmoDropIndex+=6;
else if(AmmoDropIndex<24) AmmoDropIndex+=46;
else AmmoDropIndex+=50;
}
// Setting Ammo Drop Field accordingly
MapFieldsByType[AmmoDropIndex] = Field::AmmoDropFull;
// Generating one Workshop field
auto WorkshopIndex = dis(gen);
if(WorkshopIndex<8) WorkshopIndex+=2;
else if(WorkshopIndex<16) WorkshopIndex+=6;
else if(WorkshopIndex<24) WorkshopIndex+=46;
else WorkshopIndex+=50;
while(MapFieldsByType[WorkshopIndex]!=Field::Standard) {
WorkshopIndex = dis(gen);
if(WorkshopIndex<8) WorkshopIndex+=2;
else if(WorkshopIndex<16) WorkshopIndex+=6;
else if(WorkshopIndex<24) WorkshopIndex+=46;
else WorkshopIndex+=50;
}
// Setting Workshop Field accordingly
MapFieldsByType[WorkshopIndex] = Field::WorkshopFull;
// Generating Explosives Field (2 fields on map, Can not be in some places near spawns)
dis = std::uniform_int_distribution<>(0, 83);
auto ExcludedForExplosivesField = std::set<int>{12,25,38,49,60,23,34,45,58,71};
for(auto i = 0; i<2; i++) {
auto ExplosiveFieldIndex = dis(gen);
while( (ExcludedForAllFields.find(ExplosiveFieldIndex)!=ExcludedForAllFields.end()) ||
(ExcludedForExplosivesField.find(ExplosiveFieldIndex)!=ExcludedForExplosivesField.end()) ||
MapFieldsByType[ExplosiveFieldIndex]!=Field::Standard){
ExplosiveFieldIndex = dis(gen);
}
MapFieldsByType[ExplosiveFieldIndex] = Field::Explosives;
}
// Generating House Obstacles (There will be max 10 houses)
for(auto i = 0; i<10; i++){
auto HouseFieldIndex = dis(gen);
if( (ExcludedForAllFields.find(HouseFieldIndex)!=ExcludedForAllFields.end()) ||
MapFieldsByType[HouseFieldIndex]!=Field::Standard ) continue;
else MapFieldsByType[HouseFieldIndex] = Field::Home;
}
// Generating Tank Spikes Obstacles (There will be max 6 spikes)
for(auto i = 0; i<6; i++){
auto SpikesFieldIndex = dis(gen);
if( (ExcludedForAllFields.find(SpikesFieldIndex)!=ExcludedForAllFields.end()) ||
MapFieldsByType[SpikesFieldIndex]!=Field::Standard ) continue;
else MapFieldsByType[SpikesFieldIndex] = Field::Spikes;
}
// Generating Forests (There will be max 5 forests)
for(auto i = 0; i<5; i++){
auto ForestFieldIndex = dis(gen);
if( (ExcludedForAllFields.find(ForestFieldIndex)!=ExcludedForAllFields.end()) ||
MapFieldsByType[ForestFieldIndex]!=Field::Standard ) continue;
else MapFieldsByType[ForestFieldIndex] = Field::Forest;
}
// Generating Swamps Field (There will be max 6 swamps)
for(auto i = 0; i<6; i++){
auto SwampFieldIndex = dis(gen);
if( (ExcludedForAllFields.find(SwampFieldIndex)!=ExcludedForAllFields.end()) ||
MapFieldsByType[SwampFieldIndex]!=Field::Standard ) continue;
else MapFieldsByType[SwampFieldIndex] = Field::Swamps;
}
// Generating Sands Field (There will be max 6 sands)
for(auto i = 0; i<6; i++){
auto SandsFieldIndex = dis(gen);
if( (ExcludedForAllFields.find(SandsFieldIndex)!=ExcludedForAllFields.end()) ||
MapFieldsByType[SandsFieldIndex]!=Field::Standard ) continue;
else MapFieldsByType[SandsFieldIndex] = Field::Sands;
}
// Generating Big Mine Instant Death Field (There will be max 2 mines)
for(auto i = 0; i<2; i++){
auto BigMineFieldIndex = dis(gen);
if( (ExcludedForAllFields.find(BigMineFieldIndex)!=ExcludedForAllFields.end()) ||
MapFieldsByType[BigMineFieldIndex]!=Field::Standard ) continue;
else MapFieldsByType[BigMineFieldIndex] = Field::BigMine;
}
// Generating Big Hole Instant Death Field (There will be max 3 holes)
for(auto i = 0; i<3; i++){
auto BigHoleFieldIndex = dis(gen);
if( (ExcludedForAllFields.find(BigHoleFieldIndex)!=ExcludedForAllFields.end()) ||
MapFieldsByType[BigHoleFieldIndex]!=Field::Standard ) continue;
else MapFieldsByType[BigHoleFieldIndex] = Field::Hole;
}
// Generating Mine Fields Death Field (There will be max 5 hidden minefields)
for(auto i = 0; i<5; i++){
auto MineFieldsFieldIndex = dis(gen);
if( (ExcludedForAllFields.find(MineFieldsFieldIndex)!=ExcludedForAllFields.end()) ||
MapFieldsByType[MineFieldsFieldIndex]!=Field::Standard ) continue;
else MapFieldsByType[MineFieldsFieldIndex] = Field::MineField;
}
};
//--------------------------
// --- Function for map fields building ---
auto MapFieldsBuilder (std::vector<sf::Sprite>& MapFieldsImages, std::map<Field,sf::Sprite>& EnumedImageMap, std::vector<Field>& MapFieldsByType) -> void {
auto FieldPosition = sf::Vector2f(85,89);
for(auto row = 0; row<7; row++){
FieldPosition.x = 85;
for(auto column = 0; column<12; column++){
MapFieldsImages[row*12+column] = EnumedImageMap[MapFieldsByType[row*12+column]];
MapFieldsImages[row*12+column].setPosition(FieldPosition);
FieldPosition+=sf::Vector2f(150,0);
}
FieldPosition+=sf::Vector2f(0,150);
}
};
//--- Function for Determining index of the field on which thing currently is
auto GetThingCurrentFieldIndex (sf::Sprite thing) -> int {
auto ThingCurrentRow = int((thing.getPosition().y-13)/150);
auto ThingCurrentColumn = int((thing.getPosition().x-10)/150);
auto ThingCurrentField = int(ThingCurrentRow*12+ThingCurrentColumn);
return ThingCurrentField;
};
//--- Function for Determining the type of the field on which thing currently is
auto GetThingCurrentFieldType (sf::Sprite thing, std::vector<Field>& MapFieldsByType) -> Field {
return MapFieldsByType[GetThingCurrentFieldIndex(thing)];
};
//--- Function for Getting the field origin point
auto GetThingCurrentFieldPosition (sf::Sprite thing, std::vector<sf::Sprite>& MapFieldsImages) -> sf::Vector2f {
return MapFieldsImages[GetThingCurrentFieldIndex(thing)].getPosition();
};