-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine.cpp
More file actions
461 lines (437 loc) · 13.5 KB
/
Copy pathengine.cpp
File metadata and controls
461 lines (437 loc) · 13.5 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
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
#include "engine.hpp"
#include <TGUI/SignalImpl.hpp>
#include <SFML/Window/Event.hpp>
#include <iostream>
namespace
{
int str2int(const std::string& str)
{
int res = 0;
for(sf::Uint8 i = 0; i < str.size(); ++i)
res += str[i];
return res;
}
constexpr int char2int(const char* str)
{
int res = 0;
for(sf::Uint8 i = 0; str[i] != '\0'; ++i)
res += str[i];
return res;
}
std::vector<std::string> split(const std::string& parm, char delim)
{
std::vector<std::string> temp;
for(size_t old_pos = 0;;)
{
size_t new_pos = parm.find(delim, old_pos);
temp.push_back(parm.substr(old_pos, new_pos - old_pos));
if(new_pos == std::string::npos)
break;
old_pos = new_pos + 1;
}
return temp;
}
std::vector<std::string> split2(const std::string& parm, char first, char second)
{
std::vector<std::string> temp;
for(size_t old_pos = parm.find(first);;)
{
if(old_pos == std::string::npos)
break;
size_t new_pos = parm.find(second, ++old_pos);
temp.push_back(parm.substr(old_pos, new_pos - old_pos));
if(new_pos == std::string::npos)
break;
old_pos = parm.find(first, new_pos + 1);
}
return temp;
}
}
Engine::Engine()
{
gui.editbox->connect("Focused", [&] {keyboard.block = true;});
gui.editbox->connect("Unfocused", [&] {keyboard.block = false;});
gui.editbox->connect("ReturnKeyPressed", [&](const sf::String& msg)
{
network.queueMessage((const char*)msg.toUtf8().data());
gui.editbox->setText(sf::String());
});
//TEMPORARILY HERE
setup_window(true);
network.login();
network.queueLoadSequence();
}
bool Engine::run_game()
{
process_input();
game_logic();
draw_frame();
return window.isOpen();
}
bool Engine::run_network()
{
network.sendRequest();
process_network(network.receiveResponse());
return window.isOpen();
}
void Engine::setup_window(bool fullscreen)
{
sf::VideoMode mode = sf::VideoMode::getDesktopMode();
if(fullscreen)
{
window.create(mode, "oldmargonem", sf::Style::Fullscreen);
}
else
{
mode.width = (mode.width*2)/3;
mode.height = (mode.height*2)/3;
window.create(mode, "oldmargonem", sf::Style::Close);
}
window.setKeyRepeatEnabled(false);
window.clear();
gui.tgui.setTarget(window);
}
void Engine::resize_window(sf::Vector2u screen_size)
{
gui.set_screen_size(screen_size);
sf::Vector2u usable_size = gui.usableScreenSize();
map.set_screen_size(usable_size);
hero.set_screen_size(usable_size);
}
void Engine::process_input()
{
sf::Event event;
while(window.pollEvent(event))
{
gui.tgui.handleEvent(event);
switch(event.type)
{
case sf::Event::KeyPressed:
{
switch(event.key.code)
{
case sf::Keyboard::W:
keyboard.dir = '3';
keyboard.keys[3] = true;
break;
case sf::Keyboard::A:
keyboard.dir = '1';
keyboard.keys[1] = true;
break;
case sf::Keyboard::S:
keyboard.dir = '0';
keyboard.keys[0] = true;
break;
case sf::Keyboard::D:
keyboard.dir = '2';
keyboard.keys[2] = true;
break;
case sf::Keyboard::E:
network.queueEnter();
break;
case sf::Keyboard::F:
//fight enemy
break;
case sf::Keyboard::Escape:
window.close();
break;
default:
break;
}// end switch
break;
}
case sf::Event::KeyReleased:
{
switch(event.key.code)
{
case sf::Keyboard::W:
keyboard.keys[3] = false;
break;
case sf::Keyboard::A:
keyboard.keys[1] = false;
break;
case sf::Keyboard::S:
keyboard.keys[0] = false;
break;
case sf::Keyboard::D:
keyboard.keys[2] = false;
break;
default:
break;
}// end switch
break;
}
case sf::Event::Closed:
{
window.close();
break;
}
default:
{
break;
}
}// end switch
}// end while
}
void Engine::game_logic()
{
if(keyboard.anyKey() && clock.walkInterrupt())
{
network.set_pdir(keyboard.dir);
hero.set_dir(keyboard.dir);
hero.move(keyboard.dir);
sf::Vector2i pos = hero.getPosition();
map.center(pos);
network.queueMove(pos);
}
clock.update();
}
void Engine::draw_frame()
{
map.draw(window, clock.getWalkTime());
hero.draw(window);
gui.draw(window);
window.display();
}
void Engine::process_network(const std::string& body)
{
for(size_t old_pos = 0;;)
{
size_t new_pos = body.find("<eol>\n<eol>", old_pos);
std::string line = body.substr(old_pos, new_pos - old_pos);
size_t colon = line.find(':');
std::string cmd = line.substr(0, colon);
switch(str2int(cmd))
{
case char2int("hero"):
{
std::string parm = line.substr(colon+1);
std::vector<std::string> p = split2(parm, '=', ';');
//p[0-1] is hero pos
//p[2] is hero direction
//p[3] is hero nick
//p[4] is group
//p[5] is hero level
//p[6] is hero exp
//p[7] is abilitypoints
//p[8] is hero gold
//p[9] is draconite
//p[10] is hero graphic
//p[11] is mpath
//p[12] is guild
//p[13] is pvp
//p[14] is options
//p[15] is hero profession shortcut
//p[16] is hero base strength
//p[17] is hero base agility
//p[18] is hero base intelligence
//p[19] is hero profession name
resource_manager.set_mpath(p[11]);
//load gui
resource_manager.load_graphic("panel.png", Graphic::INTERFACE);
resource_manager.load_graphic(p[10], Graphic::HERO);
gui.set_right_pannel_texture(resource_manager.get_texture("panel.png"));
hero.set_texture(resource_manager.get_texture(p[10]));
resize_window(window.getSize());//we need textures for size
sf::Vector2i pos(std::stoi(p[0]), std::stoi(p[1]));
char dir = p[2].front();
hero.set_pos(pos);
hero.set_dir(dir);
map.center(pos);
network.set_pdir(dir);
break;
}
case char2int("town")://FINISHED
{
std::string parm = line.substr(colon+1);
std::vector<std::string> p = split(parm, ';');
//p[0-1] is map size
//p[2] is map graphic
//p[3] is map name
//p[4] is map pvp
//p[5] is battle background
//p[6] is map id
resource_manager.load_graphic(p[2], Graphic::MAP);
map.set_map_size(sf::Vector2u(std::stoul(p[0]), std::stoul(p[1])));
map.set_texture(resource_manager.get_texture(p[2]));
break;
}
case char2int("move")://FINISHED
{
std::string parm = line.substr(colon+1);
std::vector<std::string> p = split(parm, ',');
//p[0-1] is hero pos
//p[2] is hero dir
sf::Vector2i pos(std::stoi(p[0]), std::stoi(p[1]));
char dir = p[2].front();
hero.set_pos(pos);
hero.set_dir(dir);
map.center(pos);
network.set_pdir(dir);
break;
}
case char2int("element"):
{
std::string parm = line.substr(colon+1);
std::vector<std::string> p = split2(parm, '=', ';');
unsigned long id;
//p[0] element type
switch(str2int(p[0]))
{
case char2int("item"):
//p[1] is item id
//p[2] is item name
//p[3-4] is item position
//p[5] is item graphic
id = std::stoul(p[1]);
resource_manager.load_graphic(p[5], Graphic::ITEM);
map.items[id].set_position(sf::Vector2i(std::stoi(p[3]), std::stoi(p[4])));
map.items[id].set_texture(resource_manager.get_texture(p[5]));
break;
case char2int("npc"):
//p[1] is npc id
//p[2] is npc nick
//p[3-4] is npc position
//p[5] is npc graphic
//p[6] is npc level
//p[7] is
//p[8] is
//p[9] is npc group fight
//p[10] is npc questmark
id = std::stoul(p[1]);
resource_manager.load_graphic(p[5], Graphic::NPC);
map.NPCs[id].set_position(sf::Vector2i(std::stoi(p[3]), std::stoi(p[4])));
map.NPCs[id].set_texture(resource_manager.get_texture(p[5]));
break;
case char2int("other"):
//p[1] is other id
//p[2] is other nick
//p[3-4] is other position
//p[5] is other profession
//p[6] is other direction
//p[7] is other graphic
//p[8] is
//p[9] is other level
//p[10] is other clan tag
id = std::stoul(p[1]);
resource_manager.load_graphic(p[7], Graphic::HERO);
map.players[id].set_position(sf::Vector2i(std::stoi(p[3]), std::stoi(p[4])));
map.players[id].set_texture(resource_manager.get_texture(p[7]));
map.players[id].set_dir(p[6].front());
break;
default:
std::cout << cmd << ' ' << p[0] << " NOT IMPLEMENTED\n";
break;
}// end switch
break;
}
case char2int("othermove"):
{
std::string parm = line.substr(colon+1);
std::vector<std::string> p1 = split(parm, ',');
std::vector<std::string> p2 = split2(p1[1], '=', ';');
//p1[0] is id
unsigned long id = std::stoul(p1[0]);
//p2[0] is event
switch(str2int(p2[0]))
{
case char2int("move"):
//p2[1-2] is id position
//p2[3] is id direction
map.players[id].set_position(sf::Vector2i(std::stoi(p2[1]), std::stoi(p2[2])));
map.players[id].set_dir(p2[3].front());
break;
default:
std::cout << cmd << ' ' << p2[0] << " NOT IMPLEMENTED\n";
break;
}// end switch
break;
}
case char2int("delete")://FINISHED
{
std::string parm = line.substr(colon+1);
std::vector<std::string> p = split(parm, ',');
//p[0] is id
unsigned long id = std::stoul(p[0]);
//p[1] is type
switch(str2int(p[1]))
{
case char2int("item"):
map.items.erase(id);
break;
case char2int("npc"):
map.NPCs.erase(id);
break;
case char2int("other"):
map.players.erase(id);
break;
default:
std::cout << cmd << ' ' << p[1] << " NOT IMPLEMENTED\n";
break;
}// end switch
break;
}
case char2int("battlemsg"):
{
std::string parm = line.substr(colon+1);
std::vector<std::string> p = split(parm, ';');
network.set_bseq(p[0]);
break;
}
case char2int("battleref"):
{
std::string parm = line.substr(colon+1);
resource_manager.load_graphic(parm, Graphic::BATTLEPLACE);
break;
}
case char2int("lastevent")://FINISHED
{
std::string parm = line.substr(colon+1);
network.set_ev(parm);
break;
}
case char2int("maxchat")://FINISHED
{
std::string parm = line.substr(colon+1);
network.set_lastch(parm);
break;
}
case char2int("maxcchat")://FINISHED
{
std::string parm = line.substr(colon+1);
network.set_lastcch(parm);
break;
}
case char2int("reload")://FINISHED
{
map.clear();
gui.chatbox->removeAllLines();
network.queueLoadSequence();
break;
}
case char2int("chat")://FINISHED
case char2int("msg")://FINISHED
case char2int("alert")://FINISHED
case char2int("log")://FINISHED
{
std::string parm = line.substr(colon+1);
gui.chatbox->addLine(sf::String::fromUtf8(parm.cbegin(), parm.cend()));
break;
}
case char2int("xy")://FINISHED
case char2int("sqltime")://FINISHED
case char2int("end")://FINISHED
{
break;
}
default:
{
std::cout << cmd << " NOT IMPLEMENTED\n";
break;
}
}// end switch
if(new_pos == std::string::npos)
break;
old_pos = new_pos + 11;
}// end for
}