Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.68 KB | None | 0 0
  1. enum FoodStageType
  2. {
  3. NONE = 0, //food stage not define
  4. RAW = 1, //default
  5. BAKED = 2,
  6. BOILED = 3,
  7. DRIED = 4,
  8. BURNED = 5,
  9. ROTTEN = 6,
  10.  
  11. COUNT //for net sync purposes
  12. }
  13.  
  14. class FoodStage
  15. {
  16. protected FoodStageType m_FoodStageTypeClientLast;
  17. protected FoodStageType m_FoodStageType;
  18. protected Edible_Base m_FoodItem;
  19.  
  20. protected int m_SelectionIndex; //visual properties
  21. protected int m_TextureIndex;
  22. protected int m_MaterialIndex;
  23.  
  24. protected float m_CookingTime;
  25.  
  26. //constructor
  27. void FoodStage( Edible_Base food_item )
  28. {
  29. //default
  30. m_FoodStageType = FoodStageType.RAW;
  31. m_FoodItem = food_item;
  32.  
  33. //reset cooking time
  34. m_CookingTime = 0;
  35.  
  36. //get config data
  37. string config_path = "CfgVehicles" + " " + GetFoodItem().GetType() + " " + "Food" + " " + "FoodStages" + " " + GetFoodStageName( m_FoodStageType );
  38.  
  39. //if not exists in config
  40. if ( !GetGame().ConfigIsExisting( config_path ) )
  41. {
  42. return;
  43. }
  44.  
  45. //set params
  46. //visual properties
  47. array<int> visual_properties = new array<int>;
  48. GetGame().ConfigGetIntArray( config_path + " " + "visual_properties", visual_properties );
  49. if ( visual_properties.Count() > 0 )
  50. {
  51. SetSelectionIndex( visual_properties.Get( 0 ) );
  52. SetTextureIndex( visual_properties.Get( 1 ) );
  53. SetMaterialIndex( visual_properties.Get( 2 ) );
  54. }
  55.  
  56. m_FoodStageTypeClientLast = m_FoodStageType;
  57. }
  58.  
  59. //Food Stage Type
  60. FoodStageType GetFoodStageType()
  61. {
  62. return m_FoodStageType;
  63. }
  64. void SetFoodStageType( FoodStageType food_stage_type )
  65. {
  66. m_FoodStageType = food_stage_type;
  67. }
  68.  
  69. //Selection index
  70. int GetSelectionIndex()
  71. {
  72. return m_SelectionIndex;
  73. }
  74. void SetSelectionIndex( int index )
  75. {
  76. m_SelectionIndex = index;
  77. }
  78.  
  79. //Texture index
  80. int GetTextureIndex()
  81. {
  82. return m_TextureIndex;
  83. }
  84. void SetTextureIndex( int index )
  85. {
  86. m_TextureIndex = index;
  87. }
  88.  
  89. //Material index
  90. int GetMaterialIndex()
  91. {
  92. return m_MaterialIndex;
  93. }
  94. void SetMaterialIndex( int index )
  95. {
  96. m_MaterialIndex = index;
  97. }
  98.  
  99. //Food properties
  100. protected static float GetNutritionPropertyFromIndex( int index, FoodStageType stage_type, FoodStage stage, string classname )
  101. {
  102. if(stage)
  103. {
  104. stage_type = stage.m_FoodStageType;
  105. classname = stage.GetFoodItem().GetType();
  106. }
  107.  
  108. string config_path;
  109. string food_stage_name = GetFoodStageName( stage_type );
  110.  
  111. config_path = "CfgVehicles" + " " + classname + " " + "Food" + " " + "FoodStages" + " " + food_stage_name + " " + "nutrition_properties";
  112. array<float> nutrition_properties = new array<float>;
  113. GetGame().ConfigGetFloatArray( config_path, nutrition_properties );
  114.  
  115. if ( nutrition_properties.Count() > 0 )
  116. {
  117. if( index > (nutrition_properties.Count() - 1))
  118. {
  119. return 0;
  120. }
  121. else
  122. {
  123. return nutrition_properties.Get( index );
  124. }
  125. }
  126. //calculate nutrition properties from base stage and nutrition modifiers
  127. else
  128. {
  129. //get modifiers class for nutrition values
  130. config_path = "CfgVehicles" + " " + classname + " " + "Food" + " " + "nutrition_modifiers_class";
  131.  
  132. if ( GetGame().ConfigIsExisting( config_path ) )
  133. {
  134. string nutr_mod_class;
  135. GetGame().ConfigGetText( config_path, nutr_mod_class );
  136.  
  137. config_path = "CfgVehicles" + " " + "NutritionModifiers" + " " + nutr_mod_class + " " + "base_stage";
  138. string nutr_base_stage;
  139. GetGame().ConfigGetText( config_path, nutr_base_stage );
  140.  
  141. //get nutrition values for food stage and modifiers
  142. config_path = "CfgVehicles" + " " + classname + " " + "Food" + " " + "FoodStages" + " " + nutr_base_stage + " " + "nutrition_properties";
  143. array<float> base_nutr_properties = new array<float>;
  144. GetGame().ConfigGetFloatArray( config_path, base_nutr_properties );
  145.  
  146. config_path = "CfgVehicles" + " " + "NutritionModifiers" + " " + nutr_mod_class + " " + food_stage_name + " " + "nutrition_properties";
  147. array<float> nutr_mod_properties = new array<float>;
  148. GetGame().ConfigGetFloatArray( config_path, nutr_mod_properties );
  149.  
  150. //base nutrition * food stage nutrition modifier
  151. if ( base_nutr_properties.Count() > 0 && nutr_mod_properties.Count() > 0 )
  152. {
  153. return ( base_nutr_properties.Get( index ) * nutr_mod_properties.Get( index ) );
  154. }
  155. }
  156. }
  157.  
  158. return 0;
  159. }
  160.  
  161. static float GetFullnessIndex(FoodStage stage, int stage_type = -1, string classname = "")
  162. {
  163. return GetNutritionPropertyFromIndex( 0 , stage_type, stage, classname );
  164. }
  165.  
  166. static float GetEnergy(FoodStage stage, int stage_type = -1, string classname = "")
  167. {
  168.  
  169. return GetNutritionPropertyFromIndex( 1 , stage_type, stage, classname );
  170. }
  171.  
  172. static float GetWater(FoodStage stage, int stage_type = -1, string classname = "")
  173. {
  174. return GetNutritionPropertyFromIndex( 2 , stage_type, stage, classname );
  175. }
  176.  
  177. static float GetNutritionalIndex(FoodStage stage, int stage_type = -1, string classname = "")
  178. {
  179. return GetNutritionPropertyFromIndex( 3 , stage_type , stage, classname);
  180. }
  181.  
  182. static float GetToxicity(FoodStage stage, int stage_type = -1, string classname = "")
  183. {
  184. return GetNutritionPropertyFromIndex( 4 , stage_type, stage, classname );
  185. }
  186.  
  187. static int GetAgents(FoodStage stage, int stage_type = -1, string classname = "")
  188. {
  189. return GetNutritionPropertyFromIndex( 5 , stage_type, stage, classname );
  190. }
  191.  
  192. static float GetDigestibility(FoodStage stage, int stage_type = -1, string classname = "")
  193. {
  194. return GetNutritionPropertyFromIndex( 6 , stage_type, stage, classname );
  195. }
  196.  
  197. //Food item
  198. protected Edible_Base GetFoodItem()
  199. {
  200. return m_FoodItem;
  201. }
  202.  
  203. //Cooking time
  204. float GetCookingTime()
  205. {
  206. return m_CookingTime;
  207. }
  208. void SetCookingTime( float time )
  209. {
  210. m_CookingTime = time;
  211. }
  212.  
  213. //********************************************/
  214. // FOOD STAGE CHANGE
  215. //********************************************/
  216. //Checks if food stage can be changed to another stage
  217. bool CanChangeToNewStage( CookingMethodType cooking_method )
  218. {
  219. if ( GetNextFoodStageType( cooking_method ) == FoodStageType.NONE )
  220. {
  221. return false;
  222. }
  223.  
  224. return true;
  225. }
  226.  
  227. //returns possible food stage type according to given cooking method
  228. FoodStageType GetNextFoodStageType( CookingMethodType cooking_method )
  229. {
  230. ref array<string> string_output = new array<string>;
  231. ref array<string> stage_transitions = new array<string>;
  232.  
  233. //get stage transitions from config
  234. string config_path = "CfgVehicles" + " " + GetFoodItem().GetType() + " " + "Food" + " " + "FoodStageTransitions" + " " + GetFoodStageName( GetFoodStageType() );;
  235.  
  236. if ( GetGame().ConfigIsExisting( config_path ) )
  237. {
  238. int child_count = GetGame().ConfigGetChildrenCount( config_path );
  239.  
  240. for ( int i = 0; i < child_count; i++ )
  241. {
  242. string child_name;
  243. GetGame().ConfigGetChildName( config_path, i, child_name );
  244. string transition_config_path = config_path + " " + child_name + " " + "cooking_method";
  245.  
  246. if ( GetGame().ConfigIsExisting( transition_config_path ) )
  247. {
  248. if ( GetGame().ConfigGetInt( transition_config_path ) == cooking_method )
  249. {
  250. string cooking_method_config_path = config_path + " " + child_name + " " + "transition_to";
  251. return GetGame().ConfigGetInt( cooking_method_config_path );
  252. }
  253. }
  254. }
  255. }
  256.  
  257. return FoodStageType.NONE;
  258. }
  259.  
  260. void ChangeFoodStage( FoodStageType new_stage_type )
  261. {
  262. string config_path = "CfgVehicles" + " " + GetFoodItem().GetType() + " " + "Food" + " " + "FoodStages" + " " + GetFoodStageName( new_stage_type );
  263.  
  264. //merge stages
  265. //food stage type
  266. SetFoodStageType( new_stage_type );
  267.  
  268. array<int> visual_properties = new array<int>;
  269. GetGame().ConfigGetIntArray( config_path + " " + "visual_properties", visual_properties );
  270. if ( visual_properties.Count() > 0 )
  271. {
  272. //selection index
  273. int index = visual_properties.Get( 0 );
  274. if ( index >= 0 )
  275. {
  276. SetSelectionIndex( index );
  277. }
  278. //texture index
  279. index = visual_properties.Get( 1 );
  280. if ( index >= 0 )
  281. {
  282. SetTextureIndex( index );
  283. }
  284. //material index
  285. index = visual_properties.Get( 2 );
  286. if ( index >= 0 )
  287. {
  288. SetMaterialIndex( index );
  289. }
  290. }
  291.  
  292. //refresh visual
  293. GetFoodItem().Synchronize();
  294. }
  295.  
  296. void UpdateVisuals()
  297. {
  298. //if item has food stages
  299. if ( GetFoodItem().HasFoodStage() )
  300. {
  301. Edible_Base food_item = GetFoodItem();
  302.  
  303. //Selections
  304. string config_path;
  305. ref array<string> config_selections = new array<string>;
  306. ref array<string> config_textures = new array<string>;
  307. ref array<string> config_materials = new array<string>;
  308.  
  309. //selections
  310. config_path = "CfgVehicles" + " " + food_item.GetType() + " " + "hiddenSelections";
  311. GetGame().ConfigGetTextArray( config_path, config_selections );
  312. //textures
  313. config_path = "CfgVehicles" + " " + food_item.GetType() + " " + "hiddenSelectionsTextures";
  314. GetGame().ConfigGetTextArray( config_path, config_textures );
  315. //materials
  316. config_path = "CfgVehicles" + " " + food_item.GetType() + " " + "hiddenSelectionsMaterials";
  317. GetGame().ConfigGetTextArray( config_path, config_materials );
  318.  
  319. //selection index
  320. int selection_index;
  321. if ( GetSelectionIndex() >= 0 && config_selections.Count() > GetSelectionIndex() )
  322. {
  323. selection_index = GetSelectionIndex();
  324. }
  325.  
  326. //texture index
  327. int texture_index;
  328. if ( GetTextureIndex() >= 0 && config_textures.Count() > GetTextureIndex() )
  329. {
  330. texture_index = GetTextureIndex();
  331. }
  332.  
  333. //material index
  334. int material_index;
  335. if ( GetMaterialIndex() >= 0 && config_materials.Count() > GetMaterialIndex() )
  336. {
  337. material_index = GetMaterialIndex();
  338. }
  339.  
  340. //hide all selection except the configured one
  341. for ( int i = 0; i < config_selections.Count(); i++ )
  342. {
  343. if ( config_selections.Get( i ) != config_selections.Get( selection_index ) )
  344. {
  345. food_item.SetAnimationPhase( config_selections.Get( i ), 1 );
  346. }
  347. }
  348.  
  349. //Debug
  350. //Print( "item = " + food_item.GetType() + " selection index = " + GetSelectionIndex().ToString() + " texture index = " + GetTextureIndex().ToString() );
  351.  
  352. //show selection
  353. food_item.SetAnimationPhase( config_selections.Get( selection_index ), 0 );
  354. //set texture
  355. food_item.SetObjectTexture( selection_index, config_textures.Get( texture_index ) );
  356. //set materials
  357. food_item.SetObjectMaterial( selection_index, config_materials.Get( material_index ) );
  358. }
  359.  
  360. m_FoodStageTypeClientLast = m_FoodStageType;
  361. }
  362.  
  363. //Food States
  364. //check food stages
  365. bool IsFoodInStage( FoodStageType food_stage_type )
  366. {
  367. if ( GetFoodStageType() == food_stage_type )
  368. {
  369. return true;
  370. }
  371.  
  372. return false;
  373. }
  374.  
  375. bool IsFoodRaw()
  376. {
  377. return IsFoodInStage( FoodStageType.RAW );
  378. }
  379.  
  380. bool IsFoodBaked()
  381. {
  382. return IsFoodInStage( FoodStageType.BAKED );
  383. }
  384.  
  385. bool IsFoodBoiled()
  386. {
  387. return IsFoodInStage( FoodStageType.BOILED );
  388. }
  389.  
  390. bool IsFoodDried()
  391. {
  392. return IsFoodInStage( FoodStageType.DRIED );
  393. }
  394.  
  395. bool IsFoodBurned()
  396. {
  397. return IsFoodInStage( FoodStageType.BURNED );
  398. }
  399.  
  400. bool IsFoodRotten()
  401. {
  402. return IsFoodInStage( FoodStageType.ROTTEN );
  403. }
  404.  
  405. //get name of food stage type
  406. static string GetFoodStageName( FoodStageType food_stage_type )
  407. {
  408. switch( food_stage_type )
  409. {
  410. case FoodStageType.RAW: return "Raw";
  411. case FoodStageType.BAKED: return "Baked";
  412. case FoodStageType.BOILED: return "Boiled";
  413. case FoodStageType.DRIED: return "Dried";
  414. case FoodStageType.BURNED: return "Burned";
  415. case FoodStageType.ROTTEN: return "Rotten";
  416. }
  417.  
  418. return "Raw";
  419. }
  420.  
  421. //================================================================
  422. // SERIALIZATION
  423. //================================================================
  424. void OnStoreSave( ParamsWriteContext ctx )
  425. {
  426. //Food stage type
  427. ctx.Write( m_FoodStageType );
  428.  
  429. //Selection index
  430. ctx.Write( m_SelectionIndex );
  431.  
  432. //Texture index
  433. ctx.Write( m_TextureIndex );
  434.  
  435. //Material index
  436. ctx.Write( m_MaterialIndex );
  437. }
  438.  
  439. bool OnStoreLoad( ParamsReadContext ctx, int version )
  440. {
  441. //Food stage type
  442. if ( !ctx.Read( m_FoodStageType ) )
  443. {
  444. m_FoodStageType = FoodStageType.RAW; //set default
  445. return false;
  446. }
  447.  
  448. //Selection index
  449. if ( !ctx.Read( m_SelectionIndex ) )
  450. {
  451. m_SelectionIndex = 0; //set default
  452. return false;
  453. }
  454.  
  455. //Texture index
  456. if ( !ctx.Read( m_TextureIndex ) )
  457. {
  458. m_TextureIndex = 0; //set default
  459. return false;
  460. }
  461.  
  462. //Material index
  463. if ( !ctx.Read( m_MaterialIndex ) )
  464. {
  465. m_MaterialIndex = 0; //set default
  466. return false;
  467. }
  468.  
  469. return true;
  470. }
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement