1 /*
2  *
3  *  Iter Vehemens ad Necem (IVAN)
4  *  Copyright (C) Timo Kiviluoto
5  *  Released under the GNU General
6  *  Public License
7  *
8  *  See LICENSING which should be included
9  *  along with this file for more details
10  *
11  */
12 
13 #ifndef MAIN_INCLUDE_CRAFT_H_
14 #define MAIN_INCLUDE_CRAFT_H_
15 
16 #include <ctime>
17 #include <vector>
18 
19 class craft;
20 class humanoid;
21 class item;
22 class lsquare;
23 class object;
24 class olterrain;
25 
26 struct v2;
27 
28 //enum craftItemType{
29 //  CIT_NONE=0,
30 //  CIT_STONE=1,
31 //  CIT_PROTOTYPE=2,
32 //  CIT_POTION=3,
33 //};
34 #define CIT_NONE 0 //TODO use, save and load enums?
35 #define CIT_STONE 1
36 #define CIT_PROTOTYPE 2
37 #define CIT_POTION 3
38 #define CIT_LUMP 4
39 #define CIT_STICK 5
40 
41 //enum craftTerrainType{
42 //  CTT_NONE=0,
43 //  CTT_WALL=1,
44 //  CTT_DOOR=2,
45 //  CTT_FURNITURE=3,
46 //};
47 #define CTT_NONE 0 //TODO use, save and load enums?
48 #define CTT_WALL 1
49 #define CTT_DOOR 2
50 #define CTT_FURNITURE 3
51 
52 class recipecore {
53   private:
54     clock_t initKey;
55 
56     humanoid* h;
57 
58     bool bCanBeSuspended;
59     int iDungeonLevelID;
60 
61   public:
62     recipecore(humanoid* H,uint sel);
63 
64     void integrityCheck(character* Char=NULL) const;
65 
66     void SetHumanoid(character* C);
H()67     humanoid* H(){return h;}
68 
SetCannotBeSuspended()69     void SetCannotBeSuspended(){bCanBeSuspended=false;}
IsCanBeSuspended()70     bool IsCanBeSuspended() const {return bCanBeSuspended;}
71 
GetDungeonLevelID()72     int GetDungeonLevelID() const {return iDungeonLevelID;}
73 
74     void Save(outputfile& SaveFile) const;
75     void Load(inputfile& SaveFile);
76 
77     void ClearRefs();
78 };
79 
80 //class recipedatacore;
81 //typedef recipedatacore* (*recipespawner)(character*);
82 //class recipeprototype
83 //{
84 // public:
85 //  recipeprototype(recipespawner, cchar*);
86 //  recipedatacore* SpawnAndLoad(inputfile&) const;
87 //  cchar* GetClassID() const { return ClassID; }
88 //  int GetIndex() const { return Index; }
89 // private:
90 //  int Index;
91 //  recipespawner Spawner;
92 //  cchar* ClassID;
93 //};
94 //class recipedatacore {
95 //  public:
96 //    typedef recipeprototype prototype;
97 //    virtual const prototype* GetProtoType() const = 0;
98 //    int GetType() const { return GetProtoType()->GetIndex(); }
99 //    virtual ~recipedatacore() = default;
100 //};
101 //class recipedata : public recipecore {
102 
103 /**/
104 struct undoremains {
105   ulong ulUndoRemainsIngredientID = 0;
106   ulong ulUndoRemainsLumpID = 0;
107   long lUndoRemainsVolume = 0;
108 };
109 class recipedata {
110   /**
111    * tip: for clarity group fields by max of 5 no matter group context (despite in context would be better)
112    */
113 
114   friend class craft;
115   friend class craftcore;
116   friend class crafthandle;
117 
118   friend struct recipe;
119   friend struct srpFluidsBASE;
120   friend struct srpOltBASE;
121 
122   friend struct srpChair;
123   friend struct srpDoor;
124   friend struct srpWall2;
125   friend struct srpPoison;
126   friend struct srpAcid;
127 
128   friend struct srpDismantle;
129   friend struct srpSplitLump;
130   friend struct srpMelt;
131   friend struct srpForgeItem;
132   friend struct srpAnvil;
133 
134   friend struct srpForge;
135   friend struct srpJoinLumps;
136   friend struct srpWorkBench;
137   friend struct srpTWorkBench;
138   friend struct srpResistanceVS;
139   friend struct srpCutWeb;
140 
141   friend struct srpInspect;
142 
143   protected:
144     recipecore rc;
145 
146     //TODO use std::bitset<32> instead of booleans?
147 
148     // references
149     item* itTool;
150     item* itTool2;
151     lsquare* lsqrPlaceAt;
152     lsquare* lsqrCharPos;
153     item* itWeakestIngredient;
154     lsquare* lsqrActor;
155 
156     /*****************************************
157      *  no need to save
158      */
159     uint SelectedRecipe;
160     bool bSpendCurrentTurn;
161     bool bAlreadyExplained;
162     bool bHasAllIngredients;
163     bool bCanStart;
164 
165     bool bCanBePlaced;
166     int xplodStr;
167     int iStrongerXplod;
168     v2 v2XplodAt;
169     bool bOnlyXplodIfCriticalFumble;
170 
171     bool bSpecialExtendedTurns;
172     int iMinTurns;
173     bool bFailedTerminateCancel;
174     bool bFailedSuspend;
175 
176     undoremains urMain;
177     undoremains urSecond;
178 
179     /*******************************************
180      * save REQUIRED fields!!!
181      * if re-organized, do also at constructor initializer please!
182      * but save and load will make existing saved games with suspended crafting incompatible then,
183      * so better avoid doing it.
184      *******************************************/
185     std::vector<ulong> ingredientsIDs;
186     int iAddDexterity;
187     int iBaseTurnsToFinish;
188     int itSpawnTot;
189     v2 v2ForgeLocation;
190 
191     v2 v2PlaceAt;
192     bool bSuccesfullyCompleted;
193     v2 v2AnvilLocation;
194     v2 v2PlayerCraftingAt;
195     festring fsCraftInfo;
196 
197     ulong itToolID;
198     ulong itTool2ID;
199     ulong itSpawnType;
200     festring fsItemSpawnSearchPrototype;
201 
202     ulong itSpawnCfg;
203 
204     //TODO if duplicating the material works with spoil, rust etc, save THE MATERIAL and load it to dup from. and remove all these vars about'em
205     ulong itSpawnMatMainCfg;
206     ulong itSpawnMatMainVol;
207 //    ulong itSpawnMatMainSpoilLevel;
208 
209     ulong itSpawnMatSecCfg;
210     ulong itSpawnMatSecVol;
211 //    ulong itSpawnMatSecSpoilLevel;
212 
213     ulong otSpawnCfg;
214     ulong otSpawnMatMainCfg;
215     ulong otSpawnMatMainVol;
216     ulong otSpawnMatSecCfg;
217     ulong otSpawnMatSecVol;
218 
219     ulong otSpawnType;
220     bool bSpawnBroken;
221     double fDifficulty; //preferably a max of 5.0
222     bool bCanBeBroken;
223     bool bMeltable;
224 
225     v2 v2WorkbenchLocation;
226     int iRemainingTurnsToFinish;
227     bool bGradativeCraftOverride;
228     bool bTailoringMode;
229     v2 v2TailoringWorkbenchLocation;
230 
231     long lDamageFinalItem;
232 
233   public:
234     recipedata(humanoid* H=NULL,uint sel=FELIST_ERROR_BIT);
235     cfestring dbgInfo() const;
236     cfestring id() const;
237 //    void SendSpawnItemToHell();
238 //    void SendTerrainToHell();
239 
240     void Save(outputfile& SaveFile) const;
241     void Load(inputfile& SaveFile);
242     void CopySpawnItemCfgFrom(item* itCfg);
243     void CopySpawnTerrainCfgFrom(olterrain* otCfg);
244 
245     void ClearRefs();
246     item* GetTool();
247     item* GetTool2();
248 
IsFailedSuspendOrCancel()249     bool IsFailedSuspendOrCancel(){return bFailedTerminateCancel || bFailedSuspend;}
SetAlreadyExplained()250     void SetAlreadyExplained(){bAlreadyExplained=true;}
251 };
252 
253 class craftcore {
254 //  private:
255 //    static recipedata* prpdSuspended; //TODO suspendable action should be more global to be reused by other actions than crafting!
256 
257   public: //TODO review if some should be protected or private
258     static void Save(outputfile& SaveFile);
259     static void Load(inputfile& SaveFile);
260     static truth Craft(character* Char);
261 
262     static bool canBeCrafted(item* it);
263 
264     static void SendToHellSafely(item* it);
265 
266     static int CurrentDungeonLevelID();
267 
268     static float CraftSkill(character* Char);
269     static bool MoreCraftDeniedFilters(item* it);
270 
271     static bool EmptyContentsIfPossible(recipedata& rpd,item* itContainer,bool bMoveToInventory=false);
272 
273 //    static material* CreateMaterial(bool bMain,recipedata* prpd,material* matOverride=NULL);
274     static material* CreateMaterial(bool bMain,recipedata& rpd);
275     static material* CreateMaterial(material* matCopyFrom);
276 
277     static truth CheckArms(humanoid* h);
278     static truth CheckArms(humanoid* h,bool& bLOk,bool& bROk);
279     static humanoid* CheckHumanoid(character* Char);
280 
281     static bool IsDegraded(item* it,bool bShowMsg=false);
282     static truth IsMeltable(material* mat);
283     static truth IsMeltable(material* matM,material* matS);
284     static truth IsMeltable(item* it);
285     static bool IsWooden(material* mat);
286     static bool IsBone(material* mat);
287 
288     static item* PrepareRemains(recipedata&,material*,int ForceType=CIT_NONE, long NewMaterialVolume = 0);
289     static void UndoRemainsIfNeeded(recipedata& rpd);
290     static void FinishSpawning(recipedata& rpd,item* itSpawn);
291 
292     static void AddSuspended(const recipedata& rpd);
293     static void RemoveIfSuspended(const recipedata&rpd);
294     static bool ResumeSuspendedTo(character* Char,recipedata& rpd);
295     static void ClearSuspendedList();
296     static bool HasSuspended();
297 
298     static recipedata FindRecipedata(festring fsRpdId);
299 
300     static void CopyDegradation(material* matM,material* matSpM);
301     static void CopyDegradation(item* itFrom,material* matTo);
302     static void CopyDegradationIfPossible(recipedata& rpd, item* itTo);
303 
304     static int CitType(item* it);
305     static bool CheckFumble(recipedata& rpd, bool& bCriticalFumble,int& iFumblePower);
306     static void CraftSkillAdvance(recipedata&);
307 };
308 
309 class crafthandle {
310   private:
311     static void CheckFumble(recipedata& rpd,bool bChangeTurns=true);
312     static void CheckIngredients(recipedata& rpd);
313     static void CheckFacilities(recipedata& rpd);
314     static void CheckTools(recipedata& rpd);
315 
316     static void GradativeCraftOverride(recipedata& rpd);
317 
318     static item* CheckBreakItem(bool bAllowBreak, recipedata& rpd, item* itSpawn, festring& fsCreated);
319     static cfestring DestroyIngredients(recipedata& rpd);
320 
321     static item* SpawnItem(recipedata& rpd, festring& fsCreated);
322     static olterrain* SpawnTerrain(recipedata& rpd, festring& fsCreated);
323 
324   public:
325     static void CheckEverything(recipedata& rpd, character* Char);
326     static void CraftWorkTurn(recipedata& rpd);
327 };
328 
329 #endif /* MAIN_INCLUDE_CRAFT_H_ */
330