forked from larspensjo/ephenation-client
/
gamedialog.cpp
1433 lines (1317 loc) · 44.2 KB
/
gamedialog.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 2012 The Ephenation Authors
//
// This file is part of Ephenation.
//
// Ephenation is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// Ephenation is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Ephenation. If not, see <http://www.gnu.org/licenses/>.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <memory>
#include <GL/glew.h>
#include <GL/glfw.h>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "primitives.h"
#include "object.h"
#include "player.h"
#include "gamedialog.h"
#include "connection.h"
#include "client_prot.h"
#include "render.h"
#include "chunk.h"
#include "msgwindow.h"
#include "parse.h"
#include "textures.h"
#include "modes.h"
#include "monsters.h"
#include "otherplayers.h"
#include "vsfl/vsfl.h"
#include "DrawText.h"
#include "shaders/ChunkShader.h"
#include "shaders/ChunkShaderPicking.h"
#include "shaders/DeferredLighting.h"
#include "shaders/TranspShader.h"
#include "shaders/AnimationShader.h"
#include "HealthBar.h"
#include "MonsterDef.h"
#include "BuildingBlocks.h"
#include "DrawTexture.h"
#include "SoundControl.h"
#include "Options.h"
#include "Inventory.h"
#include "Map.h"
#include "ScrollingMessages.h"
#include "Teleport.h"
#include "ui/dialog.h"
#include "ui/messagedialog.h"
#include "shadowrender.h"
#include "uniformbuffer.h"
#include "shadowconfig.h"
#include "billboard.h"
#include "rendercontrol.h"
#include "timemeasure.h"
#include "worsttime.h"
#include "ChunkProcess.h"
gameDialog *gameDialog::sfCurrentGameDialog = 0;
const float defaultRenderViewAngle = 60.0f;
float renderViewAngle = defaultRenderViewAngle;
// The time stamp when the player clicked on a magical portal.
static double sStartZoom;
static std::vector<unsigned>::iterator sTextureIterator;
static bool sShowAlternateBitmap = false;
static float mouseSens = -5.0f;
gameDialog gGameDialog; // For now, there will only be one
// If there are any strings in these parameters, show them once in a popup dialog.
string sgPopup, sgPopupTitle = "Ephenation";
#define NELEM(x) (sizeof x / sizeof x[0])
gameDialog::gameDialog() {
fMovingFwd = false;
fMovingBwd = false;
fMovingLeft = false;
fMovingRight = false;
fUsingTorch = false;
fUnderWater = false;
fCameraDistance = 0.0f;
fRequestedCameraDistance = 0.0f;
fMapWidth = 600;
fEnterDebugText = false;
fDrawMap = false;
fShowInventory = false;
fShowWeapon = true;
fShowMainDialog = false;
fHideDialog = false; // This will override the fShowMainDialog
fDebugTextLength = 0;
fSelectedObject = 0;
fShader = 0;
fInputPromptSentence = 0xFFFFFFFF;
fFPSsentence = 0xFFFFFFFF;
fPlayerStatusSentence = 0xFFFFFFFF;
fBuildingBlocks = 0;
fHealthBar = 0;
fDrawTexture = 0;
fCurrentEffect = EFFECT_NONE;
fCalibrationMode = CALIB_NONE;
}
gameDialog::~gameDialog() {
if (fFPSsentence != 0xFFFFFFFF) {
// Should normally be the case, unless the startup is aborted.
gDrawFont.vsfl.deleteSentence(fFPSsentence);
gDrawFont.vsfl.deleteSentence(fInputPromptSentence);
gDrawFont.vsfl.deleteSentence(fPlayerStatusSentence);
}
fFPSsentence = 0xFFFFFFFF;
fInputPromptSentence = 0xFFFFFFFF;
fPlayerStatusSentence = 0xFFFFFFFF;
fShader = 0; // Don't delete this singleton
fBuildingBlocks = 0; // Singleton
fHealthBar = 0; // Singleton
}
// In game mode, find the object that the player clicked on.
// TODO: should also draw landscape so as not to allow free sight and selecting monsters behind walls.
Object *gameDialog::FindSelectedObject(int x, int y) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Use black sky for picking
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// gMonsters.RenderMonsters(true); // Selecting monsters not supported for now
// gOtherPlayers.RenderPlayers(true); // selecting players not supported for now
unsigned char pixel[3];
glReadPixels(x, gViewport[3] - y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
// printf("gameDialog::FindSelectedObject: %d,%d,%d\n", pixel[0], pixel[1], pixel[2]);
Object *obj = 0;
switch(pixel[0]) {
case 1:
obj = gMonsters.GetSelection(pixel[0], pixel[1], pixel[2]);
break;
case 2:
obj = gOtherPlayers.GetSelection(pixel[0], pixel[1], pixel[2]);
break;
}
return obj;
}
// In build mode, find the block at the given screen position. Return the chunk and the
// data about it to the pointers.
chunk *gameDialog::FindSelectedSurface(int x, int y, ChunkOffsetCoord *coc, int *surfaceDir) {
ChunkShaderPicking *pickShader = ChunkShaderPicking::Make();
pickShader->EnableProgram();
pickShader->View(gViewMatrix);
pickShader->Projection(gProjectionMatrix);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Use black sky for picking
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawLandscape(0, DL_Picking);
pickShader->DisableProgram();
unsigned char pixel[4];
glReadPixels(x,gViewport[3] - y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel[0]);
if (pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 0)
return 0; // Click on the sky, which is drawn black. No block will have this value (facing is 1-6).
PickingData coding;
coding.rgb[0] = pixel[0]; coding.rgb[1] = pixel[1]; coding.rgb[2] = pixel[2]; // Compensate for transformation in shader.
#if 0
gMsgWindow.Add("Pick (%d,%d,%d) facing %d, d(%d,%d,%d)", coding.bitmap.x, coding.bitmap.y, coding.bitmap.z,
coding.bitmap.facing, coding.bitmap.dx-1, coding.bitmap.dy-1, coding.bitmap.dz-1);
gMsgWindow.Add("RGB: %d,%d,%d", coding.rgb[0], coding.rgb[1], coding.rgb[2]);
#endif
ChunkCoord cc;
gPlayer.GetChunkCoord(&cc);
cc.x += coding.bitmap.dx-1;
cc.y += coding.bitmap.dy-1;
cc.z += coding.bitmap.dz-1;
chunk *cp = ChunkFind(&cc, false);
coc->x = coding.bitmap.x; coc->y = coding.bitmap.y; coc->z = coding.bitmap.z;
if (surfaceDir)
*surfaceDir = coding.bitmap.facing;
checkError("gameDialog::FindSelectedSurface");
return cp;
}
ChunkCoord gRequestActivatorChunk;
int gRequestActivatorX, gRequestActivatorY, gRequestActivatorZ;
void gameDialog::AttachBlockToSurface(int row, int col) {
ChunkOffsetCoord coc;
int surfaceDir = 0;
chunk *cp = FindSelectedSurface(row, col, &coc, &surfaceDir);
if (cp == 0)
return;
// Tricky thing is that the 'coc' is not the place we want to place the new block.
// It is the surface of a current existing block. Depending on the way
// that the surface face, we get the actual block address.
int x=coc.x, y=coc.y, z=coc.z;
switch(surfaceDir) {
case 0:
return; // Not valid
case CH_TopFace:
z++;
break;
case CH_BottomFace:
z--;
break;
case CH_LeftFace:
x--;
break;
case CH_RightFace:
x++;
break;
case CH_FrontFace:
y--;
break;
case CH_BackFace:
y++;
break;
}
// It may be that the new block is outside chunk 'cp'.
ChunkCoord cc = cp->cc;
if (x < 0) {
cc.x--;
x += CHUNK_SIZE;
}
if (y < 0) {
cc.y--;
y += CHUNK_SIZE;
}
if (z < 0) {
cc.z--;
z += CHUNK_SIZE;
}
if (x == CHUNK_SIZE) {
cc.x++;
x -= CHUNK_SIZE;
}
if (y == CHUNK_SIZE) {
cc.y++;
y -= CHUNK_SIZE;
}
if (z == CHUNK_SIZE) {
cc.z++;
z -= CHUNK_SIZE;
}
// It may be that cc.z is now above the roof of the world. This will be handled by the server, that
// will not allow blocks to be added. That way, the client doesn't have to know how high the world is.
// gDebugWindow.Add("AttachBlockToSurface: chunk (%d,%d,%d) at (%d,%d,%d)\n", cc.x, cc.y, cc.z, x, y, z);
unsigned char b[19];
b[0] = sizeof b;
b[1] = 0;
b[2] = CMD_BLOCK_UPDATE;
EncodeUint32(b+3, cc.x);
EncodeUint32(b+7, cc.y);
EncodeUint32(b+11, cc.z);
b[15] = x;
b[16] = y;
b[17] = z;
b[18] = fBuildingBlocks->CurrentBlockType();
SendMsg(b, sizeof b);
if (fBuildingBlocks->CurrentBlockType() == BT_Text) {
gRequestActivatorChunk = cc;
gRequestActivatorX = x;
gRequestActivatorY = y;
gRequestActivatorZ = z;
}
}
// The player clicked on an object. Depending on mode, we either
// allow for rebuilding the environment, or attack monsters.
void gameDialog::ClickOnObject(int x, int y) {
Object *op = this->FindSelectedObject(x, y);
fSelectedObject = op;
}
// The player clicked one something. Depending on mode, we either
// allow for rebuilding the environment, or attack monsters.
void gameDialog::ClickOnBlock(int x, int y) {
static int prevX = -1, prevY = -1;
if (prevX == x && prevY == y)
return; // Ignore multiple clicks on the same block
prevX = x; prevY = y;
ChunkOffsetCoord coc;
chunk *cp = this->FindSelectedSurface(x, y, &coc, 0);
if (cp == 0)
return;
// Use the block that the surface belongs to
// gDebugWindow.Add("Pick: object (%d,%d,%d) at (%d,%d,%d)\n", cp->cc.x, cp->cc.y, cp->cc.z, coc.x, coc.y, coc.z);
unsigned char b[18];
b[0] = sizeof b;
b[1] = 0;
b[2] = CMD_HIT_BLOCK;
EncodeUint32(b+3, cp->cc.x);
EncodeUint32(b+7, cp->cc.y);
EncodeUint32(b+11, cp->cc.z);
b[15] = coc.x; // TODO: conversion from unsigned short to unsigned char.
b[16] = coc.y;
b[17] = coc.z;
SendMsg(b, sizeof b);
// printf("Click on block %d, %d, %d\n", coc.x, coc.y , coc.z);
}
static bool sTurning = false;
int xStartTurn = 0, yStartTurn = 0;
float angleHorStartTurn = 0.0f, angleVertStartTurn;
void handleMouseActiveMotion(int x, int y) {
if (gGameDialog.fShowInventory) {
sTurning = false;
} else if (sTurning) {
int deltax = xStartTurn - x;
int deltay = yStartTurn - y;
// printf("Turning dx %d dy %d\n", deltax, deltay);
_angleHor = angleHorStartTurn + deltax/mouseSens;
if (_angleHor > 360.0) _angleHor -= 360.0f;
if (_angleHor < 0.0f) _angleHor += 360.0f;
_angleVert = angleVertStartTurn + deltay/mouseSens;
if (_angleVert > 90.0f) _angleVert = 90.0f;
if (_angleVert < -90.0f) _angleVert = -90.0f;
unsigned char b[7];
unsigned short angleHorRad = (unsigned short)(_angleHor / 360.0f * 2.0f * PI * 100);
signed short angleVertRad = (unsigned short)(_angleVert / 360.0f * 2.0f * PI * 100);
// printf("Dir (%f, %f), (%d, %d)\n", _angleHor, _angleVert, angleHorRad, angleVertRad);
b[0] = sizeof b;
b[1] = 0;
b[2] = CMD_SET_DIR;
EncodeUint16(b+3, angleHorRad);
EncodeUint16(b+5, angleVertRad);
// DumpBytes(b, sizeof b);
SendMsg(b, sizeof b);
gPlayer.fAngleHor = _angleHor; // Update player data with current looking direction
gPlayer.fAngleVert = _angleVert;
}
}
void gameDialog::handleMouse(int button, int action) {
int x, y;
glfwGetMousePos(&x, &y);
if (fShowInventory) {
// Override the usual mouse handling
bool close = gInventory.HandleMouseClick(button, action, x, y);
if (close) {
fShowInventory = false;
gMsgWindow.SetAlternatePosition(0,0,false);
}
return;
} else if (fShowMainDialog && !fHideDialog) {
if (action == GLFW_RELEASE) {
fShowMainDialog = dialog::DispatchClick();
}
return;
}
// printf("Mouse function: button %d, state %d, at (%d,%d)\n", button, action, x, y);
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
if (action == GLFW_PRESS) {
xStartTurn = x;
yStartTurn = y;
angleHorStartTurn = _angleHor;
angleVertStartTurn = _angleVert;
sTurning = true;
} else {
sTurning = false;
}
} else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS && gMode.Get() == GameMode::GAME) {
ClickOnObject(x, y);
} else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS && gMode.Get() == GameMode::TELEPORT) {
int x, y;
glfwGetMousePos(&x, &y);
const ChunkCoord *cc = TeleportClick(fHealthBar, _angleHor, renderViewAngle, x, y, true);
if (cc != 0) {
// printf("TP to chunk %d,%d,%d\n", cc->x, cc->y, cc->z);
unsigned char b[6];
b[0] = 6;
b[1] = 0;
b[2] = CMD_TELEPORT;
b[3] = cc->x & 0xFF;
b[4] = cc->y & 0xFF;
b[5] = cc->z & 0xFF;
SendMsg(b, sizeof b);
// this->RequestEffect(EFFECT_ZOOM1); // TODO: Doesn't look good.
}
}
}
void handleResize(int w, int h) {
// printf("::handleResize: %dx%d", w, h);
if (w == 0 || h == 0)
return; // This will happen when window is iconified.
gameDialog::sfCurrentGameDialog->handleResize(w, h);
}
glm::mat4 gProjectionMatrix;
void gameDialog::handleResize(int w, int h) {
float aspectRatio = (float)w / (float)h;
// In full screen mode, the window is stretched to match the desktop mode.
if (Options::fgOptions.fFullScreen)
aspectRatio = gDesktopAspectRatio;
gProjectionMatrix = glm::perspective(renderViewAngle, aspectRatio, 0.01f, maxRenderDistance); // Create our perspective projection matrix
gTranspShader.EnableProgram();
gTranspShader.Projection((float)w, (float)h);
gTranspShader.DisableProgram();
glViewport(0, 0, w, h);
gViewport = glm::vec4(0.0f, 0.0f, (float)w, (float)h );
gDrawFont.UpdateProjection();
fRenderControl->Resize(w, h);
}
static void handleCharacter(int character, int action) {
// printf("handleCharacter %d, action %d\n", character, action);
gGameDialog.HandleCharacter(character, action);
}
static void handleKeypress(int key, int action) {
if (action == GLFW_PRESS)
gGameDialog.HandleKeyPress(key, action);
else
gGameDialog.HandleKeyRelease(key, action);
}
void gameDialog::HandleCharacter(int key, int action) {
if (fShowMainDialog && !fHideDialog) {
dialog::DispatchChar(key);
return;
}
if (!fEnterDebugText)
return;
if (key != 0 && action == GLFW_PRESS && fDebugTextLength+3 < sizeof fDebugText) {
// Key 0 is used to force an update of the shown string.
fDebugText[fDebugTextLength+3] = key;
fDebugTextLength++;
}
char buff[1000];
#ifdef WIN32
_snprintf(buff, sizeof buff-1, "%.*s_", fDebugTextLength, (char*)fDebugText+3);
#else
snprintf(buff, sizeof buff-1, "%.*s_", fDebugTextLength, (char*)fDebugText+3);
#endif
gDrawFont.vsfl.prepareSentence(fInputPromptSentence, buff);
// gDebugWindow.Add((char *)fDebugText+3);
}
string prevCommand("/");
void gameDialog::HandleKeyPress(int key, int action) {
if (fShowMainDialog && !fHideDialog && key != GLFW_KEY_ESC) {
if (action == GLFW_PRESS) {
dialog::DispatchKey(key);
}
return;
}
if (key >= GLFW_KEY_F1 && key <= GLFW_KEY_F25) {
gInventory.UseObjectFunctionKey(key);
return;
}
if (fEnterDebugText) {
// Override the switch, and catch all characters.
switch (key) {
case GLFW_KEY_KP_ENTER:
case GLFW_KEY_ENTER: // ENTER key
// Done.
if (fDebugTextLength > 0) {
fDebugText[fDebugTextLength+3] = 0;
const char *begin = (char *)fDebugText+3;
const char *end = strchr(begin, ' ');
if (end == 0)
prevCommand = string(begin, fDebugTextLength);
else
prevCommand = string(begin, end-begin+1); // Including the space character
// printf("Prev command: '%s'\n", prevCommand.c_str());
// There is something to send. Make a proper command of it.
fDebugText[0] = fDebugTextLength+3;
fDebugText[1] = 0;
fDebugText[2] = CMD_DEBUG;
SendMsg(fDebugText, fDebugTextLength+3);
}
// Fall through!
case GLFW_KEY_ESC: // Escape key, stop composing a command.
glfwDisable(GLFW_KEY_REPEAT);
fEnterDebugText = false;
break;
case GLFW_KEY_BACKSPACE: // Back space
if (fDebugTextLength > 0) {
if (strncmp((const char *)fDebugText+3, prevCommand.c_str(), fDebugTextLength) == 0)
fDebugTextLength = 1; // Keep the leading '/' only
else
fDebugTextLength--;
}
this->HandleCharacter(0,0); // Update the shown string
break;
}
return;
}
int x, y;
glfwGetMousePos(&x, &y);
switch (key) {
case GLFW_KEY_KP_0:
// if (gPlayer.fAdmin > 0)
gToggleTesting = !gToggleTesting;
break;
case GLFW_KEY_ESC:
if (fDrawMap)
fDrawMap = false;
else if (fShowInventory) {
fShowInventory = false;
gMsgWindow.SetAlternatePosition(0,0,false);
} else {
if (fShowMainDialog) {
fHideDialog = !fHideDialog;
}
fShowMainDialog = true;
this->ClearForDialog();
}
break;
case 'C':
if (gMode.Get() == GameMode::CONSTRUCT) { // Toggle construction mode
gMode.Set(GameMode::GAME);
gShowFramework = false;
} else {
gMode.Set(GameMode::CONSTRUCT);
}
if (gPlayer.fKnownPosition) {
ChunkCoord player_cc;
// Force the current chunk to be redrawn, adapted for construction mode
gPlayer.GetChunkCoord(&player_cc);
for (int dx=-1; dx<2; dx++) for (int dy=-1; dy<2; dy++) for (int dz=-1; dz < 2; dz++) {
ChunkCoord cc = player_cc;
cc.x += dx; cc.y += dy; cc.z += dz;
chunk *cp = ChunkFind(&cc, true);
cp->SetDirty(true);
}
}
break;
case GLFW_KEY_TAB:
if (fDrawMap && gDebugOpenGL) {
// A debug feature to iterate through various interesting bitmaps
if (!sShowAlternateBitmap) {
sShowAlternateBitmap = true;
sTextureIterator = gDebugTextures.begin();
} else
sTextureIterator++;
if (sTextureIterator == gDebugTextures.end()) {
sShowAlternateBitmap = false;
}
break;
}
// Find next monster after the selected one.
fSelectedObject = gMonsters.GetNext(fSelectedObject);
break;
case GLFW_KEY_LALT: // ALT key
if (gMode.Get() == GameMode::CONSTRUCT) {
gShowFramework = true; // Only when in construction mode
} else if (gMode.Get() == GameMode::GAME && gPlayer.fAdmin > 0) {
gMode.Set(GameMode::TELEPORT);
gAdminTP = true;
}
break;
case '1': // Autoattack
if (!fSelectedObject) {
// Find next monster after the selected one.
fSelectedObject = gMonsters.GetNext(fSelectedObject);
}
if (fSelectedObject) { // Initiate attack on a monster, if any.
unsigned char b[] = { 7, 0, CMD_ATTACK_MONSTER, 0, 0, 0, 0 };
EncodeUint32(b+3, fSelectedObject->GetId());
SendMsg(b, sizeof b);
}
break;
case '2': { // Heal self
unsigned char b[] = { 0x04, 0x00, CMD_PLAYER_ACTION, UserActionHeal };
SendMsg(b, sizeof b);
break;
}
case '3': { // Instant extra attack
unsigned char b[] = { 0x04, 0x00, CMD_PLAYER_ACTION, UserActionCombAttack };
SendMsg(b, sizeof b);
break;
}
case 'I': {
// Toggle inventory screen
fShowInventory = !fShowInventory;
if (!fShowInventory)
gMsgWindow.SetAlternatePosition(0,0,false);
break;
}
case 'T':
dumpGraphicsMemoryStats();
gShowMsgWindow = !gShowMsgWindow;
break;
case GLFW_KEY_KP_ENTER:
case GLFW_KEY_ENTER: // ENTER key
fEnterDebugText = true;
glfwEnable(GLFW_KEY_REPEAT);
// gDebugWindow.Add("Starting to capture debug message");
strcpy((char *)fDebugText+3, prevCommand.c_str());
fDebugTextLength = prevCommand.length();
fDebugText[fDebugTextLength+3] = 0;
this->HandleCharacter(0,0); // Update the shown string
break;
case GLFW_KEY_SPACE: { // space key
unsigned char b[] = { 0x03, 0x00, CMD_JUMP };
SendMsg(b, sizeof b);
gSoundControl.RequestSound(SoundControl::SPlayerJump);
break;
}
case 'W':
case GLFW_KEY_UP:
if (!fMovingFwd) {
fMovingFwd = true;
unsigned char b[] = { 0x03, 0x00, 10 };
SendMsg(b, sizeof b);
// printf("Start fwd\n");
}
break;
case 'A':
case GLFW_KEY_LEFT:
if (!fMovingLeft) {
fMovingLeft = true;
unsigned char b[] = { 0x03, 0x00, 14 };
SendMsg(b, sizeof b);
// printf("Start left\n");
}
break;
case 'S':
case GLFW_KEY_DOWN:
if (!fMovingBwd) {
fMovingBwd = true;
unsigned char b[] = { 0x03, 0x00, 12 };
SendMsg(b, sizeof b);
// printf("Start bwd\n");
}
break;
case 'D':
case GLFW_KEY_RIGHT:
if (!fMovingRight) {
fMovingRight = true;
unsigned char b[] = { 0x03, 0x00, 16 };
SendMsg(b, sizeof b);
// printf("Start right\n");
}
break;
case GLFW_KEY_INSERT: // Insert, closer to delete on a full keyboard
case 'B':
if (gMode.Get() == GameMode::CONSTRUCT) {
this->AttachBlockToSurface(x, y);
}
break;
case GLFW_KEY_DEL: // Delete
case 'V': // Closer to B when building
if (gMode.Get() == GameMode::CONSTRUCT) {
ClickOnBlock(x, y);
}
break;
case GLFW_KEY_KP_SUBTRACT:
switch(fCalibrationMode) {
case CALIB_AMBIENT:
Options::fgOptions.fAmbientLight -= 1;
gMsgWindow.Add("Ambient light: %f", Options::fgOptions.fAmbientLight/100.0);
break;
case CALIB_EXPOSURE:
Options::fgOptions.fExposure /= 1.1f;
gMsgWindow.Add("Exposure: %f", Options::fgOptions.fExposure);
break;
case CALIB_WHITE_POINT:
Options::fgOptions.fWhitePoint /= 1.1f;
gMsgWindow.Add("White point: %f", Options::fgOptions.fWhitePoint);
break;
case CALIB_NONE:
break;
}
break;
case '-': {
maxRenderDistance -= 5.0;
if (maxRenderDistance < 5.0f && gDebugOpenGL)
maxRenderDistance = 5.0f;
if (maxRenderDistance < 40.0f && !gDebugOpenGL)
maxRenderDistance = 40.0f;
handleResize(gViewport[2], gViewport[3]);
gMsgWindow.Add("Viewing distance: %f m", maxRenderDistance/2);
break;
}
case GLFW_KEY_KP_ADD:
switch(fCalibrationMode) {
case CALIB_AMBIENT:
Options::fgOptions.fAmbientLight += 1;
gMsgWindow.Add("Ambient light: %f", Options::fgOptions.fAmbientLight/100.0);
break;
case CALIB_EXPOSURE:
Options::fgOptions.fExposure *= 1.1f;
gMsgWindow.Add("Exposure: %f", Options::fgOptions.fExposure);
break;
case CALIB_WHITE_POINT:
Options::fgOptions.fWhitePoint *= 1.1f;
gMsgWindow.Add("White point: %f", Options::fgOptions.fWhitePoint);
break;
case CALIB_NONE:
break;
}
break;
case '+': {
// #255 long distances are not handled very well by neither server nor client
maxRenderDistance += 5.0;
if (maxRenderDistance > MAXRENDERDISTANCE)
maxRenderDistance = MAXRENDERDISTANCE;
handleResize(gViewport[2], gViewport[3]);
gMsgWindow.Add("Viewing distance: %f m", maxRenderDistance/2);
break;
}
case 'M': {
fDrawMap = !fDrawMap;
break;
}
case '\'': {
if (gPlayer.fAdmin > 0)
fUsingTorch = !fUsingTorch;
}
default:
gMsgWindow.Add("Unknown key '%d'", key);
break;
}
gGameDialog.UpdateRunningStatus(false);
}
void gameDialog::HandleKeyRelease(int key, int action) {
if (fShowMainDialog && !fHideDialog) {
if (action == GLFW_PRESS) {
dialog::DispatchKey(key);
}
return;
}
switch (key) {
case 'W':
case 283:
if (fMovingFwd) {
fMovingFwd = false;
unsigned char b[] = { 0x03, 0x00, 11 };
SendMsg(b, sizeof b);
// printf("stop fwd\n");
}
break;
case 'A':
case 285:
if (fMovingLeft) {
fMovingLeft = false;
unsigned char b[] = { 0x03, 0x00, 15 };
SendMsg(b, sizeof b);
// printf("stop left\n");
}
break;
case 'S':
case 284:
if (fMovingBwd) {
fMovingBwd = false;
unsigned char b[] = { 0x03, 0x00, 13 };
SendMsg(b, sizeof b);
// printf("stop bwd\n");
}
break;
case 'D':
case 286:
if (fMovingRight) {
fMovingRight = false;
unsigned char b[] = { 0x03, 0x00, 17 };
SendMsg(b, sizeof b);
// printf("stop right\n");
}
break;
case GLFW_KEY_LALT: // ALT key
if (gMode.Get() == GameMode::CONSTRUCT)
gShowFramework = false; // Only when in construction mode
else if (gMode.Get() == GameMode::TELEPORT && gPlayer.fAdmin > 0 && gAdminTP) {
gMode.Set(GameMode::GAME);
gAdminTP = false;
}
break;
}
gGameDialog.UpdateRunningStatus(false);
}
void gameDialog::ClearSelection(void) {
fSelectedObject = 0;
}
// This function is called every time the player gets aggro
void gameDialog::AggroFrom(Object *o) {
// It may be called from more than one monster. For now, use the information to set a selection for the first monster.
if (fSelectedObject == 0)
fSelectedObject = o;
}
static void revive(void) {
char msg[] = " /revive";
msg[0] = 10;
msg[1] = 0;
msg[2] = CMD_DEBUG;
SendMsg((unsigned char *)msg, (sizeof msg) - 1);
}
glm::mat4 gViewMatrix; // Store the view matrix
void gameDialog::render() {
static bool first = true; // Only true first time function is called
// Clear list of special effects. It will be added again automatically every frame
gShadows.Clear();
gFogs.Clear();
gPlayer.UpdatePositionSmooth();
// Can't select objects that are dead
if (fSelectedObject && (fSelectedObject->IsDead() || !fSelectedObject->InGame()))
ClearSelection();
glm::vec3 playerOffset = gPlayer.GetOffsetToChunk();
gViewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -fCameraDistance));
gViewMatrix = glm::rotate(gViewMatrix, _angleVert, glm::vec3(1.0f, 0.0f, 0.0f));
gViewMatrix = glm::rotate(gViewMatrix, _angleHor, glm::vec3(0.0f, 1.0f, 0.0f));
gViewMatrix = glm::translate(gViewMatrix, -playerOffset);
glm::mat4 T1 = glm::translate(glm::mat4(1), playerOffset);
glm::mat4 R1 = glm::rotate(glm::mat4(1), _angleVert, glm::vec3(-1.0f, 0.0f, 0.0f));
glm::mat4 R2 = glm::rotate(glm::mat4(1), _angleHor, glm::vec3(0.0f, -1.0f, 0.0f));
glm::mat4 T2 = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, fCameraDistance));
glm::vec4 camera = T1 * R2 * R1 * T2 * glm::vec4(0,0,0,1);
gUniformBuffer.Camera(camera);
gDrawnQuads = 0;
gNumDraw = 0;
gUniformBuffer.Update();
if (first) {
gBillboard.InitializeTextures(fShader);
}
fRenderControl->Draw(fSelectedObject, fUnderWater, this->ThirdPersonView(), fSelectedObject, fDrawMap && !gDebugOpenGL, fMapWidth);
//=========================================================================
// Various effects drawn after the deferred shader
//=========================================================================
if (!fDrawMap) {
this->DrawPlayerStats();
if (fShowWeapon && gMode.Get() != GameMode::CONSTRUCT && !fShowInventory && !this->ThirdPersonView())
this->DrawWeapon();
static bool wasDead = false;
if (gPlayer.IsDead() && !wasDead) {
wasDead = true;
fShowMainDialog = true;
fHideDialog = false;
dialog::DispatchDraw(fDrawTexture, 1.0f, new MessageDialog("Oops", "You are dead.\n\nYou will be revived, and transported back to your starting place.\n\nThe place can be changed with a scroll of resurrection point.", revive));
this->ClearForDialog();
} else if (fShowInventory)
gInventory.DrawInventory(fDrawTexture);
else if (fShowMainDialog)
dialog::DispatchDraw(fDrawTexture, fHideDialog ? 0.5f : 1.0f);
else if (sgPopup.length() > 0) {
// There are some messages that shall be shown in a popup dialog.
fShowMainDialog = true;
fHideDialog = false;
dialog::DispatchDraw(fDrawTexture, 1.0f, new MessageDialog(sgPopupTitle, sgPopup, 0));
sgPopupTitle = "Ephenation"; // Reset to default.
sgPopup.clear();
this->ClearForDialog();
}
if (!gPlayer.IsDead())
wasDead = false;
DrawCompassRose();
}
if (fDrawMap && gDebugOpenGL) {
// Very inefficient algorithm, computing the map every frame.
std::unique_ptr<Map> map(new Map);
float alpha = 1.0f;
if (!sShowAlternateBitmap)
sTextureIterator = gDebugTextures.begin();
glBindTexture(GL_TEXTURE_2D, *sTextureIterator); // Override
map->Draw(alpha);
}
if (fSelectedObject) {
fSelectedObject->RenderHealthBar(fHealthBar, _angleHor);
}
gOtherPlayers.RenderPlayerStats(fHealthBar, _angleHor);
bool newHealing = false;
if (gPlayer.fFlags & UserFlagHealed) {
newHealing = true;
gPlayer.fFlags &= ~UserFlagHealed;
}
this->DrawHealingAnimation(newHealing);
char buff[1000]; // TODO: Ugly way to create dynamic strings
static double slAverageFps = 0.0;
double tm = gCurrentFrameTime;
static double prevTime = 0.0;
double deltaTime = tm - prevTime;
// Use a decay filter on the FPS
slAverageFps = 0.97*slAverageFps + 0.03/deltaTime;
prevTime = tm;
if (gMode.Get() == GameMode::TELEPORT) {
TeleportClick(fHealthBar, _angleHor, renderViewAngle, 0, 0, false);
}
//=========================================================================
// Various text messages
//=========================================================================
{
// A big chunk with text drawing. But first some background to improve contrast.
int yOffset = Options::fgOptions.fFontSize*2;
float totalWinHeight = gViewport[3]; // Measured in pixels
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-1.0f, 1.0f - yOffset*2/totalWinHeight, 0.0f));
float yScale = yOffset*2/totalWinHeight;
model = glm::scale(model, glm::vec3(2.0f, yScale, 1.0f));
this->fHealthBar->DrawSquare(model, 0.0f, 0.0f, 0.0f, 0.3f); // Draw a black square, almost transparent, as a background
gDrawFont.Enable();
gDrawFont.UpdateProjection();
sprintf(buff, "Level %ld, Hp %d%%, Exp %d%%", gPlayer.fLevel, (int)(gPlayer.fHp * 100), (int)(gPlayer.fExp * 100));
gDrawFont.vsfl.prepareSentence(fPlayerStatusSentence, buff);
int w = int(strlen(buff)*Options::fgOptions.fFontSize*0.8f); // An approximation
gDrawFont.SetOffset(gViewport[2]-w, 0);
gDrawFont.vsfl.renderSentence(fPlayerStatusSentence);
if (gCurrentPing == 0.0)
sprintf(buff, "Triangles %7d, draw calls %d. Fps %03d", gDrawnQuads, gNumDraw, int(slAverageFps));
else
sprintf(buff, "Triangles %7d, draw calls %d. Fps %03d, ping %.1f ms", gDrawnQuads, gNumDraw, int(slAverageFps), gCurrentPing*1000.0);
gDrawFont.SetOffset(0,0);
gDrawFont.vsfl.prepareSentence(fFPSsentence, buff);
gDrawFont.vsfl.renderSentence(fFPSsentence);
if (gMode.Get() == GameMode::CONSTRUCT) {
ChunkCoord cc;
gPlayer.GetChunkCoord(&cc);
const chunk *cp = ChunkFind(&cc, false);
unsigned int uid = 1000000;
shared_ptr<ChunkBlocks> cb;
if (cp)
cb = cp->fChunkBlocks;
if (cb)
uid = cb->fOwner;
gDrawFont.SetOffset(0,float(yOffset));
sprintf(buff, "Construction mode, Chunk (%d,%d,%d) offset: %.1f,%.1f,%.1f, coord(%.1f,%.1f,%.1f) owner %d",
cc.x, cc.y, cc.z, playerOffset.x, playerOffset.y, playerOffset.z, gPlayer.x/100.0, gPlayer.y/100.0, gPlayer.z/100.0, uid);
gDrawFont.vsfl.renderAndDiscard(buff);
}
if (fEnterDebugText) {
int bottomLine = gViewport[3]-25;
gDrawFont.SetOffset(0, float(bottomLine));
gDrawFont.vsfl.renderSentence(fInputPromptSentence);
}
gDrawFont.Disable();
gScrollingMessages.Update();
}
if (!fDrawMap && !fShowInventory && (!fShowMainDialog || fHideDialog)) {
if (gShowMsgWindow)
gMsgWindow.Render();
if (gMode.Get() == GameMode::CONSTRUCT)
fBuildingBlocks->Draw(gProjectionMatrix);
}
if (gDebugOpenGL) {
checkError("gameDialog::render debug", false);
static double prevPrint = 0.0;
if (gCurrentFrameTime > prevPrint + 5.0) {
if (gToggleTesting)
WorstTime::Report();
else
TimeMeasure::Report();
prevPrint = gCurrentFrameTime;
}
}
chunk::DegradeBusyList_gl();
first = false;
}
void gameDialog::init(void) {
fRenderControl.reset(new RenderControl);