forked from vectozavr/pseudo3DEngine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
305 lines (268 loc) · 9.34 KB
/
main.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
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
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <fstream>
#include "settings.h"
#include "Poligon2D.h"
#include "World.h"
#include "Camera.h"
#include "Menu.h"
#include "Time.h"
#include "ServerUDP.h"
#include "ClientUDP.h"
#include "Object2D.h"
#include <iostream>
#include <sstream>
#include "Enemy.h"
#include "GeneticAlgorithm.h"
using namespace std;
// Read server/client settings and start both.
// If client doesn't connect to the localhost - server doesn't start.
void InitNetwork(ServerUDP& server, ClientUDP& client)
{
std::string clientIp;
sf::Uint16 clientPort;
sf::Uint16 serverPort;
std::ifstream connectfile("connect.txt", std::ifstream::in);
// If failed to read client settings
if (!connectfile.is_open() || !(connectfile >> clientIp >> clientPort) || sf::IpAddress(clientIp) == sf::IpAddress::None)
{
connectfile.close();
// Create file and write default settings
clientIp = "127.0.0.1";
clientPort = 54000;
std::ofstream temp("connect.txt", std::ofstream::out);
temp << clientIp << std::endl << clientPort;
temp.close();
}
connectfile.close();
// If failed to read server settings
connectfile.open("server.txt", std::ifstream::in);
if (!connectfile.is_open() || !(connectfile >> serverPort))
{
connectfile.close();
// Create file and write default settings
serverPort = 54000;
std::ofstream temp("server.txt", std::ofstream::out);
temp << serverPort;
temp.close();
}
connectfile.close();
if (clientIp == sf::IpAddress::LocalHost)
server.start(serverPort);
client.connect(clientIp, clientPort);
}
int main()
{
srand(500);
// Window should be created first because of drawing context.
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Pseudo3DEngine");
window.setFramerateLimit(60);
// Sounds
/*
sf::Music music;
music.openFromFile("sounds/unrealSuperHero3.ogg");
music.setVolume(10);
music.setLoop(true);
music.play();
*/
sf::Music backSounds;
backSounds.openFromFile("../sounds/backSounds.ogg");
backSounds.setVolume(20);
backSounds.setLoop(true);
backSounds.pause();
// World Init
World world;
Camera* camera = nullptr;
world.load3DObj("../maps/city.obj", WALL_TEXTURE, 0.03, { -1, 1 });
world.addBonusPoint({ 0.5, -0.5 });
world.addBonusPoint({ 9.5, -4.0 });
world.addBonusPoint({ 13.0, -15.5 });
world.addBonusPoint({ 30.5, 5.5 });
world.addBonusPoint({ 6.5, 18.0 });
world.addBonusPoint({ 9.5, 26.0 });
world.addBonusPoint({ 42, 37 });
world.addBonusPoint({ 42, 34 });
world.addBonusPoint({ 42, -6 });
world.addBonusPoint({ 40, 1 });
world.addBonusPoint({ 45, 12 });
world.addBonusPoint({ 52, -10 });
Menu menu;
bool learn = false;
bool botView = false;
ServerUDP server(world);
ClientUDP client(world);
server.loadObjSpawns("../maps/spawns_city.obj", 0.03);
//server.addSpawn({ 27.5, -12.5 });
//server.addSpawn({ 21.5, -15.0 });
//server.addSpawn({ 23.0, 27.0 });
//server.addSpawn({9.5, 26.0});
//server.addSpawn({0.6, 8.5});
//server.addSpawn({15, 8});
//server.addSpawn({20, -5});
//server.addSpawn({22, 19});
//server.addSpawn({13, 20});
//server.addSpawn({23, 14});
//server.addSpawn({28, 8});
//server.addSpawn({20, 20});
//server.addSpawn({23, 3});
//server.addSpawn({15, 6});
//server.addSpawn({20, 14});
//server.addSpawn({12, 19});
//server.addSpawn({12, -15});
//server.addSpawn({30, 30});
//server.addSpawn({30, 10});
//server.addSpawn({6.5, 20});
//server.addSpawn({0.5, -0.5});
//server.addSpawn({15, 2.3});
//server.addSpawn({30, 20});
//server.addSpawn({29, 32});
//
//server.addSpawn({53, 30});
//server.addSpawn({36, -4});
//server.addSpawn({54, -3.5});
//server.addSpawn({2, -31});
//server.addSpawn({12, -41});
//server.addSpawn({28, -42});
//server.addSpawn({40, -35});
//server.addSpawn({56, -38});
//server.addSpawn({78, -23});
//server.addSpawn({67, -29});
//server.addSpawn({80, -8});
// generation of AI's
//GeneticAlgorithm generation(world, server, 10);
//generation.loadNetwork("../neuralNetwork.txt");
double dt = 0.02;
int iterations = 0;
// Game loop
while (window.isOpen())
{
// Time update
Time::update();
double d_elapsedTime = Time::deltaTime();
// Title update
std::string title = "Pseudo3DEngine " + std::to_string((double)1 / d_elapsedTime) + "fps.";
if (camera != nullptr)
title += " x:" + std::to_string(camera->x()) + ", y:" + std::to_string(camera->y()) + ", health: " + std::to_string(camera->health());
window.setTitle(title);
// Close event search
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// Actually game
window.clear();
if (!menu.isPaused())
{
if (!learn)
{
window.setMouseCursorVisible(false);
camera->startFrameProcessing();
camera->drawCameraView(window);
camera->endFrameProcessing();
world.rotateAllBonuses(d_elapsedTime);
}
// world.draw(window); // top-down debug map. To fix exception - look into "Camera::updateDistances"
// if escape was pressed
if (!camera->keyboardControl(d_elapsedTime, window))
{
client.disconnect();
server.stop();
menu.setPause();
//music.play();
//backSounds.pause();
};
// Network update (must be after camera use)
server.update();
client.update();
/*
if (learn)
generation.update(dt, d_elapsedTime);
else
generation.update(d_elapsedTime, d_elapsedTime);
if (iterations != 0 && (iterations % 500) == 0) { // 500 * dt = 10 sec
generation.newGeneration();
cout << "gen: " << generation.generation() << " : score = \t" << generation.maxScore() << endl;
}
if (iterations != 0 && (iterations % 2000) == 0) {
cout << "saved gen: " << generation.generation() << " to file " << endl;
generation.saveNetwork("../neuralNetwork.txt");
generation.logScore("../log.txt");
}
iterations++;
*/
// if client timeout or disconnected
if (!client.isWorking())
{
menu.setPause();
//music.play();
backSounds.pause();
camera = nullptr;
};
}
// Menu
else
{
window.setMouseCursorVisible(true);
menu.drawMenu(window, d_elapsedTime);
// if "play game" pressed
if (!menu.isPaused()) {
window.clear({ 255,255,255 });
window.display();
InitNetwork(server, client);
// Waiting for connect and updating server if it's same window
while (client.isWorking() && !client.connected())
{
client.update();
server.update();
Time::update();
}
// If connect fail - return to menu
if (!client.isWorking())
{
menu.setPause();
server.stop();
}
// If connect success - init camera and start game
else
{
if (!botView) {
camera = client.localPlayer();
camera->client = &client;
//////
camera->setTextures(menu.isTextures());
camera->setSmooth(menu.isSmooth());
camera->setCollision(menu.isCollision());
}
//backSounds.play();
//music.pause();
}
/*
generation.connect(client);
if (botView) {
camera = generation.connectToEnemyCamera();
camera->setTextures(menu.isTextures());
camera->setSmooth(menu.isSmooth());
camera->setCollision(menu.isCollision());
//client.localPlayer()->setPosition({250, 0});
}
*/
//camera = generation.connectToEnemyCamera();
//camera->setTextures(menu.isTextures());
//camera->setSmooth(menu.isSmooth());
//camera->setCollision(menu.isCollision());
//client.localPlayer()->setPosition({250, 0});
if (learn)
camera->setPosition({ 252, 0 });
}
if (learn)
window.display();
}
if (!learn)
window.display();
}
//generation.saveNetwork("../neuralNetwork.txt");
ResourceManager::unloadAllResources();
return 0;
}