-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.game.cpp
1255 lines (1073 loc) · 46.1 KB
/
table.game.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
#include "model/session/session.h"
#include "table.game.h"
#include <regex>
void assert_script(const std::string& script, const std::string& regex, const std::string& fnName, const std::function<void(const std::string&)>& callback)
{
if(script.empty())
return;
if(std::filesystem::exists(script) == false)
{
std::stringstream sstream;
sstream << script
<< " 파일이 존재하지 않습니다.";
callback(script);
throw std::runtime_error(sstream.str());
}
auto in = std::ifstream(script);
auto buffer = std::stringstream();
buffer << in.rdbuf();
auto re = std::regex(regex);
auto matches = std::smatch();
auto source = buffer.str();
if(std::regex_search(source, matches, re) == false)
{
std::stringstream sstream;
sstream << script
<< " 파일에 "
<< fnName
<< " 함수가 존재하지 않습니다.";
callback(script);
throw std::runtime_error(sstream.str());
}
}
std::vector<fb::game::clan*> fb::game::table::clans;
fb::game::board fb::game::table::board;
fb::game::container::map fb::game::table::maps;
fb::game::container::worlds fb::game::table::worlds;
fb::game::container::item fb::game::table::items;
fb::game::container::npc fb::game::table::npcs;
fb::game::container::mob fb::game::table::mobs;
fb::game::container::spell fb::game::table::spells;
fb::game::container::cls fb::game::table::classes;
fb::game::container::mix fb::game::table::mixes;
fb::game::container::door fb::game::table::doors;
fb::game::table::table()
{ }
fb::game::table::~table()
{ }
fb::game::map* fb::game::container::map::name2map(const std::string& name)
{
for(auto& pair : *this)
{
if(pair.second->name() == name)
return pair.second;
}
return nullptr;
}
void fb::game::container::map::clear()
{
for(auto& x : *this)
delete x.second;
std::map<uint16_t, fb::game::map*>::clear();
}
void fb::game::container::map::clear(const std::function<bool(const fb::game::map&)>& fn)
{
for(auto i = this->begin(); i != this->end();)
{
if(fn(*i->second))
{
delete i->second;
this->erase(i++);
}
else
{
++i;
}
}
}
fb::game::map* fb::game::container::map::operator[](uint16_t id) const
{
auto found = this->find(id);
if(found == this->end())
return nullptr;
return found->second;
}
fb::game::npc::master* fb::game::container::npc::name2npc(const std::string& name)
{
auto i = std::find_if(this->begin(), this->end(),
[&name](std::pair<uint16_t, fb::game::npc::master*> pair)
{
return pair.second->name == name;
});
return i != this->end() ? (*i).second : nullptr;
}
fb::game::mob::master* fb::game::container::mob::name2mob(const std::string& name)
{
auto i = std::find_if(this->begin(), this->end(),
[&name](std::pair<uint16_t, fb::game::mob::master*> pair)
{
return pair.second->name == name;
});
return i != this->end() ? (*i).second : nullptr;
}
fb::game::item::master* fb::game::container::item::name2item(const std::string& name)
{
auto i = std::find_if(this->begin(), this->end(),
[&name](std::pair<uint16_t, fb::game::item::master*> pair)
{
return pair.second->name == name;
});
return i != this->end() ? (*i).second : nullptr;
}
fb::game::spell* fb::game::container::spell::name2spell(const std::string& name)
{
auto i = std::find_if(this->begin(), this->end(),
[&name](std::pair<uint16_t, fb::game::spell*> pair)
{
return pair.second->name() == name;
});
return i != this->end() ? (*i).second : nullptr;
}
const std::string* fb::game::container::cls::class2name(uint8_t cls, uint8_t promotion)
{
try
{
return &this->operator[](cls)->promotions[promotion];
}
catch(std::exception&)
{
return nullptr;
}
}
bool fb::game::container::cls::name2class(const std::string& name, uint8_t* class_id, uint8_t* promotion_id)
{
for(int i1 = 0; i1 < this->size(); i1++)
{
for(int i2 = 0; i2 < this->operator[](i1)->promotions.size(); i2++)
{
if(this->operator[](i1)->promotions[i2] != name)
continue;
*class_id = i1;
*promotion_id = i2;
return true;
}
}
return false;
}
uint32_t fb::game::container::cls::exp(uint8_t class_id, uint8_t level)
{
try
{
if(class_id == 0 && level >= 5)
return 0;
if(level > 99)
return 0;
return this->operator[](class_id)->abilities[level]->exp;
}
catch(std::exception&)
{
return 0;
}
}
fb::game::map::effects fb::game::container::map::to_effect(const std::string& effect)
{
if(effect == "fire")
return fb::game::map::effects::FIRE;
if(effect == "water")
return fb::game::map::effects::WATER;
return fb::game::map::effects::NONE;
}
fb::game::map::options fb::game::container::map::to_option(const Json::Value& data)
{
auto flags = fb::game::map::options::NO_OPTION;
auto& option = data["option"];
if(option["build in"].asBool())
flags |= fb::game::map::options::BUILD_IN;
if(option["enabled talk"].asBool() == false)
flags |= fb::game::map::options::DISABLE_TALK;
if(option["enabled whisper"].asBool() == false)
flags |= fb::game::map::options::DISABLE_WHISPER;
if(option["enabled magic"].asBool() == false)
flags |= fb::game::map::options::DISABLE_MAGIC;
if(option["hunting ground"].asBool())
flags |= fb::game::map::options::HUNTING_GROUND;
if(option["enabled pk"].asBool())
flags |= fb::game::map::options::ENABLE_PK;
if(option["enabled die penalty"].asBool())
flags |= fb::game::map::options::DISABLE_DIE_PENALTY;
return flags;
}
bool fb::game::container::map::load_data(uint16_t id, std::vector<char>& buffer)
{
char fname[256];
sprintf(fname, "maps/%06d.map", id);
std::ifstream map_file(fname, std::ios::binary);
if(map_file.is_open() == false)
return false;
buffer = std::vector<char>(std::istreambuf_iterator<char>(map_file), { });
map_file.close();
return true;
}
bool fb::game::container::map::load_blocks(uint16_t id, Json::Value& buffer)
{
char fname[256];
sprintf(fname, "maps/%06d.block", id);
std::ifstream file(fname);
if(file.is_open() == false)
return false;
Json::Reader reader;
if(reader.parse(file, buffer) == false)
return false;
file.close();
return true;
}
fb::game::container::item::item()
{ }
fb::game::container::item::~item()
{
for(auto x : *this)
delete x.second;
}
fb::game::item::master* fb::game::container::item::create(uint32_t id, const Json::Value& data)
{
std::string types = CP949(data["type"].asString(), PLATFORM::Windows);
std::string name = CP949(data["name"].asString(), PLATFORM::Windows);
uint16_t icon = data["icon"].asInt() + 0xBFFF;
uint8_t color = data["color"].asInt();
auto price = data["price"].asInt();
auto condition = to_condition(data);
auto penalty = to_penalty(CP949(data["death penalty"].asString(), PLATFORM::Windows));
auto capacity = std::max(1, data["capacity"].asInt());
auto trade = fb::game::item::trade(data["trade"]["enabled"].asBool());
auto storage = fb::game::item::storage(data["storage"]["enabled"].asBool(), data["storage"]["price"].asInt());
auto desc = CP949(data["desc"].asString(), PLATFORM::Windows);
auto script_active = CP949(data["script"]["active"].asString(), PLATFORM::Windows);
assert_script(script_active, "function\\s+(on_active)\\(\\w+,\\s*\\w+\\)", "on_active", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_active(me, item)\n"
<< "\n"
<< "end";
#endif
});
if(types == "stuff")
{
return new fb::game::item::master(name,
icon,
color,
id,
price,
condition,
penalty,
capacity,
trade,
storage,
desc,
script_active);
}
if(types == "consume")
{
std::string bundle_type = CP949(data["bundle type"].asString(), PLATFORM::Windows); // package Ex) 동동주
// bundle Ex) 도토리
if (bundle_type == "package")
return new fb::game::pack::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active, capacity);
else
return new fb::game::consume::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active);
}
auto& option = data["equipment option"];
auto look = option["look"].asInt();
auto durability = option["durability"].asInt();
auto repair = fb::game::equipment::repair(option["repair"]["enabled"].asBool(), option["repair"]["price"].asDouble());
auto rename = fb::game::equipment::rename(option["rename"]["enabled"].asBool(), option["rename"]["price"].asInt());
auto hit = option["hit"].asInt();
auto damage = option["damage"].asInt();
auto strength = option["strength"].asInt();
auto intelligence = option["intelligence"].asInt();
auto dexteritry = option["dexteritry"].asInt();
auto base_hp = option["base_hp"].asInt();
auto base_mp = option["base_mp"].asInt();
auto hp_percent = option["hp percent"].asInt();
auto mp_percent = option["mp percent"].asInt();
auto healing_cycle = option["healing_cycle"].asInt();
auto defensive = fb::game::defensive(option["defensive"]["physical"].asInt(), option["defensive"]["magical"].asInt());
auto script_dress = CP949(data["script"]["dress"].asString(), PLATFORM::Windows);
assert_script(script_dress, "function\\s+(on_dress)\\(\\w+,\\s*\\w+\\)", "on_dress", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_dress(me, item)\n"
<< "\n"
<< "end";
#endif
});
auto script_undress = CP949(data["script"]["undress"].asString(), PLATFORM::Windows);
assert_script(script_undress, "function\\s+(on_undress)\\(\\w+,\\s*\\w+\\)", "on_undress", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_undress(me, item)\n"
<< "\n"
<< "end";
#endif
});
if(types == "weapon")
{
auto damage_range_min = fb::game::range32_t(option["damage range"]["small"]["min"].asInt(), option["damage range"]["small"]["max"].asInt());
auto damage_range_max = fb::game::range32_t(option["damage range"]["large"]["min"].asInt(), option["damage range"]["large"]["max"].asInt());
auto sound = option["sound"].asInt();
auto spell = CP949(option["spell"].asString(), PLATFORM::Windows);
return new fb::game::weapon::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active, look, durability, repair, rename, script_dress, script_undress, hit, damage, strength, intelligence, dexteritry, base_hp, base_mp, hp_percent, mp_percent, healing_cycle, defensive, damage_range_min, damage_range_max, sound, spell);
}
if(types == "armor")
return new fb::game::armor::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active, look, durability, repair, rename, script_dress, script_undress, hit, damage, strength, intelligence, dexteritry, base_hp, base_mp, hp_percent, mp_percent, healing_cycle, defensive);
if(types == "helmet")
return new fb::game::helmet::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active, look, durability, repair, rename, script_dress, script_undress, hit, damage, strength, intelligence, dexteritry, base_hp, base_mp, hp_percent, mp_percent, healing_cycle, defensive);
if(types == "shield")
return new fb::game::shield::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active, look, durability, repair, rename, script_dress, script_undress, hit, damage, strength, intelligence, dexteritry, base_hp, base_mp, hp_percent, mp_percent, healing_cycle, defensive);
if(types == "ring")
return new fb::game::ring::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active, look, durability, repair, rename, script_dress, script_undress, hit, damage, strength, intelligence, dexteritry, base_hp, base_mp, hp_percent, mp_percent, healing_cycle, defensive);
if(types == "auxiliary")
return new fb::game::auxiliary::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active, look, durability, repair, rename, script_dress, script_undress, hit, damage, strength, intelligence, dexteritry, base_hp, base_mp, hp_percent, mp_percent, healing_cycle, defensive);
if(types == "bow")
return new fb::game::bow::master(name, icon, color, id, price, condition, penalty, capacity, trade, storage, desc, script_active, look, durability, repair, rename, script_dress, script_undress, hit, damage, strength, intelligence, dexteritry, base_hp, base_mp, hp_percent, mp_percent, healing_cycle, defensive);
return nullptr;
}
fb::game::item::conditions fb::game::container::item::to_condition(const Json::Value& data)
{
auto sex = fb::game::sex::BOTH;
if(data["condition"].isMember("sex"))
{
auto value = CP949(data["condition"]["sex"].asString(), PLATFORM::Windows);
if(value == "man")
sex = fb::game::sex::MAN;
else if(value == "woman")
sex = fb::game::sex::WOMAN;
else
throw std::runtime_error(fb::game::message::assets::INVALID_SEX);
}
return fb::game::item::conditions
(
data["condition"].isMember("level") ? data["condition"]["level"].asInt() : 0,
data["condition"].isMember("strength") ? data["condition"]["strength"].asInt() : 0,
data["condition"].isMember("dexteritry") ? data["condition"]["dexteritry"].asInt() : 0,
data["condition"].isMember("intelligence") ? data["condition"]["intelligence"].asInt() : 0,
data["condition"].isMember("class") ? data["condition"]["class"].asInt() : 0,
data["condition"].isMember("promotion") ? data["condition"]["promotion"].asInt() : 0,
sex
);
}
fb::game::item::penalties fb::game::container::item::to_penalty(const std::string& penalty)
{
if(penalty.empty())
return fb::game::item::penalties::NONE;
if(penalty == "drop")
return fb::game::item::penalties::DROP;
if(penalty == "destroy")
return fb::game::item::penalties::DESTRUCTION;
throw std::runtime_error(fb::game::message::assets::INVALID_DEATH_PENALTY);
}
fb::game::container::map::map()
{ }
fb::game::container::map::~map()
{
for(auto& x : *this)
delete x.second;
}
bool fb::game::container::map::load(const std::string& path, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
auto& config = fb::config::get();
auto group_mine = config["group"].asUInt();
auto count = fb::table::load
(
path,
[&] (Json::Value& key, Json::Value& data, double percentage)
{
auto id = std::stoi(key.asString());
auto name = CP949(data["name"].asString(), PLATFORM::Windows);
// Parse common data
uint16_t parent = data["parent"].asInt();
uint8_t bgm = data["bgm"].asInt();
auto effect = this->to_effect(CP949(data["effect"].asString(), PLATFORM::Windows));
auto option = this->to_option(data);
int group = data["host group"].asUInt();
auto required = (group == group_mine);
std::vector<char> binary;
Json::Value blocks;
if(required)
{
// Load binary
if(this->load_data(id, binary) == false)
throw std::runtime_error(fb::game::message::assets::CANNOT_LOAD_MAP_DATA);
// Load blocks
if(this->load_blocks(id, blocks) == false)
throw std::runtime_error(fb::game::message::assets::CANNOT_LOAD_MAP_BLOCK);
}
auto map = new fb::game::map(id, parent, bgm, name, option, effect, group, binary.data(), binary.size());
for (const auto& block : blocks)
map->block(block["x"].asInt(), block["y"].asInt(), true);
{ auto _ = std::lock_guard<std::mutex>(*mutex);
std::map<uint16_t, fb::game::map*>::insert(std::make_pair(id, map));
callback((map->name()), percentage);
}
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
auto name = CP949(data["name"].asString(), PLATFORM::Windows);
error(name, e);
}
);
complete(count);
return true;
}
bool fb::game::container::map::load_warps(const std::string& path, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
auto count = fb::table::load
(
path,
[&] (Json::Value& key, Json::Value& data, double percentage)
{
auto map_id = std::stoi(key.asString());
auto map = fb::game::table::maps[map_id];
if (map == nullptr)
return;
auto _ = std::lock_guard(*mutex);
for(auto& js_warp : data)
{
const point16_t before(js_warp["before"]["x"].asInt(), js_warp["before"]["y"].asInt());
if(js_warp.isMember("world"))
{
auto widx = js_warp["world"]["wm"].asInt();
auto world = fb::game::table::worlds[widx];
auto gidx = js_warp["world"]["group"].asInt();
auto& group = (*world)[gidx];
auto oidx = js_warp["world"]["offset"].asInt();
auto& offset = (*group)[oidx];
map->push_warp(offset.get(), before);
}
else
{
const point16_t after(js_warp["after"]["x"].asInt(), js_warp["after"]["y"].asInt());
const range8_t condition(js_warp["limit"]["min"].asInt(), js_warp["limit"]["max"].asInt());
auto next_map_id = js_warp["to"].asInt();
auto next_map = fb::game::table::maps[next_map_id];
map->push_warp(next_map, before, after, condition);
}
}
callback(map->name(), percentage);
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
auto map_id = std::stoi(key.asString());
auto map = fb::game::table::maps[map_id];
error(map->name(), e);
}
);
complete(count);
return true;
}
bool fb::game::container::item::load(const std::string& path, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
auto count = fb::table::load
(
path,
[&] (Json::Value& key, Json::Value& data, double percentage)
{
auto item = (fb::game::item::master*)nullptr;
auto name = CP949(data["name"].asString(), PLATFORM::Windows);
// Create x core
item = this->create(std::stoi(key.asString()), data);
uint16_t id = std::stoi(key.asString());
{
auto _ = std::lock_guard(*mutex);
std::map<uint16_t, fb::game::item::master*>::insert(std::make_pair(id, item));
callback(name, percentage);
}
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
auto name = CP949(data["name"].asString(), PLATFORM::Windows);
error(name, e);
}
);
complete(count);
return true;
}
fb::game::container::npc::npc()
{ }
fb::game::container::npc::~npc()
{
for(auto& x : *this)
delete x.second;
}
bool fb::game::container::npc::load(const std::string& path, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
auto count = fb::table::load
(
path,
[&](Json::Value& key, Json::Value& data, double percentage)
{
uint32_t id = std::stoi(key.asString());
auto name = CP949(data["name"].asString(), PLATFORM::Windows);
uint16_t look = data["look"].asInt() + 0x7FFF;
uint8_t color = data["color"].asInt();
auto script = CP949(data["script"].asString(), PLATFORM::Windows);
assert_script(script, "function\\s+(on_interact)\\(\\w+,\\s*\\w+\\)", "on_interact", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_interact(me, npc)\n"
<< "\n"
<< "end";
#endif
});
{
auto _ = std::lock_guard(*mutex);
std::map<uint16_t, fb::game::npc::master*>::insert(std::make_pair(id, new fb::game::npc::master(name, look, color, script)));
callback(name, percentage);
}
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
auto name = CP949(data["name"].asString(), PLATFORM::Windows);
error(name, e);
}
);
complete(count);
return true;
}
bool fb::game::container::npc::load_spawn(const std::string& path, fb::game::npc::listener* listener, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
auto& config = fb::config::get();
auto current_host = config["id"].asString();
auto count = fb::table::load
(
path,
[&](Json::Value& key, Json::Value& data, double percentage)
{
auto map_id = std::stoi(key.asString());
auto map = fb::game::table::maps[map_id];
if (map == nullptr)
return;
for (auto& spawn : data)
{
auto npc_id = spawn["npc"].asInt();
auto core = fb::game::table::npcs[npc_id];
if (core == nullptr)
continue;
point16_t position(spawn["position"]["x"].asInt(), spawn["position"]["y"].asInt());
auto direction_str = CP949(spawn["direction"].asString(), PLATFORM::Windows);
auto direction = fb::game::direction::BOTTOM;
if (direction_str == "top")
direction = fb::game::direction::TOP;
else if (direction_str == "right")
direction = fb::game::direction::RIGHT;
else if (direction_str == "bottom")
direction = fb::game::direction::BOTTOM;
else if (direction_str == "left")
direction = fb::game::direction::LEFT;
else
throw std::runtime_error(fb::game::message::assets::INVALID_NPC_DIRECTION);
auto cloned = new fb::game::npc(core, listener);
cloned->direction(direction);
{
auto _ = std::lock_guard(*mutex);
cloned->map(map, position);
callback(map->name(), percentage);
}
}
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
auto map_id = std::stoi(key.asString());
auto map = fb::game::table::maps[map_id];
error(map->name(), e);
}
);
complete(count);
return true;
}
fb::game::container::mob::mob()
{ }
fb::game::container::mob::~mob()
{
for(auto& x : *this)
delete x.second;
}
fb::game::mob::sizes fb::game::container::mob::to_size(const std::string& size)
{
if(size == "small")
return fb::game::mob::sizes::SMALL;
if(size == "large")
return fb::game::mob::sizes::LARGE;
throw std::runtime_error(fb::game::message::assets::INVALID_MOB_SIZE);
}
fb::game::mob::offensive_type fb::game::container::mob::to_offensive(const std::string& offensive)
{
if(offensive == "containment")
return fb::game::mob::offensive_type::CONTAINMENT;
if(offensive == "counter")
return fb::game::mob::offensive_type::COUNTER;
if(offensive == "none")
return fb::game::mob::offensive_type::NONE;
if(offensive == "non move")
return fb::game::mob::offensive_type::NON_MOVE;
if(offensive == "run away")
return fb::game::mob::offensive_type::RUN_AWAY;
throw std::runtime_error(fb::game::message::assets::INVALID_MOB_OFFENSIVE);
}
bool fb::game::container::mob::load(const std::string& path, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
auto count = fb::table::load
(
path,
[&] (Json::Value& key, Json::Value& data, double percentage)
{
uint16_t id = std::stoi(key.asString());
auto name = CP949(data["name"].asString(), PLATFORM::Windows);
uint16_t look = data["look"].asInt() + 0x7FFF;
uint8_t color = data["color"].asInt();
uint32_t base_hp = data["hp"].asInt();
uint32_t base_mp = data["mp"].asInt();
auto script_attack = CP949(data["script"]["attack"].asString(), PLATFORM::Windows);
assert_script(script_attack, "function\\s+(on_attack)\\(\\w+,\\s*\\w+\\)", "on_attack", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_attack(me, you)\n"
<< " return false\n"
<< "end";
#endif
});
auto script_die = CP949(data["script"]["die"].asString(), PLATFORM::Windows);
assert_script(script_die, "function\\s+(on_die)\\(\\w+,\\s*\\w+\\)", "on_die", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_die(me, you)\n"
<< "\n"
<< "end";
#endif
});
auto mob = new fb::game::mob::master(name, look, color, fb::game::defensive(data["defensive"]["physical"].asInt(), data["defensive"]["magical"].asInt()), base_hp, base_mp, data["experience"].asInt(), fb::game::mob::damage(data["damage"]["min"].asInt(), data["damage"]["max"].asInt()), this->to_offensive(CP949(data["offensive"].asString(), PLATFORM::Windows)), this->to_size(CP949(data["size"].asString(), PLATFORM::Windows)), std::chrono::milliseconds(data["speed"].asInt()), script_attack, script_die);
{
auto _ = std::lock_guard(*mutex);
std::map<uint16_t, fb::game::mob::master*>::insert(std::make_pair(id, mob));
callback(name, percentage);
}
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
auto name = CP949(data["name"].asString(), PLATFORM::Windows);
error(name, e);
}
);
complete(count);
return true;
}
bool fb::game::container::mob::load_drops(const std::string& path, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
char buffer[256] = {0,};
auto count = fb::table::load
(
path,
[&] (Json::Value& key, Json::Value& data, double percentage)
{
auto name = CP949(key.asString(), PLATFORM::Windows);
auto core = fb::game::table::mobs.name2mob(name);
if (core == nullptr)
{
sprintf(buffer, "%s (%s)", fb::game::message::assets::INVALID_MOB_NAME, name.c_str());
throw std::runtime_error(buffer);
}
for(auto& x : data)
{
float percentage = x["percentage"].asFloat();
auto item_name = CP949(x["item"].asString(), PLATFORM::Windows);
auto item_core = fb::game::table::items.name2item(item_name);
if (item_core == nullptr)
{
sprintf(buffer, "%s (%s)", fb::game::message::assets::INVALID_ITEM_NAME, item_name.c_str());
throw std::runtime_error(buffer);
}
{
auto _ = std::lock_guard(*mutex);
core->push_drop(fb::game::mob::drop(item_core, percentage));
}
}
{
auto _ = std::lock_guard(*mutex);
callback(name, percentage);
}
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
auto name = CP949(key.asString(), PLATFORM::Windows);
error("", e);
}
);
complete(count);
return true;
}
bool fb::game::container::mob::load_spawn(const std::string& path, fb::game::mob::listener* listener, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
auto count = fb::table::load
(
path,
[&] (Json::Value& key, Json::Value& data, double percentage)
{
auto map_id = std::stoi(key.asString());
auto map = fb::game::table::maps[map_id];
if (map == nullptr)
return;
for (auto& spawn : data)
{
auto mob_id = spawn["mob"].asInt();
auto core = fb::game::table::mobs[mob_id];
if (core == nullptr)
continue;
uint16_t x0 = spawn["area"]["x0"].asInt();
uint16_t x1 = spawn["area"]["x1"].asInt();
uint16_t y0 = spawn["area"]["y0"].asInt();
uint16_t y1 = spawn["area"]["y1"].asInt();
uint16_t count = spawn["count"].asInt();
uint32_t rezen = spawn["rezen time"].asInt();
{
auto _ = std::lock_guard(*mutex);
for (int i = 0; i < count; i++)
{
auto mob = static_cast<fb::game::mob*>(core->make(listener));
mob->spawn_point(x0, y0);
mob->spawn_size(x1, y1);
mob->respawn_time(std::chrono::seconds(rezen));
mob->map(map);
}
}
}
{
auto _ = std::lock_guard(*mutex);
callback(map->name(), percentage);
}
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
auto map_id = std::stoi(key.asString());
auto map = fb::game::table::maps[map_id];
error(map->name(), e);
}
);
complete(count);
return true;
}
fb::game::container::spell::spell()
{ }
fb::game::container::spell::~spell()
{
for(auto& x : *this)
delete x.second;
}
bool fb::game::container::spell::load(const std::string& path, fb::table::handle_callback callback, fb::table::handle_error error, fb::table::handle_complete complete)
{
auto mutex = std::make_unique<std::mutex>();
auto count = fb::table::load
(
path,
[&] (Json::Value& key, Json::Value& data, double percentage)
{
uint16_t id = std::stoi(key.asString());
const auto name = CP949(data["name"].asString(), PLATFORM::Windows);
uint8_t type = data["type"].asInt();
std::string cast;
if (data.isMember("cast"))
{
cast = CP949(data["cast"].asString(), PLATFORM::Windows);
assert_script(cast, "function\\s+(on_cast)\\(\\w+,\\s*.*\\)", "on_cast", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_cast(me, spell)\n"
<< "\n"
<< "end";
#endif
});
if(std::filesystem::exists(cast) == false)
cast = "";
}
std::string uncast;
if (data.isMember("uncast"))
{
uncast = CP949(data["uncast"].asString(), PLATFORM::Windows);
assert_script(uncast, "function\\s+(on_uncast)\\(\\w+,\\s*\\w+\\)", "on_uncast", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_uncast(me, spell)\n"
<< "\n"
<< "end";
#endif
});
if(std::filesystem::exists(uncast) == false)
uncast = "";
}
std::string concast;
if (data.isMember("concast"))
{
concast = CP949(data["concast"].asString(), PLATFORM::Windows);
assert_script(concast, "function\\s+(on_concast)\\(\\w+,\\s*\\w+\\)", "on_concast", [](const std::string& script)
{
#if defined DEBUG | defined _DEBUG
auto out = std::ofstream(script, std::ios_base::app);
out << "\n\nfunction on_concast(me, spell)\n"
<< "\n"
<< "end";
#endif
});
if(std::filesystem::exists(concast) == false)
concast = "";
}
std::string message;
if (data.isMember("message"))
{
message = CP949(data["message"].asString(), PLATFORM::Windows);
}
{
auto _ = std::lock_guard(*mutex);
std::map<uint16_t, fb::game::spell*>::insert(std::make_pair(id, new fb::game::spell(id, fb::game::spell::types(type), name, cast, uncast, concast, message)));
callback(name, percentage);
}
},
[&] (Json::Value& key, Json::Value& data, const std::string& e)
{
auto _ = std::lock_guard(*mutex);
const auto name = CP949(data["name"].asString(), PLATFORM::Windows);
error(name, e);
}
);
complete(count);
return true;