File size: 5,582 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
enum DSLevels
{
	NORMAL = 0,//no bit, default
	WARNING = 1,//first bit
	CRITICAL = 2,//second bit
	BLINKING = 3,//first + second bit
	EXTRA = 4,//third bit
}

enum DSLevelsTemp
{
	NORMAL = 0,//no bit, default
	WARNING_MINUS = 1,//
	CRITICAL_MINUS = 2,//
	BLINKING_MINUS = 3,//
	WARNING_PLUS = 4,//
	CRITICAL_PLUS = 5,//
	BLINKING_PLUS = 6,//
}

class VirtualHud
{
	const int NUMBER_OF_MASKS = 2;//how many INT numbers we need to accommodate all elements
	ref array<int> m_LastSentArray;
	//ref map<int, ref DisplayElement> m_Elements;
	const int NUMBER_OF_ELEMENTS = eDisplayElements.COUNT;
	ref DisplayElementBase m_Elements[NUMBER_OF_ELEMENTS];
	Mission mission;
	Hud m_Hud;
	int m_LastTick;
	PlayerBase m_Player;
	string m_System 		= "VirtualHud";
	
	ref array<ref Param> rpcParams;
	
	void VirtualHud(PlayerBase player)
	{
		m_Player 		= player;
		m_LastTick		= 0;

		RegisterElement(new BadgeStuffed(m_Player));
		RegisterElement(new BadgeWet(m_Player));
		RegisterElement(new BadgeSick(m_Player));
		RegisterElement(new BadgePills(m_Player));
		RegisterElement(new BadgePoisoned(m_Player));
		RegisterElement(new BadgeFracture(m_Player));
		RegisterElement(new TendencyHealth(m_Player));
		RegisterElement(new TendencyBlood(m_Player));
		RegisterElement(new TendencyTemperature(m_Player));

		RegisterElement(new TendencyHunger(m_Player));
		RegisterElement(new TendencyThirst(m_Player));
		RegisterElement(new TendencyBacteria(m_Player));
		RegisterElement(new BadgeHeartbeat(m_Player));
		RegisterElement(new BadgeLegs(m_Player));


		RegisterElement(new ElementStance(m_Player));// client only
		RegisterElement(new BadgeBleeding(m_Player));// client only
		

		mission = GetGame().GetMission();
		if ( mission )
		{
			m_Hud = mission.GetHud();
		}
		//UpdateStatus();
	}
	
	
	void OnScheduledTick()
	{
		if ( GetGame().IsServer() )
		{
			if (GetGame().GetTime() > (m_LastTick + VIRTUAL_HUD_UPDATE_INTERVAL))
			{
				SendRPC();
				m_LastTick = GetGame().GetTime();
			}
		}
		if ( !GetGame().IsDedicatedServer() )
		{
			ImmediateUpdate();
			//DisplayPresence();
		}
	}

	void RegisterElement(DisplayElementBase element)
	{
		int id = element.GetType();
		m_Elements[id] = element;
		//Log("adding element:"+id.ToString());
	}
	
	DisplayElementBase GetElement(eDisplayElements element_id)
	{
		if ( element_id < 0 || element_id >= NUMBER_OF_ELEMENTS )
		{
			return null;
		}
		return m_Elements[element_id];
	}

	//this will serialize all elements and 'compresses' them into integer(s) through bit shifting, these integers are placed into an array
	void SerializeElements(ref array<int> mask_array)
	{
		int offset = 0;
		int mask = 0;
		
		for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
		{
			if (  GetElement(i) && !GetElement(i).IsClientOnly() )
			{
				if ( (GetElement(i).GetNumberOfBits() + offset) > BIT_INT_SIZE )
				{
					mask_array.Insert(mask);
					offset = 0;
					mask = 0;
				}
				mask = mask | (GetElement(i).GetValue() << offset);
				offset = offset + GetElement(i).GetNumberOfBits();
			}
		}
		mask_array.Insert(mask);
	}

	void DeserializeElements(ref array<int> mask_array)//extracts elements from mask
	{
		int maskArrayIndex = 0;
		int offset = 0;
		int mask = 0;
		
		for (int i = 0; i < NUMBER_OF_ELEMENTS;i++)
		{
			if ( GetElement(i) && !GetElement(i).IsClientOnly() )
			{
				//Log("entity> " + ToString(GetElement(i)) );
				if (offset + GetElement(i).GetNumberOfBits() > BIT_INT_SIZE)
				{
					maskArrayIndex++;
					offset = 0;
				}
				mask = mask_array.Get(maskArrayIndex);
				int value = BitToDec( mask, offset, GetElement(i).GetCompareMask() );
				offset = offset + GetElement(i).GetNumberOfBits();
				GetElement(i).SetValue( value );
			}
		}
	}

	int BitToDec(int mask, int index, int compareMask)
	{
		int value = mask & (compareMask << index);
		value = value >> index;
		return value;
	}


	void PrintElements()
	{
		for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
		{
			PrintString(i.ToString() +": "+ GetElement(i).m_Value.ToString() );
		}
	}

	void SendRPC()
	{
		array<int> mask_array = new array<int>;
		SerializeElements(mask_array);
		if ( !m_LastSentArray || !AreArraysSame(m_LastSentArray, mask_array)  )
		{
			ScriptRPC rpc = new ScriptRPC();
			rpc.Write(mask_array);
			rpc.Send(m_Player, ERPCs.RPC_SYNC_DISPLAY_STATUS, false, m_Player.GetIdentity());
			m_LastSentArray = mask_array;
		}
	}

	bool AreArraysSame( notnull array<int> array_a, notnull array<int> array_b )
	{
		if ( array_a.Count() != array_b.Count() ) return false;
		for (int i = 0; i  <array_a.Count(); i++)
		{
			if ( array_a.Get(i) != array_b.Get(i) )
			{
				return false;
			}
		}
		return true;
	}

	void ImmediateUpdate()
	{
		for (int i = 0; i < NUMBER_OF_ELEMENTS;i++)
		{
			DisplayElementBase element = GetElement(i);
			if ( element && element.IsClientOnly() && element.IsValueChanged() )
				element.UpdateHUD();
		}
	}
	
	void UpdateStatus()
	{
		//Log("UpdateStatus called for entity: "+ToString(m_Player));
		for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
		{
			DisplayElementBase element = GetElement(i);
			if (  element && !element.IsClientOnly() && element.IsValueChanged() ) 
			{
				element.UpdateHUD();
			}
		}
	}

	void OnRPC(ParamsReadContext ctx)//on Client
	{
		//Log("OnRPC called");
		array<int> mask_array = new array<int>;
		ctx.Read(mask_array); 
		DeserializeElements(mask_array);
		UpdateStatus();
	}

	void Debug()
	{
		Log("debug");
		PluginPlayerStatus 	m_ModulePlayerStatus = PluginPlayerStatus.Cast(GetPlugin(PluginPlayerStatus));
		m_ModulePlayerStatus.DisplayTendency(NTFKEY_HUNGRY, 2);
	}
}