-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMap.cpp
2932 lines (2743 loc) · 118 KB
/
CMap.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) 2009 - 2021 Marc Vester (XaserLE)
// Copyright (C) 2009 - 2021 Settlers Freaks <sf-team at siedler25.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "CMap.h"
#include "CGame.h"
#include "CIO/CFile.h"
#include "CIO/CFont.h"
#include "CSurface.h"
#include "callbacks.h"
#include "globals.h"
#include "gameData/LandscapeDesc.h"
#include "gameData/TerrainDesc.h"
#include <cassert>
#include <iostream>
#include <string>
void bobMAP::setName(const std::string& newName)
{
name = newName;
if(name.size() > 20)
name.resize(20u);
}
void bobMAP::setAuthor(const std::string& newAuthor)
{
author = newAuthor;
if(author.size() > 20)
author.resize(20);
}
void bobMAP::initVertexCoords()
{
for(unsigned j = 0; j < height; j++)
{
for(unsigned i = 0; i < width; i++)
{
MapNode& curVertex = getVertex(i, j);
curVertex.VertexX = i;
curVertex.VertexY = j;
}
}
updateVertexCoords();
}
void bobMAP::updateVertexCoords()
{
width_pixel = width * TRIANGLE_WIDTH;
height_pixel = height * TRIANGLE_HEIGHT;
Sint32 b = 0;
for(unsigned j = 0; j < height; j++)
{
Sint32 a;
if(j % 2u == 0u)
a = TRIANGLE_WIDTH / 2u;
else
a = TRIANGLE_WIDTH;
for(unsigned i = 0; i < width; i++)
{
MapNode& curVertex = getVertex(i, j);
curVertex.x = a;
curVertex.y = b - TRIANGLE_INCREASE * (curVertex.h - 0x0A);
curVertex.z = TRIANGLE_INCREASE * (curVertex.h - 0x0A);
a += TRIANGLE_WIDTH;
}
b += TRIANGLE_HEIGHT;
}
}
CMap::CMap(const boost::filesystem::path& filepath)
{
constructMap(filepath);
}
CMap::~CMap()
{
destructMap();
}
void CMap::constructMap(const boost::filesystem::path& filepath, int width, int height, MapType type,
TriangleTerrainType texture, int border, int border_texture)
{
map = nullptr;
Surf_Map.reset();
Surf_RightMenubar.reset();
displayRect.left = 0;
displayRect.top = 0;
displayRect.setSize(global::s2->GameResolution);
if(!filepath.empty())
{
map.reset((bobMAP*)CFile::open_file(filepath, WLD));
if(map)
filepath_ = filepath;
}
if(!map)
{
filepath_.clear();
map = generateMap(width, height, type, texture, border, border_texture);
}
// load the right MAP0x.LST for all pictures
loadMapPics();
CSurface::get_nodeVectors(*map);
// for safety recalculate build and shadow data and test if fishes and water is correct
for(int i = 0; i < map->height; i++)
{
for(int j = 0; j < map->width; j++)
{
modifyBuild(j, i);
modifyShading(j, i);
modifyResource(j, i);
}
}
Surf_Map.reset();
active = true;
Vertex_ = {10, 10};
RenderBuildHelp = false;
RenderBorders = true;
BitsPerPixel = 32;
MouseBlit = correctMouseBlit(Vertex_);
ChangeSection_ = 1;
lastChangeSection = ChangeSection_;
ChangeSectionHexagonMode = true;
VertexFillRSU = true;
VertexFillUSD = true;
VertexFillRandom = false;
VertexActivityRandom = false;
// calculate the maximum number of vertices for cursor
VertexCounter = 0;
for(int i = -MAX_CHANGE_SECTION; i <= MAX_CHANGE_SECTION; i++)
{
if(abs(i) % 2 == 0)
for(int j = -MAX_CHANGE_SECTION; j <= MAX_CHANGE_SECTION; j++)
VertexCounter++;
else
for(int j = -MAX_CHANGE_SECTION; j <= MAX_CHANGE_SECTION - 1; j++)
VertexCounter++;
}
Vertices.resize(VertexCounter);
calculateVertices();
setupVerticesActivity();
mode = EDITOR_MODE_HEIGHT_RAISE;
lastMode = EDITOR_MODE_HEIGHT_RAISE;
modeContent = 0x00;
modeContent2 = 0x00;
modify = false;
MaxRaiseHeight = 0x3C;
MinReduceHeight = 0x00;
saveCurrentVertices = false;
undoBuffer.clear();
redoBuffer.clear();
// we count the players, cause the original editor writes number of players to header no matter if they are set or
// not
int CountPlayers = 0;
// now for internal reasons save all players to a new array, also players with number greater than 7
// initalize the internal array
for(int i = 0; i < MAXPLAYERS; i++)
{
PlayerHQx[i] = 0xFFFF;
PlayerHQy[i] = 0xFFFF;
}
// find out player positions
for(int y = 0; y < map->height; y++)
{
for(int x = 0; x < map->width; x++)
{
MapNode& curVertex = map->getVertex(x, y);
if(curVertex.objectInfo == 0x80)
{
CountPlayers++;
// objectType is the number of the player
if(curVertex.objectType < MAXPLAYERS)
{
PlayerHQx[curVertex.objectType] = x;
PlayerHQy[curVertex.objectType] = y;
// for compatibility with original settlers 2 save the first 7 player positions to the map header
// NOTE: this is already done by map loading, but to prevent inconsistence we do it again this way
if(curVertex.objectType < 0x07)
{
map->HQx[curVertex.objectType] = x;
map->HQy[curVertex.objectType] = y;
}
}
}
}
}
map->player = CountPlayers;
HorizontalMovementLocked = false;
VerticalMovementLocked = false;
DescIdx<LandscapeDesc> lt(0);
for(DescIdx<LandscapeDesc> i(0); i.value < global::worldDesc.landscapes.size(); i.value++)
{
if(global::worldDesc.get(i).s2Id == map->type)
{
lt = i;
break;
}
}
for(DescIdx<TerrainDesc> i(0); i.value < global::worldDesc.terrain.size(); i.value++)
{
const TerrainDesc& t = global::worldDesc.get(i);
if(t.landscape == lt)
{
if(map->s2IdToTerrain.size() <= t.s2Id)
map->s2IdToTerrain.resize(t.s2Id + 1);
map->s2IdToTerrain[t.s2Id] = i;
}
}
}
void CMap::destructMap()
{
// free all surfaces that MAP0x.LST needed
unloadMapPics();
undoBuffer.clear();
redoBuffer.clear();
Surf_Map.reset();
Surf_RightMenubar.reset();
// free vertex array
Vertices.clear();
// free map structure memory
map.reset();
filepath_.clear();
}
std::unique_ptr<bobMAP> CMap::generateMap(int width, int height, MapType type, TriangleTerrainType texture, int border,
int border_texture)
{
auto myMap = std::make_unique<bobMAP>();
myMap->setName("Ohne Namen");
myMap->width = width;
myMap->width_old = width;
myMap->height = height;
myMap->height_old = height;
myMap->type = type;
myMap->player = 0;
myMap->setAuthor("Niemand");
for(int i = 0; i < 7; i++)
{
myMap->HQx[i] = 0xFFFF;
myMap->HQy[i] = 0xFFFF;
}
myMap->vertex.resize(myMap->width * myMap->height);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
{
MapNode& curVertex = myMap->getVertex(i, j);
curVertex.h = 0x0A;
if((j < border || myMap->height - j <= border) || (i < border || myMap->width - i <= border))
{
curVertex.rsuTexture = border_texture;
curVertex.usdTexture = border_texture;
} else
{
curVertex.rsuTexture = texture;
curVertex.usdTexture = texture;
}
// initialize all other blocks -- outcommented blocks are recalculated at map load
curVertex.road = 0x00;
curVertex.objectType = 0x00;
curVertex.objectInfo = 0x00;
curVertex.animal = 0x00;
curVertex.unknown1 = 0x00;
// curVertex.build = 0x00;
curVertex.unknown2 = 0x07;
curVertex.unknown3 = 0x00;
// curVertex.resource = 0x00;
// curVertex.shading = 0x00;
curVertex.unknown5 = 0x00;
}
}
myMap->initVertexCoords();
return myMap;
}
void CMap::rotateMap()
{
// we allocate memory for the new triangle field but with x equals the height and y equals the width
std::vector<MapNode> new_vertex(map->vertex.size());
undoBuffer.clear();
redoBuffer.clear();
// copy old to new while permuting x and y
for(int y = 0; y < map->height; y++)
{
for(int x = 0; x < map->width; x++)
{
new_vertex[x * map->height + (map->height - 1 - y)] = map->getVertex(x, y);
}
}
// release old map and point to new
std::swap(map->vertex, new_vertex);
// permute width and height
Uint16 tmp_height = map->height;
Uint16 tmp_height_old = map->height_old;
Uint16 tmp_height_pixel = map->height_pixel;
Uint16 tmp_width = map->width;
Uint16 tmp_width_old = map->width_old;
Uint16 tmp_width_pixel = map->width_pixel;
map->height = tmp_width;
map->height_old = tmp_width_old;
map->height_pixel = tmp_width_pixel;
map->width = tmp_height;
map->width_old = tmp_height_old;
map->width_pixel = tmp_height_pixel;
// permute player positions
// at first the internal array
swap(PlayerHQx, PlayerHQy);
// and now the map array
swap(map->HQx, map->HQy);
// recalculate some values
map->initVertexCoords();
for(int y = 0; y < map->height; y++)
{
for(int x = 0; x < map->width; x++)
{
modifyBuild(x, y);
modifyShading(x, y);
modifyResource(x, y);
CSurface::update_shading(*map, x, y);
}
}
// reset mouse and view position to prevent failures
Vertex_ = {12, 12};
MouseBlit = correctMouseBlit(Vertex_);
calculateVertices();
displayRect.left = 0;
displayRect.top = 0;
}
void CMap::MirrorMapOnXAxis()
{
for(int y = 1; y < map->height / 2; y++)
{
for(int x = 0; x < map->width; x++)
{
map->getVertex(x, map->height - y) = map->getVertex(x, y);
}
}
// recalculate some values
map->initVertexCoords();
for(int y = 0; y < map->height; y++)
{
for(int x = 0; x < map->width; x++)
{
modifyBuild(x, y);
modifyShading(x, y);
modifyResource(x, y);
CSurface::update_shading(*map, x, y);
}
}
}
void CMap::MirrorMapOnYAxis()
{
for(int y = 0; y < map->height; y++)
{
for(int x = 0; x < map->width / 2; x++)
{
if(y % 2 != 0)
{
if(x != map->width / 2 - 1)
map->getVertex(map->width - 2 - x, y) = map->getVertex(x, y);
} else
map->getVertex(map->width - 1 - x, y) = map->getVertex(x, y);
}
}
// recalculate some values
map->initVertexCoords();
for(int y = 0; y < map->height; y++)
{
for(int x = 0; x < map->width; x++)
{
modifyBuild(x, y);
modifyShading(x, y);
modifyResource(x, y);
CSurface::update_shading(*map, x, y);
}
}
}
void CMap::loadMapPics()
{
std::string picFile, palFile;
switch(map->type)
{
case 0:
picFile = "DATA/MAP00.LST";
palFile = "GFX/PALETTE/PAL5.BBM";
break;
case 1:
picFile = "DATA/MAP01.LST";
palFile = "GFX/PALETTE/PAL6.BBM";
break;
case 2:
picFile = "DATA/MAP02.LST";
palFile = "GFX/PALETTE/PAL7.BBM";
break;
default:
picFile = "DATA/MAP00.LST";
palFile = "GFX/PALETTE/PAL5.BBM";
break;
}
CFile::set_palArray(&global::palArray[PAL_MAPxx]);
// load only the palette at this time from MAP0x.LST
std::cout << "\nLoading palette from file: " << picFile << "...";
if(!CFile::open_file(global::gameDataFilePath / picFile, LST, true))
{
std::cout << "failure";
}
// set the right palette
CFile::set_palActual(CFile::get_palArray() - 1);
std::cout << "\nLoading file: " << picFile << "...";
if(!CFile::open_file(global::gameDataFilePath / picFile, LST))
{
std::cout << "failure";
}
// set back palette
// CFile::set_palActual(CFile::get_palArray());
// std::cout << "\nLoading file: DATA/MBOB/ROM_BOBS.LST...";
// if ( !CFile::open_file(global::gameDataFilePath + "DATA/MBOB/ROM_BOBS.LST", LST) )
//{
// std::cout << "failure";
//}
// set back palette
CFile::set_palActual(CFile::get_palArray());
CFile::set_palArray(&global::palArray[PAL_xBBM]);
// load palette file for the map (for precalculated shading)
std::cout << "\nLoading palette from file: " << palFile << "...";
if(!CFile::open_file(global::gameDataFilePath / palFile, BBM, true))
{
std::cout << "failure";
}
}
void CMap::unloadMapPics()
{
for(int i = MAPPIC_ARROWCROSS_YELLOW; i <= MAPPIC_LAST_ENTRY; i++)
{
global::bmpArray[i].surface.reset();
}
// set back bmpArray-pointer, cause MAP0x.LST is no longer needed
CFile::set_bmpArray(&global::bmpArray[MAPPIC_ARROWCROSS_YELLOW]);
// set back palArray-pointer, cause PALx.BBM is no longer needed
CFile::set_palActual(&global::palArray[PAL_IO]);
CFile::set_palArray(&global::palArray[PAL_IO + 1]);
}
void CMap::moveMap(Position offset)
{
displayRect.setOrigin(displayRect.getOrigin() + offset);
// reset coords of displayRects when end of map is reached
if(displayRect.left >= map->width_pixel)
displayRect.move(Position(-map->width_pixel, 0));
else if(displayRect.left <= -static_cast<int>(displayRect.getSize().x))
displayRect.setOrigin(Position(map->width_pixel - displayRect.getSize().x, displayRect.top));
if(displayRect.top >= map->height_pixel)
displayRect.move(Position(0, -map->height_pixel));
else if(displayRect.top <= -static_cast<int>(displayRect.getSize().y))
displayRect.setOrigin(Position(displayRect.left, map->height_pixel - displayRect.getSize().y));
}
void CMap::setMouseData(const SDL_MouseMotionEvent& motion)
{
// following code important for blitting the right field of the map
// Are we scrolling?
if(startScrollPos)
{
assert(motion.state & SDL_BUTTON(3));
Position offset = Position(motion.x, motion.y) - *startScrollPos;
if(HorizontalMovementLocked)
offset.x = 0;
if(VerticalMovementLocked)
offset.y = 0;
moveMap(offset);
// this whole "warping-thing" is to prevent cursor-moving WITHIN the window while user moves over the map
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
SDL_WarpMouseInWindow(nullptr, startScrollPos->x, startScrollPos->y);
SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
}
storeVerticesFromMouse(motion.x, motion.y, motion.state);
}
void CMap::onLeftMouseDown(const Point32& pos)
{ // find out if user clicked on one of the game menu pictures
// we start with lower menubar
const Point32 displaySize(displayRect.getSize());
if(pos.x >= (displaySize.x / 2 - 236) && pos.x <= (displaySize.x / 2 - 199) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the height-mode picture was clicked
mode = EDITOR_MODE_HEIGHT_RAISE;
} else if(pos.x >= (displaySize.x / 2 - 199) && pos.x <= (displaySize.x / 2 - 162) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the texture-mode picture was clicked
mode = EDITOR_MODE_TEXTURE;
callback::EditorTextureMenu(INITIALIZING_CALL);
} else if(pos.x >= (displaySize.x / 2 - 162) && pos.x <= (displaySize.x / 2 - 125) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the tree-mode picture was clicked
mode = EDITOR_MODE_TREE;
callback::EditorTreeMenu(INITIALIZING_CALL);
} else if(pos.x >= (displaySize.x / 2 - 125) && pos.x <= (displaySize.x / 2 - 88) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the resource-mode picture was clicked
mode = EDITOR_MODE_RESOURCE_RAISE;
callback::EditorResourceMenu(INITIALIZING_CALL);
} else if(pos.x >= (displaySize.x / 2 - 88) && pos.x <= (displaySize.x / 2 - 51) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the landscape-mode picture was clicked
mode = EDITOR_MODE_LANDSCAPE;
callback::EditorLandscapeMenu(INITIALIZING_CALL);
} else if(pos.x >= (displaySize.x / 2 - 51) && pos.x <= (displaySize.x / 2 - 14) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the animal-mode picture was clicked
mode = EDITOR_MODE_ANIMAL;
callback::EditorAnimalMenu(INITIALIZING_CALL);
} else if(pos.x >= (displaySize.x / 2 - 14) && pos.x <= (displaySize.x / 2 + 23) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the player-mode picture was clicked
mode = EDITOR_MODE_FLAG;
ChangeSection_ = 0;
setupVerticesActivity();
callback::EditorPlayerMenu(INITIALIZING_CALL);
} else if(pos.x >= (displaySize.x / 2 + 96) && pos.x <= (displaySize.x / 2 + 133) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the build-help picture was clicked
RenderBuildHelp = !RenderBuildHelp;
} else if(pos.x >= (displaySize.x / 2 + 131) && pos.x <= (displaySize.x / 2 + 168) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the minimap picture was clicked
callback::MinimapMenu(INITIALIZING_CALL);
} else if(pos.x >= (displaySize.x / 2 + 166) && pos.x <= (displaySize.x / 2 + 203) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the create-world picture was clicked
callback::EditorCreateMenu(INITIALIZING_CALL);
} else if(pos.x >= (displaySize.x / 2 + 203) && pos.x <= (displaySize.x / 2 + 240) && pos.y >= (displaySize.y - 35)
&& pos.y <= (displaySize.y - 3))
{
// the editor-main-menu picture was clicked
callback::EditorMainMenu(INITIALIZING_CALL);
}
// now we check the right menubar
else if(pos.x >= (displaySize.x - 37) && pos.x <= (displaySize.x) && pos.y >= (displaySize.y / 2 + 162)
&& pos.y <= (displaySize.y / 2 + 199))
{
// the bugkill picture was clicked for quickload
callback::PleaseWait(INITIALIZING_CALL);
// we have to close the windows and initialize them again to prevent failures
callback::EditorCursorMenu(MAP_QUIT);
callback::EditorTextureMenu(MAP_QUIT);
callback::EditorTreeMenu(MAP_QUIT);
callback::EditorLandscapeMenu(MAP_QUIT);
callback::MinimapMenu(MAP_QUIT);
callback::EditorResourceMenu(MAP_QUIT);
callback::EditorAnimalMenu(MAP_QUIT);
callback::EditorPlayerMenu(MAP_QUIT);
destructMap();
constructMap(global::userMapsPath / "quicksave.swd");
callback::PleaseWait(WINDOW_QUIT_MESSAGE);
} else if(pos.x >= (displaySize.x - 37) && pos.x <= (displaySize.x) && pos.y >= (displaySize.y / 2 + 200)
&& pos.y <= (displaySize.y / 2 + 237))
{
// the bugkill picture was clicked for quicksave
callback::PleaseWait(INITIALIZING_CALL);
if(!CFile::save_file(global::userMapsPath / "quicksave.swd", SWD, getMap()))
{
callback::ShowStatus(INITIALIZING_CALL);
callback::ShowStatus(2);
}
callback::PleaseWait(WINDOW_QUIT_MESSAGE);
} else if(pos.x >= (displaySize.x - 37) && pos.x <= (displaySize.x) && pos.y >= (displaySize.y / 2 - 239)
&& pos.y <= (displaySize.y / 2 - 202))
{
// the cursor picture was clicked
callback::EditorCursorMenu(INITIALIZING_CALL);
} else
{
// no picture was clicked
// touch vertex data
modify = true;
saveCurrentVertices = true;
}
}
void CMap::setMouseData(const SDL_MouseButtonEvent& button)
{
if(button.state == SDL_PRESSED)
{
if(button.button == SDL_BUTTON_LEFT)
onLeftMouseDown({button.x, button.y});
else if(button.button == SDL_BUTTON_RIGHT)
startScrollPos = Position(button.x, button.y);
} else if(button.state == SDL_RELEASED)
{
// stop touching vertex data
if(button.button == SDL_BUTTON_LEFT)
modify = false;
else if(button.button == SDL_BUTTON_RIGHT)
startScrollPos = std::nullopt;
}
}
namespace {
SavedVertex saveVertex(Position pt, const bobMAP& map)
{
SavedVertex res;
res.pos = pt;
for(int i = pt.x - SavedVertex::NODES_PER_DIR, k = 0; i <= pt.x + SavedVertex::NODES_PER_DIR; i++, k++)
{
for(int j = pt.y - SavedVertex::NODES_PER_DIR, l = 0; j <= pt.y + SavedVertex::NODES_PER_DIR; j++, l++)
{
// Correct i and j to take wrap around map borders into account
int m = i;
if(m < 0)
m += map.width;
else if(m >= map.width)
m -= map.width;
int n = j;
if(n < 0)
n += map.height;
else if(n >= map.height)
n -= map.height;
res.PointsArroundVertex[l][k] = map.getVertex(m, n);
}
}
return res;
}
void restoreVertex(const SavedVertex& vertex, bobMAP& map)
{
for(int i = vertex.pos.x - SavedVertex::NODES_PER_DIR, k = 0; i <= vertex.pos.x + SavedVertex::NODES_PER_DIR;
i++, k++)
{
for(int j = vertex.pos.y - SavedVertex::NODES_PER_DIR, l = 0; j <= vertex.pos.y + SavedVertex::NODES_PER_DIR;
j++, l++)
{
int m = i;
if(m < 0)
m += map.width;
else if(m >= map.width)
m -= map.width;
int n = j;
if(n < 0)
n += map.height;
else if(n >= map.height)
n -= map.height;
map.getVertex(m, n) = vertex.PointsArroundVertex[l][k];
}
}
}
} // namespace
void CMap::setKeyboardData(const SDL_KeyboardEvent& key)
{
if(key.type == SDL_KEYDOWN)
{
switch(key.keysym.sym)
{
case SDLK_LSHIFT:
if(mode == EDITOR_MODE_HEIGHT_RAISE)
mode = EDITOR_MODE_HEIGHT_REDUCE;
else if(mode == EDITOR_MODE_RESOURCE_RAISE)
mode = EDITOR_MODE_RESOURCE_REDUCE;
else if(mode == EDITOR_MODE_FLAG)
mode = EDITOR_MODE_FLAG_DELETE;
break;
case SDLK_LALT:
if(mode == EDITOR_MODE_HEIGHT_RAISE)
mode = EDITOR_MODE_HEIGHT_PLANE;
break;
case SDLK_INSERT:
if(mode == EDITOR_MODE_HEIGHT_RAISE || mode == EDITOR_MODE_HEIGHT_REDUCE)
{
if(MaxRaiseHeight > 0x00)
MaxRaiseHeight--;
}
break;
case SDLK_HOME:
if(mode == EDITOR_MODE_HEIGHT_RAISE || mode == EDITOR_MODE_HEIGHT_REDUCE)
{
MaxRaiseHeight = 0x3C;
}
break;
case SDLK_PAGEUP:
if(mode == EDITOR_MODE_HEIGHT_RAISE || mode == EDITOR_MODE_HEIGHT_REDUCE)
{
if(MaxRaiseHeight < 0x3C)
MaxRaiseHeight++;
}
break;
case SDLK_DELETE:
if(mode == EDITOR_MODE_HEIGHT_RAISE || mode == EDITOR_MODE_HEIGHT_REDUCE)
{
if(MinReduceHeight > 0x00)
MinReduceHeight--;
}
break;
case SDLK_END:
if(mode == EDITOR_MODE_HEIGHT_RAISE || mode == EDITOR_MODE_HEIGHT_REDUCE)
{
MinReduceHeight = 0x00;
}
break;
case SDLK_PAGEDOWN:
if(mode == EDITOR_MODE_HEIGHT_RAISE || mode == EDITOR_MODE_HEIGHT_REDUCE)
{
if(MinReduceHeight < 0x3C)
MinReduceHeight++;
}
break;
case SDLK_LCTRL:
lastMode = mode;
mode = EDITOR_MODE_CUT;
break;
case SDLK_b:
if(mode != EDITOR_MODE_HEIGHT_MAKE_BIG_HOUSE && mode != EDITOR_MODE_TEXTURE_MAKE_HARBOUR)
{
lastMode = mode;
lastChangeSection = ChangeSection_;
ChangeSection_ = 0;
setupVerticesActivity();
mode = EDITOR_MODE_HEIGHT_MAKE_BIG_HOUSE;
}
break;
case SDLK_h:
if(mode != EDITOR_MODE_HEIGHT_MAKE_BIG_HOUSE && mode != EDITOR_MODE_TEXTURE_MAKE_HARBOUR)
{
lastMode = mode;
lastChangeSection = ChangeSection_;
ChangeSection_ = 0;
setupVerticesActivity();
mode = EDITOR_MODE_TEXTURE_MAKE_HARBOUR;
}
break;
case SDLK_r:
callback::PleaseWait(INITIALIZING_CALL);
rotateMap();
rotateMap();
callback::PleaseWait(WINDOW_QUIT_MESSAGE);
break;
case SDLK_x:
callback::PleaseWait(INITIALIZING_CALL);
MirrorMapOnXAxis();
callback::PleaseWait(WINDOW_QUIT_MESSAGE);
break;
case SDLK_y:
callback::PleaseWait(INITIALIZING_CALL);
MirrorMapOnYAxis();
callback::PleaseWait(WINDOW_QUIT_MESSAGE);
break;
case SDLK_KP_PLUS:
if(ChangeSection_ < MAX_CHANGE_SECTION)
{
ChangeSection_++;
setupVerticesActivity();
}
break;
case SDLK_KP_MINUS:
if(ChangeSection_ > 0)
{
ChangeSection_--;
setupVerticesActivity();
}
break;
case SDLK_1:
case SDLK_KP_1:
ChangeSection_ = 0;
setupVerticesActivity();
break;
case SDLK_2:
case SDLK_KP_2:
ChangeSection_ = 1;
setupVerticesActivity();
break;
case SDLK_3:
case SDLK_KP_3:
ChangeSection_ = 2;
setupVerticesActivity();
break;
case SDLK_4:
case SDLK_KP_4:
ChangeSection_ = 3;
setupVerticesActivity();
break;
case SDLK_5:
case SDLK_KP_5:
ChangeSection_ = 4;
setupVerticesActivity();
break;
case SDLK_6:
case SDLK_KP_6:
ChangeSection_ = 5;
setupVerticesActivity();
break;
case SDLK_7:
case SDLK_KP_7:
ChangeSection_ = 6;
setupVerticesActivity();
break;
case SDLK_8:
case SDLK_KP_8:
ChangeSection_ = 7;
setupVerticesActivity();
break;
case SDLK_9:
case SDLK_KP_9:
ChangeSection_ = 8;
setupVerticesActivity();
break;
case SDLK_SPACE: RenderBuildHelp = !RenderBuildHelp; break;
case SDLK_F11: RenderBorders = !RenderBorders; break;
case SDLK_q:
if(!saveCurrentVertices)
{
if(SDL_GetModState() & (KMOD_LSHIFT | KMOD_RSHIFT))
{
if(!redoBuffer.empty())
{
undoBuffer.push_back(saveVertex(redoBuffer.back().pos, *map));
restoreVertex(redoBuffer.back(), *map);
redoBuffer.pop_back();
}
} else if(!undoBuffer.empty())
{
redoBuffer.push_back(saveVertex(undoBuffer.back().pos, *map));
restoreVertex(undoBuffer.back(), *map);
undoBuffer.pop_back();
}
}
break;
case SDLK_UP:
case SDLK_DOWN:
case SDLK_LEFT:
case SDLK_RIGHT:
{
Position offset{key.keysym.sym == SDLK_LEFT ? -100 : (key.keysym.sym == SDLK_RIGHT ? 100 : 0),
key.keysym.sym == SDLK_UP ? -100 : (key.keysym.sym == SDLK_DOWN ? 100 : 0)};
moveMap(offset);
}
break;
case SDLK_F1: // help menu
callback::EditorHelpMenu(INITIALIZING_CALL);
break;
case SDLK_g: // convert map to greenland
callback::PleaseWait(INITIALIZING_CALL);
// we have to close the windows and initialize them again to prevent failures
callback::EditorCursorMenu(MAP_QUIT);
callback::EditorTextureMenu(MAP_QUIT);
callback::EditorTreeMenu(MAP_QUIT);
callback::EditorLandscapeMenu(MAP_QUIT);
callback::MinimapMenu(MAP_QUIT);
callback::EditorResourceMenu(MAP_QUIT);
callback::EditorAnimalMenu(MAP_QUIT);
callback::EditorPlayerMenu(MAP_QUIT);
map->type = MAP_GREENLAND;
unloadMapPics();
loadMapPics();
callback::PleaseWait(WINDOW_QUIT_MESSAGE);
break;
case SDLK_o: // convert map to wasteland
callback::PleaseWait(INITIALIZING_CALL);
// we have to close the windows and initialize them again to prevent failures
callback::EditorCursorMenu(MAP_QUIT);
callback::EditorTextureMenu(MAP_QUIT);
callback::EditorTreeMenu(MAP_QUIT);
callback::EditorLandscapeMenu(MAP_QUIT);
callback::MinimapMenu(MAP_QUIT);
callback::EditorResourceMenu(MAP_QUIT);
callback::EditorAnimalMenu(MAP_QUIT);
callback::EditorPlayerMenu(MAP_QUIT);
map->type = MAP_WASTELAND;
unloadMapPics();
loadMapPics();
break;
case SDLK_w: // convert map to winterland
callback::PleaseWait(INITIALIZING_CALL);
// we have to close the windows and initialize them again to prevent failures
callback::EditorCursorMenu(MAP_QUIT);
callback::EditorTextureMenu(MAP_QUIT);
callback::EditorTreeMenu(MAP_QUIT);
callback::EditorLandscapeMenu(MAP_QUIT);
callback::MinimapMenu(MAP_QUIT);
callback::EditorResourceMenu(MAP_QUIT);
callback::EditorAnimalMenu(MAP_QUIT);
callback::EditorPlayerMenu(MAP_QUIT);
map->type = MAP_WINTERLAND;
unloadMapPics();
loadMapPics();
callback::PleaseWait(WINDOW_QUIT_MESSAGE);
break;
case SDLK_p:
if(BitsPerPixel == 8)
setBitsPerPixel(32);
else
setBitsPerPixel(8);
break;
case SDLK_F9: // lock horizontal movement
HorizontalMovementLocked = !HorizontalMovementLocked;
break;
case SDLK_F10: // lock vertical movement
// VerticalMovementLocked = !VerticalMovementLocked;
default: break;
}
} else if(key.type == SDL_KEYUP)
{
switch(key.keysym.sym)
{
case SDLK_LSHIFT: // user probably released EDITOR_MODE_HEIGHT_REDUCE
if(mode == EDITOR_MODE_HEIGHT_REDUCE)
mode = EDITOR_MODE_HEIGHT_RAISE;
else if(mode == EDITOR_MODE_RESOURCE_REDUCE)
mode = EDITOR_MODE_RESOURCE_RAISE;
else if(mode == EDITOR_MODE_FLAG_DELETE)
mode = EDITOR_MODE_FLAG;
break;
case SDLK_LALT: // user probably released EDITOR_MODE_HEIGHT_PLANE
if(mode == EDITOR_MODE_HEIGHT_PLANE)
mode = EDITOR_MODE_HEIGHT_RAISE;
break;
case SDLK_LCTRL: // user probably released EDITOR_MODE_CUT
mode = lastMode;
break;
case SDLK_b: // user probably released EDITOR_MODE_HEIGHT_MAKE_BIG_HOUSE
case SDLK_h: // user probably released EDITOR_MODE_TEXTURE_MAKE_HARBOUR
mode = lastMode;
ChangeSection_ = lastChangeSection;
setupVerticesActivity();
break;
default: break;
}
}
}
void CMap::storeVerticesFromMouse(Uint16 MouseX, Uint16 MouseY, Uint8 /*MouseState*/)
{
// if user raises or reduces the height of a vertex, don't let the cursor jump to another vertex
// if ( (MouseState == SDL_PRESSED) && (mode == EDITOR_MODE_HEIGHT_RAISE || mode == EDITOR_MODE_HEIGHT_REDUCE) )
// return;
int X = 0, Xeven = 0, Xodd = 0;
int Y = 0, MousePosY = 0;
// get X
// following out commented lines are the correct ones, but for tolerance (to prevent to early jumps of the cursor)
// we subtract "TRIANGLE_WIDTH/2" Xeven = (MouseX + displayRect.left) / TRIANGLE_WIDTH;
Xeven = (MouseX + displayRect.left - TRIANGLE_WIDTH / 2) / TRIANGLE_WIDTH;
if(Xeven < 0)
Xeven += (map->width);
else if(Xeven > map->width - 1)
Xeven -= (map->width - 1);