catchingcontexttraps.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. class CatchingContextTrapsBase : CatchingContextBase
  2. {
  3. /**@var m_CumulativeTrappingSuccess
  4. * @brief after N attempts, the chance to catch should be this. Only highest one applies.
  5. * @NOTE: Take care, only way to achieve guaranteed 1.0 chance at the end is to have EVERY chance at 1.0.
  6. **/
  7. protected float m_CumulativeTrappingSuccess;
  8. protected int m_AttemptsCount;
  9. protected ItemBase m_Bait;
  10. override protected void DeserializeData(Param par)
  11. {
  12. Param2<EntityAI,int> p;
  13. if (Class.CastTo(p,par))
  14. {
  15. m_MainItem = p.param1;
  16. m_AttemptsCount = p.param2;
  17. }
  18. }
  19. override protected void CreateResultDataStructure()
  20. {
  21. m_Result = new CatchingResultTrapBase(m_MainItem);
  22. super.CreateResultDataStructure();
  23. }
  24. override protected void ClearCatchingItemData()
  25. {
  26. super.ClearCatchingItemData();
  27. m_CumulativeTrappingSuccess = 0.0;
  28. m_QualityBaseMod = 0.0;
  29. m_QualityDispersionMinMod = 0.0;
  30. m_QualityDispersionMaxMod = 0.0;
  31. }
  32. override protected void InitItemValues(EntityAI item)
  33. {
  34. //skip ruined or deleted items entirely
  35. if (item.IsRuined() || item.IsSetForDeletion())
  36. return;
  37. string path = "" + CFG_VEHICLESPATH + " " + item.GetType() + " Trapping";
  38. if (GetGame().ConfigIsExisting(path))
  39. {
  40. if (GetGame().ConfigIsExisting(path + " baitTypes") && GetGame().ConfigIsExisting(path + " baitTypeChances"))
  41. {
  42. CachedObjectsArrays.ARRAY_INT.Clear();
  43. CachedObjectsArrays.ARRAY_FLOAT.Clear();
  44. GetGame().ConfigGetIntArray(path + " baitTypes",CachedObjectsArrays.ARRAY_INT);
  45. GetGame().ConfigGetFloatArray(path + " baitTypeChances",CachedObjectsArrays.ARRAY_FLOAT);
  46. int count = CachedObjectsArrays.ARRAY_INT.Count();
  47. if (count == CachedObjectsArrays.ARRAY_FLOAT.Count())
  48. {
  49. int key;
  50. float value;
  51. for (int i = 0; i < count; i++)
  52. {
  53. key = CachedObjectsArrays.ARRAY_INT[i];
  54. value = AdjustBaitItemChance(item) * CachedObjectsArrays.ARRAY_FLOAT[i];
  55. if (!m_BaitCompatibilityMap.Contains(key) || (m_BaitCompatibilityMap.Get(key).m_BaseProbability < value))
  56. {
  57. m_BaitCompatibilityMap.Set(key,new BaitData(value,item));
  58. }
  59. }
  60. }
  61. else
  62. {
  63. ErrorEx("'baitTypes' and 'baitTypeChances' arrray counts of " + item.GetType() + " do not match!",ErrorExSeverity.INFO);
  64. }
  65. }
  66. if (GetGame().ConfigIsExisting(path + " resultQuantityBaseMod"))
  67. m_QualityBaseMod += GetGame().ConfigGetFloat(path + " resultQuantityBaseMod");
  68. if (GetGame().ConfigIsExisting(path + " resultQuantityDispersionMin"))
  69. m_QualityDispersionMinMod += GetGame().ConfigGetFloat(path + " resultQuantityDispersionMin");
  70. if (GetGame().ConfigIsExisting(path + " resultQuantityDispersionMax"))
  71. m_QualityDispersionMaxMod += GetGame().ConfigGetFloat(path + " resultQuantityDispersionMax");
  72. }
  73. }
  74. //! Allows for adjustment of all catch probabilities from item qualities (damage, qty...)
  75. float AdjustBaitItemChance(EntityAI item)
  76. {
  77. float ret = 1.0;
  78. ItemBase ib;
  79. if (Class.CastTo(ib,item))
  80. {
  81. //ret *= ib.GetBaitEffectivity(); //disconnected for the Time Being
  82. }
  83. return ret;
  84. }
  85. override protected void InitCatchEnviroMask()
  86. {
  87. vector pos = m_MainItem.GetPosition();
  88. if (GetGame().SurfaceIsSea(pos[0], pos[2]))
  89. m_EnviroMask = AnimalCatchingConstants.MASK_ENVIRO_SEA;
  90. else if (GetGame().SurfaceIsPond( pos[0], pos[2]))
  91. m_EnviroMask = AnimalCatchingConstants.MASK_ENVIRO_POND;
  92. else
  93. m_EnviroMask = AnimalCatchingConstants.MASK_ENVIRO_LAND_ALL;
  94. }
  95. override protected void Init(Param par)
  96. {
  97. super.Init(par);
  98. GenerateResult();
  99. }
  100. override protected void SetupInitialTypes()
  101. {
  102. #ifdef DEVELOPER
  103. if (IsCLIParam("catchingLogs"))
  104. {
  105. Print("********************"); //just for debug purposes to track the start
  106. Print("dbgTrapz | START");
  107. Print("********************");
  108. }
  109. #endif
  110. super.SetupInitialTypes();
  111. #ifdef DEVELOPER
  112. if (IsCLIParam("catchingLogs"))
  113. {
  114. Print("***"); //just for debug purposes to track the start
  115. }
  116. #endif
  117. }
  118. void UpdateDataAndMasks()
  119. {
  120. InitCatchingItemData();
  121. InitCatchMethodMask(); //bait check
  122. //InitCatchEnviroMask(); //skipping, raycasts are unreliable outside network bubbles
  123. SetupInitialTypes();
  124. SetupProbabilityArray();
  125. }
  126. int UpdateTrapEnviroMask()
  127. {
  128. InitCatchEnviroMask();
  129. return GetCatchEnviroMask();
  130. }
  131. void SetTrapEnviroMask(int value)
  132. {
  133. m_EnviroMask = value;
  134. }
  135. //! if non-empty bait type is used, some 'Bait' attachment is picked as an active bait (currently no direct link between item and sensitivity-weighted target probability)
  136. void UpdateUsedBait(ECatchingBaitCategories type)
  137. {
  138. m_Bait = null;
  139. if (type != ECatchingBaitCategories.BAIT_TYPE_EMPTY)
  140. {
  141. BaitData dta = m_BaitCompatibilityMap.Get(type);
  142. if (dta)
  143. {
  144. ItemBase potentialBait = ItemBase.Cast(dta.m_Owner);
  145. if (potentialBait != m_MainItem)
  146. m_Bait = potentialBait;
  147. }
  148. else
  149. ErrorEx("failed to acquire BaitData from type: " + type);
  150. }
  151. #ifdef DEVELOPER
  152. if (IsCLIParam("catchingLogs"))
  153. {
  154. if (m_Bait)
  155. Print("dbgTrapz | UpdateUsedBait to: " + m_Bait + " | with base bait probability used: " + m_BaitCompatibilityMap.Get(type).m_BaseProbability);
  156. else
  157. Print("dbgTrapz | UpdateUsedBait to 'null'!");
  158. }
  159. #endif
  160. }
  161. override void UpdateBaseProbability(YieldItemBase yItem)
  162. {
  163. int baitType = ECatchingBaitCategories.BAIT_TYPE_EMPTY;
  164. int usedType = -1;
  165. float probability = -1;
  166. float highestProbability = 0.0;
  167. int count = m_BaitCompatibilityMap.Count();
  168. for (int i = 0; i < count; i++)
  169. {
  170. baitType = m_BaitCompatibilityMap.GetKey(i);
  171. probability = m_BaitCompatibilityMap.Get(baitType).m_BaseProbability * yItem.GetBaitTypeSensitivity(baitType);
  172. if (probability > highestProbability)
  173. {
  174. highestProbability = probability;
  175. usedType = baitType;
  176. }
  177. }
  178. m_CumulativeTrappingSuccess = Math.Clamp(highestProbability,0,1);
  179. #ifdef DEVELOPER
  180. if (IsCLIParam("catchingLogs"))
  181. {
  182. //Print("********************");
  183. Print("dbgTrapz | using bait type: " + baitType + " to catch: " + yItem);
  184. }
  185. #endif
  186. UpdateUsedBait(usedType);
  187. #ifdef DEVELOPER
  188. if (IsCLIParam("catchingLogs"))
  189. {
  190. float dbgProb;
  191. ModifySignalProbability(dbgProb);
  192. Print("dbgTrapz | starting catching of " + yItem + " | at probability: " + dbgProb + " | with target success: " + m_CumulativeTrappingSuccess);
  193. }
  194. #endif
  195. }
  196. override bool ModifySignalProbability(inout float probability)
  197. {
  198. probability = 1 - Math.Pow((1 - m_CumulativeTrappingSuccess),(1/m_AttemptsCount));
  199. return true;
  200. }
  201. void RemoveBait()
  202. {
  203. if (m_Bait)
  204. m_Bait.DeleteSafe();
  205. }
  206. void ReduceBaitQty(float qtyNorm)
  207. {
  208. if (m_Bait)
  209. {
  210. if (m_Bait.HasQuantity() && !m_Bait.CanBeSplit())
  211. m_Bait.SetQuantityNormalized((m_Bait.GetQuantityNormalized() - qtyNorm));
  212. else
  213. m_Bait.DeleteSafe();
  214. }
  215. }
  216. }
  217. class CatchingContextTrapFishSmall : CatchingContextTrapsBase
  218. {
  219. override protected void InitCatchMethodMask()
  220. {
  221. m_MethodMask = AnimalCatchingConstants.MASK_METHOD_FISHTRAP_SMALL;
  222. }
  223. }
  224. class CatchingContextTrapFishLarge : CatchingContextTrapsBase
  225. {
  226. override protected void InitCatchMethodMask()
  227. {
  228. m_MethodMask = AnimalCatchingConstants.MASK_METHOD_FISHTRAP_LARGE;
  229. }
  230. }
  231. class CatchingContextTrapLandSnare : CatchingContextTrapsBase
  232. {
  233. override protected void InitCatchMethodMask()
  234. {
  235. m_MethodMask = AnimalCatchingConstants.MASK_METHOD_LANDTRAP_SNARE;
  236. }
  237. }