actionskinning.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. Example of skinning config which should be inside animal's base class:
  3. class Skinning
  4. {
  5. // All classes in this scope are parsed, so they can have any name.
  6. class ObtainedSteaks
  7. {
  8. item = "DeerSteakMeat"; // item to spawn
  9. count = 10; // How many items to spawn
  10. transferToolDamageCoef = 1; // Optional: How much tool's damage is transfered to item's damage.
  11. // Make sure to have only 1 of the following quantity paramenter
  12. quantity = 100; // Optional: Set item's quantity
  13. quantityCoef = 1; // Optional: Set item's quantity (in coefficient)
  14. quantityMinMax[] = {100, 200}; // Optional: Set item's quantity within min/max values
  15. quantityMinMaxCoef[] = {0, 1}; // Optional: Set item's quantity (in coefficient) within min/max values
  16. };
  17. };
  18. */
  19. class ActionSkinningCB : ActionContinuousBaseCB
  20. {
  21. override void CreateActionComponent()
  22. {
  23. m_ActionData.m_ActionComponent = new CAContinuousTime(UATimeSpent.SKIN);
  24. }
  25. }
  26. class ActionSkinning: ActionContinuousBase
  27. {
  28. void ActionSkinning()
  29. {
  30. m_CallbackClass = ActionSkinningCB;
  31. m_SpecialtyWeight = UASoftSkillsWeight.PRECISE_MEDIUM;
  32. m_CommandUID = DayZPlayerConstants.CMD_ACTIONFB_ANIMALSKINNING;
  33. m_StanceMask = DayZPlayerConstants.STANCEMASK_CROUCH;
  34. m_FullBody = true;
  35. m_Text = "#skin";
  36. }
  37. override void CreateConditionComponents()
  38. {
  39. m_ConditionItem = new CCINonRuined();
  40. m_ConditionTarget = new CCTCursorNoRuinCheck();
  41. }
  42. override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item)
  43. {
  44. Object targetObject = target.GetObject();
  45. if (targetObject.CanBeSkinned() && !targetObject.IsAlive())
  46. {
  47. EntityAI target_EAI;
  48. if (Class.CastTo(target_EAI, targetObject) && target_EAI.CanBeSkinnedWith(item))
  49. return true;
  50. }
  51. return false;
  52. }
  53. // Spawns the loot according to the Skinning class in the dead agent's config
  54. override void OnFinishProgressServer(ActionData action_data)
  55. {
  56. super.OnFinishProgressServer(action_data);
  57. Object targetObject = action_data.m_Target.GetObject();
  58. // Mark the body as skinned to forbid another skinning action on it
  59. EntityAI body = EntityAI.Cast(targetObject);
  60. body.SetAsSkinned();
  61. MiscGameplayFunctions.RemoveAllAttachedChildrenByTypename(body, {Bolt_Base});
  62. HandlePlayerBody(action_data);
  63. SpawnItems(action_data);
  64. if (body)
  65. {
  66. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).Call(GetGame().ObjectDelete,body);
  67. }
  68. MiscGameplayFunctions.DealAbsoluteDmg(action_data.m_MainItem, UADamageApplied.SKINNING);
  69. PluginLifespan moduleLifespan = PluginLifespan.Cast(GetPlugin(PluginLifespan));
  70. moduleLifespan.UpdateBloodyHandsVisibility(action_data.m_Player, true);
  71. }
  72. // Spawns an organ defined in the given config
  73. ItemBase CreateOrgan(PlayerBase player, vector body_pos, string item_to_spawn, string cfg_skinning_organ_class, ItemBase tool)
  74. {
  75. // Create item from config
  76. ItemBase added_item;
  77. vector posHead;
  78. MiscGameplayFunctions.GetHeadBonePos(player,posHead);
  79. vector posRandom = MiscGameplayFunctions.GetRandomizedPositionVerified(posHead,body_pos,UAItemsSpreadRadius.NARROW,player);
  80. Class.CastTo(added_item, GetGame().CreateObjectEx(item_to_spawn, posRandom, ECE_PLACE_ON_SURFACE));
  81. // Check if skinning is configured for this body
  82. if (!added_item)
  83. return null;
  84. // Set item's quantity from config, if it's defined there.
  85. float item_quantity = 0;
  86. array<float> quant_min_max = new array<float>;
  87. array<float> quant_min_max_coef = new array<float>;
  88. GetGame().ConfigGetFloatArray(cfg_skinning_organ_class + "quantityMinMax", quant_min_max);
  89. GetGame().ConfigGetFloatArray(cfg_skinning_organ_class + "quantityMinMaxCoef", quant_min_max_coef);
  90. // Read config for quantity value
  91. if (quant_min_max.Count() > 0)
  92. {
  93. float soft_skill_manipulated_value = (quant_min_max.Get(0)+ quant_min_max.Get(1)) / 2;
  94. item_quantity = Math.RandomFloat(soft_skill_manipulated_value, quant_min_max.Get(1));
  95. }
  96. if (quant_min_max_coef.Count() > 0)
  97. {
  98. float coef = Math.RandomFloat(quant_min_max_coef.Get(0), quant_min_max_coef.Get(1));
  99. item_quantity = added_item.GetQuantityMax() * coef;
  100. }
  101. if (GetGame().ConfigGetFloat(cfg_skinning_organ_class + "quantity") > 0)
  102. item_quantity = g_Game.ConfigGetFloat(cfg_skinning_organ_class + "quantity");
  103. if (GetGame().ConfigGetFloat(cfg_skinning_organ_class + "quantityCoef") > 0)
  104. {
  105. float coef2 = g_Game.ConfigGetFloat(cfg_skinning_organ_class + "quantityCoef");
  106. item_quantity = added_item.GetQuantityMax() * coef2;
  107. }
  108. if (item_quantity > 0)
  109. {
  110. item_quantity = Math.Round(item_quantity);
  111. added_item.SetQuantity(item_quantity, false);
  112. }
  113. // Transfer tool's damage to the item's condition
  114. float item_apply_tool_damage_coef = GetGame().ConfigGetFloat(cfg_skinning_organ_class + "transferToolDamageCoef");
  115. if (item_apply_tool_damage_coef > 0)
  116. {
  117. float tool_dmg_coef = 1 - tool.GetHealth01();
  118. float organ_dmg_coef = tool_dmg_coef * item_apply_tool_damage_coef;
  119. added_item.DecreaseHealthCoef(organ_dmg_coef);
  120. }
  121. added_item.InsertAgent(eAgents.SALMONELLA, 1);
  122. return added_item;
  123. }
  124. override void OnFinishProgressClient(ActionData action_data)
  125. {
  126. super.OnFinishProgressClient(action_data);
  127. if (action_data.m_Target)
  128. {
  129. Object target_obj = action_data.m_Target.GetObject();
  130. if (target_obj.IsKindOf("Animal_CapreolusCapreolus") || target_obj.IsKindOf("Animal_CapreolusCapreolusF") || target_obj.IsKindOf("Animal_CervusElaphus") || target_obj.IsKindOf("Animal_CervusElaphusF"))
  131. {
  132. GetGame().GetAnalyticsClient().OnActionFinishedGutDeer();
  133. }
  134. }
  135. }
  136. //! This section drops all clothes (and attachments) from the dead player before deleting their body
  137. void HandlePlayerBody(ActionData action_data)
  138. {
  139. PlayerBase body;
  140. if (Class.CastTo(body,action_data.m_Target.GetObject()))
  141. {
  142. if (body.IsRestrained() && body.GetHumanInventory().GetEntityInHands())
  143. MiscGameplayFunctions.TransformRestrainItem(body.GetHumanInventory().GetEntityInHands(), null, action_data.m_Player, body);
  144. //Remove splint if target is wearing one
  145. if (body.IsWearingSplint())
  146. {
  147. EntityAI entity = action_data.m_Player.SpawnEntityOnGroundOnCursorDir("Splint", 0.5);
  148. ItemBase newItem = ItemBase.Cast(entity);
  149. EntityAI attachment = body.GetItemOnSlot("Splint_Right");
  150. if (attachment && attachment.GetType() == "Splint_Applied")
  151. {
  152. if (newItem)
  153. {
  154. MiscGameplayFunctions.TransferItemProperties(attachment, newItem);
  155. //Lower health level of splint after use
  156. if (newItem.GetHealthLevel() < 4)
  157. {
  158. int newDmgLevel = newItem.GetHealthLevel() + 1;
  159. float max = newItem.GetMaxHealth("", "");
  160. switch (newDmgLevel)
  161. {
  162. case GameConstants.STATE_BADLY_DAMAGED:
  163. newItem.SetHealth("", "", max * GameConstants.DAMAGE_BADLY_DAMAGED_VALUE);
  164. break;
  165. case GameConstants.STATE_DAMAGED:
  166. newItem.SetHealth("", "", max * GameConstants.DAMAGE_DAMAGED_VALUE);
  167. break;
  168. case GameConstants.STATE_WORN:
  169. newItem.SetHealth("", "", max * GameConstants.DAMAGE_WORN_VALUE);
  170. break;
  171. case GameConstants.STATE_RUINED:
  172. newItem.SetHealth("", "", max * GameConstants.DAMAGE_RUINED_VALUE);
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. }
  179. attachment.Delete();
  180. }
  181. }
  182. int deadBodyLifetime;
  183. if (GetCEApi())
  184. {
  185. deadBodyLifetime = GetCEApi().GetCEGlobalInt("CleanupLifetimeDeadPlayer");
  186. if (deadBodyLifetime <= 0)
  187. deadBodyLifetime = 3600;
  188. }
  189. DropInventoryItems(body,deadBodyLifetime);
  190. }
  191. }
  192. void DropInventoryItems(PlayerBase body, float newLifetime)
  193. {
  194. array<EntityAI> children = new array<EntityAI>;
  195. body.GetInventory().EnumerateInventory(InventoryTraversalType.LEVELORDER, children);
  196. foreach (EntityAI child : children)
  197. {
  198. if (child)
  199. {
  200. child.SetLifetime(newLifetime);
  201. body.GetInventory().DropEntity(InventoryMode.SERVER, body, child);
  202. }
  203. }
  204. }
  205. void SpawnItems(ActionData action_data)
  206. {
  207. EntityAI body = EntityAI.Cast(action_data.m_Target.GetObject());
  208. // Get config path to the animal
  209. string cfg_animal_class_path = "cfgVehicles " + body.GetType() + " " + "Skinning ";
  210. vector bodyPosition = body.GetPosition();
  211. if (!g_Game.ConfigIsExisting(cfg_animal_class_path))
  212. {
  213. Debug.Log("Failed to find class 'Skinning' in the config of: " + body.GetType());
  214. return;
  215. }
  216. // Getting item type from the config
  217. int child_count = g_Game.ConfigGetChildrenCount(cfg_animal_class_path);
  218. string item_to_spawn;
  219. string cfg_skinning_organ_class;
  220. // Parsing of the 'Skinning' class in the config of the dead body
  221. for (int i1 = 0; i1 < child_count; i1++)
  222. {
  223. // To make configuration as convenient as possible, all classes are parsed and parameters are read
  224. g_Game.ConfigGetChildName(cfg_animal_class_path, i1, cfg_skinning_organ_class); // out cfg_skinning_organ_class
  225. cfg_skinning_organ_class = cfg_animal_class_path + cfg_skinning_organ_class + " ";
  226. g_Game.ConfigGetText(cfg_skinning_organ_class + "item", item_to_spawn); // out item_to_spawn
  227. if (item_to_spawn != "") // Makes sure to ignore incompatible parameters in the Skinning class of the agent
  228. {
  229. // Spawning items in action_data.m_Player's inventory
  230. int item_count = g_Game.ConfigGetInt(cfg_skinning_organ_class + "count");
  231. array<string> itemZones = new array<string>;
  232. array<float> itemCount = new array<float>;
  233. float zoneDmg = 0;
  234. GetGame().ConfigGetTextArray(cfg_skinning_organ_class + "itemZones", itemZones);
  235. GetGame().ConfigGetFloatArray(cfg_skinning_organ_class + "countByZone", itemCount);
  236. if (itemCount.Count() > 0)
  237. {
  238. item_count = 0;
  239. for (int z = 0; z < itemZones.Count(); z++)
  240. {
  241. zoneDmg = body.GetHealth01(itemZones[z], "Health");
  242. zoneDmg *= itemCount[z]; //just re-using variable
  243. item_count += Math.Floor(zoneDmg);
  244. }
  245. }
  246. for (int i2 = 0; i2 < item_count; i2++)
  247. {
  248. ItemBase spawn_result = CreateOrgan(action_data.m_Player, bodyPosition, item_to_spawn, cfg_skinning_organ_class, action_data.m_MainItem);
  249. //Damage pelts based on the average values on itemZones
  250. //It only works if the "quantityCoef" in the config is more than 0
  251. float qtCoeff = GetGame().ConfigGetFloat(cfg_skinning_organ_class + "quantityCoef");
  252. if (qtCoeff > 0)
  253. {
  254. float avgDmgZones = 0;
  255. for (int c2 = 0; c2 < itemZones.Count(); c2++)
  256. {
  257. avgDmgZones += body.GetHealth01(itemZones[c2], "Health");
  258. }
  259. avgDmgZones = avgDmgZones/itemZones.Count(); // Evaluate the average Health
  260. if (spawn_result)
  261. spawn_result.SetHealth01("","", avgDmgZones);
  262. }
  263. // inherit temperature
  264. if (body.CanHaveTemperature() && spawn_result.CanHaveTemperature())
  265. {
  266. spawn_result.SetTemperatureDirect(body.GetTemperature());
  267. spawn_result.SetFrozen(body.GetIsFrozen());
  268. }
  269. // handle fat/guts from human bodies
  270. if ((item_to_spawn == "Lard") || (item_to_spawn == "Guts"))
  271. {
  272. if (body.IsKindOf("SurvivorBase"))
  273. {
  274. spawn_result.InsertAgent(eAgents.BRAIN, 1);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. //DEPRECATED
  282. vector GetRandomPos(vector body_pos)
  283. {
  284. return body_pos + Vector(Math.RandomFloat01() - 0.5, 0, Math.RandomFloat01() - 0.5);
  285. }
  286. }