-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectXPractice.cpp
4705 lines (3944 loc) · 143 KB
/
DirectXPractice.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
//***************************************************************************************
// MyApp.cpp by Frank Luna (C) 2015 All Rights Reserved.
//***************************************************************************************
#include "Common/d3dApp.h"
#include "Common/MathHelper.h"
#include "Common/UploadBuffer.h"
#include "Common/GeometryGenerator.h"
#include "Waves.h"
#include "YTML.h"
#include "Yscript.h"
using Microsoft::WRL::ComPtr;
using namespace DirectX;
using namespace DirectX::PackedVector;
const int gNumFrameResources = 3;
// Lightweight structure stores parameters to draw a shape. This will
// vary from app-to-app.
struct RenderItem
{
RenderItem() = default;
bool Visible = true;
BoundingBox Bounds;
// World matrix of the shape that describes the object's local space
// relative to the world space, which defines the position, orientation,
// and scale of the object in the world.
XMFLOAT4X4 World = MathHelper::Identity4x4();
XMFLOAT4X4 TexTransform = MathHelper::Identity4x4();
// Dirty flag indicating the object data has changed and we need to update the constant buffer.
// Because we have an object cbuffer for each FrameResource, we have to apply the
// update to each FrameResource. Thus, when we modify obect data we should set
// NumFramesDirty = gNumFrameResources so that each frame resource gets the update.
int NumFramesDirty = gNumFrameResources;
// Index into GPU constant buffer corresponding to the ObjectCB for this render item.
UINT ObjCBIndex = 0xFFFFFFFF;
Material* Mat = nullptr;
MeshGeometry* Geo = nullptr;
// Primitive topology.
D3D12_PRIMITIVE_TOPOLOGY PrimitiveType = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
// DrawIndexedInstanced parameters.
UINT IndexCount = 0;
UINT StartIndexLocation = 0;
int BaseVertexLocation = 0;
std::string Name = "None";
};
enum class RenderLayer : int
{
Opaque = 0,
Transparent,
AlphaTested,
Province,
Water,
Count
};
template<typename T>
Color32 rgb2dex(const T& r, const T& g, const T& b)
{
return ((r * 256) + g) * 256 + b;
}
void dex2rgb(float& r, float& g, float& b, const Color32& dex)
{
r = ((dex & 16711680) / 65536) / 255.f;
g = ((dex & 65208) / 256) / 255.f;
b = (dex & 255) / 255.f;
}
void dex2rgb(unsigned char& r, unsigned char& g, unsigned char& b, const Color32& dex)
{
r = static_cast<unsigned char>((dex & 16711680) / 65536);
g = static_cast<unsigned char>((dex & 65208) / 256);
b = static_cast<unsigned char>((dex & 255));
}
void dex2rgb(unsigned int& r, unsigned int& g, unsigned int& b, const Color32& dex)
{
r = (dex & 16711680) / 65536;
g = (dex & 65208) / 256;
b = (dex & 255);
}
struct Province {
std::wstring name;
Color32 color;
std::uint64_t p_num = 1;
XMFLOAT3 pixel;
XMFLOAT3 on3Dpos;
bool is_rebel = false;
NationId owner = 0;
NationId ruler = 0;
std::int64_t man = 1000 + rand() % 1600;
std::int64_t maxman = 4000 + rand() % 6400;
std::int64_t hp = 1000;
float length_from_contry_side = 0;
float prioriy, require;
Province()
{
}
Province(std::wstring _name, Color32 _color, XMFLOAT3 _pixel) :name(_name), color(_color), pixel(_pixel)
{
}
};
using WCHAR3 = WCHAR[3];
struct Nation
{
XMFLOAT4 MainColor = { 0.f, 0.f, 0.f, 0.f };
std::wstring MainName = L"오류";
std::unordered_map<std::wstring, std::wstring> flag;
bool Ai = true;
float abb_disp = 1;
float abb_man = 1;
float abb_army_sieze = 1;
float abb_army_move = 1;
float abb_attr = 1;
size_t own_province = 0;
size_t rule_province = 0;
NationId rival = -1;
};
enum class CommandType
{
Move,
Sieze,
Attack
};
struct Command
{
const CommandType type;
const ProvinceId target_prov;
const LeaderId target_leader;
const float need;
Command(const CommandType _type, const ProvinceId prov = 0, const LeaderId leader = 0, const float _need = 0) : type(_type), target_prov(prov), target_leader(leader), need(_need) {}
};
enum class LeaderType
{
Attack,
Defend,
All
};
struct Leader
{
std::list<Command> cmd;
float cmd_pr = 0.f;
bool enable = true;
ProvinceId location;
bool selected = false;
std::int64_t size = 1000;
LeaderType type = (LeaderType)(rand() % (int)LeaderType::All);
float abb_sieze = 1;
float abb_move = 1;
float abb_disp = 1;
NationId owner;
Leader(const ProvinceId& loc, const NationId& own, const std::int64_t& _size) : location(loc), owner(own), size(_size) {};
};
enum class ProvincePathOutState {
NotOut,
WaitOut,
Out
};
struct ProvincePathNode
{
float Length = FLT_MAX;
ProvinceId Nearest = 0;
ProvincePathOutState Out = ProvincePathOutState::NotOut;
};
struct ProvincePath
{
std::list<ProvinceId> path;
std::map<ProvinceId, ProvincePathNode> prv;
float length;
decltype(path)::iterator begin() { return path.begin(); }
decltype(path)::iterator end() { return path.end(); }
ProvincePath(const std::map<ProvinceId, std::unique_ptr<Province>>& _prv, const std::map<std::pair<ProvinceId, ProvinceId>, float> conn, const ProvinceId& Start, const ProvinceId& End)
{
if (Start == End) return;
for (const auto& O : _prv) prv[O.first] = ProvincePathNode();
prv.at(End).Length = 0;
prv.at(End).Out = ProvincePathOutState::WaitOut;
size_t limit = 0;
while (limit++ <= prv.size())
{
for (auto& O : prv)
{
if (O.second.Out == ProvincePathOutState::WaitOut)
{
for (auto& P : prv)
{
if (P.second.Out == ProvincePathOutState::NotOut)
{
if (auto Q = conn.find(std::make_pair(O.first, P.first)); Q != conn.end())
{
if (P.second.Length > O.second.Length + Q->second)
{
P.second.Length = O.second.Length + Q->second;
P.second.Nearest = O.first;
}
}
}
}
O.second.Out = ProvincePathOutState::Out;
}
}
/*if (prv.at(Start).Out == ProvincePathOutState::Out)
{
break;
}*/
float meter = FLT_MAX;
decltype(prv)::iterator itr = prv.end();
for (auto O = prv.begin(); O != prv.end(); ++O)
{
if (O->second.Out == ProvincePathOutState::NotOut && meter > O->second.Length)
{
meter = O->second.Length;
itr = O;
}
}
if (itr != prv.end())
{
itr->second.Out = ProvincePathOutState::WaitOut;
}
else
{
break;
}
}
if (prv.at(Start).Out != ProvincePathOutState::NotOut)
{
ProvinceId Index = Start;
length = prv.at(Start).Length;
do
{
Index = prv.at(Index).Nearest;
path.push_back(Index);
} while (Index != End);
}
}
ProvincePath()
{
};
};
class Arrows
{
public:
std::vector<Vertex> vertices;
std::vector<std::uint16_t> indices;
std::vector<XMFLOAT3> points;
float width = 1.f;
void BuildLine()
{
float width2 = width / 2.f;
size_t offset = vertices.size();
if (points.size() == 0) return;
if (points.size() > 0x2000)
points.resize(0x2000);
float center = (points.size() - 1) / 2.f;
for (int i = 0; i < points.size() - 1; ++i)
{
//float y = 2 - powf((points.size() - 1) / 2 - i, 2)/((points.size() - 1) / 2);
float y = (powf((i - center) / center, 2) - 1.f) * 1 ;
XMVECTOR v = XMLoadFloat2(new XMFLOAT2(points[i + 1].x - points[i].x, points[i +1].z - points[i].z));
v = XMVector2Orthogonal(v);
v = XMVector2Normalize(v);
XMFLOAT2 f;
XMStoreFloat2(&f, v);
vertices.push_back({ XMFLOAT3(points[i].x - f.x * width2,points[i].y - y, points[i].z - f.y * width2) ,XMFLOAT3(0,1,0), XMFLOAT2(1, 1) });
vertices.push_back({ XMFLOAT3(points[i].x + f.x * width2,points[i].y - y, points[i].z + f.y * width2) ,XMFLOAT3(0,1,0), XMFLOAT2(0, 1) });
}
for (size_t i = points.size() - 1; i < points.size();++i)
{
XMVECTOR v = XMLoadFloat2(new XMFLOAT2(points[i].x - points[i - 1].x, points[i].z - points[i - 1].z));
v = XMVector2Orthogonal(v);
v = XMVector2Normalize(v);
XMFLOAT2 f;
XMStoreFloat2(&f, v);
vertices.push_back({ XMFLOAT3(points[i].x - f.x * width2,points[i].y, points[i].z - f.y * width2) ,XMFLOAT3(0,1,0), XMFLOAT2(1, 0) });
vertices.push_back({ XMFLOAT3(points[i].x + f.x * width2,points[i].y, points[i].z + f.y * width2) ,XMFLOAT3(0,1,0), XMFLOAT2(0, 0) });
}
for (std::uint16_t i = 0; i < points.size() - 1; ++i)
{
indices.push_back(static_cast<std::uint16_t>(offset + i * 2));
indices.push_back(static_cast<std::uint16_t>(offset + i * 2 + 1));
indices.push_back(static_cast<std::uint16_t>(offset + i * 2 + 2));
indices.push_back(static_cast<std::uint16_t>(offset + i * 2 + 1));
indices.push_back(static_cast<std::uint16_t>(offset + i * 2 + 2));
indices.push_back(static_cast<std::uint16_t>(offset + i * 2 + 3));
/*indices.push_back(i * 2);
indices.push_back(i * 2 + 3);
indices.push_back(i * 2 + 2);
indices.push_back(i * 2 + 2);
indices.push_back(i * 2 + 3);
indices.push_back(i * 2);*/
}
}
Arrows()
{
BuildLine();
}
};
struct Data
{
bool run = true;
std::map<ProvinceId, std::unique_ptr<Province>> province;
std::map<std::pair<ProvinceId, ProvinceId>, float> province_connect;
std::unordered_map<NationId, std::unique_ptr<Nation>> nations;
std::unordered_map<LeaderId, std::shared_ptr<Leader>> leaders;
std::uint64_t leader_progress = 1;
LeaderId last_leader_id = 0;
ProvinceId last_prov_id = 0;
std::random_device rd;
std::mt19937 mt;
Data()
{
mt = std::mt19937(rd());
}
Leader NewLeader(const ProvinceId& loc, const NationId& own, const std::int64_t& _size)
{
return Leader(loc, own, _size);
}
};
enum class DragType
{
None,
View,
Leader
};
enum class GameControlType
{
View,
Leader
};
class MyApp : public D3DApp
{
public:
MyApp(HINSTANCE hInstance);
MyApp(const MyApp& rhs) = delete;
MyApp& operator=(const MyApp& rhs) = delete;
~MyApp();
virtual bool Initialize()override;
private:
virtual void OnResize()override;
virtual void Update(const GameTimer& gt)override;
virtual void Draw(const GameTimer& gt)override;
void DrawUI();
virtual void OnKeyDown(WPARAM btnState)override;
virtual void OnMouseDown(WPARAM btnState, int x, int y)override;
virtual void OnMouseUp(WPARAM btnState, int x, int y)override;
virtual void OnMouseMove(WPARAM btnState, int x, int y)override;
void OnKeyboardInput(const GameTimer& gt);
void UpdateCamera(const GameTimer& gt);
void AnimateMaterials(const GameTimer& gt);
void UpdateObjectCBs(const GameTimer& gt);
void UpdateMaterialCBs(const GameTimer& gt);
void UpdateMainPassCB(const GameTimer& gt);
void UpdateWaves(const GameTimer& gt);
void LoadTextures();
void BuildRootSignature();
void BuildDescriptorHeaps();
void BuildShadersAndInputLayout();
void BuildLandGeometry();
void BuildWavesGeometry();
void BuildArrowGeometry();
void BuildBoxGeometry();
void BuildPSOs();
void BuildFrameResources();
void BuildMaterials();
void BuildRenderItems();
void BuildImage();
void BuildProvinceConnect();
void DrawRenderItems(ID3D12GraphicsCommandList* cmdList, const std::vector<RenderItem*>& ritems);
void Pick(WPARAM btnState, int sx, int sy);
void LoadSizeDependentResources();
std::array<const CD3DX12_STATIC_SAMPLER_DESC, 6> GetStaticSamplers();
const XMFLOAT3 MyApp::Convert3Dto2D(XMVECTOR pos);
void UILayerInitialize();
void UILayerResize();
void GameInit();
void GameUpdate();
void GameStep();
void GameSave();
void GameLoad();
void GameClose();
void ProvinceMousedown(WPARAM btnState, ProvinceId id);
void CreateBrush();
void InsertArrow(const ProvinceId& start, ProvincePath& path, bool clear);
void UpdateArrow();
void Query(const std::wstring& query);
void Execute(const std::wstring& func_name, const std::uint64_t& uuid);
void MainGame();
std::list<YTML::DrawItem>::reverse_iterator MyApp::MouseOnUI(int x, int y);
private:
void GUIUpdatePanelLeader(LeaderId leader_id = 0);
void GUIUpdatePanelProvince(ProvinceId prov_id = 0);
UINT mCbvSrvDescriptorSize = 0;
ComPtr<ID3D12RootSignature> mRootSignature = nullptr;
ComPtr<ID3D12DescriptorHeap> mSrvDescriptorHeap = nullptr;
std::wofstream log_main;
std::unordered_map<std::string, std::unique_ptr<MeshGeometry>> mGeometries;
std::unordered_map<std::string, std::unique_ptr<Material>> mMaterials;
std::unordered_map<std::string, std::unique_ptr<Texture>> mTextures;
std::unordered_map<std::string, ComPtr<ID3DBlob>> mShaders;
std::unordered_map<std::string, ComPtr<ID3D12PipelineState>> mPSOs;
std::vector<D3D12_INPUT_ELEMENT_DESC> mInputLayout;
std::vector<D3D12_INPUT_ELEMENT_DESC> mInputLayout_prv;
RenderItem* mWavesRitem = nullptr;
RenderItem* mArrowsRitem = nullptr;
// List of all the render items.
std::vector<std::unique_ptr<RenderItem>> mAllRitems;
// Render items divided by PSO.
std::vector<RenderItem*> mRitemLayer[(int)RenderLayer::Count];
std::vector<VertexForProvince> mLandVertices;
std::unique_ptr<Waves> mWaves;
PassConstants mMainPassCB;
XMFLOAT3 mEyePos = { 0.0f, 0.0f, 0.0f };
XMFLOAT4X4 mView = MathHelper::Identity4x4();
XMFLOAT4X4 mProj = MathHelper::Identity4x4();
float mTheta = 1.5f*XM_PI;
float mPhi = XM_PIDIV2 - 0.1f;
float mRadius = 50.0f;
size_t map_w = 0;
size_t map_h = 0;
float mX = 0;
POINT mLastMousePos;
Arrows mArrows;
struct {
std::wstring DebugText = L"선택 되지 않음";
NationId nationPick = 0;
} mUser;
std::unordered_map<std::wstring, std::wstring> Act(std::wstring wstr, std::initializer_list<std::wstring> args, bool need_return = false, bool try_lock = true);
std::shared_ptr<Data> m_gamedata = std::make_shared<Data>();
XMVECTOR mEyetarget = XMVectorSet(0.0f, 15.0f, 0.0f, 0.0f);
std::unordered_map<std::wstring, std::wstring> captions;
std::unordered_map<std::wstring, HBITMAP> mBitmap;
std::shared_ptr<YTML::DrawItemList> m_DrawItems = std::make_shared<YTML::DrawItemList>();
//YScript::YScript m_script;
float mEyeMoveX = 0.f;
float mEyeMoveZ = 0.f;
bool mUI_isInitial = false;
struct Query
{
bool enable = false;
bool isString = false;
bool isLineComment = false;
bool isComment = false;
size_t pos;
std::vector<std::wstring> word;
std::wstring index;
ProvinceId tag_prov;
LeaderId tag_leader;
decltype(Data::province)::iterator tag_prov_it;
} mQuery;
D2D1_POINT_2F Draw_point;
D2D1_RECT_F Draw_rect;
D2D1_SIZE_F Draw_size;
D2D1_SIZE_F Draw_scale;
std::uint64_t focus = 0;
std::future<void> trdGame;
DragType dragtype;
int dragx, dragy;
GameControlType game_contype = GameControlType::View;
std::mutex draw_mutex;
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
try
{
MyApp theApp(hInstance);
if (!theApp.Initialize())
return 0;
return theApp.Run();
}
catch (DxException& e)
{
MessageBox(nullptr, e.ToString().c_str(), L"HR Failed", MB_OK);
return 0;
}
}
MyApp::MyApp(HINSTANCE hInstance)
: D3DApp(hInstance)
{
}
MyApp::~MyApp()
{
GameClose();
if (md3dDevice != nullptr)
FlushCommandQueue();
}
void MyApp::MainGame() //!@
{
//std::this_thread::sleep_for(std::chrono::seconds(1));
OutputDebugStringA("Start Thread\n");
float NowTime = mTimer.TotalTime();
bool flag_update_leaders = false;
while (m_gamedata->run)
{
for (auto& N : m_gamedata->nations)
{
N.second->own_province = 0;
N.second->rule_province = 0;
}
for (auto& O : m_gamedata->province)
{
O.second->man += (int64_t)round(min(max((O.second->maxman - O.second->man)/1200.0,-10),10));
if (O.second->owner != O.second->ruler && O.second->man > O.second->maxman / 4)
{
if (O.second->man + O.second->hp * 2 > rand() % (1 + (int)(1.0 * rand() / RAND_MAX * 30000)) + O.second->maxman / 4)
{
Act(L"Draft", { L"location", Str(O.first), L"size", Str(O.second->man), L"owner", Str(O.second->owner) });
}
}
if (O.second->owner == O.second->ruler)
{
auto& N = m_gamedata->nations.find(O.second->ruler);
if (N != m_gamedata->nations.end())
{
O.second->man += (int64_t)round(min(max((O.second->maxman - O.second->man) / 1200.0 * N->second->abb_man, -10), 10));
}
}
if (auto & N = m_gamedata->nations.find(O.second->owner); N != m_gamedata->nations.end()) ++N->second->own_province;
if (auto & N = m_gamedata->nations.find(O.second->ruler); N != m_gamedata->nations.end()) ++N->second->rule_province;
if (O.second->hp < 0) O.second->hp = 0;
else if (O.second->hp >= 1000)
{
O.second->hp = 1000;
O.second->owner = O.second->ruler;
}
else O.second->hp += 1;
if (m_gamedata->last_prov_id == O.first)
{
GUIUpdatePanelProvince();
}
}
draw_mutex.lock();
for (auto O = m_gamedata->leaders.begin(); O != m_gamedata->leaders.end(); ++O)
{
if (O->second->size <= 0)
{
if (m_gamedata->last_leader_id == O->first) m_gamedata->last_leader_id = 0;
for (const auto& E : m_DrawItems->$(L"#leader" + Str(O->first) + L" flag")) m_DrawItems->data.erase(E);
for (const auto& E : m_DrawItems->$(L"#leader" + Str(O->first) + L" state")) m_DrawItems->data.erase(E);
for (const auto& E : m_DrawItems->$(L"#leader" + Str(O->first) + L" num")) m_DrawItems->data.erase(E);
for (const auto& E : m_DrawItems->$(L"#leader" + Str(O->first) + L" background")) m_DrawItems->data.erase(E);
for (const auto& E : m_DrawItems->$(L"#leader" + Str(O->first) + L" progress")) m_DrawItems->data.erase(E);
for (const auto& E : m_DrawItems->$(L"#leader" + Str(O->first))) m_DrawItems->data.erase(E);
m_gamedata->leaders.erase((O--)->first);
}
}
draw_mutex.unlock();
flag_update_leaders = false;
for (auto& O : m_gamedata->leaders)
{
if (O.second->owner != m_gamedata->province.at(O.second->location)->ruler)
{
if (const auto & N = m_gamedata->nations.find(m_gamedata->province.at(O.second->location)->ruler); N != m_gamedata->nations.end())
{
O.second->size -= (std::int64_t)std::round(O.second->size * N->second->abb_attr / 10000.f * 2 * rand() / RAND_MAX);
}
else
{
O.second->size -= (std::int64_t)std::round(O.second->size * 5 / 10000.f * 2 * rand() / RAND_MAX);
}
///if (O.second->size > 1000) O.second->size -= (O.second->size - 1000) / 10000;
}
else
{
if (auto& P = m_gamedata->province.at(O.second->location); O.second->cmd.size() == 0)
{
//if (O.second->size < 2992)
{
if (P->man >= 9 && O.second->owner == m_gamedata->province.at(O.second->location)->owner)
{
O.second->size += 9;
P->man -= 9;
}
if (P->hp < 1000) P->hp += 1;
}
}
}
if (O.second->cmd.size() > 0)
{
auto B = O.second->cmd.begin();
if (O.second->cmd_pr >= B->need)
{
O.second->cmd_pr = 0;
switch (B->type)
{
case CommandType::Move:
O.second->location = B->target_prov;
break;
case CommandType::Sieze:
m_gamedata->province.at(O.second->location)->hp -= (77 + rand() % 100 + O.second->size / 600) * 2;
//O.second->size = (int)(0.9 * O.second->size);
if (m_gamedata->province.at(O.second->location)->hp <= 0)
{
m_gamedata->province.at(O.second->location)->ruler = O.second->owner;
O.second->size += m_gamedata->province.at(O.second->location)->man;
m_gamedata->province.at(O.second->location)->man = 0;
m_gamedata->province.at(O.second->location)->hp = 0;
}
break;
case CommandType::Attack:
auto L = m_gamedata->leaders.find(std::move(B->target_leader));
if (L != m_gamedata->leaders.end() && L->second->location == O.second->location && O.second->size > 0 && L->second->size > 0)
{
L->second->size -= O.second->size / 4;
if (L->second->size > 0)
{
O.second->size -= L->second->size / 4;
if (L->second->cmd.size() > 0 && (L->second->cmd.begin()->type == CommandType::Sieze || L->second->cmd.begin()->type == CommandType::Move))
{
L->second->cmd_pr = 0;
L->second->cmd.pop_front();
}
}
}
break;
}
O.second->cmd.pop_front();
}
else
{
switch (B->type)
{
case CommandType::Sieze:
if (m_gamedata->province.at(O.second->location)->ruler == O.second->owner)
{
O.second->cmd.pop_front();
O.second->cmd_pr = -1;
}
break;
case CommandType::Attack:
auto L = m_gamedata->leaders.find(std::move(B->target_leader));
if (L == m_gamedata->leaders.end())
{
O.second->cmd.pop_front();
O.second->cmd_pr = -1;
}
else if (L->second->location != O.second->location)
{
O.second->cmd.pop_front();
O.second->cmd_pr = -1;
}
break;
}
O.second->cmd_pr += 1;
}
if (O.second->selected) flag_update_leaders = true;
}
else
{
std::vector<LeaderId> sameLocLeader;
for (auto& L : m_gamedata->leaders)
{
if (L.second->location == O.second->location && L.second->owner != O.second->owner)
{
if (L.second->cmd.size() > 0 && L.second->cmd.begin()->type == CommandType::Sieze)
{
sameLocLeader.clear();
sameLocLeader.push_back(L.first);
break;
}
else
{
sameLocLeader.push_back(L.first);
}
}
}
std::shuffle(sameLocLeader.begin(), sameLocLeader.end(), m_gamedata->rd);
if (sameLocLeader.size() > 0) O.second->cmd.push_back(Command(CommandType::Attack, 0, *sameLocLeader.begin(), 10));
else if (m_gamedata->province.at(O.second->location)->ruler != O.second->owner)
{
O.second->cmd.push_back(Command(CommandType::Sieze, O.second->location, 0, 20 / O.second->abb_sieze));
}
else
{
if (m_gamedata->province.at(O.second->location)->hp < 1000) m_gamedata->province.at(O.second->location)->hp += O.second->size / 1000;
}
}
}
//AI
for (auto& N : m_gamedata->nations)
{
if (N.second->Ai && N.second->own_province > 0 && N.second->rule_province > 0)
{
if (N.second->rival != -1)
{
if (auto & n = m_gamedata->nations.find(N.second->rival); n != m_gamedata->nations.end())
{
if (n->second->own_province == 0 && n->second->rule_province == 0)
{
N.second->rival = -1;
}
}
else
{
N.second->rival = -1;
}
}
std::list<ProvinceId> myProv;
std::list<LeaderId> myLead;
for (auto& P : m_gamedata->province)
{
if (P.second->owner == N.first || P.second->ruler == N.first) myProv.push_back(P.first);
if (P.second->owner == N.first)
{
if (P.second->ruler == N.first) //내 영토의 내 소유
{
P.second->prioriy = 1 * (2000 - P.second->hp);
}
else //내 영토의 적 소유
{
P.second->prioriy = 3 * (2000 - P.second->hp);
}
}
else
{
if (P.second->ruler == N.first) //적 영토의 내 소유
{
P.second->prioriy = 2 * (2000 - P.second->hp) * (N.second->rival == P.second->ruler ? 2 : 1);
}
else //적 영토의 적 소유
{
P.second->prioriy = 1 * (2000 - P.second->hp) * (N.second->rival == P.second->ruler ? 2 : 1);
}
}
}
for (auto& L : m_gamedata->leaders)
{
ProvinceId lastLoc = L.second->location;
if (L.second->cmd.size() > 0)
{
float rate_time = -L.second->cmd_pr;
for (auto& C : L.second->cmd)
{
rate_time += C.need;
if (C.type == CommandType::Move) lastLoc = C.target_prov;
}
}
auto& P = m_gamedata->province.at(lastLoc);
if (L.second->owner == N.first)
{
myLead.push_back(L.first);
P->require -= L.second->size;
if (P->ruler == N.first)// 내 땅에 내 군사
P->prioriy -= 2 * L.second->size;
else // 남 땅에 내 군사
P->prioriy -= 0.1f * L.second->size * (N.second->rival == P->owner ? 0.5f : 1.f);
}
else
{
P->require += L.second->size;
if (P->ruler == N.first)// 내 땅에 남 군사
P->prioriy += 2 * L.second->size * (N.second->rival == P->owner ? 2 : 1);
else // 남 땅에 남 군사
P->prioriy -= 1 * L.second->size * (N.second->rival == P->owner ? 0.5 : 1);
}
}
if (N.second->rival == -1)
{
float syn = -FLT_MAX;
for (auto& n : m_gamedata->nations)
{
if (n.second->own_province > 0 && n.second->rule_province > 0 && n.first != N.first)
{
float my_syn = 0;
for (auto& P : m_gamedata->province)
{
if (P.second->owner == N.first && P.second->ruler == n.first)
{
my_syn += P.second->maxman / 1000.f;
}
else if (P.second->ruler == N.first && P.second->owner == n.first)
{
my_syn += P.second->maxman / 1000.f;
}
else if (P.second->ruler == n.first && P.second->owner == n.first)
{
float distance = FLT_MAX;
for (const auto& p : myProv)
{
auto path = ProvincePath(m_gamedata->province, m_gamedata->province_connect, P.first, p);
if (path.path.size() > 0)
{
if (path.length < distance)
distance = path.length;
}
}
my_syn += (P.second->maxman / 1000.f) * 30 / pow(distance, 2);
}
}
if (my_syn > syn)
{
syn = my_syn;
N.second->rival = n.first;
}
}
}
}
size_t LeaderCount = myLead.size();
for (const auto& p : myProv)
{
if (LeaderCount >= myProv.size() / 2 + 1 ) break;
auto& P = m_gamedata->province[p];
if (N.first == P->ruler && P->man >= 1000)
{
if (auto X = Act(L"Draft", { L"location", Str(p), L"size", Str(P->man), L"owner", Str(P->owner), L"abb_sieze", Str(N.second->abb_army_sieze), L"abb_move", Str(N.second->abb_army_move) }); X.find(L"SUCCESS") != X.end()) ++LeaderCount;
}
}
for (const auto& l : myLead)
{
auto& L = m_gamedata->leaders[l];
if (L->cmd.size() == 0)
{
ProvinceId target = L->location;
float org_syn = m_gamedata->province[L->location]->prioriy + L->size;
float syn = org_syn;
float tmp = 0;
for (auto& P : m_gamedata->province)
{
if (P.second->prioriy < org_syn) continue;
auto path = ProvincePath(m_gamedata->province, m_gamedata->province_connect, L->location, P.first);
if (syn < P.second->prioriy - path.length * 16)
{
syn = P.second->prioriy - path.length * 16;
target = P.first;
}
}
auto path = ProvincePath(m_gamedata->province, m_gamedata->province_connect, L->location, target);
if (path.path.size() > 0)
{
m_gamedata->province[target]->prioriy -= L->size;
m_gamedata->province[L->location]->prioriy += L->size;
L->cmd_pr = 0;
L->cmd.clear();
ProvinceId lastLoc = L->location;
for (auto P : path)
{
L->cmd.push_back(Command(CommandType::Move, P, 0, m_gamedata->province_connect.at(std::make_pair(lastLoc, P)) / L->abb_move));
lastLoc = P;
break;
}
}
}
}
}
else
{
size_t myProvCount = 0;
size_t myLeaderCount = 0;
for (auto& P : m_gamedata->province)
{
if (P.second->ruler == N.first || P.second->owner == N.first) ++myProvCount;
}
for (auto& L : m_gamedata->leaders)
{