Repository navigation
Expand file tree
/
Copy pathcmdcraft.cpp
More file actions
4397 lines (3732 loc) · 144 KB
/
Copy pathcmdcraft.cpp
File metadata and controls
4397 lines (3732 loc) · 144 KB
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
/*
*
* Iter Vehemens ad Necem (IVAN)
* Copyright (C) Timo Kiviluoto
* Released under the GNU General
* Public License
*
* See LICENSING which should be included
* along with this file for more details
*
*/
/* compiled thru command.cpp */
#include <typeinfo>
#include "hiteffect.h"
#include "traps.h"
#include "dbgmsgproj.h"
#include "cmdcraftfilters.cpp"
std::vector<recipedata> vSuspended; //TODO suspendable action should be more global to be reused by other actions than crafting!
//recipedata* craftcore::prpdSuspended=NULL;
void craftcore::AddSuspended(const recipedata& rpd)
{
rpd.rc.integrityCheck();
if(!rpd.rc.IsCanBeSuspended())
ABORT("action can't be suspended %s",rpd.dbgInfo().CStr());
for(int i=0;i<vSuspended.size();i++)
if(vSuspended[i].id()==rpd.id())
ABORT("this crafting was already suspended '%s'",vSuspended[i].id().CStr());
// rpd.rc.ClearRefs();
vSuspended.push_back(rpd);
}
void craftcore::RemoveIfSuspended(const recipedata&rpd)
{
for(int i=0;i<vSuspended.size();i++)
if(vSuspended[i].id()==rpd.id()){
vSuspended.erase(vSuspended.begin()+i);
break;
}
}
recipedata craftcore::FindRecipedata(festring fsRpdId)
{
for(int i=0;i<vSuspended.size();i++)
if(vSuspended[i].id()==fsRpdId)
return vSuspended[i];
ABORT("unable to find recipedata id='%s'",fsRpdId.CStr());
return recipedata(); //dummy... just to let it be compiled...
}
void craftcore::ClearSuspendedList()
{
vSuspended.clear();
}
bool craftcore::EmptyContentsIfPossible(recipedata& rpd,item* itContainer, bool bMoveToInventory)
{
materialcontainer* mc = dynamic_cast<materialcontainer*>(itContainer); //potions, mines... also bananas xD
itemcontainer* ic = dynamic_cast<itemcontainer*>(itContainer); //chests
bool bEmptied = false;
if(mc!=NULL){
if(mc->GetSecondaryMaterial()!=NULL){
if(bMoveToInventory)
PrepareRemains(rpd,mc->GetSecondaryMaterial());
else
delete mc->RemoveSecondaryMaterial(); //prevents keeping: ex. random liquids like antidote
}
bEmptied = true;
}
if(ic!=NULL){ //empty chests etc
stack* stkC = ic->GetContained();
itemvector ivC;
stkC->FillItemVector(ivC);
for(int i=0;i<ivC.size();i++){
if(bMoveToInventory)
ivC[i]->MoveTo(rpd.rc.H()->GetStack());
else
SendToHellSafely(ivC[i]);
}
bEmptied = true;
}
return bEmptied;
}
/**
* Why this is important?
*
* Because if the item remain on slot, and 1 turn has not passed,
* what may happpen on crafting code in case user gives up on crafting action,
* it will provide inconsistent inventory contents (something that was sent to hell
* but is still on inventory as the turn has not passed to properly send it to hell).
*
* If all the above is correctly understood as "it is how thing work",
* then crafting should be fixed to always pass one turn even if user giveup?
* But that doesnt looks good, giveup = cancel, should not pass a turn at all.
*
* This shall be used for anything related to crafting until something better is coded.
*/
void craftcore::SendToHellSafely(item* it)
{
it->RemoveFromSlot(); //just in case to prevent problems later... like crashing elsewhere!!!
it->SendToHell(); //DBG3("SentToHell",it,it->GetID());//,lumpAtInv,lumpAtInv->GetID());
DBGITEM(it,"SentToHell:Safely");
// **rit=NULL;
}
float craftcore::CraftSkill(character* Char){ //is the current capability of successfully crafting
float fBonus = 0; // influence/weights of each stat will be the FINAL divider!
float fWeight = 0;
float fDivFinal = 0;
#define CALCSK(weig,attr) fWeight = weig; fBonus += Char->GetAttribute(attr)*weig; fDivFinal+=weig;
CALCSK(15.0,DEXTERITY); //by importance order
CALCSK(7.5,WISDOM);
CALCSK(3.0,PERCEPTION); //TODO could counter fumbles directly
CALCSK(3.0,ARM_STRENGTH); //TODO half if only one arm?
CALCSK(2.5,AGILITY);
CALCSK(2.5,INTELLIGENCE);
CALCSK(2.0,ENDURANCE); //TODO could lower 0.1 per turn crafting, til 0.0
CALCSK(1.5,WILL_POWER); //TODO could lower 0.1 per turn crafting, til 0.0
CALCSK(0.25,LEG_STRENGTH);
//TODO CHARISMA //if one day there is item quality, well finished, attribute that could increase sell price
//TODO MANA //if one day anything magical is allowed to be crafted
float fSkill = 0;
float fCharSkill = Char->GetCWeaponSkill(CRAFTING)->GetLevel(); // base/learned
// if polymorphed, goes as override (tho we cant poly into uniques right? we would need generic tailors and smiths...)
if(dynamic_cast<guard*>(Char))
fCharSkill=25;
else
if(dynamic_cast<kamikazedwarf*>(Char))
fCharSkill=50;
else
if(dynamic_cast<tailor*>(Char))
fCharSkill=75;
else
if(dynamic_cast<smith*>(Char))
fCharSkill=100;
fSkill += fCharSkill;
fSkill += fBonus/fDivFinal; // in short, if all stats are 10, craft skill would be 10
fSkill -= 10.0; // to make advancing important on the beggining
if(fSkill<1.0)fSkill=1.0; //safety
return fSkill;
}
bool craftcore::canBeCrafted(item* it){
if(dynamic_cast<lump*>(it)!=NULL)
return true;
if(dynamic_cast<stick*>(it)!=NULL) //checked when splitting
return true;
if(dynamic_cast<itemcontainer*>(it)!=NULL) //to allow crafting chests again
return true;
const itemdatabase* itdb = it->GetDataBase();
if(it->GetCategory()==POTION)
{
item* Bottle = dynamic_cast<potion*>(it);
if(Bottle && (Bottle->GetNameSingular() == "bottle" || Bottle->GetNameSingular() == "vial")) //TODO really necessary the singular name check? excessive?
{
if(!Bottle->GetSecondaryMaterial())
return true;
if( // extracting from corpses
// TODO may be the "kind of action" (as a parameter for this func) could allow some specific materials only, so a new action EXTRACT_FROM_CORPSE would allow only the ones below
Bottle->GetSecondaryMaterial()->GetConfig()==SULPHURIC_ACID ||
Bottle->GetSecondaryMaterial()->GetConfig()==POISON_LIQUID ||
Bottle->GetSecondaryMaterial()->GetConfig()==MAGIC_LIQUID
)
return true;
}
DBG3(Bottle->GetSecondaryMaterial()->GetConfig(),SULPHURIC_ACID,POISON_LIQUID);
return false;
}
// TODO allow near oven, but the problem is the ingredients... also may be blocked below too
if(it->GetCategory()==FOOD)
{
item* Can = dynamic_cast<can*>(it);
if(Can && !Can->GetSecondaryMaterial())
return true;
DBGLN;
return false;
}
//TODO all these things should have some easier kind of property to be checked
if(!craftcore::MoreCraftDeniedFilters(it)){
DBGLN;
return false;
}
if( // more complex/generic filter, better keep as last check?
game::IsQuestItem(it) ||
it->GetEnchantment()!=0 ||
it->GetCategory()==BOOK ||
it->GetCategory()==MISC ||
it->GetCategory()==SCROLL ||
!itdb->CanBeWished ||
itdb->Possibility <= 0 ||
!itdb->PostFix.IsEmpty() ||
false // just to make it easier to re-organize and add checks above
){
DBGLN;
return false;
}
return true;
}
bool craftcore::HasSuspended() {
return vSuspended.size()>0;
}
bool craftcore::ResumeSuspendedTo(character* Char,recipedata& rpd)
{
if(!HasSuspended())
ABORT("no suspended craft action to set to %s!",Char->GetName(DEFINITE).CStr());
if(Char->GetAction()!=NULL){DBG1("AlreadySomethingElse,How?");DBGSTK; //may never happen tho... TODO ABORT() ?
ADD_MESSAGE("You are already doing something else.");
return false;
}
rpd.rc.ClearRefs(); //good to cleanup, will be set again also
rpd.rc.integrityCheck();
bool bReqSamePos = false;
// if(rpd.bMeltable)bReqSamePos=true;
if(!rpd.v2AnvilLocation.Is0())bReqSamePos=true; DBGSV2(rpd.v2AnvilLocation);
if(!rpd.v2ForgeLocation.Is0())bReqSamePos=true; DBGSV2(rpd.v2ForgeLocation);
if(!rpd.v2WorkbenchLocation.Is0())bReqSamePos=true; DBGSV2(rpd.v2WorkbenchLocation);
if(!rpd.v2TailoringWorkbenchLocation.Is0())bReqSamePos=true;
if(rpd.otSpawnType!=CTT_NONE && !rpd.v2PlaceAt.Is0())bReqSamePos=true; DBG1(rpd.otSpawnType);
if(bReqSamePos){
if(rpd.rc.GetDungeonLevelID() != craftcore::CurrentDungeonLevelID()){
//TODO better message: place? location? dungeon level sounds a bit non-immersive, or not?
ADD_MESSAGE("You need to be in the same dungeon as before to continue crafting.");
return false;
}
if(rpd.v2PlayerCraftingAt != Char->GetPos()){
festring fsDist;
int iDist = (rpd.v2PlayerCraftingAt - Char->GetPos()).GetLengthSquare();
if(iDist<=2)fsDist<<"and you are almost there!";
else
if(iDist<=10)fsDist<<"and you are nearby.";
else
if(iDist<=20)fsDist<<"but you are still a bit far away from it...";
else
fsDist<<"but it seems to be quite far from here.";
ADD_MESSAGE("You need to be where you were crafting before, %s",fsDist.CStr());
game::SetDrawMapOverlay(true); //TODO make this optional?
game::PositionQuestion(CONST_S("It was here!"), rpd.v2PlayerCraftingAt, 0, 0, false);
game::SetDrawMapOverlay(false);
return false;
}
}else{
rpd.v2PlayerCraftingAt = Char->GetPos();
}
Char->SwitchToCraft(rpd);
return true;
}
void recipecore::Save(outputfile& SaveFile) const
{
integrityCheck();
SaveFile //commented ones are just to keep the clarity/organization
<< bCanBeSuspended
<< iDungeonLevelID
;
}
void recipedata::Save(outputfile& SaveFile) const
{
rc.Save(SaveFile);
SaveFile //commented ones are just to keep the clarity/organization
<< ingredientsIDs
<< iAddDexterity
<< iBaseTurnsToFinish
<< itSpawnTot
<< v2ForgeLocation
<< v2PlaceAt
<< bSuccesfullyCompleted
<< v2AnvilLocation
<< v2PlayerCraftingAt
<< fsCraftInfo
<< itToolID
<< itTool2ID
<< itSpawnType
<< fsItemSpawnSearchPrototype
<< itSpawnCfg
<< itSpawnMatMainCfg
<< itSpawnMatMainVol
<< itSpawnMatSecCfg
<< itSpawnMatSecVol
<< otSpawnCfg
<< otSpawnMatMainCfg
<< otSpawnMatMainVol
<< otSpawnMatSecCfg
<< otSpawnMatSecVol
<< otSpawnType
<< bSpawnBroken
<< fDifficulty
<< bCanBeBroken
<< bMeltable
<< v2WorkbenchLocation
<< iRemainingTurnsToFinish
<< bGradativeCraftOverride
<< bTailoringMode
<< v2TailoringWorkbenchLocation
<< lDamageFinalItem
;
}
void recipecore::Load(inputfile& SaveFile)
{
SaveFile //commented ones are just to keep the clarity/organization
>> bCanBeSuspended
>> iDungeonLevelID
;
}
void recipedata::Load(inputfile& SaveFile)
{
rc.Load(SaveFile);
SaveFile //commented ones are just to keep the clarity/organization
>> ingredientsIDs
>> iAddDexterity
>> iBaseTurnsToFinish
>> itSpawnTot
>> v2ForgeLocation
>> v2PlaceAt
>> bSuccesfullyCompleted
>> v2AnvilLocation
>> v2PlayerCraftingAt
>> fsCraftInfo
>> itToolID
>> itTool2ID
>> itSpawnType
>> fsItemSpawnSearchPrototype
>> itSpawnCfg
>> itSpawnMatMainCfg
>> itSpawnMatMainVol
>> itSpawnMatSecCfg
>> itSpawnMatSecVol
>> otSpawnCfg
>> otSpawnMatMainCfg
>> otSpawnMatMainVol
>> otSpawnMatSecCfg
>> otSpawnMatSecVol
>> otSpawnType
>> bSpawnBroken
>> fDifficulty
>> bCanBeBroken
>> bMeltable
>> v2WorkbenchLocation
>> iRemainingTurnsToFinish
>> bGradativeCraftOverride
>> bTailoringMode
>> v2TailoringWorkbenchLocation
>> lDamageFinalItem
;
// if(otSpawnType!=CTT_NONE)
// SaveFile >> otSpawn;
rc.integrityCheck();
}
cfestring recipedata::id() const
{
/**
* this is a simple way to detect if the recipedata is the same,
* here shall only contain things that would not change thru the
* whole crafting process/time
* TODO just use an ulong ID like chars and items do? but keep this as debug info at least!!!
*/
festring fs;
#define RPDINFO(o) fs<<(#o)<<"="<<(o)<<"; ";
RPDINFO(rc.IsCanBeSuspended());
RPDINFO(itToolID);
RPDINFO(itTool2ID);
RPDINFO(itSpawnCfg);
RPDINFO(itSpawnMatMainCfg);
RPDINFO(itSpawnMatMainVol);
RPDINFO(itSpawnMatSecCfg);
RPDINFO(itSpawnMatSecVol);
RPDINFO(otSpawnCfg);
RPDINFO(otSpawnMatMainCfg);
RPDINFO(otSpawnMatMainVol);
RPDINFO(otSpawnMatSecCfg);
RPDINFO(otSpawnMatSecVol);
RPDINFO(fsItemSpawnSearchPrototype);
RPDINFO(fsCraftInfo);
#define RPDINFOV2(o) fs<<(#o)<<"="<<(o.X)<<","<<(o.Y)<<"; ";
RPDINFOV2(v2AnvilLocation);
RPDINFOV2(v2ForgeLocation);
RPDINFOV2(v2WorkbenchLocation);
RPDINFOV2(v2TailoringWorkbenchLocation);
RPDINFOV2(v2PlaceAt);
RPDINFOV2(v2PlayerCraftingAt);
return fs;
}
cfestring recipedata::dbgInfo() const
{
festring fs;
fs << id();
return fs;
}
clock_t RPDInitKey = clock();
void recipecore::SetHumanoid(character* C){
integrityCheck(C);
if(C==NULL)
ABORT("actor can't be null, btw h='%s'",h==NULL?"NULL":h->GetName(DEFINITE).CStr());
if(C==h)
return;
humanoid* hNew = C->AsHumanoid();
if(hNew==NULL)
ABORT("Only humanoids can craft C='%s' h='%s'",C->GetName(DEFINITE).CStr(),h==NULL?"NULL":h->GetName(DEFINITE).CStr());
h = hNew;
}
void recipecore::integrityCheck(character* Actor) const
{
if(initKey!=RPDInitKey){ //FIRST TEST!!!!!!!!!!!!!!!!!!!!!
//TODO bools get crazy values too if not initialized
ABORT("recipedata corrupted, not initialized or invalid");// it will not be possible to show info, would crash on it... , dbgInfo().CStr());
}
}
recipecore::recipecore(humanoid* H,uint sel){
initKey = RPDInitKey;
h=H;
bCanBeSuspended=true;
iDungeonLevelID=craftcore::CurrentDungeonLevelID();
}
recipedata::recipedata(humanoid* H,uint sel) : rc(H,sel)
{
itTool=NULL;
itTool2=NULL;
lsqrPlaceAt = NULL;
lsqrCharPos = rc.H()==NULL ? NULL : game::GetCurrentLevel()->GetLSquare(rc.H()->GetPos());
itWeakestIngredient = NULL;
lsqrActor=NULL;
// no need to save
SelectedRecipe=sel;
bSpendCurrentTurn=false; //TODO review everywhere to let it work as expected instead of always spending a turn what ignores this
bAlreadyExplained=false;
bHasAllIngredients=false;
bCanStart=false;
bCanBePlaced=false;
xplodStr=0;
iStrongerXplod=0;
v2XplodAt=v2(0,0);
bOnlyXplodIfCriticalFumble=false;
bSpecialExtendedTurns=false;
iMinTurns=0;
bFailedTerminateCancel=false;
bFailedSuspend=false;
////////////////////////////////////////////////////////////////////////////////////
/// saveables
//////////////////////////////////
ingredientsIDs.clear(); //just to init
iAddDexterity=0;
iBaseTurnsToFinish=1; //TODO should be based on attributes
itSpawnTot=1;
v2ForgeLocation=v2(0,0);
v2PlaceAt=v2(0,0);
bSuccesfullyCompleted=false;
v2AnvilLocation=v2(0,0);
v2PlayerCraftingAt=v2(0,0);
fsCraftInfo="";
itToolID=0;
itTool2ID=0;
itSpawnType=CIT_NONE;
fsItemSpawnSearchPrototype="";
itSpawnCfg=0;
itSpawnMatMainCfg=0;
itSpawnMatMainVol=0;
itSpawnMatSecCfg=0;
itSpawnMatSecVol=0;
otSpawnCfg=0;
otSpawnMatMainCfg=0;
otSpawnMatMainVol=0;
otSpawnMatSecCfg=0;
otSpawnMatSecVol=0;
otSpawnType=CTT_NONE;
bSpawnBroken=false;
fDifficulty=1.0;
bCanBeBroken=true; //most can, anyway b4 breaking should be revalidated
bMeltable=false;
v2WorkbenchLocation=v2(0,0);
iRemainingTurnsToFinish=iBaseTurnsToFinish;
bGradativeCraftOverride=false;
bTailoringMode=false;
v2TailoringWorkbenchLocation=v2(0,0);
lDamageFinalItem=0;
}
int craftcore::CurrentDungeonLevelID(){
return game::GetCurrentDungeonIndex()*100+game::GetCurrentLevelIndex();
}
void recipedata::CopySpawnItemCfgFrom(item* itCfg)
{
rc.integrityCheck();
if(itCfg==NULL)
ABORT("NULL itCfg");
itSpawnCfg = itCfg->GetConfig();
material* matM = itCfg->GetMainMaterial();
itSpawnMatMainCfg = matM->GetConfig();
itSpawnMatMainVol = matM->GetVolume();
// itSpawnMatMainSpoilLevel = matM->GetSpoilLevel();
if(itCfg->GetSecondaryMaterial()!=NULL){
material* matS = itCfg->GetSecondaryMaterial();
itSpawnMatSecCfg = matS->GetConfig();
itSpawnMatSecVol = matS->GetVolume();
// itSpawnMatSecSpoilLevel = matS->GetSpoilLevel();
}
}
void recipedata::CopySpawnTerrainCfgFrom(olterrain* otCfg){
rc.integrityCheck();
if(otCfg==NULL)
ABORT("NULL otCfg");
otSpawnCfg = otCfg->GetConfig();
otSpawnMatMainCfg = otCfg->GetMainMaterial()->GetConfig();
otSpawnMatMainVol = otCfg->GetMainMaterial()->GetVolume();
if(otCfg->GetSecondaryMaterial()!=NULL){
otSpawnMatSecCfg = otCfg->GetSecondaryMaterial()->GetConfig();
otSpawnMatSecVol = otCfg->GetSecondaryMaterial()->GetVolume();
}
}
olterrain* crafthandle::SpawnTerrain(recipedata& rpd, festring& fsCreated){
rpd.rc.integrityCheck();
olterrain* otSpawn = NULL;
switch(rpd.otSpawnType){
case CTT_FURNITURE:
otSpawn=decoration::Spawn(rpd.otSpawnCfg);
game::CheckAddAutoMapNote(otSpawn->GetLSquareUnder());
break;
case CTT_DOOR:
otSpawn=door::Spawn(rpd.otSpawnCfg);
((door*)otSpawn)->SetIsLocked(false);
((door*)otSpawn)->SetIsOpened(true);
//TODO configure lock type based randomly in one of the keys available on player's inventory
break;
case CTT_WALL:
otSpawn=wall::Spawn(rpd.otSpawnCfg); //earth::Spawn();
break;
}
if(otSpawn==NULL)
ABORT("craft spawned no terrain.");
delete otSpawn->SetMainMaterial(material::MakeMaterial(rpd.otSpawnMatMainCfg,rpd.otSpawnMatMainVol));
if(rpd.otSpawnMatSecCfg>0)
delete otSpawn->SetSecondaryMaterial(material::MakeMaterial(rpd.otSpawnMatSecCfg,rpd.otSpawnMatSecVol));
fsCreated << "You built ";
rpd.lsqrPlaceAt->ChangeOLTerrainAndUpdateLights(otSpawn); //TODO a forge seems to not be emitting light...
fsCreated << otSpawn->GetName(INDEFINITE);
return otSpawn;
}
void recipecore::ClearRefs()
{
h = NULL;
}
void recipedata::ClearRefs(){
/**
* This is to help on granting consistency.
* As in case of save/load all pointers will change.
* This is important to revalidate all pointers from IDs.
*/
itTool = NULL;
itTool2 = NULL;
lsqrPlaceAt = NULL;
lsqrCharPos = NULL;
itWeakestIngredient = NULL;
lsqrActor = NULL;
rc.ClearRefs();
}
struct ci{ //create item info/helper/data/config/param
bool bMultSelect = true;
int iReqCfg=0;
bool bMainMaterRemainsBecomeLump=false;
bool bOverridesQuestion=false;
bool bMsgInsuficientMat=true;
bool bInstaAddIngredients=false;
bool bFirstMainMaterIsFilter=true;
int iReqMatCfgMain=0; //setting this overrides bFirstMainMaterIsFilter
int iReqMatCfgSec=0;
bool bJustAcceptFirstChosenAndReturn=false;
bool bAllowDegradation = false;
bool bFirstItemMustHaveFullVolumeRequired=false;
bool bAllowMeltables=true;
bool bAllowWood=true;
bool bAllowBones=true;
int iMinMainMaterStr=0;
float fUsablePercVol=1.0;
bool bMustBeTailorable=false;
bool bMixRemainingLump=true;
bool bAddEquippedItemsToChoiceList=false;
int iIngredientSlot=ingredient_slot_main;
};
struct recipe{
festring action;
festring name;
int iListIndex;
festring desc;
void init(cchar* act,cchar* nm){
action=(act);
name=(nm);
iListIndex=(-1);//,desc(""){};
}
virtual void fillInfo(){ ABORT("missing recipe info implementation"); }
void failPlacementMsg(recipedata& rpd){
ADD_MESSAGE("%s can't be placed here.",name.CStr());
rpd.SetAlreadyExplained();
}
void failIngredientsMsg(recipedata& rpd){
festring fsMsg;
fsMsg<<"Required ingredients to "<<action<<" "<<name<<" are not met.";
ADD_MESSAGE(fsMsg.CStr());
rpd.SetAlreadyExplained();
}
void failToolMsg(recipedata& rpd,festring tool){
ADD_MESSAGE("You don't have a strong enough %s to work on the requested materials.",tool.CStr());
// "strong enough" means that a too weak material wont be able to work on a much stronger material
rpd.SetAlreadyExplained();
}
bool IsTheSelectedOne(recipedata& rpd){
if(desc.IsEmpty())
fillInfo();
if(desc.IsEmpty())
ABORT("recipe info is still empty");
return rpd.SelectedRecipe == iListIndex;
}
virtual bool work(recipedata& rpd){ return false; }
virtual ~recipe(){}
bool whereRaw(recipedata& rpd,festring fsMsg,bool acceptSelfLocation=false){
int Dir = game::DirectionQuestion(fsMsg, false, acceptSelfLocation);
if(Dir != DIR_ERROR) {
v2 v2W = rpd.rc.H()->GetPos() + game::GetMoveVector(Dir);
if(rpd.rc.H()->GetArea()->IsValidPos(v2W)){
rpd.lsqrPlaceAt = rpd.rc.H()->GetNearLSquare(v2W);
return true;
}
}
return false;
}
bool where(recipedata& rpd,bool acceptSelfLocation=false)
{
if(whereRaw(rpd,"Build it where?",acceptSelfLocation))
if(rpd.lsqrPlaceAt != NULL && rpd.lsqrPlaceAt->GetOLTerrain() == NULL &&
rpd.lsqrPlaceAt->GetCharacter() == NULL &&
!(rpd.lsqrPlaceAt->GetRoom() && rpd.lsqrPlaceAt->GetRoom()->GetMaster() &&
rpd.lsqrPlaceAt->GetRoom()->GetMaster() != PLAYER)) // Prevents building in owned rooms.
{
rpd.v2PlaceAt = rpd.lsqrPlaceAt->GetPos();
rpd.bCanBePlaced=true;
return true;
}
failPlacementMsg(rpd);
return false;
}
static int calcMeltableTurns(material* mat,float fMult=1.0){
/**
* min is gold: str 55 and spends 5 turns each 1000cm3.
* TODO quite arbritrary but gameplay wise enough?
* TODO should use density? or something else than str? fireresistance is not defined for most and is not melting point either...
*/
DBG2(mat->GetStrengthValue(),mat->GetVolume());
static const int iBaseTurns = 5;
static const float fMinStr = 55.0; //float for precision
static const float fBaseVol = 1000.0; //float for precision
float f = iBaseTurns * fMult * (mat->GetStrengthValue()/fMinStr) * (mat->GetVolume()/fBaseVol); //vol is in cm3, so per 1L or 1Kg(water)
if(f>0 && f<1)f=1;
return f;
}
static bool chkWeapon(item* it, int iCfg, int iWeaponCategory, int iMainMatterMinStrengh){
return
dynamic_cast<meleeweapon*>(it)!=NULL
&&
it->GetMainMaterial()->GetStrengthValue()>=iMainMatterMinStrengh
&&
(
(iCfg!=0 && it->GetConfig()==iCfg) ||
(iWeaponCategory!=0 && it->GetWeaponCategory()==iWeaponCategory)
);
}
static bool setTools(recipedata& rpd,item* itTool,item* itTool2){
addTool(rpd,itTool);
addTool(rpd,itTool2);
return rpd.itTool2!=NULL;
}
static void addTool(recipedata& rpd,item* itTool){
if(rpd.itTool==itTool)return;
if(rpd.itTool2==itTool)return;
if(rpd.itTool==NULL){
rpd.itTool=itTool;
return;
}
if(rpd.itTool2==NULL){
rpd.itTool2=itTool;
return;
}
ABORT("at most 2 tools '%s' '%s' '%s'",rpd.itTool->GetName(INDEFINITE).CStr(),rpd.itTool2->GetName(INDEFINITE).CStr(),itTool->GetName(INDEFINITE).CStr());
}
static item* findCarvingToolSpecific(recipedata& rpd, item* itTool,int iMinCarvingStr, int iType, int& riMult, int iIncMult){ //this one is to avoid using #define mess :>, but instead of a simple if() we get several method calls... tho more readable...
if(itTool==NULL){
itTool = FindTool(rpd, iType, 0, iMinCarvingStr);
if(itTool!=NULL)riMult+=iIncMult;
}
return itTool;
}
static item* findTailoringTool(recipedata& rpd,item* itToWorkOn){
int iCarvingStr=0;
material* matM = itToWorkOn->GetMainMaterial();
material* matS = itToWorkOn->GetSecondaryMaterial();
if(!craftcore::IsMeltable(matM))
iCarvingStr=matM->GetStrengthValue();
if(matS!=NULL && !craftcore::IsMeltable(matS))
iCarvingStr=Max(iCarvingStr,matS->GetStrengthValue());
int iMinCarvingStr = iCarvingStr/2;
// any blanded thing bug preferably a dagger
int iMult=1;
item* itTool = FindTool(rpd, DAGGER, 0, iMinCarvingStr); //carving: tool cant be too much weaker
itTool = findCarvingToolSpecific(rpd,itTool,iMinCarvingStr,DAGGER,iMult,0); //TODO should be SCISSORS
if(itTool!=NULL){
if(iCarvingStr>1){
int itStr=itTool->GetMainMaterial()->GetStrengthValue();
if(itStr<iCarvingStr)
iMult++;
if(itStr==iMinCarvingStr)
iMult++;
}
calcToolTurns(rpd,iMult);
if(!recipe::findOLT(rpd,TAILORING_BENCH)){
ADD_MESSAGE("As you lack a workbench, it will take a while."); //it is good to measure, hold tight, has a good height etc...
rpd.iBaseTurnsToFinish *= 3;
}
}else{
//ADD_MESSAGE("You have no carving tool good enough to work on the requested material.");
// Already covered by failToolMsg()
rpd.SetAlreadyExplained();
}
return itTool;
}
static item* findCarvingTool(recipedata& rpd,item* itToWorkOn){
int iCarvingStr=0;
material* matM = itToWorkOn->GetMainMaterial();
material* matS = itToWorkOn->GetSecondaryMaterial();
if(!craftcore::IsMeltable(matM))
iCarvingStr=matM->GetStrengthValue();
if(matS!=NULL && !craftcore::IsMeltable(matS))
iCarvingStr=Max(iCarvingStr,matS->GetStrengthValue());
int iMinCarvingStr = iCarvingStr/2;
// any blanded thing bug preferably a dagger
int iMult=1;
item* itTool = FindTool(rpd, DAGGER, 0, iMinCarvingStr); //carving: tool cant be too much weaker
itTool = findCarvingToolSpecific(rpd,itTool,iMinCarvingStr,DAGGER,iMult,0);
itTool = findCarvingToolSpecific(rpd,itTool,iMinCarvingStr,SICKLE,iMult,1);
itTool = findCarvingToolSpecific(rpd,itTool,iMinCarvingStr,SHORT_SWORD,iMult,1);
itTool = findCarvingToolSpecific(rpd,itTool,iMinCarvingStr,AXE,iMult,2);
itTool = findCarvingToolSpecific(rpd,itTool,iMinCarvingStr,MEAT_CLEAVER,iMult,2);
itTool = findCarvingToolSpecific(rpd,itTool,iMinCarvingStr,LONG_SWORD,iMult,2);
itTool = findCarvingToolSpecific(rpd,itTool,iMinCarvingStr,SPEAR,iMult,3); // mmm... spear is easy enough to be found already TODO all other more difficult to be used weapons with blades may be added below here :), basically create a balsa dagger using a balsa spear and you are good to go xD
if(itTool!=NULL){
if(iCarvingStr>1){
int itStr=itTool->GetMainMaterial()->GetStrengthValue();
if(itStr<iCarvingStr)
iMult++;
if(itStr==iMinCarvingStr)
iMult++;
}
calcToolTurns(rpd,iMult);
if(!recipe::findOLT(rpd,WORK_BENCH)){
ADD_MESSAGE("As you lack a workbench, it will take a while."); //it is good to measure, hold tight, has a good height etc...
rpd.iBaseTurnsToFinish *= 3;
}
}else{
//ADD_MESSAGE("You have no carving tool good enough to work on the requested material.");
// Already covered by failToolMsg()
rpd.SetAlreadyExplained();
}
return itTool;
}
static item* FindTool(recipedata& rpd, int iCfg, int iWeaponCategory=0, int iMainMatterMinStrengh=0){
itemvector vit = vitInv(rpd);
for(int i=0;i<vit.size();i++)
if(chkWeapon(vit[i],iCfg,iWeaponCategory,iMainMatterMinStrengh))
return vit[i];
for(int i=0;i<vit.size();i++)
if(chkWeapon(vit[i],iCfg|BROKEN,iWeaponCategory,iMainMatterMinStrengh))
return vit[i];
return NULL;
}
static item* FindByWeaponCategory(recipedata& rpd,int iWCategory){
return FindTool(rpd,0,iWCategory);
}
static void calcToolTurns(recipedata& rpd,int iPrecalculatedMultBasedOnTool){
if(iPrecalculatedMultBasedOnTool==0)
return;
int iBaseTurns = rpd.iBaseTurnsToFinish;
float fIncTurnsStep=0.25;
int iAddTurns=iBaseTurns*(iPrecalculatedMultBasedOnTool*fIncTurnsStep);
rpd.iBaseTurnsToFinish = iBaseTurns + iAddTurns;
}
static item* FindBluntTool(recipedata& rpd){
//TODO could be based on volume and weight vs strengh and dexterity to determine how hard is to use the tool, unbalanced considering the required precision
static const int iTotToolTypes=3;
static const int aiTypes[iTotToolTypes]={HAMMER, FRYING_PAN, WAR_HAMMER};
item* it = NULL;
int iMult = 0;
for(int j=0;j<iTotToolTypes;j++){DBG2(j,aiTypes[j]);
it = FindTool(rpd, aiTypes[j]);
if(it){DBG2(it->GetConfig(),it->GetName(DEFINITE).CStr());
iMult=j;
break;
}
}
if(it==NULL){
it = FindByWeaponCategory(rpd,BLUNT_WEAPONS);
iMult=iTotToolTypes; //max
}
if(it!=NULL)
calcToolTurns(rpd,iMult);
else{
ADD_MESSAGE("You have no blunt tool.");
rpd.SetAlreadyExplained();
}
return it;
}
static item* FindCuttingTool(recipedata& rpd){//,bool bReversedTimeMult=false){
int iBaseTurns = rpd.iBaseTurnsToFinish;
item* it = NULL;
int iMult = 0;
int iMaxMult = 0;
#define FBWC(WC) \
if(it==NULL){ \
it = FindByWeaponCategory(rpd,WC); \
if(it==NULL)iMult++; \
} \
iMaxMult++;
FBWC(SMALL_SWORDS); //DAGGER is here
FBWC(AXES); //has more cutting power but less precision and takes more time TODO is this good?
FBWC(LARGE_SWORDS); //slower
FBWC(POLE_ARMS); //even slower, hard to use
if(it!=NULL){
// if(bReversedTimeMult)
// iMult = iMaxMult-iMult;
calcToolTurns(rpd,iMult);
}else{
ADD_MESSAGE("You have no cutting tool.");
rpd.SetAlreadyExplained();
}
return it;
}
/**
* prepare the filter for ALL items also resetting them first!
*/
template <typename T> static void prepareFilter(
recipedata& rpd,
const itemvector& vi,
long reqVol,
ci CI=ci(),
std::vector<ulong>* ptmpIngredientsIDs = NULL
){
for(int i=0;i<vi.size();i++){
vi[i]->SetValidRecipeIngredient(false);
if(dynamic_cast<T*>(vi[i])!=NULL){
if(vi[i]->IsBurning())continue;