-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
Copy pathViZDoomController.cpp
1289 lines (1003 loc) · 44 KB
/
ViZDoomController.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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2016 by Wojciech Jaśkowski, Michał Kempka, Grzegorz Runc, Jakub Toczek, Marek Wydmuch
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "ViZDoomController.h"
#include "ViZDoomExceptions.h"
#include "ViZDoomPathHelpers.h"
#include "ViZDoomUtilities.h"
#include "ViZDoomVersion.h"
#include "boost/process.hpp"
#include <boost/algorithm/string.hpp>
#include <boost/chrono.hpp>
#include <boost/lexical_cast.hpp>
namespace vizdoom {
namespace bal = boost::algorithm;
namespace bc = boost::chrono;
namespace bfs = boost::filesystem;
namespace bpr = boost::process;
namespace bpri = boost::process::initializers;
/* Public methods */
/*----------------------------------------------------------------------------------------------------------------*/
DoomController::DoomController() {
/* Message queues */
this->MQController = nullptr;
this->MQDoom = nullptr;
/* Shared memory */
this->SM = nullptr;
this->gameState = nullptr;
this->input = nullptr;
this->screenBuffer = nullptr;
this->depthBuffer = nullptr;
this->labelsBuffer = nullptr;
this->automapBuffer = nullptr;
/* Threads */
this->signalThread = nullptr;
this->doomThread = nullptr;
/* Flow control */
this->doomRunning = false;
this->doomWorking = false;
this->mapStartTime = 1;
this->mapTimeout = 0;
this->mapRestartCount = 0;
this->mapChanging = false;
this->mapLastTic = 1;
// TODO move default settings to separate file (ViZDoomConsts.h perhaps?
/* Settings */
this->ticrate = DEFAULT_TICRATE;
this->exePath = "";
this->iwadPath = "";
this->filePath = "";
this->map = "map01";
this->demoPath = "";
this->configPath = "";
this->skill = 3;
this->screenWidth = 320;
this->screenHeight = 240;
this->screenChannels = 3;
this->screenPitch = 320;
this->screenSize = this->screenWidth * this->screenHeight;
this->screenDepth = 8;
this->screenFormat = CRCGCB;
this->depth = false;
this->labels = false;
this->automap = false;
this->amMode = NORMAL;
this->amRotate = false;
this->amTextures = true;
this->hud = false;
this->minHud = false;
this->weapon = true;
this->crosshair = false;
this->decals = true;
this->particles = true;
this->sprites = true;
this->messages = false;
this->corpses = true;
this->renderAll = false;
this->windowHidden = false;
this->noXServer = false;
this->noConsole = true;
this->noSound = true;
this->allowDoomInput = false;
this->runDoomAsync = false;
this->doomStaticSeed = true;
this->doomSeed = 0;
this->instanceRng.seed(static_cast<unsigned int>(bc::high_resolution_clock::now().time_since_epoch().count()));
this->_input = new SMInputState();
}
DoomController::~DoomController() {
this->close();
delete _input;
}
/* Flow Control */
/*----------------------------------------------------------------------------------------------------------------*/
bool DoomController::init() {
if (!this->doomRunning) {
try {
this->generateInstanceId();
// Generate Doom process's arguments
this->createDoomArgs();
// Create message queues
this->MQDoom = new MessageQueue(MQ_DOOM_NAME_BASE + this->instanceId);
this->MQController = new MessageQueue(MQ_CTR_NAME_BASE + this->instanceId);
// Signal handle thread
this->signalThread = new b::thread(b::bind(&DoomController::handleSignals, this));
// Doom thread
this->doomThread = new b::thread(b::bind(&DoomController::launchDoom, this));
this->doomRunning = true;
// Wait for first message from Doom
this->waitForDoomStart();
// Open shared memory
this->SM = new SharedMemory(SM_NAME_BASE + this->instanceId);
this->gameState = this->SM->getGameState();
this->input = this->SM->getInputState();
this->screenBuffer = this->SM->getScreenBuffer();
this->depthBuffer = this->SM->getDepthBuffer();
this->labelsBuffer = this->SM->getLabelsBuffer();
this->automapBuffer = this->SM->getAutomapBuffer();
// Check version
if (this->gameState->VERSION != VIZDOOM_LIB_VERSION)
throw ViZDoomErrorException(
std::string("Controlled ViZDoom version (") + this->gameState->VERSION_STR +
") does not match library version (" + VIZDOOM_LIB_VERSION_STR + ").");
this->waitForDoomMapStartTime();
// Update state
this->MQDoom->send(MSG_CODE_UPDATE);
this->waitForDoomWork();
*this->input = *this->_input;
this->mapLastTic = this->gameState->MAP_TIC;
}
catch (...) {
this->close();
throw;
}
}
return this->doomRunning;
}
void DoomController::close() {
if (this->doomRunning) {
this->doomRunning = false;
this->doomWorking = false;
this->MQDoom->send(MSG_CODE_CLOSE);
}
if (this->signalThread && this->signalThread->joinable()) {
this->ioService->stop();
this->signalThread->interrupt();
this->signalThread->join();
delete this->signalThread;
this->signalThread = nullptr;
delete this->ioService;
this->ioService = nullptr;
}
if (this->doomThread && this->doomThread->joinable()) {
this->doomThread->interrupt();
this->doomThread->join();
delete this->doomThread;
this->doomThread = nullptr;
}
if (this->SM) {
delete this->SM;
this->SM = nullptr;
}
if (this->MQDoom) {
delete this->MQDoom;
this->MQDoom = nullptr;
}
if (this->MQController) {
delete this->MQController;
this->MQController = nullptr;
}
this->gameState = nullptr;
this->input = nullptr;
this->screenBuffer = nullptr;
this->depthBuffer = nullptr;
this->labelsBuffer = nullptr;
this->automapBuffer = nullptr;
}
void DoomController::restart() {
this->close();
this->init();
}
bool DoomController::isTicPossible() {
return !((!this->gameState->GAME_MULTIPLAYER && this->gameState->PLAYER_DEAD)
|| (this->mapTimeout > 0 && this->mapTimeout + this->mapStartTime <= this->gameState->MAP_TIC)
|| (this->gameState->MAP_TICLIMIT > 0 && this->gameState->MAP_TICLIMIT <= this->gameState->MAP_TIC)
|| (this->gameState->MAP_END));
}
void DoomController::tic(bool update) {
if (this->doomRunning) {
if (this->isTicPossible()) {
this->mapLastTic = this->gameState->MAP_TIC + 1;
if (update) this->MQDoom->send(MSG_CODE_TIC_AND_UPDATE);
else this->MQDoom->send(MSG_CODE_TIC);
this->waitForDoomWork();
}
} else throw ViZDoomIsNotRunningException();
}
void DoomController::tics(unsigned int tics, bool update) {
if (this->allowDoomInput && !this->runDoomAsync) {
for (int i = 0; i < DELTA_BUTTON_COUNT; ++i) {
this->input->BT_MAX_VALUE[i] = tics * this->_input->BT_MAX_VALUE[i];
}
}
int ticsMade = 0;
for (int i = 0; i < tics; ++i) {
if (i == tics - 1) this->tic(update);
else this->tic(false);
++ticsMade;
if (!this->isTicPossible() && i != tics - 1) {
this->MQDoom->send(MSG_CODE_UPDATE);
this->waitForDoomWork();
break;
}
}
if (this->allowDoomInput && !this->runDoomAsync) {
for (int i = BINARY_BUTTON_COUNT; i < BUTTON_COUNT; ++i) {
this->input->BT_MAX_VALUE[i - BINARY_BUTTON_COUNT] = this->_input->BT_MAX_VALUE[i -
BINARY_BUTTON_COUNT];
this->input->BT[i] = this->input->BT[i] / ticsMade;
}
}
}
void DoomController::restartMap(std::string demoPath) {
this->setMap(this->map, demoPath);
}
void DoomController::respawnPlayer() {
if (this->doomRunning && !this->mapChanging) {
if (this->gameState->GAME_MULTIPLAYER && this->gameState->PLAYER_DEAD && isTicPossible()) {
bool useAvailable = this->input->BT_AVAILABLE[USE];
this->input->BT_AVAILABLE[USE] = true;
do {
this->sendCommand(std::string("+use"));
this->MQDoom->send(MSG_CODE_TIC);
this->waitForDoomWork();
if(!isTicPossible()) return;
} while (this->gameState->PLAYER_DEAD);
this->sendCommand(std::string("-use"));
this->MQDoom->send(MSG_CODE_UPDATE);
this->waitForDoomWork();
this->input->BT_AVAILABLE[USE] = useAvailable;
this->mapLastTic = this->gameState->MAP_TIC;
} else this->restartMap();
}
}
void DoomController::sendCommand(std::string command) {
if (this->doomRunning && this->MQDoom && command.length() <= MQ_MAX_CMD_LEN)
this->MQDoom->send(MSG_CODE_COMMAND, command.c_str());
}
void DoomController::addCustomArg(std::string arg) {
this->customArgs.push_back(arg);
}
void DoomController::clearCustomArgs() {
this->customArgs.clear();
}
bool DoomController::isDoomRunning() { return this->doomRunning; }
std::string DoomController::getMap() { return this->map; }
void DoomController::setMap(std::string map, std::string demoPath) {
this->map = map;
this->demoPath = demoPath;
if (this->doomRunning && !this->mapChanging) {
if (this->gameState->DEMO_RECORDING) this->sendCommand("stop");
if(this->gameState->GAME_MULTIPLAYER){
this->setDoomSeed(this->getNextDoomSeed());
if(this->gameState->GAME_SETTINGS_CONTROLLER) this->sendCommand(std::string("changemap ") + this->map);
}
else if(this->demoPath.length()){
this->forceDoomSeed(this->getNextDoomSeed());
this->sendCommand(std::string("recordmap ") + prepareFilePathCmd(this->demoPath) + " " + this->map);
}
else {
this->forceDoomSeed(this->getNextDoomSeed());
this->sendCommand(std::string("map ") + this->map);
}
if (map != this->map) this->mapRestartCount = 0;
else ++this->mapRestartCount;
this->mapChanging = true;
this->resetButtons();
int restartTics = 0;
bool useAvailable = this->input->BT_AVAILABLE[USE];
if (this->gameState->GAME_MULTIPLAYER) {
this->input->BT_AVAILABLE[USE] = true;
this->sendCommand(std::string("-use"));
}
do {
++restartTics;
if (this->gameState->GAME_MULTIPLAYER) {
if (restartTics % 2) this->sendCommand(std::string("+use"));
else this->sendCommand(std::string("-use"));
}
this->MQDoom->send(MSG_CODE_TIC);
this->waitForDoomWork();
if (restartTics > 3 && !this->gameState->GAME_MULTIPLAYER) {
if (this->demoPath.length())
this->sendCommand(std::string("recordmap ") + this->demoPath + " " + this->map);
else this->sendCommand(std::string("map ") + this->map);
restartTics = 0;
}
} while (this->gameState->MAP_END || this->gameState->MAP_TIC > this->mapLastTic);
if (this->gameState->GAME_MULTIPLAYER) {
this->sendCommand(std::string("-use"));
this->input->BT_AVAILABLE[USE] = useAvailable;
}
this->waitForDoomMapStartTime();
this->sendCommand("viz_override_player 0");
this->MQDoom->send(MSG_CODE_UPDATE);
this->waitForDoomWork();
this->mapLastTic = this->gameState->MAP_TIC;
this->mapChanging = false;
}
}
void DoomController::playDemo(std::string demoPath, int player) {
if (this->doomRunning && !this->mapChanging) {
if (this->gameState->DEMO_RECORDING) this->sendCommand("stop");
this->sendCommand(std::string("playdemo ") + prepareLmpFilePath(demoPath));
this->mapChanging = true;
this->resetButtons();
int restartTics = 0;
do {
++restartTics;
this->MQDoom->send(MSG_CODE_TIC);
this->waitForDoomWork();
if (restartTics > 3) {
this->sendCommand(std::string("playdemo ") + demoPath);
restartTics = 0;
}
} while (this->gameState->MAP_END || this->gameState->MAP_TIC > this->mapLastTic);
this->waitForDoomMapStartTime();
this->sendCommand(std::string("viz_override_player ") + b::lexical_cast<std::string>(player));
this->MQDoom->send(MSG_CODE_UPDATE);
this->waitForDoomWork();
this->mapLastTic = this->gameState->MAP_TIC;
this->mapChanging = false;
}
}
/* Settings */
/*----------------------------------------------------------------------------------------------------------------*/
unsigned int DoomController::getTicrate() { return this->ticrate; }
void DoomController::setTicrate(unsigned int ticrate) { this->ticrate = ticrate; }
std::string DoomController::getExePath() { return this->exePath; }
void DoomController::setExePath(std::string exePath) { if (!this->doomRunning) this->exePath = exePath; }
std::string DoomController::getIwadPath() { return this->iwadPath; }
void DoomController::setIwadPath(std::string iwadPath) { if (!this->doomRunning) this->iwadPath = iwadPath; }
std::string DoomController::getFilePath() { return this->filePath; }
void DoomController::setFilePath(std::string filePath) {
this->filePath = filePath;
if (this->doomRunning) {
this->map = "file:" + prepareWadFilePath(this->filePath);
}
}
std::string DoomController::getConfigPath() { return this->configPath; }
void
DoomController::setConfigPath(std::string configPath) { if (!this->doomRunning) this->configPath = configPath; }
int DoomController::getSkill() { return this->skill; }
void DoomController::setSkill(int skill) {
if (skill > 5) skill = 5;
else if (skill < 1) skill = 1;
this->skill = skill;
if (this->doomRunning) {
this->sendCommand(std::string("skill set ") + b::lexical_cast<std::string>(this->skill - 1));
//this->resetMap(); // needs map restart to take effect
}
}
unsigned int DoomController::getDoomSeed() {
if (this->doomRunning) return this->gameState->GAME_STATIC_SEED;
else return this->doomSeed;
}
void DoomController::setDoomSeed(unsigned int seed) {
this->doomStaticSeed = true;
this->doomSeed = seed;
if (this->doomRunning) {
this->sendCommand(std::string("rngseed set ") + b::lexical_cast<std::string>(this->doomSeed));
}
}
void DoomController::clearDoomSeed() {
this->doomStaticSeed = false;
this->doomSeed = 0;
if (this->doomRunning) {
this->sendCommand("rngseed clear");
}
}
void DoomController::setInstanceSeed(unsigned int seed) {
this->instanceSeed = seed;
this->instanceRng.seed(seed);
}
unsigned int DoomController::getInstanceSeed() { return this->instanceSeed; }
unsigned int DoomController::getMapStartTime() { return this->mapStartTime; }
void DoomController::setMapStartTime(unsigned int tics) { this->mapStartTime = tics ? tics : 1; }
unsigned int DoomController::getMapTimeout() { return this->mapTimeout; }
void DoomController::setMapTimeout(unsigned int tics) { this->mapTimeout = tics; }
bool DoomController::isMapFirstTic() {
return this->doomRunning && this->gameState->MAP_TIC <= 1;
}
bool DoomController::isMapLastTic() {
return this->doomRunning && this->mapTimeout > 0
&& this->gameState->MAP_TIC >= this->mapTimeout + this->mapStartTime;
}
bool DoomController::isMapEnded() {
return this->doomRunning && this->gameState->MAP_END;
}
unsigned int DoomController::getMapLastTic() {
return this->mapLastTic;
}
void DoomController::setNoConsole(bool console) {
if (!this->doomRunning) this->noConsole = console;
}
void DoomController::setNoSound(bool sound) {
if (!this->doomRunning) this->noSound = sound;
}
void DoomController::setScreenResolution(unsigned int width, unsigned int height) {
if (!this->doomRunning) {
this->screenWidth = width;
this->screenHeight = height;
}
}
/* Depth buffer */
bool DoomController::isDepthBufferEnabled() {
if (this->doomRunning) return this->gameState->DEPTH_BUFFER;
else return depth;
}
void DoomController::setDepthBufferEnabled(bool depthBuffer) {
this->depth = depthBuffer;
if (this->doomRunning) {
if (this->automap) this->sendCommand("viz_depth 1");
else this->sendCommand("viz_depth 0");
}
this->updateSettings = true;
}
/* Labels */
bool DoomController::isLabelsEnabled() {
if (this->doomRunning) return this->gameState->LABELS;
else return labels;
}
void DoomController::setLabelsEnabled(bool labels) {
this->labels = labels;
if (this->doomRunning) {
if (this->automap) this->sendCommand("viz_labels 1");
else this->sendCommand("viz_labels 0");
}
}
/* Automap */
bool DoomController::isAutomapEnabled() {
if (this->doomRunning) return this->gameState->AUTOMAP;
else return automap;
}
void DoomController::setAutomapEnabled(bool automap) {
this->automap = automap;
if (this->doomRunning) {
if (this->automap) this->sendCommand("viz_automap 1");
else this->sendCommand("viz_automap 0");
}
}
void DoomController::setAutomapMode(AutomapMode mode) {
this->amMode = mode;
if (this->doomRunning) this->sendCommand("viz_automap_mode " + b::lexical_cast<std::string>(this->amMode));
}
void DoomController::setAutomapRotate(bool rotate) {
this->amRotate = rotate;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setAutomapRenderTextures(bool textures) {
this->amTextures = textures;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setScreenWidth(unsigned int width) {
if (!this->doomRunning) this->screenWidth = width;
}
void DoomController::setScreenHeight(unsigned int height) {
if (!this->doomRunning) this->screenHeight = height;
}
void DoomController::setScreenFormat(ScreenFormat format) {
if (!this->doomRunning) {
this->screenFormat = format;
switch (format) {
case CRCGCB:
case RGB24:
case CBCGCR:
case BGR24:
this->screenChannels = 3;
break;
case RGBA32:
case ARGB32:
case BGRA32:
case ABGR32:
this->screenChannels = 4;
break;
case GRAY8:
case DOOM_256_COLORS8:
this->screenChannels = 1;
break;
default:
this->screenChannels = 0;
}
switch (format) {
case RGB24:
case BGR24:
this->screenDepth = 24;
break;
case RGBA32:
case ARGB32:
case BGRA32:
case ABGR32:
this->screenDepth = 32;
break;
case CRCGCB:
case CBCGCR:
case GRAY8:
case DOOM_256_COLORS8:
this->screenDepth = 8;
break;
default:
this->screenDepth = 0;
}
}
if (this->doomRunning) {
this->sendCommand("viz_screen_format " + b::lexical_cast<std::string>(this->screenFormat));
}
}
void DoomController::setWindowHidden(bool windowHidden) {
if (!this->doomRunning) this->windowHidden = windowHidden;
}
void DoomController::setNoXServer(bool noXServer) {
if (!this->doomRunning) this->noXServer = noXServer;
}
void DoomController::setRenderHud(bool hud) {
this->hud = hud;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderMinimalHud(bool minHud) {
this->minHud = minHud;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderWeapon(bool weapon) {
this->weapon = weapon;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderCrosshair(bool crosshair) {
this->crosshair = crosshair;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderDecals(bool decals) {
this->decals = decals;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderParticles(bool particles) {
this->particles = particles;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderEffectsSprites(bool sprites) {
this->sprites = sprites;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderMessages(bool messages) {
this->messages = messages;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderCorpses(bool corpses){
this->corpses = corpses;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderScreenFlashes(bool flashes){
this->flashes = flashes;
if (this->doomRunning) this->setRenderMode(this->getRenderModeValue());
}
void DoomController::setRenderAllFrames(bool allFrames){
this->renderAll = allFrames;
}
unsigned int DoomController::getScreenWidth() {
if (this->doomRunning) return this->gameState->SCREEN_WIDTH;
else return this->screenWidth;
}
unsigned int DoomController::getScreenHeight() {
if (this->doomRunning) return this->gameState->SCREEN_HEIGHT;
else return this->screenHeight;
}
unsigned int DoomController::getScreenChannels() { return this->screenChannels; }
unsigned int DoomController::getScreenDepth() { return this->screenDepth; }
size_t DoomController::getScreenPitch() {
if (this->doomRunning) return this->gameState->SCREEN_PITCH;
else return static_cast<size_t>(this->screenDepth / 8 * this->screenWidth);
}
ScreenFormat DoomController::getScreenFormat() {
if (this->doomRunning) return static_cast<ScreenFormat>(this->gameState->SCREEN_FORMAT);
else return this->screenFormat;
}
size_t DoomController::getScreenSize() {
if (this->doomRunning) return (size_t) this->gameState->SCREEN_SIZE;
else return (size_t) this->screenChannels * this->screenWidth * this->screenHeight;
}
int DoomController::getRenderModeValue() {
int renderMode = 0;
if(this->hud) renderMode |= 1;
if(this->minHud) renderMode |= 2;
if(this->weapon) renderMode |= 4;
if(this->crosshair) renderMode |= 8;
if(this->decals) renderMode |= 16;
if(this->particles) renderMode |= 32;
if(this->sprites) renderMode |= 64;
if(this->messages) renderMode |= 128;
if(this->amRotate) renderMode |= 256;
if(this->amTextures) renderMode |= 512;
if(this->corpses) renderMode |= 1024;
if(this->flashes) renderMode |= 2048;
return renderMode;
}
void DoomController::setRenderMode(int value) {
this->sendCommand("viz_render_mode " + b::lexical_cast<std::string>(this->getRenderModeValue()));
}
/* SM setters & getters */
/*----------------------------------------------------------------------------------------------------------------*/
uint8_t *const DoomController::getScreenBuffer() { return this->screenBuffer; }
uint8_t *const DoomController::getDepthBuffer() { return this->depthBuffer; }
uint8_t *const DoomController::getLabelsBuffer() { return this->labelsBuffer; }
uint8_t *const DoomController::getAutomapBuffer() { return this->automapBuffer; }
SMInputState *const DoomController::getInput() { return this->input; }
SMGameState *const DoomController::getGameState() { return this->gameState; }
double DoomController::getButtonState(Button button) {
if (this->doomRunning) return this->input->BT[button];
else return 0;
}
void DoomController::setButtonState(Button button, double state) {
if (button < BUTTON_COUNT && button >= 0 && this->doomRunning)
this->input->BT[button] = state;
}
void DoomController::toggleButtonState(Button button) {
if (button < BUTTON_COUNT && button >= 0 && this->doomRunning)
this->input->BT[button] = !this->input->BT[button];
}
bool DoomController::isButtonAvailable(Button button) {
if (this->doomRunning) return this->input->BT_AVAILABLE[button];
else return this->_input->BT_AVAILABLE[button];
}
void DoomController::setButtonAvailable(Button button, bool allow) {
if (button < BUTTON_COUNT && button >= 0) {
if (this->doomRunning) this->input->BT_AVAILABLE[button] = allow;
this->_input->BT_AVAILABLE[button] = allow;
}
}
void DoomController::resetButtons() {
if (this->doomRunning)
for (int i = 0; i < BUTTON_COUNT; ++i)
this->input->BT[i] = 0;
}
void DoomController::disableAllButtons() {
for (int i = 0; i < BUTTON_COUNT; ++i) {
if (this->doomRunning) this->input->BT_AVAILABLE[i] = false;
this->_input->BT_AVAILABLE[i] = false;
}
}
void DoomController::availableAllButtons() {
for (int i = 0; i < BUTTON_COUNT; ++i) {
if (this->doomRunning) this->input->BT_AVAILABLE[i] = true;
this->_input->BT_AVAILABLE[i] = true;
}
}
void DoomController::setButtonMaxValue(Button button, double value) {
if (button >= BINARY_BUTTON_COUNT) {
if (this->doomRunning) this->input->BT_MAX_VALUE[button - BINARY_BUTTON_COUNT] = value;
this->_input->BT_MAX_VALUE[button - BINARY_BUTTON_COUNT] = value;
}
}
double DoomController::getButtonMaxValue(Button button) {
if (button >= BINARY_BUTTON_COUNT) {
if (this->doomRunning) return this->input->BT_MAX_VALUE[button - BINARY_BUTTON_COUNT];
else return this->_input->BT_MAX_VALUE[button - BINARY_BUTTON_COUNT];
} else return 1;
}
bool DoomController::isAllowDoomInput() { return this->allowDoomInput; }
void DoomController::setAllowDoomInput(bool set) { if (!this->doomRunning) this->allowDoomInput = set; }
bool DoomController::isRunDoomAsync() { return this->runDoomAsync; }
void DoomController::setRunDoomAsync(bool set) { if (!this->doomRunning) this->runDoomAsync = set; }
/* GameVariables getters */
/*----------------------------------------------------------------------------------------------------------------*/
double DoomController::getGameVariable(GameVariable var){
switch (var) {
case KILLCOUNT :
return this->gameState->MAP_KILLCOUNT;
case ITEMCOUNT :
return this->gameState->MAP_ITEMCOUNT;
case SECRETCOUNT :
return this->gameState->MAP_SECRETCOUNT;
case FRAGCOUNT:
return this->gameState->PLAYER_FRAGCOUNT;
case DEATHCOUNT:
return this->gameState->PLAYER_DEATHCOUNT;
case HEALTH :
return this->gameState->PLAYER_HEALTH;
case ARMOR :
return this->gameState->PLAYER_ARMOR;
case DEAD :
return this->gameState->PLAYER_DEAD;
case ON_GROUND :
return static_cast<double>(this->gameState->PLAYER_ON_GROUND);
case ATTACK_READY :
return static_cast<double>(this->gameState->PLAYER_ATTACK_READY);
case ALTATTACK_READY :
return static_cast<double>(this->gameState->PLAYER_ALTATTACK_READY);
case SELECTED_WEAPON :
return this->gameState->PLAYER_SELECTED_WEAPON;
case SELECTED_WEAPON_AMMO :
return this->gameState->PLAYER_SELECTED_WEAPON_AMMO;
case PLAYER_NUMBER:
return static_cast<double>(this->gameState->PLAYER_NUMBER);
case PLAYER_COUNT:
return static_cast<double>(this->gameState->PLAYER_COUNT);
}
if (var >= AMMO0 && var <= AMMO9)
return this->gameState->PLAYER_AMMO[var - AMMO0];
else if (var >= WEAPON0 && var <= WEAPON9)
return this->gameState->PLAYER_WEAPON[var - WEAPON0];
else if(var >= POSITION_X && var <= VELOCITY_Z)
return this->gameState->PLAYER_MOVEMENT[var - POSITION_X];
else if(var >= USER1 && var <= USER60)
return this->gameState->MAP_USER_VARS[var - USER1];
else if (var >= PLAYER1_FRAGCOUNT && var <= PLAYER16_FRAGCOUNT)
return this->gameState->PLAYER_N_FRAGCOUNT[var - PLAYER1_FRAGCOUNT];
return 0;
}
unsigned int DoomController::getGameTic() { return this->gameState->GAME_TIC; }
bool DoomController::isMultiplayerGame() { return this->gameState->GAME_MULTIPLAYER; }
bool DoomController::isNetGame() { return this->gameState->GAME_NETGAME; }
unsigned int DoomController::getMapTic() { return this->gameState->MAP_TIC; }
int DoomController::getMapReward() { return this->gameState->MAP_REWARD; }
bool DoomController::isPlayerDead() { return this->gameState->PLAYER_DEAD; }
/* Protected and private functions */
/*----------------------------------------------------------------------------------------------------------------*/
void DoomController::generateInstanceId() {
std::string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
this->instanceId = "";
br::uniform_int_distribution<size_t> charDist(0, static_cast<size_t>(chars.length() - 1));
br::mt19937 rng;
rng.seed((unsigned int) bc::high_resolution_clock::now().time_since_epoch().count());
for (int i = 0; i < INSTANCE_ID_LENGTH; ++i) {
this->instanceId += chars[charDist(rng)];
}
}
unsigned int DoomController::getNextDoomSeed(){
br::uniform_int_distribution<unsigned int> mapSeedDist(0, UINT_MAX);
return mapSeedDist(this->instanceRng);
}
void DoomController::forceDoomSeed(unsigned int seed) {
this->doomStaticSeed = true;
this->doomSeed = seed;
if (this->doomRunning) {
this->sendCommand(std::string("viz_set_seed ") + b::lexical_cast<std::string>(this->doomSeed));
}
}
/* Signals */
/*----------------------------------------------------------------------------------------------------------------*/
void DoomController::handleSignals() {
this->ioService = new ba::io_service();
ba::signal_set signals(*this->ioService, SIGINT, SIGABRT, SIGTERM);
signals.async_wait(b::bind(signalHandler, b::ref(signals), this, _1, _2));
this->ioService->run();
}
void DoomController::signalHandler(ba::signal_set &signal, DoomController *controller, const bs::error_code &error,
int sigNumber) {
controller->intSignal(sigNumber);
}