File size: 4,745 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class PluginRepairing extends PluginBase
{
	bool Repair(PlayerBase player, ItemBase repair_kit, Object item, float specialty_weight, string damage_zone = "", bool use_kit_qty = true)
	{
		switch (item.GetHealthLevel(damage_zone))
		{
			case GameConstants.STATE_PRISTINE:
				break;
			case GameConstants.STATE_RUINED:
				#ifdef DEVELOPER
				Debug.Log("repairing from GameConstants.STATE_RUINED");
				#endif
				CalculateHealth( player, repair_kit, item, specialty_weight, damage_zone, use_kit_qty );
				break;
			case GameConstants.STATE_WORN:
				if (CanRepairToPristine(player) || CanBeRepairedToPristine(item))
				{
					CalculateHealth( player, repair_kit, item, specialty_weight,/* GameConstants.DAMAGE_PRISTINE_VALUE,*/ damage_zone, use_kit_qty );
				}
				break;
			default:
				CalculateHealth( player, repair_kit, item, specialty_weight, damage_zone, use_kit_qty );
				break;
		}

		return true;
	}

	void CalculateHealth( PlayerBase player, ItemBase kit, Object item, float specialty_weight, string damage_zone = "", bool use_kit_qty = true )
	{
		EntityAI entity;
		Class.CastTo(entity,item);
		
		if (entity != null)
		{
			entity.SetAllowDamage(true);
		}
		
		bool kit_has_quantity = kit.HasQuantity();
		int health_levels_count = item.GetNumberOfHealthLevels(damage_zone);
		float cur_kit_quantity = kit.GetQuantity();
		float kit_repair_cost_per_level = GetKitRepairCost( kit, item );
		float kit_repair_cost_adjusted; //used with specialty_weight, disconnected
		float new_quantity;
		
		int target_level = Math.Clamp(item.GetHealthLevel(damage_zone) - 1, 0, health_levels_count - 1);
		float health_coef;
		if ( !CanRepairToPristine( player ) && !CanBeRepairedToPristine( item ) )
		{
			target_level = Math.Clamp(target_level, GameConstants.STATE_WORN, health_levels_count - 1);
		}
		health_coef = item.GetHealthLevelValue(target_level,damage_zone);
		
		//handles kit depletion; TODO: move to separate method.
		if ( cur_kit_quantity > kit_repair_cost_per_level )
		{
			kit_repair_cost_adjusted = kit_repair_cost_per_level; //TODO: removed speciality weight for now, it should affect speed only (?).
			//kit_repair_cost_adjusted = player.GetSoftSkillsManager().SubtractSpecialtyBonus( kit_repair_cost_per_level, specialty_weight );
			kit_repair_cost_adjusted = Math.Clamp( kit_repair_cost_adjusted, 0, 100 );
			if(use_kit_qty)
			{
				new_quantity = kit.GetQuantity() - kit_repair_cost_adjusted;
				kit.SetQuantity( new_quantity );
			}
		}
		else if (!kit_has_quantity) //"kit" without quantity (hammers and such) for your every day repairing needs
		{
		}
		else
		{
			if(use_kit_qty)
			{
				kit.SetQuantity( 0 );
			}
		}
		
		if (item.GetHealth01(damage_zone,"Health") < health_coef)
		{
			item.SetHealth01(damage_zone,"Health",health_coef);
		}
		
		if (entity != null)
		{
			entity.ProcessInvulnerabilityCheck(entity.GetInvulnerabilityTypeString());
		}
	}

	bool CanRepair( ItemBase repair_kit, Object item, string damage_zone = "" )
	{
		int state = item.GetHealthLevel(damage_zone);
		
		if ( state != GameConstants.STATE_RUINED && (item.CanBeRepairedToPristine() && state >= GameConstants.STATE_WORN) || (!item.CanBeRepairedToPristine() && state >= GameConstants.STATE_DAMAGED ) )
		{
			int repair_kit_type = repair_kit.ConfigGetInt( "repairKitType" );
			
			array<int> repairable_with_types = new array<int>;
			item.ConfigGetIntArray( "repairableWithKits", repairable_with_types );	
			
			for ( int i = 0; i < repairable_with_types.Count(); i++ )
			{
				int repairable_with_type = repairable_with_types.Get(i);
				
				if ( IsRepairValid( repair_kit_type, repairable_with_type ) )
				{
					return true;
				}
			}
		}
		return false;
		
	}
	
	private bool IsRepairValid(int repair_kit_type, int repairable_with_type)
	{
		if (repair_kit_type > 0 && repairable_with_type > 0)
		{
			return repair_kit_type == repairable_with_type;
		}
		
		return false;
	}

	//! Player can repair items to 100%; currently unused
	private bool CanRepairToPristine(PlayerBase player)
	{
		return false; 
	}
	
	//! Item can be repaired to 100%
	private bool CanBeRepairedToPristine(Object item)
	{
		return item.CanBeRepairedToPristine();
	}
	
	private float GetKitRepairCost(ItemBase repair_kit, Object item)
	{
		array<int> allowedRepairKitTypes 	= new array<int>();	
		array<float> repairKitCosts 		= new array<float>();
		
		item.ConfigGetIntArray("repairableWithKits", allowedRepairKitTypes);
		item.ConfigGetFloatArray("repairCosts", repairKitCosts);
		
		int repairKitType = repair_kit.ConfigGetInt("repairKitType");
		
		foreach (int i, int allowedKitType : allowedRepairKitTypes)
		{
			if (allowedKitType == repairKitType)
			{
				return repairKitCosts.Get(i);
			}
		} 
					
		return 0;
	}
}