File size: 3,151 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
class ArrowManagerPlayer : ArrowManagerBase
{	
	private static ref map<int,typename> m_TypeHashTable;
	
	void ArrowManagerPlayer(PlayerBase player)
	{
		if (!m_TypeHashTable)
		{
			InitializeHash();
		}
	}
	
	private static void InitializeHash()
	{
		m_TypeHashTable = new map<int,typename>();
		
		AddArrowTypeToHash("Ammo_HuntingBolt");
		AddArrowTypeToHash("Ammo_ImprovisedBolt_1");
		AddArrowTypeToHash("Ammo_ImprovisedBolt_2");
		AddArrowTypeToHash("Ammo_ImprovisedBolt_3");
	}
	
	private static void AddArrowTypeToHash(string ArrowType)
	{
		m_TypeHashTable.Insert(ArrowType.Hash(), ArrowType.ToType());
	}
	
	private static typename GetArrowTypeFromHash(int hash)
	{
		return m_TypeHashTable.Get(hash);
	}
	
	bool Save(ParamsWriteContext ctx)
	{
		ctx.Write(VERSION);
		int count = m_Arrows.Count();
		int i;
		//TODO MW Delete after find why sometimes arrow missing - most likely Life span
		for (i = count - 1; i >= 0; i--)
		{
			if (!m_Arrows.Get(i))
			{
				m_Arrows.Remove(i);
			}
		}
		count = m_Arrows.Count();
		
		ctx.Write(count);
		
		for (i = 0; i < count; i++)
		{
			EntityAI arrow = m_Arrows.Get(i);
			
			string type = arrow.GetType();
			ctx.Write(type.Hash());
			
			vector angle = arrow.GetLocalYawPitchRoll();
			vector pos = arrow.GetLocalPosition();
			
			ctx.Write(angle[0]);
			ctx.Write(angle[1]);
			ctx.Write(angle[2]);
			ctx.Write(pos[0]);
			ctx.Write(pos[1]);
			ctx.Write(pos[2]);
			
			int pivot = arrow.GetHierarchyPivot();
			ctx.Write(pivot);
		}

		return true;
	}

	
	bool Load(ParamsReadContext ctx)
	{
		int version;
		if (!ctx.Read(version))
		{
			return false;
		}
		
		int count;
		if (!ctx.Read(count))
		{
			return false;
		}
		
		for (int i = 0; i < count; i++)
		{
		
			if (version >= 1)
			{
				int hash;
				if (!ctx.Read(hash))
				{
					return false;
				}

				float angleF[3];
				float posF[3];
				float value;
				
				if (!ctx.Read(value))
				{
					return false;
				}
				angleF[0] = value;
				
				if (!ctx.Read(value))
				{
					return false;
				}
				angleF[1] = value;
				
				if (!ctx.Read(value))
				{
					return false;
				}
				angleF[2] = value;
				
				if (!ctx.Read(value))
				{
					return false;
				}
				posF[0] = value;
				
				if (!ctx.Read(value))
				{
					return false;
				}
				posF[1] = value;
				
				if (!ctx.Read(value))
				{
					return false;
				}
				posF[2] = value;
				
				vector angle = "0 0 0";
				vector pos = "0 0 0";

				angle = vector.ArrayToVec(angleF);
				pos = vector.ArrayToVec(posF);
				
				int pivot;
				if (!ctx.Read(pivot))
				{
					return false;
				}
				
				if (version >= 2)
				{
					#ifdef SERVER
					int spawnFlags = ECE_KEEPHEIGHT|ECE_DYNAMIC_PERSISTENCY;
					#else
					int spawnFlags = ECE_LOCAL|ECE_KEEPHEIGHT|ECE_DYNAMIC_PERSISTENCY;
					#endif
					
					typename arrowType = GetArrowTypeFromHash(hash);
					EntityAI arrow = EntityAI.Cast(GetGame().CreateObjectEx(arrowType.ToString(), pos, spawnFlags));
					if (arrow)
					{
						arrow.SetQuantityToMinimum();
						arrow.SetYawPitchRoll(angle);
						m_Owner.AddChild(arrow, pivot);
					}
				}
			}
		
		}

		return true;
	}
}