-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utilities.cpp
257 lines (219 loc) · 6.32 KB
/
Utilities.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
//
// Created by KunstDerFuge on 1/26/18.
//
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include "WorldMap.h"
#include <random>
#include <iostream>
#include <algorithm>
#include "Utilities.h"
using namespace std;
static std::default_random_engine ENGINE;
int randomChoiceOfN(int n) {
uniform_int_distribution<int> distribution(0, n-1);
return distribution(ENGINE);
}
float randomFloatZeroToN(float n) {
uniform_real_distribution<float> distribution(0.f, n);
return distribution(ENGINE);
}
float randomSampleNormal(float mean, float dev) {
normal_distribution<float> distribution(mean, dev);
return distribution(ENGINE);
}
float randomSampleNormal(float mean, float dev, float min, float max) {
float out = randomSampleNormal(mean, dev);
if (out < min) out = min;
if (out > max) out = max;
return out;
}
template<typename t>
t randomChoiceFromVector(std::vector<t> vector) {
size_t size = vector.size();
return vector[randomChoiceOfN(size)];
}
int randomChoiceFromWeights(std::initializer_list<int> list) {
vector<int> vector = list;
int sum = 0;
for (auto number : vector) {
sum += number;
}
int random = randomChoiceOfN(sum);
for (int i = 0; i < vector.size(); i++) {
if (random < vector[i]) {
return i;
}
random -= vector[i];
}
throw;
}
bool randomBool(float chance) {
if (chance == 0.5)
return randomChoiceOfN(2) == 1;
return randomFloatZeroToN(1.f) < chance;
}
void toLowercase(std::string& in) {
std::transform(in.begin(), in.end(), in.begin(), ::tolower);
}
void toSentenceCase(std::string& in) {
toLowercase(in);
in[0] = toupper(in[0]);
}
std::string toOrdinal(int in) {
string ordinals[20] = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
"tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth",
"seventeenth", "eighteenth", "nineteenth", "twentieth"};
if (in <= 19)
return ordinals[in];
if (in < 29)
return "twenty-" + ordinals[in - 20];
if (in == 29)
return "thirtieth";
if (in < 39)
return "thirty-" + ordinals[in - 30];
if (in == 39)
return "fortieth";
if (in < 49)
return "forty-" + ordinals[in - 40];
if (in == 49)
return "fiftieth";
cerr << "Number not supported by toOrdinal(): " << in << endl;
return "ERROR";
}
void concatenateWord(std::string& sentence, std::string word) {
if (word.empty())
return;
char lastChar = sentence[sentence.size()-1];
if (!sentence.empty() && lastChar != ' ')
sentence += ' ';
sentence += word;
}
/*
* Same as concatenateWord, but doesn't modify the sentence, just returns the concatenated sentence.
*/
std::string concatenateWordToCopy(std::string sentence, std::string word) {
if (word.empty())
return sentence;
char lastChar = sentence[sentence.size()-1];
if (!sentence.empty() && lastChar != ' ')
sentence += ' ';
sentence += word;
return sentence;
}
std::string namePosition(int position, int numberOfPositions, bool useLeftRight) {
if (numberOfPositions == 3 && useLeftRight) {
if (position == 0) {
return "left";
} else if (position == 1) {
return "middle";
} else if (position == 2) {
return "right";
}
} else if (numberOfPositions == 2 && useLeftRight) {
if (position == 0) {
return "left";
} else {
return "right";
}
}
return toOrdinal(position);
}
int randomFromXToY(int x, int y) {
return randomChoiceOfN(y-x + 1) + x;
}
std::string toPossessive(std::string in) {
char lastChar = in[ in.size()-1 ];
if (lastChar == 's')
return in + "\'";
return in + "'s";
}
sf::Vector2f tileToRenderCoord(float x, float y) {
return {static_cast<int>(x * TILE_WIDTH), static_cast<int>(y * TILE_WIDTH)};
}
long mod(long k, long n) { // https://stackoverflow.com/questions/12276675/modulus-with-negative-numbers-in-c
return ((k %= n) < 0) ? k+n : k;
}
const std::string assetsPath() {
return "../assets";
}
const std::string graphicsPath() {
return assetsPath() + "/graphics";
}
const std::string fontsPath() {
return assetsPath() + "/fonts";
}
const std::string shadersPath() {
return assetsPath() + "/shaders";
}
Point getAdjacentLocation(Point loc, direction dir) {
switch (dir) {
case north:
--loc.y;
break;
case south:
++loc.y;
break;
case east:
++loc.x;
break;
case west:
--loc.x;
break;
case northwest:
--loc.x;
--loc.y;
break;
case northeast:
++loc.x;
--loc.y;
break;
case southwest:
--loc.x;
++loc.y;
break;
case southeast:
++loc.x;
++loc.y;
break;
}
return loc;
}
Point::Point(long x, long y) {
this->x = x;
this->y = y;
}
float Point::squaredDistanceTo(Point& b) {
float dx = b.x - this->x;
float dy = b.y - this->y;
return (dx*dx) + (dy*dy);
}
float Point::distanceTo(Point& b) {
return float(sqrt(squaredDistanceTo(b)));
}
pair<long, long> Point::toPair() {
return make_pair(x, y);
}
bool Point::operator==(const Point &other) {
return this->x == other.x && this->y == other.y;
}
void Tile::render(long x, long y, sf::RenderTexture& mapWindow, bool inFOV) {
sf::RectangleShape tile;
tile.setPosition(tileToRenderCoord(x, y));
tile.setSize(sf::Vector2f(TILE_WIDTH, TILE_WIDTH));
if (!inFOV) {
return;
// tile.setFillColor(sf::Color::Black);
} else {
auto textureXCoord = int(mod(x, this->textureWidthTiles) * 32);
auto textureYCoord = int(mod(y, this->textureHeightTiles) * 32);
tile.setTexture(this->texture);
tile.setTextureRect(sf::IntRect(textureXCoord, textureYCoord, 32, 32));
}
mapWindow.draw(tile);
}
Tile::Tile(terrainType terrain, int textureWidth, int textureHeight) {
this->terrain = terrain;
this->textureWidthTiles = textureWidth;
this->textureHeightTiles = textureHeight;
}