File size: 6,272 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
232
233
234
235
236
237
238
239
240
241
class InventoryItem extends EntityAI
{	
	static private const float SOUND_CONTACT_SKIP = 0.33;//second
	
#ifdef DIAG_DEVELOPER
	static private ref array<ref string> s_ImpactSoundsInfo = new array<ref string>();
#endif

	private SoundLookupTable m_SoundImpactTable;
	private float m_SoundContactTickTime;
	private bool m_IsMeleeWeapon = false;
	
	proto native InventoryItemType GetInventoryItemType();

	//! Some inventoryItem devices can be switched on/off (radios, transmitters)
	proto native void SwitchOn(bool onOff);
	//! Some inventoryItem devices can be switched on/off (radios, transmitters)
	proto native bool IsOn();
	
	//! collisions with character
	proto native void EnableCollisionsWithCharacter(bool state);
	proto native bool HasCollisionsWithCharacter();

	proto native MeleeCombatData GetMeleeCombatData();	
	
	proto native void ThrowPhysically(DayZPlayer player, vector force, bool collideWithCharacters = true);

	//! Sets the item to use the server configured 'networkRangeFar' instead of 'networkRangeNear'
	//  This method performs an OR operation with the config 'forceFarBubble'. If set in the config 
	//  this method has no effect. 
	proto native void ForceFarBubble(bool state);
	
	void InventoryItem()
	{
		InitImpactSoundData();
		
		if (ConfigIsExisting("isMeleeWeapon"))
			m_IsMeleeWeapon = ConfigGetBool("isMeleeWeapon");
	}
	
	
	void OnRightClick()
	{
	
	}

	event bool OnUseFromInventory(Man owner)
	{
		return false;
	}

	//! Get tooltip text
	string GetTooltip()
	{
		string temp;
		if (!DescriptionOverride(temp))
			temp = ConfigGetString("descriptionShort");
		return temp;
	}

	override bool IsInventoryItem()
	{
		return true;
	}
	
	int GetMeleeMode()
	{
		return 0;
	}

	int GetMeleeHeavyMode()
	{
		return 1;
	}	
	
	int GetMeleeSprintMode()
	{
		return 2;
	}
	
	override bool IsMeleeWeapon()
	{
		return m_IsMeleeWeapon;
	}
	
	bool IsMeleeFinisher()
	{
		return false;
	}
	
	// -------------------------------------------------------------------------------
	void PlayImpactSound(float weight, float velocity, int surfaceHash)
	{
		if (!m_SoundImpactTable)
			return;

		SoundObjectBuilder soundBuilder = m_SoundImpactTable.GetSoundBuilder(surfaceHash);
		if (soundBuilder != null)
		{
			soundBuilder.AddVariable("weight", weight);
			soundBuilder.AddVariable("speed", velocity);
			soundBuilder.AddEnvSoundVariables(GetPosition());
				
			SoundObject soundObject = soundBuilder.BuildSoundObject();
			if (soundObject != null)
			{
				soundObject.SetKind(WaveKind.WAVEEFFECTEX);
				PlaySound(soundObject, soundBuilder);
			}
		}
	}
	
	// -------------------------------------------------------------------------------
	protected void InitImpactSoundData()
	{
		#ifndef SERVER
		string soundImpactType = "default";
		if ( ConfigIsExisting("soundImpactType") )
			soundImpactType = ConfigGetString("soundImpactType");
		
		m_SoundImpactTable = AnimSoundLookupTableBank.GetInstance().GetImpactTable(soundImpactType + "_Impact_LookupTable");
		#endif
	}
	
	// -------------------------------------------------------------------------------
	AbstractWave PlaySound(SoundObject so, SoundObjectBuilder sob)
	{
		if (so == null)
			return null;

		so.SetPosition(GetPosition());
		AbstractWave wave = GetGame().GetSoundScene().Play3D(so, sob);

		return wave;
	}
	
	// -------------------------------------------------------------------------------
	void PlaySoundByAnimEvent(EAnimSoundEventID id)
	{
		AnimSoundEvent soundEvent = GetInventoryItemType().GetSoundEvent(id);
		if (soundEvent)
		{
			SoundObjectBuilder builder = soundEvent.GetSoundBuilder();
			SoundObject soundObject = builder.BuildSoundObject();
			if (soundObject)
				PlaySound(soundObject, builder);
		}
	}
	
	
	// -------------------------------------------------------------------------------
	string GetImpactSurfaceType(IEntity other, Contact impact)
	{
		string surface;
		int liquid = -1;
		return GetImpactSurfaceTypeEx(other, impact, liquid);
	}
	
	// -------------------------------------------------------------------------------
	string GetImpactSurfaceTypeEx(IEntity other, Contact impact, out int liquid)
	{		
		vector mins, maxs;
		GetWorldBounds(mins, maxs);
		vector size = maxs - mins;		

		vector add = impact.RelativeVelocityBefore.Normalized() * size.Length();
		string surfaceImpact;
		if (DayZPhysics.GetHitSurfaceAndLiquid(
			Object.Cast(other),
			impact.Position + add,
			impact.Position - add,
			surfaceImpact,
			liquid))
		{
			return surfaceImpact;
		}
		string surface;
		GetGame().SurfaceUnderObjectEx(this, surface, surfaceImpact, liquid);

		return surfaceImpact;
	}
	
	//! returns ammo (projectile) used in melee if the item is destroyed. Override higher for specific use
	string GetRuinedMeleeAmmoType()
	{
		return "MeleeSoft";
	}

	// -------------------------------------------------------------------------------
	float ProcessImpactSound(IEntity other, Contact extra, float weight, out int surfaceHash)
	{
		int liquidType = -1;
		return ProcessImpactSoundEx(other, extra, weight, surfaceHash,liquidType);
	}
	
	
	// -------------------------------------------------------------------------------
	float ProcessImpactSoundEx(IEntity other, Contact extra, float weight, out int surfaceHash, out int liquidType)
	{
		float impactVelocity = extra.RelativeVelocityBefore.Length();
		if ( impactVelocity < 0.3 )
			return 0.0;
		
		float tickTime = GetGame().GetTickTime();
		if ( m_SoundContactTickTime + SOUND_CONTACT_SKIP > tickTime )
			return 0.0;
		
		string surfaceName = GetImpactSurfaceTypeEx(other, extra, liquidType);
		if ( surfaceName == "" )
			return 0.0;

#ifdef DIAG_DEVELOPER
		string infoText = "Surface: " + surfaceName + ", Weight: " + weight + ", Speed: " + impactVelocity;
		
		if ( s_ImpactSoundsInfo.Count() == 10 )
			s_ImpactSoundsInfo.Remove(9);
		
		s_ImpactSoundsInfo.InsertAt(infoText, 0);
#endif

		m_SoundContactTickTime = tickTime;

		surfaceHash = surfaceName.Hash();
		return impactVelocity;		
	}
	
#ifdef DIAG_DEVELOPER
	static void DrawImpacts()
	{
		DbgUI.Begin("Item impact sounds", 10, 200);
		
		for ( int i = 0; i < s_ImpactSoundsInfo.Count(); ++i )
		{
			string line = (i + 1).ToString() + ". " + s_ImpactSoundsInfo.Get(i);
			DbgUI.Text(line);
		}
		
		DbgUI.End();
	}
#endif
};