DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
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
11
12enum ProjectileType
13{
14 None = 0,
15 Tracer = 1,
16 AP = 2
17}
18
19
20class AmmoData
21{
22 bool m_IsValid;
23 CartridgeType m_CartridgeType;
24 ProjectileType m_ProjectileType;
25
26 void AmmoData( string init_type )
27 {
28 m_IsValid = GetGame().ConfigIsExisting( "CfgMagazines " + init_type );
29 if ( m_IsValid )
30 {
31 m_CartridgeType = GetGame().ConfigGetInt( "CfgMagazines " + init_type + " iconCartridge" );
32 m_ProjectileType = GetGame().ConfigGetInt( "CfgMagazines " + init_type + " iconType" );
33 }
34 }
35}
36
37class Magazine : InventoryItemSuper
38{
39 protected static ref map<string, ref AmmoData> m_AmmoData;
40 ref array<string> m_CompatiableAmmo;
41 ref array<float> m_ChanceToJam;
42 protected float m_ManipulationDamage;
43
44 void Magazine ()
45 {
46 m_ChanceToJam = new array<float>;
47 InitReliability(m_ChanceToJam);
48 m_ManipulationDamage = ConfigGetFloat("manipulationDamage");
49 m_CompatiableAmmo = new array<string>;
50 ConfigGetTextArray("ammoItems", m_CompatiableAmmo);
51 if ( !GetGame().IsDedicatedServer() )
52 {
53 if ( !m_AmmoData )
54 m_AmmoData = new map<string, ref AmmoData>;
55
56 string classname = ClassName();
57 if ( !m_AmmoData.Contains(classname) )
58 {
59 ref AmmoData new_data = new AmmoData( classname );
60 if ( new_data.m_IsValid )
61 m_AmmoData.Insert( classname, new AmmoData( classname ) );
62 }
63 }
64 }
65
67 proto native int GetAmmoCount();
69 proto native void ServerSetAmmoCount(int ammoCount);
70 proto native void LocalSetAmmoCount(int ammoCount);
71
78 proto bool LocalAcquireCartridge(out float dmg, out string cartTypeName);
79 proto bool ServerAcquireCartridge(out float dmg, out string cartTypeName);
86 proto native bool LocalStoreCartridge(float ammoDamage, string cartTypeName);
87 proto native bool ServerStoreCartridge(float ammoDamage, string cartTypeName);
88
96 proto bool GetCartridgeAtIndex(int cartIndex, out float dmg, out string cartTypeName);
97
105 proto bool SetCartridgeAtIndex(int cartIndex, out float dmg, out string cartTypeName);
106
113 proto bool SetCartridgeDamageAtIndex(int cartIndex, float dmg);
114
115
116 static AmmoData GetAmmoData( string classname )
117 {
118 if ( !m_AmmoData )
119 m_AmmoData = new map<string, ref AmmoData>;
120 if ( !m_AmmoData.Contains(classname) )
121 {
122 ref AmmoData new_data = new AmmoData( classname );
123 if ( new_data.m_IsValid )
124 m_AmmoData.Insert( classname, new AmmoData( classname ) );
125 return new_data;
126 }
127 else
128 {
129 return m_AmmoData.Get( classname );
130 }
131 }
132
133 bool IsCompatiableAmmo( ItemBase ammo )
134 {
135 if ( m_CompatiableAmmo && ammo )
136 return ( m_CompatiableAmmo.Find( ammo.GetType() ) > -1 );
137 else
138 return false;
139 }
140
141 bool CanAddCartridges(int count)
142 {
143 int spc_avail = GetAmmoMax() - GetAmmoCount();
144 return count <= spc_avail;
145 }
146
148 void ServerAddAmmoCount(int ammoCount)
149 {
150 ServerSetAmmoCount(GetAmmoCount() + ammoCount);
151 }
152 void LocalAddAmmoCount(int ammoCount)
153 {
154 LocalSetAmmoCount(GetAmmoCount() + ammoCount);
155 }
157 int GetAmmoMax()
158 {
159 return m_Count;
160 }
162 void ServerSetAmmoMax()
163 {
164 ServerSetAmmoCount( GetAmmoMax() );
165 }
166 void LocalSetAmmoMax()
167 {
168 LocalSetAmmoCount( GetAmmoMax() );
169 }
171 override bool IsMagazine()
172 {
173 return true;
174 }
175
176
177 override bool CanBeSplit()
178 {
179 if ( m_CanThisBeSplit )
180 return ( GetAmmoCount() > 1 );
181
182 return false;
183 }
184
185 bool InitReliability(out array<float> reliability_array)
186 {
187 if (GetGame().ConfigIsExisting("cfgMagazines " + GetType() + " Reliability ChanceToJam"))
188 {
189 GetGame().ConfigGetFloatArray("cfgMagazines " + GetType() + " Reliability ChanceToJam",reliability_array);
190 return true;
191 }
192 return false;
193 }
194
195 float GetChanceToJam()
196 {
197 int level = GetHealthLevel();
198
199 if (level >= 0 && level < m_ChanceToJam.Count())
200 return m_ChanceToJam[level];
201 else
202 return 0.0;
203 }
204 /*
205 override void SplitItem()
206 {
207 if ( !CanBeSplit() )
208 {
209 return;
210 }
211
212 float quantity = this.GetAmmoCount();
213 float split_quantity_new = Math.Floor( quantity / 2 );
214
215 this.AddAmmoCount(-split_quantity_new);
216
217 PlayerBase player = this.GetHierarchyRootPlayer();
218 Magazine new_item;
219 if( player )
220 {
221 new_item = player.CopyInventoryItem( this );
222 }
223 else
224 {
225 new_item = GetGame().CreateObject(this.GetType(), this.GetPosition() );
226 }
227
228 new_item.SetAmmoCount( split_quantity_new );
229 }
230 */
231
232 override void SplitItemToInventoryLocation( notnull InventoryLocation dst )
233 {
234 if ( !CanBeSplit() )
235 return;
236
237 Magazine new_pile = Magazine.Cast( GameInventory.LocationCreateEntity( dst, GetType(), ECE_IN_INVENTORY, RF_DEFAULT ) );
238 if( new_pile )
239 {
240 MiscGameplayFunctions.TransferItemProperties(dst.GetItem(), new_pile);
241
242 new_pile.ServerSetAmmoCount(0);
243 int quantity = GetAmmoCount();
244
245 for (int i = 0; i < Math.Floor( quantity * 0.5 ); ++i)
246 {
247 float damage;
248 string cartrige_name;
249 ServerAcquireCartridge(damage, cartrige_name);
250 new_pile.ServerStoreCartridge(damage, cartrige_name);
251 }
252 new_pile.SetSynchDirty();
253 SetSynchDirty();
254 }
255 }
256
257 override void SplitItem(PlayerBase player)
258 {
259 if ( !CanBeSplit() )
260 return;
261
262
263 Magazine new_pile = Magazine.Cast( player.CreateCopyOfItemInInventoryOrGround( this ) );
264 if( new_pile )
265 {
266 new_pile.ServerSetAmmoCount(0);
267 int quantity = this.GetAmmoCount();
268
269 for (int i = 0; i < Math.Floor( quantity / 2 ); i++)
270 {
271 float damage;
272 string cartrige_name;
273 ServerAcquireCartridge(damage, cartrige_name);
274 new_pile.ServerStoreCartridge(damage, cartrige_name);
275 }
276 new_pile.SetSynchDirty();
277 SetSynchDirty();
278 }
279 }
280
281 void ApplyManipulationDamage()
282 {
283 AddHealth("","Health",-m_ManipulationDamage);
284 }
285
286 override bool IsFullQuantity()
287 {
288 if ( GetAmmoCount() == GetAmmoMax() )
289 {
290 return true;
291 }
292 else
293 {
294 return false;
295 }
296 }
297
298 override protected float GetWeightSpecialized(bool forceRecalc = false)
299 {
300 #ifdef DEVELOPER
301 if (WeightDebug.m_VerbosityFlags & WeightDebugType.RECALC_FORCED)
302 {
303 WeightDebugData data = WeightDebug.GetWeightDebug(this);
304 data.SetCalcDetails("TMAG: ("+GetAmmoCount()+"(Ammo count) * " + ConfigGetFloat("weightPerQuantityUnit")+"(weightPerQuantityUnit)) + " + GetConfigWeightModifiedDebugText());
305 }
306 #endif
307 return GetConfigWeightModified() + (GetAmmoCount() * ConfigGetFloat("weightPerQuantityUnit"));
308 }
309 /*
310 override void CombineItems( ItemBase other_item )
311 {
312 if( !CanBeCombined(other_item) ) return;
313 if( other_item.GetType() != this.GetType() ) return;
314
315 Magazine other_magazine = other_item;
316 float other_item_quantity = other_magazine.GetAmmoCount();
317 float this_free_space = this.GetAmmoMax() - this.GetAmmoCount();
318 float quantity_used = 0;
319
320 if( other_item_quantity > this_free_space )
321 {
322 quantity_used = this_free_space;
323 }
324 else
325 {
326 quantity_used = other_item_quantity;
327 }
328 if( quantity_used!= 0 )
329 {
330 this.AddAmmoCount(quantity_used);
331 other_magazine.AddAmmoCount(-quantity_used);
332 if(other_magazine.GetAmmoCount() <= 0) other_magazine.Delete();
333 }
334 }
335 */
336
337 override bool IsCombineAll( ItemBase other_item, bool use_stack_max = false)
338 {
339 Magazine other_magazine = Magazine.Cast(other_item);
340 int free_space = GetAmmoMax() - GetAmmoCount();
341
342 return free_space >= other_magazine.GetAmmoCount();
343 }
344
345 override void CombineItems( ItemBase other_item, bool use_stack_max = false )
346 {
347 if ( !CanBeCombined(other_item) )
348 return;
349
350 if ( other_item.GetType() != GetType() )
351 return;
352
353 Magazine other_magazine;
354 if ( Class.CastTo(other_magazine, other_item) )
355 {
356 //int other_item_quantity = other_magazine.GetAmmoCount();
357 int this_free_space = GetAmmoMax() - GetAmmoCount();
358 int numberOfTransferredBullets = 0;
359 int currentAmount = GetAmmoCount();
360
361 for (int i = 0; i < this_free_space && other_magazine.GetAmmoCount() > 0 ; i++)
362 {
363 float damage;
364 string cartrige_name;
365 other_magazine.ServerAcquireCartridge(damage, cartrige_name);
366 if (ServerStoreCartridge(damage, cartrige_name))
367 ++numberOfTransferredBullets;
368 }
369
370 if (GetGame().IsServer())
371 {
372 float resultingHealth = (currentAmount * GetHealth() + numberOfTransferredBullets * other_magazine.GetHealth()) / GetAmmoCount();
373 SetHealth("", "", resultingHealth);
374 }
375 OnCombine(other_item);
376 other_magazine.SetSynchDirty();
377 SetSynchDirty();
378 }
379 }
380
381 override bool CanDetachAttachment(EntityAI parent)
382 {
383 PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
384 if (player)
385 {
386 Weapon_Base wpn = Weapon_Base.Cast(parent);
387 if (wpn)
388 {
389 return player.GetWeaponManager().CanDetachMagazine(wpn,this);
390 }
391 }
392 return super.CanDetachAttachment(parent);
393 }
394
395 override void OnInventoryEnter(Man player)
396 {
397 super.OnInventoryEnter(player);
398
399 PlayerBase p = PlayerBase.Cast(player);
400 p.GetWeaponManager().OnMagazineInventoryEnter(this);
401 }
402
403 override void OnInventoryExit(Man player)
404 {
405 super.OnInventoryExit(player);
406
407 PlayerBase p = PlayerBase.Cast(player);
408 p.GetWeaponManager().OnMagazineInventoryExit(this);
409 }
410
411 override void OnWasAttached( EntityAI parent, int slot_id )
412 {
413 super.OnWasAttached(parent, slot_id);
414
415 PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
416 Weapon_Base wpn = Weapon_Base.Cast(parent);
417 if (wpn && player)
418 {
419 player.GetWeaponManager().OnMagazineAttach(this);
420 }
421 /*PlayerBase player = PlayerBase.Cast(parent);
422 if (player)
423 //player.SwitchItemTypeAttach(this, slot_id);
424 //{
425 // player.UpdateQuickBarEntityVisibility(this);
426 //}*/
427 //Print("OnWasAttached: " + GetType());
428 }
429
430 override void OnWasDetached( EntityAI parent, int slot_id )
431 {
432 super.OnWasDetached(parent, slot_id);
433
434 PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
435 Weapon_Base wpn = Weapon_Base.Cast(parent);
436
437 if (wpn && player)
438 {
439 player.GetWeaponManager().OnMagazineDetach(this);
440 }
441 }
442
443 override void EEHealthLevelChanged( int oldLevel, int newLevel, string zone )
444 {
445 super.EEHealthLevelChanged(oldLevel, newLevel, zone);
446 float damage = 1 - GetHealthLevelValue(newLevel) + 0.001;
447
448 int cartridgeCount = GetAmmoCount();
449 for (int i = 0; i < cartridgeCount; ++i)
450 SetCartridgeDamageAtIndex(i, damage);
451 }
452
453 override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
454 {
455 super.GetDebugActions(outputList);
456
457 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "", FadeColors.LIGHT_GREY));
458 outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.PRINT_BULLETS, "Print Bullets", FadeColors.LIGHT_GREY));
459 }
460
461 override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
462 {
463 if (GetGame().IsServer())
464 {
465 if (action_id == EActions.PRINT_BULLETS)
466 {
467 Magazine magazine;
468 Class.CastTo(magazine, this);
469 for (int i = 0; i < magazine.GetAmmoCount(); i++)
470 {
471 float damage;
472 string className;
473 magazine.GetCartridgeAtIndex(i, damage, className);
474 Debug.Log(string.Format("Bullet: %1, Damage %2", className, damage));
475 }
476 }
477 }
478
479 return super.OnAction(action_id, player, ctx);
480 }
481}
482
483class MagazineStorage : Magazine
484{
485 override void SetActions()
486 {
487 super.SetActions();
491 }
492}
eBleedingSourceType GetType()
Param4< int, int, string, int > TSelectableActionInfoWithColor
void AddAction(typename actionName)
void SetActions()
const int ECE_IN_INVENTORY
const int RF_DEFAULT
EActions
Definition EActions.c:2
void OnCombine(ItemBase other_item)
Definition ItemBase.c:6675
bool IsFullQuantity()
Definition ItemBase.c:7972
override bool CanBeSplit()
Definition ItemBase.c:6092
bool OnAction(int action_id, Man player, ParamsReadContext ctx)
Definition ItemBase.c:6773
override protected float GetWeightSpecialized(bool forceRecalc=false)
Definition ItemBase.c:7992
void SplitItemToInventoryLocation(notnull InventoryLocation dst)
Definition ItemBase.c:6355
bool IsCombineAll(ItemBase other_item, bool use_stack_max=false)
Definition ItemBase.c:6610
override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
Definition ItemBase.c:6447
int m_Count
Definition ItemBase.c:4656
void CombineItems(ItemBase other_item, bool use_stack_max=true)
Definition ItemBase.c:6649
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:8390
override bool CanBeCombined(EntityAI other_item, bool reservation_check=true, bool stack_max_limit=false)
Definition ItemBase.c:6547
override void OnWasAttached(EntityAI parent, int slot_id)
Definition ItemBase.c:5895
void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
Definition ItemBase.c:6704
bool m_CanThisBeSplit
Definition ItemBase.c:4697
void SplitItem(PlayerBase player)
Definition ItemBase.c:6388
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:8403
Magazine Magazine_Base
Definition Magazine.c:1
CartridgeType
Definition Magazine.c:4
@ Pistol
Definition Magazine.c:6
@ Shell
Definition Magazine.c:9
@ 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)
proto native void ConfigGetFloatArray(string path, out TFloatArray values)
Get array of floats from config on path.
proto native int ConfigGetInt(string path)
Get int value from config on path.
proto native bool ConfigIsExisting(string path)
Super root of all classes in Enforce script.
Definition EnScript.c:11
Definition Debug.c:14
static void Log(string message=LOG_DEFAULT, string plugin=LOG_DEFAULT, string author=LOG_DEFAULT, string label=LOG_DEFAULT, string entity=LOG_DEFAULT)
Prints debug message with normal prio.
Definition Debug.c:133
script counterpart to engine's class Inventory
Definition Inventory.c:77
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
void SetCalcDetails(string details)
Definition Debug.c:728
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
proto native CGame GetGame()
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
static proto float Floor(float f)
Returns floor of value.
const int SAT_DEBUG_ACTION
Definition constants.c:408