Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
magazine.c
Go to the documentation of this file.
1 typedef Magazine Magazine_Base;
2 
4 {
5  None = 0,
6  Pistol = 1,
8  FullPower = 3,
9  Shell = 4
10 }
11 
12 enum ProjectileType
13 {
14  None = 0,
15  Tracer = 1,
16  AP = 2
17 }
18 
19 
20 class 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 
37 class 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 SplitItemToInventoryLocation( notnull InventoryLocation dst )
206  {
207  if ( !CanBeSplit() )
208  return;
209 
210  Magazine new_pile = Magazine.Cast( GameInventory.LocationCreateEntity( dst, GetType(), ECE_IN_INVENTORY, RF_DEFAULT ) );
211  if( new_pile )
212  {
213  MiscGameplayFunctions.TransferItemProperties(dst.GetItem(), new_pile);
214 
215  new_pile.ServerSetAmmoCount(0);
216  int quantity = GetAmmoCount();
217 
218  for (int i = 0; i < Math.Floor( quantity * 0.5 ); ++i)
219  {
220  float damage;
221  string cartrige_name;
222  ServerAcquireCartridge(damage, cartrige_name);
223  new_pile.ServerStoreCartridge(damage, cartrige_name);
224  }
225  new_pile.SetSynchDirty();
226  SetSynchDirty();
227  }
228  }
229 
230  override void SplitItem(PlayerBase player)
231  {
232  if ( !CanBeSplit() )
233  return;
234 
235 
236  Magazine new_pile = Magazine.Cast( player.CreateCopyOfItemInInventoryOrGround( this ) );
237  if( new_pile )
238  {
239  new_pile.ServerSetAmmoCount(0);
240  int quantity = this.GetAmmoCount();
241 
242  for (int i = 0; i < Math.Floor( quantity / 2 ); i++)
243  {
244  float damage;
245  string cartrige_name;
246  ServerAcquireCartridge(damage, cartrige_name);
247  new_pile.ServerStoreCartridge(damage, cartrige_name);
248  }
249  new_pile.SetSynchDirty();
250  SetSynchDirty();
251  }
252  }
253 
254  void ApplyManipulationDamage()
255  {
256  AddHealth("","Health",-m_ManipulationDamage);
257  }
258 
259  override bool IsFullQuantity()
260  {
261  if ( GetAmmoCount() == GetAmmoMax() )
262  {
263  return true;
264  }
265  else
266  {
267  return false;
268  }
269  }
270 
271  override protected float GetWeightSpecialized(bool forceRecalc = false)
272  {
273  #ifdef DEVELOPER
274  if (WeightDebug.m_VerbosityFlags & WeightDebugType.RECALC_FORCED)
275  {
276  WeightDebugData data = WeightDebug.GetWeightDebug(this);
277  data.SetCalcDetails("TMAG: ("+GetAmmoCount()+"(Ammo count) * " + ConfigGetFloat("weightPerQuantityUnit")+"(weightPerQuantityUnit)) + " + GetConfigWeightModifiedDebugText());
278  }
279  #endif
280  return GetConfigWeightModified() + (GetAmmoCount() * ConfigGetFloat("weightPerQuantityUnit"));
281  }
282 
283  override bool IsCombineAll( ItemBase other_item, bool use_stack_max = false)
284  {
285  Magazine other_magazine = Magazine.Cast(other_item);
286  int free_space = GetAmmoMax() - GetAmmoCount();
287 
288  return free_space >= other_magazine.GetAmmoCount();
289  }
290 
291  override void CombineItems( ItemBase other_item, bool use_stack_max = false )
292  {
293  if ( !CanBeCombined(other_item) )
294  return;
295 
296  if ( other_item.GetType() != GetType() )
297  return;
298 
299  Magazine other_magazine;
300  if ( Class.CastTo(other_magazine, other_item) )
301  {
302  //int other_item_quantity = other_magazine.GetAmmoCount();
303  int this_free_space = GetAmmoMax() - GetAmmoCount();
304  int numberOfTransferredBullets = 0;
305  int currentAmount = GetAmmoCount();
306 
307  for (int i = 0; i < this_free_space && other_magazine.GetAmmoCount() > 0 ; i++)
308  {
309  float damage;
310  string cartrige_name;
311  other_magazine.ServerAcquireCartridge(damage, cartrige_name);
312  if (ServerStoreCartridge(damage, cartrige_name))
313  ++numberOfTransferredBullets;
314  }
315 
316  if (GetGame().IsServer())
317  {
318  float resultingHealth = (currentAmount * GetHealth() + numberOfTransferredBullets * other_magazine.GetHealth()) / GetAmmoCount();
319  SetHealth("", "", resultingHealth);
320  }
321  OnCombine(other_item);
322  other_magazine.SetSynchDirty();
323  SetSynchDirty();
324  }
325  }
326 
327  override bool CanDetachAttachment(EntityAI parent)
328  {
329  PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
330  if (player)
331  {
332  Weapon_Base wpn = Weapon_Base.Cast(parent);
333  if (wpn)
334  {
335  return player.GetWeaponManager().CanDetachMagazine(wpn,this);
336  }
337  }
338  return super.CanDetachAttachment(parent);
339  }
340 
341  override void OnInventoryEnter(Man player)
342  {
343  super.OnInventoryEnter(player);
344 
345  PlayerBase p = PlayerBase.Cast(player);
346  p.GetWeaponManager().OnMagazineInventoryEnter(this);
347  }
348 
349  override void OnInventoryExit(Man player)
350  {
351  super.OnInventoryExit(player);
352 
353  PlayerBase p = PlayerBase.Cast(player);
354  p.GetWeaponManager().OnMagazineInventoryExit(this);
355  }
356 
357  override void OnWasAttached( EntityAI parent, int slot_id )
358  {
359  super.OnWasAttached(parent, slot_id);
360 
361  PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
362  Weapon_Base wpn = Weapon_Base.Cast(parent);
363  if (wpn && player)
364  {
365  player.GetWeaponManager().OnMagazineAttach(this);
366  }
367  }
368 
369  override void OnWasDetached( EntityAI parent, int slot_id )
370  {
371  super.OnWasDetached(parent, slot_id);
372 
373  PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
374  Weapon_Base wpn = Weapon_Base.Cast(parent);
375 
376  if (wpn && player)
377  {
378  player.GetWeaponManager().OnMagazineDetach(this);
379  }
380  }
381 
382  override void EEHealthLevelChanged( int oldLevel, int newLevel, string zone )
383  {
384  super.EEHealthLevelChanged(oldLevel, newLevel, zone);
385  float damage = 1 - GetHealthLevelValue(newLevel) + 0.001;
386 
387  int cartridgeCount = GetAmmoCount();
388  for (int i = 0; i < cartridgeCount; ++i)
389  SetCartridgeDamageAtIndex(i, damage);
390  }
391 
392  override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
393  {
394  super.GetDebugActions(outputList);
395 
396  if (GetAmmoCount() > 0)
397  {
398  outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "", FadeColors.LIGHT_GREY));
399  outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.PRINT_BULLETS, "Print Bullets", FadeColors.LIGHT_GREY));
400  }
401  }
402 
403  override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
404  {
405  if (GetGame().IsServer())
406  {
407  if (action_id == EActions.PRINT_BULLETS)
408  {
409  Magazine magazine;
410  Class.CastTo(magazine, this);
411  for (int i = 0; i < magazine.GetAmmoCount(); i++)
412  {
413  float damage;
414  string className;
415  magazine.GetCartridgeAtIndex(i, damage, className);
416  Debug.Log(string.Format("Bullet: %1, Damage %2", className, damage));
417  }
418  }
419  }
420 
421  return super.OnAction(action_id, player, ctx);
422  }
423 
424  override bool CanBeFSwaped()
425  {
426  Weapon_Base wpn = Weapon_Base.Cast(GetHierarchyParent());
427  if (wpn)
428  {
429  return false;
430  }
431 
432  return true;
433  }
434 }
435 
436 class MagazineStorage : Magazine
437 {
438  override void SetActions()
439  {
440  super.SetActions();
444  }
445 }
ItemBase
Definition: inventoryitem.c:730
ActionLoadMagazineQuick
Definition: actionloadmagazine.c:90
RF_DEFAULT
const int RF_DEFAULT
Definition: centraleconomy.c:65
GetGame
proto native CGame GetGame()
OnWasAttached
override void OnWasAttached(EntityAI parent, int slot_id)
Definition: torch.c:945
GetWeightSpecialized
override protected float GetWeightSpecialized(bool forceRecalc=false)
Definition: itembase.c:3341
SplitItemToInventoryLocation
void SplitItemToInventoryLocation(notnull InventoryLocation dst)
Definition: itembase.c:1764
WeightDebugData
Definition: debug.c:939
Tracer
enum CartridgeType Tracer
CanBeSplit
override bool CanBeSplit()
Definition: itembase.c:1501
InventoryLocation
InventoryLocation.
Definition: inventorylocation.c:27
Intermediate
@ Intermediate
Definition: magazine.c:7
OnInventoryEnter
override protected void OnInventoryEnter(Man player)
Definition: fireworksbase.c:71
IsCombineAll
bool IsCombineAll(ItemBase other_item, bool use_stack_max=false)
Definition: itembase.c:2019
OnInventoryExit
override protected void OnInventoryExit(Man player)
Definition: fireworksbase.c:79
FullPower
@ FullPower
Definition: magazine.c:8
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
SplitItem
void SplitItem(PlayerBase player)
Definition: itembase.c:1797
IsFullQuantity
bool IsFullQuantity()
Definition: itembase.c:3321
PlayerBase
Definition: playerbaseclient.c:1
CartridgeType
CartridgeType
Definition: magazine.c:3
map
map
Definition: controlsxboxnew.c:3
m_Count
int m_Count
Definition: itembase.c:21
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
m_CanThisBeSplit
bool m_CanThisBeSplit
Definition: itembase.c:63
SetActions
void SetActions()
Definition: advancedcommunication.c:79
GetDebugActions
override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
Definition: edible_base.c:744
OnWasDetached
override void OnWasDetached(EntityAI parent, int slot_id)
Definition: remotedetonator.c:237
Shell
@ Shell
Definition: magazine.c:9
OnCombine
void OnCombine(ItemBase other_item)
Definition: itembase.c:2084
ActionEmptyMagazine
Definition: actionemptymagazine.c:14
EActions
EActions
Definition: eactions.c:1
OnAction
override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
Definition: edible_base.c:755
array< string >
InventoryItemSuper
Definition: inventoryitem.c:3
ActionLoadMagazine
Definition: actionloadmagazine.c:9
CanBeCombined
override bool CanBeCombined(EntityAI other_item, bool reservation_check=true, bool stack_max_limit=false)
Definition: itembase.c:1956
EEHealthLevelChanged
override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
Definition: basebuildingbase.c:463
Pistol
@ Pistol
Definition: magazine.c:6
Debug
Definition: debug.c:13
Weapon_Base
shorthand
Definition: boltactionrifle_base.c:5
AP
Mag_FNX45_15Rnd AP
None
enum CartridgeType None
Definition: magazine.c:5
SAT_DEBUG_ACTION
const int SAT_DEBUG_ACTION
Definition: constants.c:424
Math
Definition: enmath.c:6
Magazine_Base
Magazine Magazine_Base
Definition: magazine.c:1
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
TSelectableActionInfoWithColor
Param4< int, int, string, int > TSelectableActionInfoWithColor
Definition: entityai.c:97
EntityAI
Definition: building.c:5
GetType
override int GetType()
Definition: huddebugwincharagents.c:49
GameInventory
script counterpart to engine's class Inventory
Definition: inventory.c:78
ECE_IN_INVENTORY
const int ECE_IN_INVENTORY
Definition: centraleconomy.c:36
CombineItems
void CombineItems(ItemBase other_item, bool use_stack_max=true)
Definition: itembase.c:2058