forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstockpiles.cpp
2834 lines (2559 loc) · 107 KB
/
stockpiles.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 "Core.h"
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "MiscUtils.h"
#include "modules/Materials.h"
#include "modules/Items.h"
#include "DataDefs.h"
#include "df/world.h"
#include "df/world_data.h"
#include "df/inorganic_raw.h"
#include "df/organic_mat_category.h"
#include "df/furniture_type.h"
#include "df/item_quality.h"
#include "df/item_type.h"
#include "df/creature_raw.h"
#include "df/caste_raw.h"
#include "df/material.h"
#include "df/inorganic_raw.h"
#include "df/plant_raw.h"
#include "df/stockpile_group_set.h"
#include "df/ui.h"
#include "df/building_stockpilest.h"
#include "df/stockpile_settings.h"
#include "df/global_objects.h"
#include "df/viewscreen_dwarfmodest.h"
#include <df/itemdef_ammost.h>
#include <df/itemdef_weaponst.h>
#include <df/itemdef_ammost.h>
#include <df/itemdef_trapcompst.h>
#include <df/itemdef_armorst.h>
#include <df/itemdef_helmst.h>
#include <df/itemdef_shoesst.h>
#include <df/itemdef_glovesst.h>
#include <df/itemdef_pantsst.h>
#include <df/itemdef_shieldst.h>
// protobuf
#include "proto/stockpiles.pb.h"
#include <google/protobuf/io/zero_copy_stream_impl.h>
// os
#include <sys/stat.h>
// stl
#include <functional>
#include <vector>
using std::vector;
using std::string;
using std::endl;
using namespace DFHack;
using namespace df::enums;
using namespace google::protobuf;
using namespace dfstockpiles;
DFHACK_PLUGIN ( "stockpiles" );
REQUIRE_GLOBAL ( world );
REQUIRE_GLOBAL ( ui );
REQUIRE_GLOBAL ( selection_rect );
using df::building_stockpilest;
using std::placeholders::_1;
static command_result copystock ( color_ostream &out, vector <string> & parameters );
static bool copystock_guard ( df::viewscreen *top );
static command_result savestock ( color_ostream &out, vector <string> & parameters );
static bool savestock_guard ( df::viewscreen *top );
static command_result loadstock ( color_ostream &out, vector <string> & parameters );
static bool loadstock_guard ( df::viewscreen *top );
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands )
{
if ( world && ui )
{
commands.push_back (
PluginCommand (
"copystock", "Copy stockpile under cursor.",
copystock, copystock_guard,
" - In 'q' or 't' mode: select a stockpile and invoke in order\n"
" to switch to the 'p' stockpile creation mode, and initialize\n"
" the custom settings from the selected stockpile.\n"
" - In 'p': invoke in order to switch back to 'q'.\n"
)
);
commands.push_back (
PluginCommand (
"savestock", "Save the active stockpile's settings to a file.",
savestock, savestock_guard,
"Must be in 'q' mode and have a stockpile selected.\n"
"example: 'savestock food.dfstock' will save the settings to 'food.dfstock'\n"
"in your stockpile folder.\n"
"Omitting the filename will result in text output directly to the console\n\n"
" -d, --debug: enable debug output\n"
" <filename> : filename to save stockpile settings to (will be overwritten!)\n"
)
);
commands.push_back (
PluginCommand (
"loadstock", "Load settings from a file and apply them to the active stockpile.",
loadstock, loadstock_guard,
"Must be in 'q' mode and have a stockpile selected.\n"
"example: 'loadstock food.dfstock' will load the settings from 'food.dfstock'\n"
"in your stockpile folder and apply them to the selected stockpile.\n"
" -d, --debug: enable debug output\n"
" <filename> : filename to load stockpile settings from\n"
)
);
}
std::cerr << "world: " << sizeof ( df::world ) << " ui: " << sizeof ( df::ui )
<< " b_stock: " << sizeof ( building_stockpilest ) << endl;
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
static bool copystock_guard ( df::viewscreen *top )
{
using namespace ui_sidebar_mode;
if ( !Gui::dwarfmode_hotkey ( top ) )
return false;
switch ( ui->main.mode )
{
case Stockpiles:
return true;
case BuildingItems:
case QueryBuilding:
return !!virtual_cast<building_stockpilest> ( world->selected_building );
default:
return false;
}
}
static command_result copystock ( color_ostream &out, vector <string> & parameters )
{
// HOTKEY COMMAND: CORE ALREADY SUSPENDED
// For convenience: when used in the stockpiles mode, switch to 'q'
if ( ui->main.mode == ui_sidebar_mode::Stockpiles )
{
world->selected_building = NULL; // just in case it contains some kind of garbage
ui->main.mode = ui_sidebar_mode::QueryBuilding;
selection_rect->start_x = -30000;
out << "Switched back to query building." << endl;
return CR_OK;
}
building_stockpilest *sp = virtual_cast<building_stockpilest> ( world->selected_building );
if ( !sp )
{
out.printerr ( "Selected building isn't a stockpile.\n" );
return CR_WRONG_USAGE;
}
ui->stockpile.custom_settings = sp->settings;
ui->main.mode = ui_sidebar_mode::Stockpiles;
world->selected_stockpile_type = stockpile_category::Custom;
out << "Stockpile options copied." << endl;
return CR_OK;
}
static bool savestock_guard ( df::viewscreen *top )
{
using namespace ui_sidebar_mode;
if ( !Gui::dwarfmode_hotkey ( top ) )
return false;
switch ( ui->main.mode )
{
case Stockpiles:
return true;
case BuildingItems:
case QueryBuilding:
return !!virtual_cast<building_stockpilest> ( world->selected_building );
default:
return false;
}
}
static bool loadstock_guard ( df::viewscreen *top )
{
using namespace ui_sidebar_mode;
if ( !Gui::dwarfmode_hotkey ( top ) )
return false;
switch ( ui->main.mode )
{
case Stockpiles:
return true;
case BuildingItems:
case QueryBuilding:
return !!virtual_cast<building_stockpilest> ( world->selected_building );
default:
return false;
}
}
// Utility Functions {{{
// A set of convenience functions for doing common lookups
/**
* Retrieve creature raw from index
*/
static df::creature_raw* find_creature ( int32_t idx )
{
return world->raws.creatures.all[idx];
}
/**
* Retrieve creature index from id string
* @return -1 if not found
*/
static int16_t find_creature ( const std::string &creature_id )
{
return linear_index ( world->raws.creatures.all, &df::creature_raw::creature_id, creature_id );
}
/**
* Retrieve plant raw from index
*/
static df::plant_raw* find_plant ( size_t idx )
{
return world->raws.plants.all[idx];
}
/**
* Retrieve plant index from id string
* @return -1 if not found
*/
static size_t find_plant ( const std::string &plant_id )
{
return linear_index ( world->raws.plants.all, &df::plant_raw::id, plant_id );
}
// }}} utility Functions
/**
* Helper class for mapping the various organic mats between their material indices
* and their index in the stockpile_settings structures.
*/
class OrganicMatLookup
{
// pair of material type and index
typedef std::pair<int16_t, int32_t> FoodMatPair;
// map for using type,index pairs to find the food index
typedef std::map<FoodMatPair, size_t> FoodMatMap;
public:
struct FoodMat
{
MaterialInfo material;
df::creature_raw *creature;
df::caste_raw * caste;
FoodMat() : material ( -1 ), creature ( 0 ), caste ( 0 ) {}
};
static void food_mat_by_idx ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector<int16_t>::size_type food_idx, FoodMat & food_mat )
{
out << "food_lookup: food_idx(" << food_idx << ") ";
df::world_raws &raws = world->raws;
df::special_mat_table table = raws.mat_table;
int32_t main_idx = table.organic_indexes[mat_category][food_idx];
int16_t type = table.organic_types[mat_category][food_idx];
if ( mat_category == organic_mat_category::Fish ||
mat_category == organic_mat_category::UnpreparedFish ||
mat_category == organic_mat_category::Eggs )
{
food_mat.creature = raws.creatures.all[type];
food_mat.caste = food_mat.creature->caste[main_idx];
out << " special creature type(" << type << ") caste("<< main_idx <<")" <<endl;
}
else
{
food_mat.material.decode ( type, main_idx );
out << " type(" << type << ") index("<< main_idx <<") token(" << food_mat.material.getToken() << ")" << endl;
}
}
static std::string food_token_by_idx ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector<int16_t>::size_type idx )
{
FoodMat food_mat;
food_mat_by_idx ( out, mat_category, idx, food_mat );
if ( food_mat.material.isValid() )
{
return food_mat.material.getToken();
}
else if ( food_mat.creature )
{
return food_mat.creature->creature_id + ":" + food_mat.caste->caste_id;
}
return std::string();
}
static size_t food_max_size ( organic_mat_category::organic_mat_category mat_category )
{
return world->raws.mat_table.organic_types[mat_category].size();
}
static void food_build_map ( std::ostream &out )
{
if ( index_built )
return;
df::world_raws &raws = world->raws;
df::special_mat_table table = raws.mat_table;
using df::enums::organic_mat_category::organic_mat_category;
df::enum_traits<organic_mat_category> traits;
for ( int32_t mat_category = traits.first_item_value; mat_category <= traits.last_item_value; ++mat_category )
{
for ( size_t i = 0; i < table.organic_indexes[mat_category].size(); ++i )
{
int16_t type = table.organic_types[mat_category].at ( i );
int32_t index = table.organic_indexes[mat_category].at ( i );
food_index[mat_category].insert ( std::make_pair ( std::make_pair ( type,index ), i ) ); // wtf.. only in c++
}
}
index_built = true;
}
static int16_t food_idx_by_token ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token )
{
int16_t food_idx = -1;
df::world_raws &raws = world->raws;
df::special_mat_table table = raws.mat_table;
out << "food_idx_by_token: ";
if ( mat_category == organic_mat_category::Fish ||
mat_category == organic_mat_category::UnpreparedFish ||
mat_category == organic_mat_category::Eggs )
{
std::vector<string> tokens;
split_string ( &tokens, token, ":" );
if ( tokens.size() != 2 )
{
out << "creature " << "invalid CREATURE:CASTE token: " << token << endl;
}
else
{
int16_t creature_idx = find_creature ( tokens[0] );
if ( creature_idx < 0 )
{
out << " creature invalid token " << tokens[0];
}
else
{
food_idx = linear_index ( table.organic_types[mat_category], creature_idx );
if ( tokens[1] == "MALE" )
food_idx += 1;
if (table.organic_types[mat_category][food_idx] == creature_idx )
out << "creature " << token << " caste " << tokens[1] << " creature_idx(" << creature_idx << ") food_idx("<< food_idx << ")" << endl;
else {
out << "ERROR creature caste not found: " << token << " caste " << tokens[1] << " creature_idx(" << creature_idx << ") food_idx("<< food_idx << ")" << endl;
food_idx = -1;
}
}
}
}
else
{
if ( !index_built )
food_build_map ( out );
MaterialInfo mat_info = food_mat_by_token ( out, token );
int16_t type = mat_info.type;
int32_t index = mat_info.index;
int16_t food_idx2 = -1;
auto it = food_index[mat_category].find ( std::make_pair ( type, index ) );
if ( it != food_index[mat_category].end() )
{
out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx(" << it->second << ")" << endl;
food_idx = it->second;
}
else
{
out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx not found :(" << endl;
}
}
return food_idx;
}
static MaterialInfo food_mat_by_token ( std::ostream &out, const std::string & token )
{
MaterialInfo mat_info;
mat_info.find ( token );
return mat_info;
}
static bool index_built;
static std::vector<FoodMatMap> food_index;
private:
OrganicMatLookup() {}
};
bool OrganicMatLookup::index_built = false;
std::vector<OrganicMatLookup::FoodMatMap> OrganicMatLookup::food_index = std::vector<OrganicMatLookup::FoodMatMap> ( 37 );
/**
* Null buffer that acts like /dev/null for when debug is disabled
*/
class NullBuffer : public std::streambuf
{
public:
int overflow ( int c )
{
return c;
}
};
class NullStream : public std::ostream
{
public:
NullStream() : std::ostream ( &m_sb ) {}
private:
NullBuffer m_sb;
};
/**
* Class for serializing the stockpile_settings structure into a Google protobuf
*/
class StockpileSerializer
{
public:
/**
* @param out for debugging
* @param stockpile stockpile to read or write settings to
*/
StockpileSerializer ( building_stockpilest * stockpile )
: mDebug ( false )
, mOut ( 0 )
, mNull()
, mPile ( stockpile )
{
// build other_mats indices
furniture_setup_other_mats();
bars_blocks_setup_other_mats();
finished_goods_setup_other_mats();
weapons_armor_setup_other_mats();
}
~StockpileSerializer() {}
void enable_debug ( color_ostream&out )
{
mDebug = true;
mOut = &out;
}
/**
* Since we depend on protobuf-lite, not the full lib, we copy this function from
* protobuf message.cc
*/
bool serialize_to_ostream(ostream* output)
{
mBuffer.Clear();
write();
{
io::OstreamOutputStream zero_copy_output(output);
if (!mBuffer.SerializeToZeroCopyStream(&zero_copy_output)) return false;
}
return output->good();
}
/**
* Will serialize stockpile settings to a file (overwrites existing files)
* @return success/failure
*/
bool serialize_to_file ( const std::string & file )
{
std::fstream output ( file, std::ios::out | std::ios::binary | std::ios::trunc );
return serialize_to_ostream(&output);
}
/**
* Again, copied from message.cc
*/
bool parse_from_istream(istream* input)
{
mBuffer.Clear();
io::IstreamInputStream zero_copy_input(input);
const bool res = mBuffer.ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
if( res ) read();
return res;
}
/**
* Read stockpile settings from file
*/
bool unserialize_from_file ( const std::string & file )
{
std::fstream input ( file, std::ios::in | std::ios::binary );
return parse_from_istream(&input);
}
private:
bool mDebug;
std::ostream * mOut;
NullStream mNull;
building_stockpilest * mPile;
StockpileSettings mBuffer;
std::map<int, std::string> mOtherMatsFurniture;
std::map<int, std::string> mOtherMatsFinishedGoods;
std::map<int, std::string> mOtherMatsBars;
std::map<int, std::string> mOtherMatsBlocks;
std::map<int, std::string> mOtherMatsWeaponsArmor;
std::ostream & debug()
{
if ( mDebug )
return *mOut;
return mNull;
}
/**
read memory structures and serialize to protobuf
*/
void write()
{
// debug() << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl;
write_general();
if ( mPile->settings.flags.bits.animals )
write_animals();
if ( mPile->settings.flags.bits.food )
write_food();
if ( mPile->settings.flags.bits.furniture )
write_furniture();
if ( mPile->settings.flags.bits.refuse )
write_refuse();
if ( mPile->settings.flags.bits.stone )
write_stone();
if ( mPile->settings.flags.bits.ammo )
write_ammo();
if ( mPile->settings.flags.bits.coins )
write_coins();
if ( mPile->settings.flags.bits.bars_blocks )
write_bars_blocks();
if ( mPile->settings.flags.bits.gems )
write_gems();
if ( mPile->settings.flags.bits.finished_goods )
write_finished_goods();
if ( mPile->settings.flags.bits.leather )
write_leather();
if ( mPile->settings.flags.bits.cloth )
write_cloth();
if ( mPile->settings.flags.bits.wood )
write_wood();
if ( mPile->settings.flags.bits.weapons )
write_weapons();
if ( mPile->settings.flags.bits.armor )
write_armor();
}
// parse serialized data into ui indices
void read ()
{
debug() << endl << "==READ==" << endl;
read_general();
read_animals();
read_food();
read_furniture();
read_refuse();
read_stone();
read_ammo();
read_coins();
read_bars_blocks();
read_gems();
read_finished_goods();
read_leather();
read_cloth();
read_wood();
read_weapons();
read_armor();
}
/**
* Find an enum's value based off the string label.
* @param traits the enum's trait struct
* @param token the string value in key_table
* @return the enum's value, -1 if not found
*/
template<typename E>
static typename df::enum_traits<E>::base_type linear_index ( std::ostream & out, df::enum_traits<E> traits, const std::string &token )
{
auto j = traits.first_item_value;
auto limit = traits.last_item_value;
// sometimes enums start at -1, which is bad news for array indexing
if ( j < 0 )
{
j += abs ( traits.first_item_value );
limit += abs ( traits.first_item_value );
}
for ( ; j <= limit; ++j )
{
// out << " linear_index("<< token <<") = table["<<j<<"/"<<limit<<"]: " <<traits.key_table[j] << endl;
if ( token.compare ( traits.key_table[j] ) == 0 )
return j;
}
return -1;
}
// read the token from the serailized list during import
typedef std::function<std::string ( const size_t& ) > FuncReadImport;
// add the token to the serialized list during export
typedef std::function<void ( const string & ) > FuncWriteExport;
// are item's of item_type allowed?
typedef std::function<bool ( item_type::item_type ) > FuncItemAllowed;
// is this material allowed?
typedef std::function<bool ( const MaterialInfo & ) > FuncMaterialAllowed;
// convenient struct for parsing food stockpile items
struct food_pair
{
// exporting
FuncWriteExport set_value;
std::vector<char> * stockpile_values;
// importing
FuncReadImport get_value;
size_t serialized_count;
bool valid;
food_pair ( FuncWriteExport s, std::vector<char>* sp_v, FuncReadImport g, size_t count )
: set_value ( s )
, stockpile_values ( sp_v )
, get_value ( g )
, serialized_count ( count )
, valid ( true )
{}
food_pair(): valid( false ) {}
};
/**
* There are many repeated (un)serialization cases throughout the stockpile_settings structure,
* so the most common cases have been generalized into generic functions using lambdas.
*
* The basic process to serialize a stockpile_settings structure is:
* 1. loop through the list
* 2. for every element that is TRUE:
* 3. map the specific stockpile_settings index into a general material, creature, etc index
* 4. verify that type is allowed in the list (e.g., no stone in gems stockpiles)
* 5. add it to the protobuf using FuncWriteExport
*
* The unserialization process is the same in reverse.
*/
void serialize_list_organic_mat ( FuncWriteExport add_value, const std::vector<char> * list, organic_mat_category::organic_mat_category cat )
{
if ( !list )
{
debug() << "serialize_list_organic_mat: list null" << endl;
}
for ( size_t i = 0; i < list->size(); ++i )
{
if ( list->at ( i ) )
{
std::string token = OrganicMatLookup::food_token_by_idx ( debug(), cat, i );
if ( !token.empty() )
{
add_value ( token );
debug() << " organic_material " << i << " is " << token << endl;
}
else
{
debug() << "food mat invalid :(" << endl;
}
}
}
}
/**
* @see serialize_list_organic_mat
*/
void unserialize_list_organic_mat ( FuncReadImport get_value, size_t list_size, std::vector<char> *pile_list, organic_mat_category::organic_mat_category cat )
{
pile_list->clear();
pile_list->resize ( OrganicMatLookup::food_max_size ( cat ), '\0' );
for ( size_t i = 0; i < list_size; ++i )
{
std::string token = get_value ( i );
int16_t idx = OrganicMatLookup::food_idx_by_token ( debug(), cat, token );
debug() << " organic_material " << idx << " is " << token << endl;
if ( idx >= pile_list->size() )
{
debug() << "error organic mat index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl;
continue;
}
pile_list->at ( idx ) = 1;
}
}
/**
* @see serialize_list_organic_mat
*/
void serialize_list_item_type ( FuncItemAllowed is_allowed, FuncWriteExport add_value, const std::vector<char> &list )
{
using df::enums::item_type::item_type;
df::enum_traits<item_type> type_traits;
debug() << "item_type size = " << list.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl;
for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i )
{
if ( list.at ( i ) )
{
const item_type type = ( item_type ) ( ( df::enum_traits<item_type>::base_type ) i );
std::string r_type ( type_traits.key_table[i+1] );
if ( !is_allowed ( type ) ) continue;
add_value ( r_type );
debug() << "item_type key_table[" << i+1 << "] type[" << ( int16_t ) type << "] is " << r_type <<endl;
}
}
}
/**
* @see serialize_list_organic_mat
*/
void unserialize_list_item_type ( FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector<char> *pile_list )
{
pile_list->clear();
pile_list->resize ( 112, '\0' ); // TODO remove hardcoded list size value
for ( int i = 0; i < pile_list->size(); ++i )
{
pile_list->at ( i ) = is_allowed ( ( item_type::item_type ) i ) ? 0 : 1;
}
using df::enums::item_type::item_type;
df::enum_traits<item_type> type_traits;
for ( int32_t i = 0; i < list_size; ++i )
{
const std::string token = read_value ( i );
// subtract one because item_type starts at -1
const df::enum_traits<item_type>::base_type idx = linear_index ( debug(), type_traits, token ) - 1;
const item_type type = ( item_type ) idx;
if ( !is_allowed ( type ) ) continue;
debug() << " item_type " << idx << " is " << token << endl;
if ( idx >= pile_list->size() )
{
debug() << "error item_type index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl;
continue;
}
pile_list->at ( idx ) = 1;
}
}
/**
* @see serialize_list_organic_mat
*/
void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, const std::vector<char> &list )
{
MaterialInfo mi;
for ( size_t i = 0; i < list.size(); ++i )
{
if ( list.at ( i ) )
{
mi.decode ( 0, i );
if ( !is_allowed ( mi ) ) continue;
debug() << " material " << i << " is " << mi.getToken() << endl;
add_value ( mi.getToken() );
}
}
}
/**
* @see serialize_list_organic_mat
*/
void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector<char> *pile_list )
{
// we initialize all possible (allowed) values to 0,
// then all other not-allowed values to 1
// why? because that's how the memory is in DF before
// we muck with it.
std::set<int32_t> idx_set;
pile_list->clear();
pile_list->resize ( world->raws.inorganics.size(), 0 );
for ( int i = 0; i < pile_list->size(); ++i )
{
MaterialInfo mi ( 0, i );
pile_list->at ( i ) = is_allowed ( mi ) ? 0 : 1;
}
for ( int i = 0; i < list_size; ++i )
{
const std::string token = read_value ( i );
MaterialInfo mi;
mi.find ( token );
if ( !is_allowed ( mi ) ) continue;
debug() << " material " << mi.index << " is " << token << endl;
if ( mi.index >= pile_list->size() )
{
debug() << "error material index too large! idx[" << mi.index << "] max_size[" << pile_list->size() << "]" << endl;
continue;
}
pile_list->at ( mi.index ) = 1;
}
}
/**
* @see serialize_list_organic_mat
*/
void serialize_list_quality ( FuncWriteExport add_value, const bool ( &quality_list ) [7] )
{
using df::enums::item_quality::item_quality;
df::enum_traits<item_quality> quality_traits;
for ( size_t i = 0; i < 7; ++i )
{
if ( quality_list[i] )
{
const std::string f_type ( quality_traits.key_table[i] );
add_value ( f_type );
debug() << " quality: " << i << " is " << f_type <<endl;
}
}
}
/**
* Set all values in a bool[7] to false
*/
void quality_clear ( bool ( &pile_list ) [7] )
{
std::fill ( pile_list, pile_list+7, false );
}
/**
* @see serialize_list_organic_mat
*/
void unserialize_list_quality ( FuncReadImport read_value, int32_t list_size, bool ( &pile_list ) [7] )
{
if ( list_size > 0 && list_size <= 7 )
{
using df::enums::item_quality::item_quality;
df::enum_traits<item_quality> quality_traits;
for ( int i = 0; i < list_size; ++i )
{
const std::string quality = read_value ( i );
df::enum_traits<item_quality>::base_type idx = linear_index ( debug(), quality_traits, quality );
if ( idx < 0 )
{
debug() << " invalid quality token " << quality << endl;
continue;
}
debug() << " quality: " << idx << " is " << quality << endl;
pile_list[idx] = true;
}
}
else
{
quality_clear ( pile_list );
}
}
/**
* @see serialize_list_organic_mat
*/
void serialize_list_other_mats ( const std::map<int, std::string> other_mats, FuncWriteExport add_value, std::vector<char> list )
{
for ( size_t i = 0; i < list.size(); ++i )
{
if ( list.at ( i ) )
{
const std::string token = other_mats_index ( other_mats, i );
if ( token.empty() )
{
debug() << " invalid other material with index " << i << endl;
continue;
}
add_value ( token );
debug() << " other mats " << i << " is " << token << endl;
}
}
}
/**
* @see serialize_list_organic_mat
*/
void unserialize_list_other_mats ( const std::map<int, std::string> other_mats, FuncReadImport read_value, int32_t list_size, std::vector<char> *pile_list )
{
pile_list->clear();
pile_list->resize ( other_mats.size(), '\0' );
for ( int i = 0; i < list_size; ++i )
{
const std::string token = read_value ( i );
size_t idx = other_mats_token ( other_mats, token );
if ( idx < 0 )
{
debug() << "invalid other mat with token " << token;
continue;
}
debug() << " other_mats " << idx << " is " << token << endl;
if ( idx >= pile_list->size() )
{
debug() << "error other_mats index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl;
continue;
}
pile_list->at ( idx ) = 1;
}
}
/**
* @see serialize_list_organic_mat
*/
void serialize_list_itemdef ( FuncWriteExport add_value, std::vector<char> list, std::vector<df::itemdef *> items, item_type::item_type type )
{
for ( size_t i = 0; i < list.size(); ++i )
{
if ( list.at ( i ) )
{
const df::itemdef *a = items.at ( i );
// skip procedurally generated items
if ( a->base_flags.is_set ( 0 ) ) continue;
ItemTypeInfo ii;
if ( !ii.decode ( type, i ) ) continue;
add_value ( ii.getToken() );
debug() << " itemdef type" << i << " is " << ii.getToken() << endl;
}
}
}
/**
* @see serialize_list_organic_mat
*/
void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size, std::vector<char> *pile_list, item_type::item_type type )
{
pile_list->clear();
pile_list->resize ( Items::getSubtypeCount ( type ), '\0' );
for ( int i = 0; i < list_size; ++i )
{
std::string token = read_value ( i );
ItemTypeInfo ii;
if ( !ii.find ( token ) ) continue;
debug() << " itemdef " << ii.subtype << " is " << token << endl;
if ( ii.subtype >= pile_list->size() )
{
debug() << "error itemdef index too large! idx[" << ii.subtype << "] max_size[" << pile_list->size() << "]" << endl;
continue;
}
pile_list->at ( ii.subtype ) = 1;
}
}
/**
* Given a list of other_materials and an index, return its corresponding token
* @return empty string if not found
* @see other_mats_token
*/
std::string other_mats_index ( const std::map<int, std::string> other_mats, int idx )
{
auto it = other_mats.find ( idx );
if ( it == other_mats.end() )
return std::string();
return it->second;
}
/**
* Given a list of other_materials and a token, return its corresponding index
* @return -1 if not found
* @see other_mats_index
*/
int other_mats_token ( const std::map<int, std::string> other_mats, const std::string & token )
{
for ( auto it = other_mats.begin(); it != other_mats.end(); ++it )
{
if ( it->second == token )
return it->first;
}
return -1;
}
void write_general()
{
mBuffer.set_max_bins ( mPile->max_barrels );
mBuffer.set_max_wheelbarrows ( mPile->max_wheelbarrows );
mBuffer.set_use_links_only ( mPile->use_links_only );
mBuffer.set_unknown1 ( mPile->settings.unk1 );
mBuffer.set_allow_inorganic ( mPile->settings.allow_inorganic );
mBuffer.set_allow_organic ( mPile->settings.allow_organic );
mBuffer.set_corpses ( mPile->settings.flags.bits.corpses );
}
void read_general()
{
if ( mBuffer.has_max_bins() )
mPile->max_bins = mBuffer.max_bins();
if ( mBuffer.has_max_wheelbarrows() )
mPile->max_wheelbarrows = mBuffer.max_wheelbarrows();
if ( mBuffer.has_max_barrels() )
mPile->max_barrels = mBuffer.max_barrels();