File size: 1,655 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
class Battery9V : ItemBase
{
	private int				m_Efficiency0To10; // Synchronized variable
	static private float 	m_EfficiencyDecayStart = 0.1; // At this % of maximum energy the output of the battery starts to weaken.
	
	void Battery9V()
	{
		m_Efficiency0To10 = 10;
		RegisterNetSyncVariableInt("m_Efficiency0To10");
	}
	
	//! Returns efficiency of this battery. The value is synchronized from server to all clients and is accurate down to 0.1 units.
	float GetEfficiency0To1()
	{
		return m_Efficiency0To10 / 10;
	}
	
	//! Returns efficiency of this battery. The value is synchronized from server to all clients and is accurate down to 0.1 unit.
	float GetEfficiencyDecayStart()
	{
		return m_EfficiencyDecayStart;
	}
	
	
	override void OnEnergyConsumed()
	{
		super.OnEnergyConsumed();
		
		if (GetGame().IsServer())
		{
			float energyCoef = GetCompEM().GetEnergy0To1();
			
			if (energyCoef < m_EfficiencyDecayStart && m_EfficiencyDecayStart > 0)
			{
				m_Efficiency0To10 = Math.Round((energyCoef / m_EfficiencyDecayStart) * 10);
				SetSynchDirty();
			}
		}
	}
	
	// Not needed right now, but it will be useful if we add rechargable batteries.
	override void OnEnergyAdded()
	{
		super.OnEnergyAdded();
		
		if (GetGame().IsServer())
		{
			float energyCoef = GetCompEM().GetEnergy0To1();
			if (energyCoef < m_EfficiencyDecayStart && m_EfficiencyDecayStart > 0)
			{
				m_Efficiency0To10 = Math.Round((energyCoef / m_EfficiencyDecayStart) * 10);
				SetSynchDirty();
			}
			else
			{
				m_Efficiency0To10 = 10;
				SetSynchDirty();
			}
		}
	}
	
	override void SetActions()
	{
		super.SetActions();
		
		AddAction(ActionMeasureBattery);
	}
}