Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
magazine.c
Go to the documentation of this file.
1typedef Magazine Magazine_Base;
2
4{
5 None = 0,
6 Pistol = 1,
9 Shell = 4,
10 Arrow = 5
12
13enum ProjectileType
14{
15 None = 0,
16 Tracer = 1,
17 AP = 2
18}
19
20
21class AmmoData
22{
23 bool m_IsValid;
24 CartridgeType m_CartridgeType;
25 ProjectileType m_ProjectileType;
26
27 void AmmoData( string init_type )
28 {
29 m_IsValid = GetGame().ConfigIsExisting( "CfgMagazines " + init_type );
30 if ( m_IsValid )
31 {
32 m_CartridgeType = GetGame().ConfigGetInt( "CfgMagazines " + init_type + " iconCartridge" );
33 m_ProjectileType = GetGame().ConfigGetInt( "CfgMagazines " + init_type + " iconType" );
34 }
35 }
36}
37
38class Magazine : InventoryItemSuper
39{
40 protected static ref map<string, ref AmmoData> m_AmmoData;
41 ref array<string> m_CompatiableAmmo;
42 ref array<float> m_ChanceToJam;
43 protected float m_ManipulationDamage;
44
45 void Magazine ()
46 {
47 m_ChanceToJam = new array<float>;
48 InitReliability(m_ChanceToJam);
49 m_ManipulationDamage = ConfigGetFloat("manipulationDamage");
50 m_CompatiableAmmo = new array<string>;
51 ConfigGetTextArray("ammoItems", m_CompatiableAmmo);
52 if ( !GetGame().IsDedicatedServer() )
53 {
54 if ( !m_AmmoData )
55 m_AmmoData = new map<string, ref AmmoData>;
56
57 string classname = ClassName();
58 if ( !m_AmmoData.Contains(classname) )
59 {
60 ref AmmoData new_data = new AmmoData( classname );
61 if ( new_data.m_IsValid )
62 m_AmmoData.Insert( classname, new AmmoData( classname ) );
63 }
64 }
65 }
66
68 proto native int GetAmmoCount();
70 proto native void ServerSetAmmoCount(int ammoCount);
71 proto native void LocalSetAmmoCount(int ammoCount);
72
79 proto bool LocalAcquireCartridge(out float dmg, out string cartTypeName);
80 proto bool ServerAcquireCartridge(out float dmg, out string cartTypeName);
87 proto native bool LocalStoreCartridge(float ammoDamage, string cartTypeName);
88 proto native bool ServerStoreCartridge(float ammoDamage, string cartTypeName);
89
97 proto bool GetCartridgeAtIndex(int cartIndex, out float dmg, out string cartTypeName);
98
106 proto bool SetCartridgeAtIndex(int cartIndex, out float dmg, out string cartTypeName);
107
114 proto bool SetCartridgeDamageAtIndex(int cartIndex, float dmg);
115
116
117 static AmmoData GetAmmoData( string classname )
118 {
119 if ( !m_AmmoData )
120 m_AmmoData = new map<string, ref AmmoData>;
121 if ( !m_AmmoData.Contains(classname) )
122 {
123 ref AmmoData new_data = new AmmoData( classname );
124 if ( new_data.m_IsValid )
125 m_AmmoData.Insert( classname, new AmmoData( classname ) );
126 return new_data;
127 }
128 else
129 {
130 return m_AmmoData.Get( classname );
131 }
132 }
133
134 bool IsCompatiableAmmo( ItemBase ammo )
135 {
136 if ( m_CompatiableAmmo && ammo )
137 return ( m_CompatiableAmmo.Find( ammo.GetType() ) > -1 );
138 else
139 return false;
140 }
141
142 bool CanAddCartridges(int count)
143 {
144 int spc_avail = GetAmmoMax() - GetAmmoCount();
145 return count <= spc_avail;
146 }
147
149 void ServerAddAmmoCount(int ammoCount)
150 {
151 ServerSetAmmoCount(GetAmmoCount() + ammoCount);
152 }
153 void LocalAddAmmoCount(int ammoCount)
154 {
155 LocalSetAmmoCount(GetAmmoCount() + ammoCount);
156 }
158 int GetAmmoMax()
159 {
160 return m_Count;
161 }
163 void ServerSetAmmoMax()
164 {
165 ServerSetAmmoCount( GetAmmoMax() );
166 }
167 void LocalSetAmmoMax()
168 {
169 LocalSetAmmoCount( GetAmmoMax() );
170 }
172 override bool IsMagazine()
173 {
174 return true;
175 }
176
177
178 override bool CanBeSplit()
179 {
180 if ( m_CanThisBeSplit )
181 return ( GetAmmoCount() > 1 );
182
183 return false;
184 }
185
186 bool InitReliability(out array<float> reliability_array)
187 {
188 if (GetGame().ConfigIsExisting("cfgMagazines " + GetType() + " Reliability ChanceToJam"))
189 {
190 GetGame().ConfigGetFloatArray("cfgMagazines " + GetType() + " Reliability ChanceToJam",reliability_array);
191 return true;
192 }
193 return false;
194 }
195
196 float GetChanceToJam()
197 {
198 int level = GetHealthLevel();
199
200 if (level >= 0 && level < m_ChanceToJam.Count())
201 return m_ChanceToJam[level];
202 else
203 return 0.0;
204 }
205
206 override void SplitItemToInventoryLocation( notnull InventoryLocation dst )
207 {
208 if ( !CanBeSplit() )
209 return;
210
211 Magazine new_pile = Magazine.Cast( GameInventory.LocationCreateEntity( dst, GetType(), ECE_IN_INVENTORY, RF_DEFAULT ) );
212 if( new_pile )
213 {
214 MiscGameplayFunctions.TransferItemProperties(dst.GetItem(), new_pile);
215
216 new_pile.ServerSetAmmoCount(0);
217 int quantity = GetAmmoCount();
218
219 for (int i = 0; i < Math.Floor( quantity * 0.5 ); ++i)
220 {
221 float damage;
222 string cartrige_name;
223 ServerAcquireCartridge(damage, cartrige_name);
224 new_pile.ServerStoreCartridge(damage, cartrige_name);
225 }
226 new_pile.SetSynchDirty();
227 SetSynchDirty();
228 }
229 }
230
231 override void SplitItem(PlayerBase player)
232 {
233 if ( !CanBeSplit() )
234 return;
235
236
237 Magazine new_pile = Magazine.Cast( player.CreateCopyOfItemInInventoryOrGround( this ) );
238 if( new_pile )
239 {
240 new_pile.ServerSetAmmoCount(0);
241 int quantity = this.GetAmmoCount();
242
243 for (int i = 0; i < Math.Floor( quantity / 2 ); i++)
244 {
245 float damage;
246 string cartrige_name;
247 ServerAcquireCartridge(damage, cartrige_name);
248 new_pile.ServerStoreCartridge(damage, cartrige_name);
249 }
250 new_pile.SetSynchDirty();
251 SetSynchDirty();
252 }
253 }
254
255 void ApplyManipulationDamage()
256 {
257 AddHealth("","Health",-m_ManipulationDamage);
258 }
259
260 override bool IsFullQuantity()
261 {
262 if ( GetAmmoCount() == GetAmmoMax() )
263 {
264 return true;
265 }
266 else
267 {
268 return false;
269 }
270 }
271
272 override protected float GetWeightSpecialized(bool forceRecalc = false)
273 {
274 #ifdef DEVELOPER
275 if (WeightDebug.m_VerbosityFlags & WeightDebugType.RECALC_FORCED)
276 {
277 WeightDebugData data = WeightDebug.GetWeightDebug(this);
278 data.SetCalcDetails("TMAG: ("+GetAmmoCount()+"(Ammo count) * " + ConfigGetFloat("weightPerQuantityUnit")+"(weightPerQuantityUnit)) + " + GetConfigWeightModifiedDebugText());
279 }
280 #endif
281 return GetConfigWeightModified() + (GetAmmoCount() * ConfigGetFloat("weightPerQuantityUnit"));
282 }
283
284 override bool IsCombineAll( ItemBase other_item, bool use_stack_max = false)
285 {
286 Magazine other_magazine = Magazine.Cast(other_item);
287 int free_space = GetAmmoMax() - GetAmmoCount();
288
289 return free_space >= other_magazine.GetAmmoCount();
290 }
291
292 override void CombineItems( ItemBase other_item, bool use_stack_max = false )
293 {
294 if ( !CanBeCombined(other_item) )
295 return;
296
297 if ( other_item.GetType() != GetType() )
298 return;
299
300 Magazine other_magazine;
301 if ( Class.CastTo(other_magazine, other_item) )
302 {
303 //int other_item_quantity = other_magazine.GetAmmoCount();
304 int this_free_space = GetAmmoMax() - GetAmmoCount();
305 int numberOfTransferredBullets = 0;
306 int currentAmount = GetAmmoCount();
307
308 for (int i = 0; i < this_free_space && other_magazine.GetAmmoCount() > 0 ; i++)
309 {
310 float damage;
311 string cartrige_name;
312 other_magazine.ServerAcquireCartridge(damage, cartrige_name);
313 if (ServerStoreCartridge(damage, cartrige_name))
314 ++numberOfTransferredBullets;
315 }
316
317 if (GetGame().IsServer())
318 {
319 float resultingHealth = (currentAmount * GetHealth() + numberOfTransferredBullets * other_magazine.GetHealth()) / GetAmmoCount();
320 SetHealth("", "", resultingHealth);
321 }
322 OnCombine(other_item);
323 other_magazine.SetSynchDirty();
324 SetSynchDirty();
325 }
326 }
327
328 override bool CanDetachAttachment(EntityAI parent)
329 {
330 PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
331 if (player)
332 {
333 Weapon_Base wpn = Weapon_Base.Cast(parent);
334 if (wpn)
335 {
336 return player.GetWeaponManager().CanDetachMagazine(wpn,this);
337 }
338 }
339 return super.CanDetachAttachment(parent);
340 }
341
342 override void OnInventoryEnter(Man player)
343 {
344 super.OnInventoryEnter(player);
345
346 PlayerBase p = PlayerBase.Cast(player);
347 p.GetWeaponManager().OnMagazineInventoryEnter(this);
348 }
349
350 override void OnInventoryExit(Man player)
351 {
352 super.OnInventoryExit(player);
353
354 PlayerBase p = PlayerBase.Cast(player);
355 p.GetWeaponManager().OnMagazineInventoryExit(this);
356 }
357
358 override void OnWasAttached( EntityAI parent, int slot_id )
359 {
360 super.OnWasAttached(parent, slot_id);
361
362 PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
363 Weapon_Base wpn = Weapon_Base.Cast(parent);
364 if (wpn && player)
365 {
366 player.GetWeaponManager().OnMagazineAttach(this);
367 }
368 }
369
370 override void OnWasDetached( EntityAI parent, int slot_id )
371 {
372 super.OnWasDetached(parent, slot_id);
373
374 PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
375 Weapon_Base wpn = Weapon_Base.Cast(parent);
376
377 if (wpn && player)
378 {
379 player.GetWeaponManager().OnMagazineDetach(this);
380 }
381 }
382
383 override void EEHealthLevelChanged( int oldLevel, int newLevel, string zone )
384 {
385 super.EEHealthLevelChanged(oldLevel, newLevel, zone);
386 float damage = 1 - GetHealthLevelValue(newLevel) + 0.001;
387
388 int cartridgeCount = GetAmmoCount();
389 for (int i = 0; i < cartridgeCount; ++i)
390 SetCartridgeDamageAtIndex(i, damage);
391 }
392
393 override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
394 {
395 if (GetAmmoCount() > 0)
396 {
397 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "", FadeColors.LIGHT_GREY));
398 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.PRINT_BULLETS, "Print Bullets", FadeColors.LIGHT_GREY));
399 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "___________________________", FadeColors.RED));
400 }
401
402 super.GetDebugActions(outputList);
403 }
404
405 override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
406 {
407 if (GetGame().IsServer())
408 {
409 if (action_id == EActions.PRINT_BULLETS)
410 {
411 Magazine magazine;
412 Class.CastTo(magazine, this);
413 for (int i = 0; i < magazine.GetAmmoCount(); i++)
414 {
415 float damage;
416 string className;
417 magazine.GetCartridgeAtIndex(i, damage, className);
418 Debug.Log(string.Format("Bullet: %1, Damage %2", className, damage));
419 }
420 }
421 }
422
423 return super.OnAction(action_id, player, ctx);
424 }
425
426 override bool CanBeFSwaped()
427 {
428 Weapon_Base wpn = Weapon_Base.Cast(GetHierarchyParent());
429 if (wpn)
430 {
431 return false;
432 }
433
434 return true;
435 }
436}
437
438class MagazineStorage : Magazine
439{
440 override void SetActions()
441 {
442 super.SetActions();
446 }
447}
Param4< int, int, string, int > TSelectableActionInfoWithColor
Definition entityai.c:97
eBleedingSourceType GetType()
void AddAction(typename actionName)
void SetActions()
bool m_IsValid
const int ECE_IN_INVENTORY
const int RF_DEFAULT
Super root of all classes in Enforce script.
Definition enscript.c:11
Definition debug.c:2
script counterpart to engine's class Inventory
Definition inventory.c:79
static proto native EntityAI LocationCreateEntity(notnull InventoryLocation inv_loc, string type, int iSetupFlags, int iRotation)
creates new item directly at location
InventoryLocation.
Definition enmath.c:7
Serialization general interface. Serializer API works with:
Definition serializer.c:56
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
Definition dayzanimal.c:136
override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
Definition dayzanimal.c:125
EActions
Definition eactions.c:2
proto native CGame GetGame()
const int SAT_DEBUG_ACTION
Definition constants.c:454
void SplitItem(PlayerBase player)
Definition itembase.c:6831
void SplitItemToInventoryLocation(notnull InventoryLocation dst)
Definition itembase.c:6798
float GetWeightSpecialized(bool forceRecalc=false)
Definition itembase.c:8321
void CombineItems(ItemBase other_item, bool use_stack_max=true)
Definition itembase.c:7122
void OnInventoryEnter(Man player)
Event called on item when it is placed in the player(Man) inventory, passes the owner as a parameter.
Definition itembase.c:8708
void OnCombine(ItemBase other_item)
Definition itembase.c:7148
int m_Count
Definition itembase.c:4874
override bool CanBeCombined(EntityAI other_item, bool reservation_check=true, bool stack_max_limit=false)
Definition itembase.c:7016
bool IsCombineAll(ItemBase other_item, bool use_stack_max=false)
Definition itembase.c:7078
void OnInventoryExit(Man player)
Event called on item when it is removed from the player(Man) inventory, passes the old owner as a par...
Definition itembase.c:8721
bool IsFullQuantity()
Definition itembase.c:8301
bool m_CanThisBeSplit
Definition itembase.c:4909
override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
Definition itembase.c:6900
Magazine Magazine_Base
Definition magazine.c:1
CartridgeType
Definition magazine.c:4
@ Pistol
Definition magazine.c:6
@ Shell
Definition magazine.c:9
@ Arrow
Definition magazine.c:10
@ FullPower
Definition magazine.c:8
@ Intermediate
Definition magazine.c:7
enum CartridgeType Tracer
enum CartridgeType None
Definition magazine.c:5
override void OnWasDetached(EntityAI parent, int slot_id)