Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
pluginrepairing.c
Go to the documentation of this file.
1 class PluginRepairing extends PluginBase
2 {
3  bool Repair(PlayerBase player, ItemBase repair_kit, Object item, float specialty_weight, string damage_zone = "", bool use_kit_qty = true)
4  {
5  switch (item.GetHealthLevel(damage_zone))
6  {
7  case GameConstants.STATE_PRISTINE:
8  break;
9  case GameConstants.STATE_RUINED:
10  #ifdef DEVELOPER
11  Debug.Log("repairing from GameConstants.STATE_RUINED");
12  #endif
13  CalculateHealth( player, repair_kit, item, specialty_weight, damage_zone, use_kit_qty );
14  break;
15  case GameConstants.STATE_WORN:
16  if (CanRepairToPristine(player) || CanBeRepairedToPristine(item))
17  {
18  CalculateHealth( player, repair_kit, item, specialty_weight,/* GameConstants.DAMAGE_PRISTINE_VALUE,*/ damage_zone, use_kit_qty );
19  }
20  break;
21  default:
22  CalculateHealth( player, repair_kit, item, specialty_weight, damage_zone, use_kit_qty );
23  break;
24  }
25 
26  return true;
27  }
28 
29  void CalculateHealth( PlayerBase player, ItemBase kit, Object item, float specialty_weight, string damage_zone = "", bool use_kit_qty = true )
30  {
31  EntityAI entity;
32  Class.CastTo(entity,item);
33 
34  if (entity != null)
35  {
36  entity.SetAllowDamage(true);
37  }
38 
39  bool kit_has_quantity = kit.HasQuantity();
40  int health_levels_count = item.GetNumberOfHealthLevels(damage_zone);
41  float cur_kit_quantity = kit.GetQuantity();
42  float kit_repair_cost_per_level = GetKitRepairCost( kit, item );
43  float kit_repair_cost_adjusted; //used with specialty_weight, disconnected
44  float new_quantity;
45 
46  int target_level = Math.Clamp(item.GetHealthLevel(damage_zone) - 1, 0, health_levels_count - 1);
47  float health_coef;
48  if ( !CanRepairToPristine( player ) && !CanBeRepairedToPristine( item ) )
49  {
50  target_level = Math.Clamp(target_level, GameConstants.STATE_WORN, health_levels_count - 1);
51  }
52  health_coef = item.GetHealthLevelValue(target_level,damage_zone);
53 
54  //handles kit depletion; TODO: move to separate method.
55  if ( cur_kit_quantity > kit_repair_cost_per_level )
56  {
57  kit_repair_cost_adjusted = kit_repair_cost_per_level; //TODO: removed speciality weight for now, it should affect speed only (?).
58  //kit_repair_cost_adjusted = player.GetSoftSkillsManager().SubtractSpecialtyBonus( kit_repair_cost_per_level, specialty_weight );
59  kit_repair_cost_adjusted = Math.Clamp( kit_repair_cost_adjusted, 0, 100 );
60  if(use_kit_qty)
61  {
62  new_quantity = kit.GetQuantity() - kit_repair_cost_adjusted;
63  kit.SetQuantity( new_quantity );
64  }
65  }
66  else if (!kit_has_quantity) //"kit" without quantity (hammers and such) for your every day repairing needs
67  {
68  }
69  else
70  {
71  if(use_kit_qty)
72  {
73  kit.SetQuantity( 0 );
74  }
75  }
76 
77  if (item.GetHealth01(damage_zone,"Health") < health_coef)
78  {
79  item.SetHealth01(damage_zone,"Health",health_coef);
80  }
81 
82  if (entity != null)
83  {
84  entity.ProcessInvulnerabilityCheck(entity.GetInvulnerabilityTypeString());
85  }
86  }
87 
88  bool CanRepair( ItemBase repair_kit, Object item, string damage_zone = "" )
89  {
90  int state = item.GetHealthLevel(damage_zone);
91 
92  if ( state != GameConstants.STATE_RUINED && (item.CanBeRepairedToPristine() && state >= GameConstants.STATE_WORN) || (!item.CanBeRepairedToPristine() && state >= GameConstants.STATE_DAMAGED ) )
93  {
94  int repair_kit_type = repair_kit.ConfigGetInt( "repairKitType" );
95 
96  array<int> repairable_with_types = new array<int>;
97  item.ConfigGetIntArray( "repairableWithKits", repairable_with_types );
98 
99  for ( int i = 0; i < repairable_with_types.Count(); i++ )
100  {
101  int repairable_with_type = repairable_with_types.Get(i);
102 
103  if ( IsRepairValid( repair_kit_type, repairable_with_type ) )
104  {
105  return true;
106  }
107  }
108  }
109  return false;
110 
111  }
112 
113  private bool IsRepairValid(int repair_kit_type, int repairable_with_type)
114  {
115  if (repair_kit_type > 0 && repairable_with_type > 0)
116  {
117  return repair_kit_type == repairable_with_type;
118  }
119 
120  return false;
121  }
122 
124  private bool CanRepairToPristine(PlayerBase player)
125  {
126  return false;
127  }
128 
130  private bool CanBeRepairedToPristine(Object item)
131  {
132  return item.CanBeRepairedToPristine();
133  }
134 
135  private float GetKitRepairCost(ItemBase repair_kit, Object item)
136  {
137  array<int> allowedRepairKitTypes = new array<int>();
138  array<float> repairKitCosts = new array<float>();
139 
140  item.ConfigGetIntArray("repairableWithKits", allowedRepairKitTypes);
141  item.ConfigGetFloatArray("repairCosts", repairKitCosts);
142 
143  int repairKitType = repair_kit.ConfigGetInt("repairKitType");
144 
145  foreach (int i, int allowedKitType : allowedRepairKitTypes)
146  {
147  if (allowedKitType == repairKitType)
148  {
149  return repairKitCosts.Get(i);
150  }
151  }
152 
153  return 0;
154  }
155 }
ItemBase
Definition: inventoryitem.c:730
PluginBase
Definition: pluginadminlog.c:1
PlayerBase
Definition: playerbaseclient.c:1
Repair
bool Repair(PlayerBase player, ItemBase item_repair_kit, float specialty_weight)
Definition: itembase.c:2383
Object
Definition: objecttyped.c:1
CanRepair
bool CanRepair(ItemBase item_repair_kit)
Definition: itembase.c:2376
array
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
Definition: isboxcollidinggeometryproxyclasses.c:27
GameConstants
Definition: constants.c:612
Debug
Definition: debug.c:13
Math
Definition: enmath.c:6
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
EntityAI
Definition: building.c:5