/
dwarf.cpp
3124 lines (2760 loc) · 114 KB
/
dwarf.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
/*
Dwarf Therapist
Copyright (c) 2009 Trey Stout (chmod)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "dwarf.h"
#include "dfinstance.h"
#include "trait.h"
#include "belief.h"
#include "dwarfjob.h"
#include "gamedatareader.h"
#include "customprofession.h"
#include "memorylayout.h"
#include "dwarftherapist.h"
#include "mainwindow.h"
#include "profession.h"
#include "dwarfstats.h"
#include "races.h"
#include "reaction.h"
#include "histfigure.h"
#include "fortressentity.h"
#include "columntypes.h"
#include "plant.h"
#include "bodypart.h"
#include "unitemotion.h"
#include "labor.h"
#include "preference.h"
#include "rolepreference.h"
#include "material.h"
#include "caste.h"
#include "roleaspect.h"
#include "squad.h"
#include "uniform.h"
#include "itemuniform.h"
#include "itemweapon.h"
#include "itemarmor.h"
#include "itemammo.h"
#include "iteminstrument.h"
#include <QAction>
#include <QClipboard>
#include <QDateTime>
#include <QDialog>
#include <QMessageBox>
#include <QTextEdit>
#include <QTreeWidgetItem>
#include <QVBoxLayout>
#include <QVector>
#include <QJSEngine>
Dwarf::Dwarf(DFInstance *df, VIRTADDR addr, QObject *parent)
: QObject(parent)
, m_id(-1)
, m_df(df)
, m_mem(df->memory_layout())
, m_address(addr)
, m_first_soul(0)
, m_race_id(-1)
, m_happiness(DH_FINE)
, m_happiness_desc("")
, m_stress_level(0)
, m_mood_id(MT_NONE)
, m_had_mood(false)
, m_artifact_name("")
, m_curse_name("")
, m_caste_id(-1)
, m_show_full_name(false)
, m_total_xp(0)
, m_migration_wave(0)
, m_body_size(60000)
, m_animal_type(none)
, m_raw_prof_id(-1)
, m_raw_profession(0)
, m_can_set_labors(false)
, m_locked_mood(false)
, m_stressed_mood(false)
, m_current_job_id(-1)
, m_hist_figure(0x0)
, m_squad_id(-1)
, m_squad_position(-1)
, m_pending_squad_name(QString::null)
, m_age(0)
, m_noble_position("")
, m_is_pet(false)
, m_race(0)
, m_caste(0)
, m_pref_tooltip(QString::null)
, m_emotions_desc(QString::null)
, m_is_child(false)
, m_is_baby(false)
, m_is_animal(false)
, m_true_name("")
, m_true_birth_year(0)
, m_is_valid(false)
, m_uniform(0x0)
, m_goals_realized(0)
, m_worst_rust_level(0)
, m_labor_reason("")
, m_histfig_id(0)
, m_occ_type(OCC_NONE)
, m_can_assign_military(true)
, m_active_military(false)
, m_is_citizen(true)
, m_curse_type(eCurse::NONE)
{
read_settings();
read_data();
connect(DT, SIGNAL(settings_changed()), this, SLOT(read_settings()));
// setup context actions
#ifdef QT_DEBUG
m_actions_memory.clear();
QAction *dump_mem = new QAction(tr("Dump Memory..."), this);
connect(dump_mem, SIGNAL(triggered()), SLOT(dump_memory()));
m_actions_memory << dump_mem;
QAction *dump_mem_to_file = new QAction(tr("Dump Memory To File"), this);
connect(dump_mem_to_file, SIGNAL(triggered()), SLOT(dump_memory_to_file()));
m_actions_memory << dump_mem_to_file;
QAction *copy_address_to_clipboard = new QAction(tr("Copy Address to Clipboard"), this);
connect(copy_address_to_clipboard, SIGNAL(triggered()),SLOT(copy_address_to_clipboard()));
m_actions_memory << copy_address_to_clipboard;
#endif
}
Dwarf::~Dwarf() {
qDeleteAll(m_actions_memory);
m_actions_memory.clear();
m_traits.clear();
m_beliefs.clear();
m_conflicting_beliefs.clear();
m_skills.clear();
m_sorted_skills.clear();
m_moodable_skills.clear();
m_attributes.clear();
m_labors.clear();
m_pending_labors.clear();
m_role_ratings.clear();
m_raw_role_ratings.clear();
m_sorted_role_ratings.clear();
m_sorted_custom_role_ratings.clear();
m_states.clear();
qDeleteAll(m_grouped_preferences);
m_grouped_preferences.clear();
qDeleteAll(m_emotions);
m_emotions.clear();
m_thoughts.clear();
m_syndromes.clear();
m_inventory_grouped.clear();
m_hist_figure = 0;
m_uniform = 0;
m_race = 0;
m_caste = 0;
m_raw_profession = 0;
m_mem = 0;
m_df = 0;
disconnect(DT, SIGNAL(settings_changed()), this, SLOT(read_settings()));
}
bool Dwarf::has_invalid_flags(QHash<uint, QString> invalid_flags, quint32 dwarf_flags){
foreach(uint invalid_flag, invalid_flags.uniqueKeys()) {
QString reason = invalid_flags[invalid_flag];
if(dwarf_flags & invalid_flag) {
set_validation(QString("%1 flags:%2").arg(reason).arg(dwarf_flags));
return true;
}
}
return false;
}
void Dwarf::read_settings() {
/* This is pretty fucked up. There is no good way to let the options for
full name, or show dabbling to just update existing cells. Will have to
send signals when this stuff changes, or just bite the bullet and
subclass the QStandardItem for the name items in the main model
*/
QSettings *s = DT->user_settings();
bool new_show_full_name = s->value("options/show_full_dwarf_names",false).toBool();
if (new_show_full_name != m_show_full_name) {
build_names();
emit name_changed();
}
m_show_full_name = new_show_full_name;
}
void Dwarf::read_data() {
if (!m_df || !m_df->memory_layout() || !m_df->memory_layout()->is_valid()) {
LOGW << "refresh unit called but we're not connected";
return;
}
// make sure our reference is up to date to the active memory layout
m_mem = m_df->memory_layout();
TRACE << "Starting refresh of unit data at" << hexify(m_address);
int civ_id = m_df->read_int(m_address +m_mem->dwarf_offset("civ"));
TRACE << " CIV:" << civ_id;
//read the core information we need to validate if we should continue loading this unit
read_id();
read_flags();
read_race(); //also sets m_is_animal
read_first_name();
read_last_name(m_address + m_mem->dwarf_offset("name"));
read_nick_name();
build_names(); //build names now for logging
read_states(); //read states before job and validation
read_caste(); //read before age
read_turn_count(); //load time/date stuff for births/migrations - read before age
set_age_and_migration(m_address + m_mem->dwarf_offset("birth_year"), m_address + m_mem->dwarf_offset("birth_time")); //set age before profession, after caste
m_raw_prof_id = m_df->read_byte(m_address + m_mem->dwarf_offset("profession"));
m_raw_profession = GameDataReader::ptr()->get_profession(m_raw_prof_id);
m_histfig_id = m_df->read_int(m_address + m_mem->dwarf_offset("hist_id"));
bool validated = true;
//attempt to do some initial filtering on the civilization
if(civ_id != m_df->dwarf_civ_id()){
set_validation("doesn't belong to our civilization",&validated,false,LL_DEBUG);
}else if(!m_is_animal){
//filter out babies/children based on settings
if(DT->hide_non_adults() && !is_adult()){
set_validation("IGNORING child/baby",&validated,false,LL_DEBUG);
}
//filter out any non-mercenary visitors if necessary
if (m_df->fortress()->address())
m_is_citizen = m_df->fortress()->hist_figures().contains(m_histfig_id);
TRACE << "HIST_FIG_ID:" << m_histfig_id;
if(DT->hide_non_citizens() && !m_is_citizen && m_occ_type != OCC_MERC){
set_validation("IGNORING visitor/guest",&validated,false,LL_DEBUG);
}
}
//check caged untame but trainable beasts
if(get_flag_value(FLAG_CAGED)){
if(!get_flag_value(FLAG_TAME)){
//if it's a caged, trainable beast, keep it in our list
if(m_race){
LOGI << QString("FOUND caged creature!");
validated = m_race->caste_flag(TRAINABLE);
}
}
}
//if the initial validation has passed and the soul is valid, perform a more in depth check with the flags
if(validated && read_soul()){
this->validate();
}
if(m_is_valid){
m_hist_figure = new HistFigure(m_histfig_id,m_df,this);
// use fake identity to match DF
find_fake_ident();
read_squad_info(); //read squad before job
read_gender_orientation(); //read before profession
read_profession(); //read profession before building the names, and before job
read_mood(); //read after profession and before job, emotions/skills (soul aspect)
read_labors(); //read after profession and mood
read_uniform(); //read after labors (hunters, miners, wood cutters)
check_availability(); //after labors/profession/age
read_current_job();
read_syndromes(); //read syndromes before attributes
read_body_size(); //body size after caste and age
//curse check will change the name and age
read_curse(); //read curse before attributes
read_soul_aspects(); //assumes soul already read, and requires caste to be read first
read_animal_type(); //need skills loaded to check for hostiles
read_noble_position();
read_preferences();
if(!m_is_animal){
m_unit_health = UnitHealth(m_df,this,!DT->user_settings()->value("options/diagnosis_not_required", false).toBool());
}else if(DT->user_settings()->value("options/animal_health", false).toBool()){
m_unit_health = UnitHealth(m_df,this,false);
}
read_inventory();
if(m_is_animal || m_nice_name == "")
build_names(); //calculate names again as we need to check tameness for animals
}
if(m_is_valid){
LOGI << QString("FOUND %1 (%2) name:%3 id:%4 histfig_id:%5")
.arg(race_name(true)).arg(hexify(m_address))
.arg(m_nice_name).arg(m_id).arg(m_histfig_id);
}
}
void Dwarf::refresh_minimal_data(){
if(m_is_valid){
read_flags(); //butcher/caged
read_nick_name();
read_labors();
read_profession();
build_names();
}
}
bool Dwarf::validate(){
if (m_mem->is_complete()) {
//check for migrants (which aren't dead/killed/ghosts)
if(this->state_value(FLAG_MIGRANT) > 0
&& (!get_flag_value(FLAG_DEAD) || get_flag_value(FLAG_INCOMING))
&& !get_flag_value(FLAG_KILLED)
&& !get_flag_value(FLAG_GHOST)){
set_validation("migrant",&m_is_valid,true);
return true;
}
//check opposed to life
if(m_curse_flags & eCurse::OPPOSED_TO_LIFE){
set_validation("appears to be a hostile undead!",&m_is_valid);
return false;
}
if(m_is_animal && (get_flag_value(FLAG_TAME) || get_flag_value(FLAG_CAGED))){ //tame or caged animals
//exclude cursed animals, this may be unnecessary with the civ check
//the full curse information hasn't been loaded yet, so just read the curse name
QString curse_name = m_df->read_string(m_address + m_mem->dwarf_offset("curse"));
if(!curse_name.isEmpty()){
set_validation("appears to be cursed or undead",&m_is_valid);
return false;
}
}
//check other invalid flags (invaders, ghosts, dead, merchants, etc.)
if(has_invalid_flags(m_mem->invalid_flags_1(),m_unit_flags.at(0)) ||
has_invalid_flags(m_mem->invalid_flags_2(),m_unit_flags.at(1)) ||
has_invalid_flags(m_mem->invalid_flags_3(),m_unit_flags.at(2))){
m_is_valid = false;
return false;
}
if(!m_first_soul){
set_validation("appears to be soulless",&m_is_valid);
return false;
}
m_is_valid = true;
return true;
}else{
m_is_valid = false;
return false;
}
}
void Dwarf::set_validation(QString reason, bool *valid_var, bool valid, LOG_LEVEL l){
QString msg = QString("%1 %2 name:%3 id:%4 reason:%5")
.arg(valid ? tr("FOUND") : tr("IGNORING"))
.arg(race_name()).arg(m_nice_name).arg(m_id).arg(reason);
if(l == LL_DEBUG){
LOGD << msg;
}else{
LOGI << msg;
}
if(valid_var)
*valid_var = valid;
}
void Dwarf::set_age_and_migration(VIRTADDR birth_year_offset, VIRTADDR birth_time_offset){
m_birth_year = m_df->read_int(birth_year_offset);
m_age = m_df->current_year() - m_birth_year;
m_birth_time = m_df->read_int(birth_time_offset);
quint32 arrival_time = m_df->current_time() - m_turn_count;
quint32 arrival_year = arrival_time / m_df->ticks_per_year;
quint32 arrival_season = (arrival_time % m_df->ticks_per_year) / m_df->ticks_per_season;
quint32 arrival_month = (arrival_time % m_df->ticks_per_year) / m_df->ticks_per_month;
quint32 arrival_day = ((arrival_time % m_df->ticks_per_year) % m_df->ticks_per_month) / m_df->ticks_per_day;
m_ticks_since_birth = m_age * m_df->ticks_per_year + m_df->current_year_time() - m_birth_time;
//this way we have the right sort order and all the data needed for the group by migration wave
m_migration_wave = 100000 * arrival_year + 10000 * arrival_season + 100 * arrival_month + arrival_day;
m_born_in_fortress = (m_ticks_since_birth == m_turn_count);
m_age_in_months = m_ticks_since_birth / m_df->ticks_per_month;
if(m_caste){
if(m_age == 0 || m_ticks_since_birth < m_caste->baby_age() * m_df->ticks_per_year)
m_is_baby = true;
else if(m_ticks_since_birth < m_caste->child_age() * m_df->ticks_per_year)
m_is_child = true;
}
}
QString Dwarf::get_migration_desc(){
qint32 wave = m_migration_wave;
quint32 season = 0;
quint32 year = 0;
quint32 month = 0;
quint32 day = 0;
QString suffix = "th";
day = (wave % 100) + 1;
month = (wave / 100) % 100;
season = (wave / 10000) % 10;
year = wave / 100000;
if ((day == 1) || (day == 21))
suffix = "st";
else if ((day == 2) || (day == 22))
suffix = "nd";
else if ((day == 3) || (day == 23))
suffix = "rd";
if(m_born_in_fortress){
return QString("Born on the %1%4 of %2 in the year %3").arg(day).arg(GameDataReader::ptr()->m_months.at(month)).arg(year).arg(suffix);
}else{
return QString("Arrived in the %2 of the year %1").arg(year).arg(GameDataReader::ptr()->m_seasons.at(season));
}
}
/*******************************************************************************
DATA POPULATION METHODS
*******************************************************************************/
void Dwarf::read_id() {
m_id = m_df->read_int(m_address + m_mem->dwarf_offset("id"));
TRACE << "UNIT ID:" << m_id;
}
static const char *sex_interest_icon_suffix (Dwarf::SEX_COMMITMENT interest)
{
switch (interest) {
case Dwarf::COMMIT_LOVER:
return "i";
case Dwarf::COMMIT_MARRIAGE:
return "c";
default:
LOGE << "Invalid interest in suffix";
return "";
}
}
void Dwarf::read_gender_orientation() {
QSettings *s = DT->user_settings();
auto gender_info_option = s->value("options/gender_info", Option_ShowOrientation).toInt();
bool show_orientation = gender_info_option >= Option_ShowOrientation;
bool show_commitment = !m_is_animal && gender_info_option >= Option_ShowCommitment;
BYTE sex = m_df->read_byte(m_address + m_mem->dwarf_offset("sex"));
TRACE << "GENDER:" << sex;
m_gender_info.gender = static_cast<GENDER_TYPE>(sex);
m_gender_info.orientation = ORIENT_HETERO; //default
QStringList icon_name;
if(m_gender_info.gender == SEX_UNK){
icon_name.append("sex-unknown");
}else if(m_gender_info.gender == SEX_M){
icon_name.append("male");
}else{
icon_name.append("female");
}
int orient_offset = m_mem->soul_detail("orientation");
if(m_gender_info.gender != SEX_UNK && m_first_soul && orient_offset != -1){
quint32 orientation = m_df->read_addr(m_first_soul + orient_offset);
m_gender_info.male = static_cast<SEX_COMMITMENT>((orientation & (3<<1))>>1);
m_gender_info.female = static_cast<SEX_COMMITMENT>((orientation & (3<<3))>>3);
SEX_COMMITMENT other, same;
if (m_gender_info.gender == SEX_M) {
other = m_gender_info.female;
same = m_gender_info.male;
}
else {
other = m_gender_info.male;
same = m_gender_info.female;
}
if (other && same) {
if (show_orientation)
icon_name.append("bi");
if (show_commitment) {
icon_name.append(sex_interest_icon_suffix(m_gender_info.male));
icon_name.append(sex_interest_icon_suffix(m_gender_info.female));
}
m_gender_info.orientation = ORIENT_BISEXUAL;
}
else if (other) {
if (show_orientation)
icon_name.append("hetero");
if (show_commitment)
icon_name.append(sex_interest_icon_suffix(other));
m_gender_info.orientation = ORIENT_HETERO;
}
else if (same) {
if (show_orientation)
icon_name.append("homo");
if (show_commitment)
icon_name.append(sex_interest_icon_suffix(same));
m_gender_info.orientation = ORIENT_HOMO;
}
else {
if (show_orientation)
icon_name.append("asexual");
if (show_commitment)
icon_name.append("bg");
m_gender_info.orientation = ORIENT_ASEXUAL;
}
}
icon_name.removeAll("");
m_icn_gender = QString(":img/%1.png").arg(icon_name.join("-"));
QStringList gender_desc;
gender_desc << get_gender_desc(m_gender_info.gender);
if (show_orientation)
gender_desc << get_orientation_desc(m_gender_info.orientation);
if (show_commitment) {
static const QStringList preferences = { tr("Not interested in %1"), tr("Likes %1"), tr("Will marry %1") };
for (const auto &t: {
std::make_tuple(m_gender_info.male, tr("males")),
std::make_tuple(m_gender_info.female, tr("females")) }) {
auto pref = std::get<0>(t);
if (pref) {
if (pref < preferences.count())
gender_desc << preferences[std::get<0>(t)].arg(std::get<1>(t));
else
gender_desc << "Invalid orientation flag";
}
}
}
gender_desc.removeAll("");
m_gender_info.full_desc = gender_desc.join(" - ");
}
void Dwarf::read_mood(){
m_mood_id = static_cast<MOOD_TYPE>(m_df->read_short(m_address + m_mem->dwarf_offset("mood")));
int temp_offset = m_mem->dwarf_offset("temp_mood");
if(m_mood_id == MT_NONE && temp_offset != -1){
short temp_mood = m_df->read_short(m_address + temp_offset); //check temporary moods
if(temp_mood > -1)
m_mood_id = static_cast<MOOD_TYPE>(10 + temp_mood); //appended to craft/stress moods enum
}
//baby are currently ignored
if(m_mood_id == MT_BABY){
m_mood_id = MT_NONE;
}
//check if they've had a mood/artifact if they're not currently in a craft-type mood
if(m_mood_id == MT_NONE || (int)m_mood_id > 4){
if(get_flag_value(FLAG_HAD_MOOD)){
m_had_mood = true;
m_artifact_name = m_df->get_translated_word(m_address + m_mem->dwarf_offset("artifact_name"));
}
//filter out any other temporary combat moods, and set stressed mood flag
if(m_mood_id != MT_NONE && m_mood_id != MT_MARTIAL && m_mood_id != MT_ENRAGED){
m_stressed_mood = true;
}
}
//additionally if it's a mood that disables labors (trying to make an artifact or insane), set the flag
if(m_mood_id != MT_NONE &&
(
(m_mood_id == MT_BERSERK || m_mood_id == MT_INSANE || m_mood_id == MT_MELANCHOLY || m_mood_id == MT_TRAUMA) ||
(int)m_mood_id <= 4)
){
m_locked_mood = true;
}
}
void Dwarf::read_body_size(){
//actual size of the creature
int offset = m_mem->dwarf_offset("size_info");
if(offset){
m_body_size = m_df->read_int(m_address + offset);
}else{
LOGW << "Missing size_info offset!";
m_body_size = body_size(true);
}
}
int Dwarf::body_size(bool use_default) const {
//we include returning the default size because for the weapon columns, the size actually doesn't matter to DF (bug?)
if(use_default){
int def_size = 6000; //default adult size for a dwarf
if(m_is_baby)
def_size = 300;
else if(m_is_child)
def_size = 1500;
return def_size;
}else{
return m_body_size;
}
}
void Dwarf::read_animal_type(){
if(m_is_animal){
qint32 animal_offset = m_mem->dwarf_offset("animal_type");
if(animal_offset>=0)
m_animal_type = static_cast<TRAINED_LEVEL>(m_df->read_int(m_address + animal_offset));
//additionally if it's an animal set a flag if it's currently a pet
//since butchering available pets simply by setting the flag breaks shit in game
qint32 owner_offset = m_mem->dwarf_offset("pet_owner_id");
if(owner_offset >=0){
int pet_owner_id = m_df->read_int(m_address + owner_offset); //check for an owner
m_is_pet = (pet_owner_id > 0);
}else{
m_is_pet = (!m_first_name.isEmpty() && !m_last_name.isEmpty()); //assume that a first and last name on an animal is a pet
}
}
}
void Dwarf::read_states(){
//set of misc. traits and a value (cave adapt, migrant, likes outdoors, etc..)
m_states.clear();
uint states_offset = m_mem->dwarf_offset("states");
if(states_offset) {
VIRTADDR states_addr = m_address + states_offset;
QVector<VIRTADDR> entries = m_df->enumerate_vector(states_addr);
foreach(VIRTADDR entry, entries) {
m_states.insert(m_df->read_short(entry), m_df->read_int(entry+0x4));
}
}
}
void Dwarf::read_curse(){
QString curse_name = capitalizeEach(m_df->read_string(m_address + m_mem->dwarf_offset("curse")));
if(!curse_name.isEmpty()){
m_curse_type = eCurse::OTHER;
//keep track of the curse type; currently vampires and werebeasts are the only truly cursed creatures we
//want to be aware of for the purpose of highlighting them
//TODO: check for removed flags as well
if(m_curse_flags & eCurse::BLOODSUCKER){ //if(!has_flag(eCurse::BLOODSUCKER, m_curse_rem_flags) && has_flag(eCurse::BLOODSUCKER,m_curse_flags)){
m_curse_type = eCurse::VAMPIRE;
}
m_curse_name = curse_name;
}
}
void Dwarf::find_fake_ident(){
if(m_hist_figure->has_fake_identity()){
//save the true name for display
m_true_name = m_nice_name;
m_true_birth_year = m_birth_year;
//overwrite the default name, birth year with the assumed identity
m_first_name = m_hist_figure->fake_name();
m_nick_name = m_hist_figure->fake_nick_name();
read_last_name(m_hist_figure->fake_name_offset());
build_names();
//vamps also use a fake age
set_age_and_migration(m_hist_figure->fake_birth_year_offset(),m_hist_figure->fake_birth_time_offset());
}
}
int Dwarf::historical_id(){
return m_histfig_id;
}
HistFigure* Dwarf::hist_figure(){
return m_hist_figure;
}
void Dwarf::read_caste() {
m_caste_id = m_df->read_short(m_address + m_mem->dwarf_offset("caste"));
m_caste = m_race->get_caste_by_id(m_caste_id);
TRACE << "CASTE:" << m_caste_id;
}
void Dwarf::read_flags(){
m_unit_flags.clear();
quint32 flags1 = m_df->read_addr(m_address + m_mem->dwarf_offset("flags1"));
TRACE << " FLAGS1:" << hexify(flags1);
quint32 flags2 = m_df->read_addr(m_address + m_mem->dwarf_offset("flags2"));
TRACE << " FLAGS2:" << hexify(flags2);
quint32 flags3 = m_df->read_addr(m_address + m_mem->dwarf_offset("flags3"));
TRACE << " FLAGS3:" << hexify(flags3);
m_unit_flags << flags1 << flags2 << flags3;
m_pending_flags = m_unit_flags;
m_curse_flags = m_df->read_addr(m_address + m_mem->dwarf_offset("curse_add_flags1"));
// m_curse_flags2 = m_df->read_addr(m_address + m_mem->dwarf_offset("curse_add_flags2"));
}
void Dwarf::read_race() {
m_race_id = m_df->read_int(m_address + m_mem->dwarf_offset("race"));
m_race = m_df->get_race(m_race_id);
TRACE << "RACE ID:" << m_race_id;
if(m_race){
m_is_animal = (!m_race->flags().has_flag(CAN_LEARN) && m_race->caste_flag(TRAINABLE));
}
}
void Dwarf::read_first_name() {
m_first_name = m_df->read_string(m_address + m_mem->dwarf_offset("name") + m_mem->word_offset("first_name"));
if (m_first_name.size() > 1)
m_first_name[0] = m_first_name[0].toUpper();
TRACE << "FIRSTNAME:" << m_first_name;
}
void Dwarf::read_last_name(VIRTADDR name_offset) {
//Generic
bool use_generic = DT->user_settings()->value("options/use_generic_names", false).toBool();
m_translated_last_name = m_df->get_translated_word(name_offset);
if (use_generic)
m_last_name = m_translated_last_name;
else
m_last_name = m_df->get_language_word(name_offset);
}
void Dwarf::read_nick_name() {
m_nick_name = m_df->read_string(m_address + m_mem->dwarf_offset("name") + m_mem->word_offset("nickname"));
TRACE << "\tNICKNAME:" << m_nick_name;
m_pending_nick_name = m_nick_name;
}
void Dwarf::build_names() {
if (m_pending_nick_name.isEmpty()) {
m_nice_name = QString("%1 %2").arg(m_first_name, m_last_name);
m_translated_name = QString("%1 %2").arg(m_first_name, m_translated_last_name);
} else {
if (m_show_full_name) {
m_nice_name = QString("%1 '%2' %3").arg(m_first_name, m_pending_nick_name, m_last_name);
m_translated_name = QString("%1 '%2' %3").arg(m_first_name, m_pending_nick_name, m_translated_last_name);
} else {
m_nice_name = QString("'%1' %2").arg(m_pending_nick_name, m_last_name);
m_translated_name = QString("'%1' %2").arg(m_pending_nick_name, m_translated_last_name);
}
}
m_nice_name = m_nice_name.trimmed();
m_translated_name = m_translated_name.trimmed();
QString creature_name = "";
if(is_adult()){
creature_name = caste_name();
}else{
creature_name = race_name();
}
if(m_is_animal){
if(is_adult()){
//for adult animals, check their profession for training and adjust the name accordingly
if (m_raw_prof_id == 99) //trained war
creature_name = tr("War ") + m_race->name();
else if (m_raw_prof_id == 98) //trained hunt
creature_name = tr("Hunting ") + m_race->name();
}
//append pet name if necessary
if(!m_nice_name.isEmpty()){
m_nice_name = tr("%1 (%2)").arg(creature_name).arg(m_nice_name);
}
}
//if there's still no name, use the creature name
if(m_nice_name==""){
m_nice_name = capitalizeEach(creature_name);
m_translated_name = "";
}
}
void Dwarf::read_profession() {
// first see if there is a custom prof set...
VIRTADDR custom_addr = m_address + m_mem->dwarf_offset("custom_profession");
m_custom_prof_name = m_df->read_string(custom_addr);
TRACE << "\tCUSTOM PROF:" << m_custom_prof_name;
// we set both to the same to know it hasn't been edited yet
m_pending_custom_profession = m_custom_prof_name;
QString prof_name = tr("Unknown Profession %1").arg(m_raw_prof_id);
if(m_raw_profession){
prof_name = m_raw_profession->name(is_male());
}
if (!m_custom_prof_name.isEmpty()) {
m_prof_name = m_custom_prof_name;
} else {
m_prof_name = prof_name;
}
if(is_animal()){
if(m_raw_prof_id == 102 && is_adult())
m_prof_name = tr("Adult"); //adult animals have a profession of peasant by default, just use adult
else if(m_is_child){
m_prof_name = tr("Child");
m_raw_prof_id = 103;
}else if(m_is_baby){
m_prof_name = tr("Baby");
m_raw_prof_id = 104;
}
}
int img_idx = 102; //default to peasant
if(m_raw_prof_id > -1 && m_raw_prof_id < GameDataReader::ptr()->get_professions().count())
img_idx = m_raw_prof_id + 1; //images start at 1, professions at 0, offest to match image
//set the default path for the profession icon
m_icn_prof = QPixmap(":/profession/prof_" + QString::number(img_idx) + ".png");
//see if we have a custom profession or icon override
CustomProfession *cp;
if(!m_custom_prof_name.isEmpty()){
cp = DT->get_custom_profession(m_custom_prof_name);
}else{
cp = DT->get_custom_prof_icon(m_raw_prof_id);
}
if(cp && cp->has_icon())
m_icn_prof = cp->get_pixmap();
LOGD << "reading profession for" << nice_name() << m_raw_prof_id << prof_name;
TRACE << "EFFECTIVE PROFESSION:" << m_prof_name;
}
void Dwarf::read_noble_position(){
m_noble_position = m_df->fortress()->get_noble_positions(m_histfig_id,is_male());
}
void Dwarf::read_preferences(){
if(m_is_animal)
return;
QVector<VIRTADDR> preferences = m_df->enumerate_vector(m_first_soul + m_mem->soul_detail("preferences"));
foreach(VIRTADDR pref, preferences){
auto pref_type = static_cast<PREF_TYPES>(m_df->read_short(pref));
//0x2 unk
auto pref_id = m_df->read_short(pref + 0x4);
//0x6 unk
auto item_sub_type = m_df->read_short(pref + 0x8);
auto mat_type = m_df->read_int(pref + 0xc);
auto mat_index = m_df->read_int(pref + 0x10);
auto mat_state = static_cast<MATERIAL_STATES>(m_df->read_short(pref + 0x14));
auto i_type = static_cast<ITEM_TYPE>(pref_id);
std::unique_ptr<Preference> p;
//for each preference type, we have some flags we need to check and add so we get matches to the role's preferences
//materials are the exception as all flags are passed in, moving forward it may be better to pass in flagarrays instead
switch(pref_type){
case LIKE_MATERIAL:
if (Material *m = m_df->find_material(mat_index,mat_type))
p = std::make_unique<MaterialPreference>(m, mat_state);
else {
LOGE << "Material for preference not found" << mat_type << mat_index;
}
break;
case LIKE_CREATURE:
if (Race* r = m_df->get_race(pref_id))
p = std::make_unique<CreaturePreference>(r);
else {
LOGE << "Creature for preference not found" << pref_id;
}
break;
case LIKE_FOOD:
{
QString pref_name = tr("Unknown");
if(mat_index < 0 || i_type==MEAT){
int creature_id = mat_index;
if(i_type==FISH)
creature_id = mat_type;
if (Race* r = m_df->get_race(creature_id))
pref_name = r->name().toLower();
else {
LOGE << "Creature for food preference not found" << creature_id;
}
}
else
pref_name = m_df->find_material_name(mat_index,mat_type,i_type,mat_state);
p = std::make_unique<Preference>(LIKE_FOOD, pref_name);
//TODO: add FoodPreference type
//p->set_item_type(i_type);
break;
}
case HATE_CREATURE:
if (Race* r = m_df->get_race(pref_id))
p = std::make_unique<CreatureDislike>(r);
else {
LOGE << "Creature for hate preference not found" << pref_id;
}
break;
case LIKE_ITEM:
if(item_sub_type >= 0 && Item::has_subtypes(i_type)) {
if (ItemSubtype *s = m_df->get_item_subtype(i_type,item_sub_type))
p = std::make_unique<ItemPreference>(s);
else {
LOGE << "Item subtype for preference not found" << i_type << item_sub_type;
}
}
else
p = std::make_unique<ItemPreference>(i_type, m_df->get_preference_item_name(pref_id,item_sub_type).toLower());
break;
case LIKE_PLANT:
if (Plant *plnt = m_df->get_plant(pref_id))
p = std::make_unique<PlantPreference>(plnt);
else {
LOGE << "Plant for preference not found" << pref_id;
}
break;
case LIKE_TREE:
if (Plant *plnt = m_df->get_plant(pref_id))
p = std::make_unique<TreePreference>(plnt);
else {
LOGE << "Tree for preference not found" << pref_id;
}
break;
case LIKE_COLOR:
case LIKE_SHAPE:
case LIKE_POETRY:
case LIKE_MUSIC:
case LIKE_DANCE:
p = std::make_unique<Preference>(pref_type, m_df->get_preference_other_name(pref_id, pref_type));
break;
default:
LOGE << "Unknown preference type" << pref_type;
}
if (!p)
p = std::make_unique<Preference>(pref_type, tr("Unknown"));
m_pref_names.append(p->get_name());
m_preferences.emplace(pref_type, std::move(p));
}
//add a special preference (actually a misc trait) for like outdoors
if (has_state(STATE_OUTDOORS))
m_preferences.emplace(
LIKE_OUTDOORS,
std::make_unique<OutdoorPreference>(state_value(STATE_OUTDOORS))
);
bool build_tooltip = (!m_is_animal && !m_preferences.empty() && DT->user_settings()->value("options/tooltip_show_preferences",true).toBool());
//group preferences into pref desc - values (string list)
QString desc_key;
//lists for the tooltip
QStringList likes;
QStringList consume;
QStringList hates;
QStringList other;
for (const auto &p: m_preferences) {
auto pType = static_cast<PREF_TYPES>(p.first);
desc_key = Preference::get_pref_desc(pType);
auto pref_name = p.second->get_name();
auto pref_desc = p.second->get_description();
if(!pref_name.isEmpty()){
//create/append to groups based on the categories description
if(!m_grouped_preferences.contains(desc_key))
m_grouped_preferences.insert(desc_key, new QStringList);
m_grouped_preferences.value(desc_key)->append(pref_name);
//build the tooltip at the same time, organizing by likes, dislikes
if(build_tooltip){
switch (pType) {
case LIKE_ITEM:
case LIKE_MATERIAL:
case LIKE_PLANT:
case LIKE_TREE:
case LIKE_CREATURE:
case LIKE_COLOR:
case LIKE_SHAPE:
case LIKE_POETRY:
case LIKE_MUSIC:
case LIKE_DANCE:
likes.append(pref_desc);
break;
case LIKE_FOOD:
consume.append(pref_desc);
break;
case HATE_CREATURE:
hates.append(pref_desc);
break;
default:
other.append(capitalize(pref_desc));
}
}
}
}
if(build_tooltip){
m_pref_tooltip.clear();