12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310 |
- class WeaponManager
- {
- const float MAX_DROP_MAGAZINE_DISTANCE_SQ = 4;
-
- protected PlayerBase m_player;
-
- protected int m_LastAcknowledgmentID;
-
- protected int m_PendingWeaponActionAcknowledgmentID;
- protected Magazine m_PendingTargetMagazine;
- protected ref InventoryLocation m_TargetInventoryLocation;
- protected int m_PendingWeaponAction;
- protected ref InventoryLocation m_PendingInventoryLocation;
-
- protected bool m_canEnd;
- protected bool m_justStart;
- protected bool m_InProgress;
- protected bool m_IsEventSended;
- protected bool m_WantContinue;
- protected bool m_InIronSight;
- protected bool m_InOptic;
- protected bool m_readyToStart;
- protected Weapon_Base m_WeaponInHand;
- protected MagazineStorage m_MagazineInHand;
- protected ActionBase m_ControlAction;
- protected int m_ForceEjectBulletTimestamp;
-
- protected const int FORCE_EJECT_BULLET_TIMEOUT = 2000;
- #ifdef DIAG_DEVELOPER
- protected int m_BurstOption;
- #endif
- //Reload
- protected ref array<Magazine> m_MagazinePilesInInventory;
- protected ref array<MagazineStorage> m_MagazineStorageInInventory;
- protected ref array<Magazine> m_SuitableMagazines;
- protected Magazine m_PreparedMagazine;
-
- //Jamming
- protected float m_NewJamChance;
- protected bool m_WaitToSyncJamChance;
-
-
- protected int m_AnimationRefreshCooldown;
-
- void WeaponManager(PlayerBase player)
- {
- m_ForceEjectBulletTimestamp = -1;
- m_player = player;
- m_PendingWeaponActionAcknowledgmentID = -1;
- m_PendingTargetMagazine = NULL;
- m_PendingInventoryLocation = NULL;
- m_WeaponInHand = NULL;
- m_MagazineInHand = NULL;
- m_ControlAction = NULL;
- m_PendingWeaponAction = -1;
- m_LastAcknowledgmentID = 1;
- m_InProgress = false;
- m_WantContinue = true;
- m_IsEventSended = false;
- m_canEnd = false;
- m_readyToStart = false;
- m_AnimationRefreshCooldown = 0;
-
- m_NewJamChance = -1;
- m_WaitToSyncJamChance = false;
-
- m_MagazinePilesInInventory = new array<Magazine>;
- m_MagazineStorageInInventory = new array<MagazineStorage>;
- m_SuitableMagazines = new array<Magazine>;
- m_PreparedMagazine = null;
-
- #ifdef DEVELOPER
- m_BurstOption = 0;
- #endif
- }
- //----------------------------------------------------------------------------
- // Weapon Action conditions
- //----------------------------------------------------------------------------
- bool CanFire(Weapon_Base wpn)
- {
- if( m_player.GetHumanInventory().GetEntityInHands() != wpn )
- return false;
-
- if( m_player.IsLiftWeapon() || !m_player.IsRaised() || wpn.IsDamageDestroyed() || m_player.GetDayZPlayerInventory().IsProcessing() || !m_player.IsWeaponRaiseCompleted() || m_player.IsFighting() )
- return false;
-
- return !wpn.IsCoolDown();
- }
-
-
- bool CanAttachMagazine(Weapon_Base wpn, Magazine mag, bool reservationCheck = true )
- {
- if ( !wpn || !mag )
- return false;
-
- if ( m_player.GetHumanInventory().GetEntityInHands() != wpn )
- return false;
-
- if ( wpn.IsDamageDestroyed())
- return false;
-
- //if ( mag.GetHierarchyRootPlayer() && mag.GetHierarchyRootPlayer() != m_player )
- //return false;
-
- if( m_player.IsItemsToDelete())
- return false;
-
- if ( reservationCheck && (m_player.GetInventory().HasInventoryReservation(wpn, null) || m_player.GetInventory().HasInventoryReservation(mag, null)))
- return false;
-
- InventoryLocation invLoc;
- invLoc = new InventoryLocation();
-
- mag.GetInventory().GetCurrentInventoryLocation(invLoc);
- Weapon_Base wnp2;
-
- //magazine is already in weapon
- if ( Class.CastTo(wnp2, invLoc.GetParent()) )
- return false;
-
- int muzzleIndex = wpn.GetCurrentMuzzle();
-
- if (wpn.CanAttachMagazine(muzzleIndex, mag))
- return true;
- return false;
-
- }
- //---------------------------------------------------------------------------
-
- bool CanSwapMagazine(Weapon_Base wpn, Magazine mag, bool reservationCheck = true)
- {
- if ( !wpn || !mag )
- return false;
-
- if ( m_player.GetHumanInventory().GetEntityInHands() != wpn )
- return false;
-
- if ( mag.IsDamageDestroyed() || wpn.IsDamageDestroyed())
- return false;
-
- //if ( mag.GetHierarchyRootPlayer() && mag.GetHierarchyRootPlayer() != m_player )
- //return false;
- if( m_player.IsItemsToDelete())
- return false;
-
- if ( reservationCheck && (m_player.GetInventory().HasInventoryReservation(wpn, null) || m_player.GetInventory().HasInventoryReservation(mag, null)))
- return false;
-
- InventoryLocation invLoc;
- invLoc = new InventoryLocation();
-
- mag.GetInventory().GetCurrentInventoryLocation(invLoc);
- Weapon_Base wnp2;
-
- //second magazine is already in weapon
- if( Class.CastTo(wnp2, invLoc.GetParent()) )
- return false;
-
- int muzzleIndex = wpn.GetCurrentMuzzle();
-
- Magazine mag2;
- if( !Class.CastTo(mag2, wpn.GetMagazine(muzzleIndex)) )
- return false;
-
- if( GameInventory.CanSwapEntitiesEx( mag, mag2 ) )
- return true;
-
- InventoryLocation il = new InventoryLocation();
-
- if( GameInventory.CanForceSwapEntitiesEx( mag, null, mag2, il ) )
- return true;
-
- return false;
- }
- //----------------------------------------------------------------------------
- bool CanDetachMagazine(Weapon_Base wpn, Magazine mag, bool reservationCheck = true)
- {
- if ( !wpn || !mag )
- return false;
-
- if ( m_player.GetHumanInventory().GetEntityInHands() != wpn )
- return false;
-
- if ( mag.GetHierarchyParent() != wpn )
- return false;
-
- if( m_player.IsItemsToDelete())
- return false;
-
- if ( reservationCheck && (m_player.GetInventory().HasInventoryReservation(wpn, null) || m_player.GetInventory().HasInventoryReservation(mag, null)))
- return false;
-
- return true;
- }
- //---------------------------------------------------------------------------
- bool CanLoadBullet(Weapon_Base wpn, Magazine mag, bool reservationCheck = true)
- {
- if (!wpn || !mag)
- return false;
-
- if (m_player.GetHumanInventory().GetEntityInHands() != wpn)
- return false;
-
- if (mag.IsDamageDestroyed() || wpn.IsDamageDestroyed())
- return false;
-
- if (wpn.IsJammed())
- return false;
-
- if (m_player.IsItemsToDelete())
- return false;
-
- if (reservationCheck && (m_player.GetInventory().HasInventoryReservation(wpn, null) || m_player.GetInventory().HasInventoryReservation(mag, null)))
- return false;
-
- for (int i = 0; i < wpn.GetMuzzleCount(); i++)
- {
- if (wpn.CanChamberBullet(i, mag))
- return true;
- }
-
- return false;
- }
- //---------------------------------------------------------------------------
- bool CanUnjam(Weapon_Base wpn, bool reservationCheck = true)
- {
- if( !wpn )
- return false;
-
- if( m_player.GetHumanInventory().GetEntityInHands() != wpn)
- return false;
-
- if( wpn.IsDamageDestroyed())
- return false;
-
- if( m_player.IsItemsToDelete())
- return false;
-
- if ( reservationCheck && m_player.GetInventory().HasInventoryReservation(wpn, null))
- return false;
-
- if( !wpn.IsJammed(/*wpn.GetCurrentMuzzle()*/) )
- return false;
-
- return true;
- }
-
- bool CanEjectBullet(Weapon_Base wpn, bool reservationCheck = true)
- {
- if( !wpn )
- return false;
-
- if( m_player.GetHumanInventory().GetEntityInHands() != wpn)
- return false;
-
- if( m_player.IsItemsToDelete())
- return false;
-
- if( reservationCheck && m_player.GetInventory().HasInventoryReservation(wpn, null))
- return false;
-
- if( !wpn.CanEjectBullet() )
- return false;
-
- if( wpn.IsJammed(/*wpn.GetCurrentMuzzle()*/) )
- return false;
-
- return true;
- }
-
- void SetEjectBulletTryTimestamp()
- {
- m_ForceEjectBulletTimestamp = GetGame().GetTime();
- }
- //----------------------------------------------------------------------------
- bool InventoryReservation( Magazine mag, InventoryLocation invLoc)
- {
- Weapon_Base weapon;
- InventoryLocation ilWeapon = new InventoryLocation();
- if (Weapon_Base.CastTo(weapon, m_player.GetItemInHands()) )
- {
- weapon.GetInventory().GetCurrentInventoryLocation(ilWeapon);
- if ( m_player.GetInventory().HasInventoryReservation(weapon, ilWeapon) )
- {
- return false;
- }
- else
- {
- m_player.GetInventory().AddInventoryReservationEx(weapon,ilWeapon,GameInventory.c_InventoryReservationTimeoutMS);
- }
- }
-
- if( invLoc )
- {
- if ( m_player.GetInventory().HasInventoryReservation(invLoc.GetItem(),invLoc) )
- {
- m_player.GetInventory().ClearInventoryReservationEx(weapon, ilWeapon);
- return false;
- }
- else
- {
- m_player.GetInventory().AddInventoryReservationEx(invLoc.GetItem(),invLoc,GameInventory.c_InventoryReservationTimeoutMS);
- }
- }
-
- if( mag )
- {
- m_TargetInventoryLocation = new InventoryLocation();
- m_TargetInventoryLocation.SetAttachment( m_WeaponInHand, mag, InventorySlots.MAGAZINE);
-
- if ( m_player.GetInventory().HasInventoryReservation(mag, m_TargetInventoryLocation) )
- //if ( !m_player.GetInventory().AddInventoryReservationEx( mag, m_TargetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS) )
- {
- m_player.GetInventory().ClearInventoryReservationEx(weapon, ilWeapon);
- if (invLoc)
- {
- m_player.GetInventory().ClearInventoryReservationEx(invLoc.GetItem(), invLoc);
- }
- return false;
- }
- else
- {
- m_player.GetInventory().AddInventoryReservationEx(mag, m_TargetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
- }
- }
- m_PendingTargetMagazine = mag;
- m_PendingInventoryLocation = invLoc;
-
- return true;
- }
-
- //----------------------------------------------------------------------------
- // Weapon Actions
- //----------------------------------------------------------------------------
- bool AttachMagazine( Magazine mag , ActionBase control_action = NULL )
- {
- return StartAction(AT_WPN_ATTACH_MAGAZINE, mag, NULL, control_action);
- }
-
- bool DetachMagazine( InventoryLocation invLoc, ActionBase control_action = NULL)
- {
- return StartAction(AT_WPN_DETACH_MAGAZINE, NULL, invLoc, control_action);
- }
-
- bool SwapMagazine( Magazine mag, ActionBase control_action = NULL )
- {
- InventoryLocation il = new InventoryLocation();
- if (PrepareInventoryLocationForMagazineSwap(m_WeaponInHand, mag, il) )
- {
- return StartAction(AT_WPN_SWAP_MAGAZINE, mag, il, control_action);
- }
- return false;
- }
-
- bool SwapMagazineEx( Magazine mag, InventoryLocation invLoc, ActionBase control_action = NULL )
- {
- return StartAction(AT_WPN_SWAP_MAGAZINE, mag, invLoc, control_action);
- }
-
- bool LoadBullet( Magazine mag, ActionBase control_action = NULL )
- {
- return StartAction(AT_WPN_LOAD_BULLET, mag, NULL, control_action);
- }
-
- bool LoadMultiBullet( Magazine mag, ActionBase control_action = NULL )
- {
- return StartAction(AT_WPN_LOAD_MULTI_BULLETS_START, mag, NULL, control_action);
- }
-
- void LoadMultiBulletStop( )
- {
- if(m_InProgress) m_WantContinue = false;
- }
-
- bool Unjam( ActionBase control_action = NULL )
- {
- return StartAction(AT_WPN_UNJAM, NULL, NULL, control_action);
- }
- bool EjectBullet( ActionBase control_action = NULL )
- {
- return StartAction(AT_WPN_EJECT_BULLET, NULL, NULL, control_action);
- }
-
- bool CanEjectBulletVerified()
- {
- int mi = m_WeaponInHand.GetCurrentMuzzle();
- if( !m_WeaponInHand.IsChamberFiredOut(mi) && !m_WeaponInHand.IsChamberEmpty(mi) )
- {
- int actual_time = GetGame().GetTime();
- if ( actual_time - m_ForceEjectBulletTimestamp > FORCE_EJECT_BULLET_TIMEOUT )
- {
- return false;
- }
- }
- return true;
- }
-
- bool EjectBulletVerified( ActionBase control_action = NULL )
- {
- if ( m_WeaponInHand )
- {
- int mi = m_WeaponInHand.GetCurrentMuzzle();
- if ( !m_WeaponInHand.IsChamberFiredOut(mi) && !m_WeaponInHand.IsChamberEmpty(mi) )
- {
- m_ForceEjectBulletTimestamp = GetGame().GetTime();
- return StartAction(AT_WPN_EJECT_BULLET, NULL, NULL, control_action);
- }
- else
- {
- return StartAction(AT_WPN_EJECT_BULLET, NULL, NULL, control_action);
- }
- }
- return false;
- }
-
- bool SetNextMuzzleMode ()
- {
- return StartAction(AT_WPN_SET_NEXT_MUZZLE_MODE, NULL, NULL, NULL);
- }
-
- void Fire(Weapon_Base wpn)
- {
- int mi = wpn.GetCurrentMuzzle();
- if ( wpn.IsChamberFiredOut(mi) || wpn.IsJammed() || wpn.IsChamberEmpty(mi) )
- {
- wpn.ProcessWeaponEvent(new WeaponEventTrigger(m_player));
- return;
- }
-
- if (wpn.JamCheck(0))
- {
- wpn.ProcessWeaponEvent(new WeaponEventTriggerToJam(m_player));
- }
- else
- {
- wpn.ProcessWeaponEvent(new WeaponEventTrigger(m_player));
- }
- }
-
- #ifdef DIAG_DEVELOPER
- int GetBurstOption()
- {
- return m_BurstOption;
- }
-
- void SetBurstOption(int value)
- {
- m_BurstOption = value;
- }
- #endif
- //-------------------------------------------------------------------------------------
- // Synchronize - initialize from client side
- //-------------------------------------------------------------------------------------
- //Client
- private void Synchronize( )
- {
- if ( GetGame().IsClient() )
- {
- m_PendingWeaponActionAcknowledgmentID = ++m_LastAcknowledgmentID;
- ScriptInputUserData ctx = new ScriptInputUserData;
-
- ctx.Write(INPUT_UDT_WEAPON_ACTION);
- ctx.Write(m_PendingWeaponAction);
- ctx.Write(m_PendingWeaponActionAcknowledgmentID);
-
-
- switch (m_PendingWeaponAction)
- {
- case AT_WPN_ATTACH_MAGAZINE:
- {
- ctx.Write(m_PendingTargetMagazine);
- break;
- }
- case AT_WPN_SWAP_MAGAZINE:
- {
- ctx.Write(m_PendingTargetMagazine);
- m_PendingInventoryLocation.WriteToContext(ctx);
- break;
- }
- case AT_WPN_DETACH_MAGAZINE:
- {
- m_PendingInventoryLocation.WriteToContext(ctx);
- break;
- }
- case AT_WPN_LOAD_BULLET:
- {
- ctx.Write(m_PendingTargetMagazine);
- break;
- }
- case AT_WPN_LOAD_MULTI_BULLETS_START:
- {
- ctx.Write(m_PendingTargetMagazine);
- break;
- }
- case AT_WPN_UNJAM:
- {
- break;
- }
- case AT_WPN_EJECT_BULLET:
- {
- break;
- }
- default:
- break;
- }
- ctx.Send();
- //if( !m_player.GetDayZPlayerInventory().HasLockedHands() )
- // m_player.GetDayZPlayerInventory().LockHands();
- }
- }
-
-
- void OnSyncJuncture(int pJunctureID, ParamsReadContext pCtx)
- {
- if (pJunctureID == DayZPlayerSyncJunctures.SJ_WEAPON_SET_JAMMING_CHANCE )
- {
- pCtx.Read(m_NewJamChance);
- }
- else
- {
- int AcknowledgmentID;
- pCtx.Read(AcknowledgmentID);
- if ( AcknowledgmentID == m_PendingWeaponActionAcknowledgmentID)
- {
- if (pJunctureID == DayZPlayerSyncJunctures.SJ_WEAPON_ACTION_ACK_ACCEPT)
- {
- m_readyToStart = true;
- }
- else if (pJunctureID == DayZPlayerSyncJunctures.SJ_WEAPON_ACTION_ACK_REJECT)
- {
- if(m_PendingWeaponAction >= 0 )
- {
- if(!(GetGame().IsServer() && GetGame().IsMultiplayer()))
- {
- InventoryLocation ilWeapon = new InventoryLocation();
- ItemBase weapon = m_player.GetItemInHands();
- weapon.GetInventory().GetCurrentInventoryLocation(ilWeapon);
- m_player.GetInventory().ClearInventoryReservationEx(m_player.GetItemInHands(),ilWeapon);
-
- if( m_PendingTargetMagazine )
- {
- m_PendingTargetMagazine.GetInventory().ClearInventoryReservationEx(m_PendingTargetMagazine, m_TargetInventoryLocation );
- }
-
- if( m_PendingInventoryLocation )
- {
- m_player.GetInventory().ClearInventoryReservationEx( NULL, m_PendingInventoryLocation );
- }
- }
- m_PendingWeaponActionAcknowledgmentID = -1;
- m_PendingTargetMagazine = NULL;
- m_PendingWeaponAction = -1;
- m_PendingInventoryLocation = NULL;
- m_InProgress = false;
- }
- }
- }
- }
- }
-
- //Server
- bool OnInputUserDataProcess(int userDataType, ParamsReadContext ctx)
- {
- Weapon_Base wpn;
- int mi;
- InventoryLocation il;
- int slotID;
- bool accepted = false;
- if( userDataType == INPUT_UDT_WEAPON_ACTION)
- {
- if (!ctx.Read(m_PendingWeaponAction))
- return false;
-
- if (!ctx.Read(m_PendingWeaponActionAcknowledgmentID))
- return false;
-
- if(m_PendingTargetMagazine)
- {
- GetGame().ClearJuncture(m_player, m_PendingTargetMagazine);
- m_PendingTargetMagazine = NULL;
- }
- m_InProgress = true;
- m_IsEventSended = false;
- Magazine mag = NULL;
-
- Weapon_Base.CastTo( wpn, m_player.GetItemInHands() );
- if ( wpn )
- mi = wpn.GetCurrentMuzzle();
-
- switch (m_PendingWeaponAction)
- {
- case AT_WPN_ATTACH_MAGAZINE:
- {
- if ( !ctx.Read(mag) )
- break;
-
- if ( !mag || !wpn )
- break;
- slotID = wpn.GetSlotFromMuzzleIndex(mi);
- il = new InventoryLocation();
- il.SetAttachment(wpn,mag,slotID);
- if( GetGame().AddInventoryJunctureEx(m_player, mag, il, false, 10000) )
- accepted = true;
- m_PendingTargetMagazine = mag;
- break;
- }
- case AT_WPN_SWAP_MAGAZINE:
- {
- if ( !ctx.Read(mag) )
- break;
-
- il = new InventoryLocation();
- if (!il.ReadFromContext(ctx))
- break;
-
- if ( !mag || !wpn )
- break;
- if ( !wpn.GetMagazine(mi) )
- break;
-
- if ( GetGame().AddActionJuncture(m_player,mag,10000) )
- accepted = true;
- m_PendingInventoryLocation = il;
- m_PendingTargetMagazine = mag;
-
- break;
- }
- case AT_WPN_DETACH_MAGAZINE:
- {
- il = new InventoryLocation();
- if ( !il.ReadFromContext(ctx) )
- break;
-
- if ( !il.IsValid() )
- break;
-
- if ( !wpn )
- break;
-
- Magazine det_mag = wpn.GetMagazine(mi);
- mag = Magazine.Cast(il.GetItem());
- if ( !det_mag || ( mag != det_mag) )
- break;
-
- if (GetGame().AddInventoryJunctureEx(m_player, il.GetItem(), il, true, 10000))
- accepted = true;
- m_PendingInventoryLocation = il;
- m_PendingTargetMagazine = mag;
- break;
- }
- case AT_WPN_LOAD_BULLET:
- {
- ctx.Read(mag);
-
- if ( !mag )
- break;
-
- if( GetGame().AddActionJuncture(m_player,mag,10000) )
- accepted = true;
- m_PendingTargetMagazine = mag;
- break;
- }
- case AT_WPN_LOAD_MULTI_BULLETS_START:
- {
- ctx.Read(mag);
-
- if ( !mag )
- break;
-
- if( GetGame().AddActionJuncture(m_player,mag,10000) )
- accepted = true;
- m_PendingTargetMagazine = mag;
- break;
- }
- case AT_WPN_UNJAM:
- {
- accepted = true;
- //Unjam();
- break;
- }
- case AT_WPN_EJECT_BULLET:
- {
- accepted = true;
- break;
- }
- case AT_WPN_SET_NEXT_MUZZLE_MODE:
- {
- accepted = true;
- break;
- }
- default:
- Error("unknown actionID=" + m_PendingWeaponAction);
- break;
- }
- DayZPlayerSyncJunctures.SendWeaponActionAcknowledgment(m_player, m_PendingWeaponActionAcknowledgmentID, accepted);
- }
-
- return accepted;
- }
-
- bool StartAction(int action, Magazine mag, InventoryLocation il, ActionBase control_action = NULL)
- {
- //if it is controled by action inventory reservation and synchronization provide action itself
- if(control_action)
- {
- m_ControlAction = ActionBase.Cast(control_action);
- m_PendingWeaponAction = action;
- m_InProgress = true;
- m_IsEventSended = false;
- m_PendingTargetMagazine = mag;
- m_PendingInventoryLocation = il;
- StartPendingAction();
-
- return true;
- }
-
-
- if (GetGame().IsMultiplayer() && GetGame().IsServer())
- return false;
-
- if ( !ScriptInputUserData.CanStoreInputUserData() )
- return false;
- if ( !InventoryReservation(mag, il) )
- return false;
- m_PendingWeaponAction = action;
- m_InProgress = true;
- m_IsEventSended = false;
-
- if ( !GetGame().IsMultiplayer() )
- m_readyToStart = true;
- else
- Synchronize();
-
- return true;
- }
-
- void StartPendingAction()
- {
- m_WeaponInHand = Weapon_Base.Cast(m_player.GetItemInHands());
- if(!m_WeaponInHand)
- {
- OnWeaponActionEnd();
- return;
- }
- switch (m_PendingWeaponAction)
- {
- case AT_WPN_ATTACH_MAGAZINE:
- {
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventAttachMagazine(m_player, m_PendingTargetMagazine) );
- break;
- }
- case AT_WPN_SWAP_MAGAZINE:
- {
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventSwapMagazine(m_player, m_PendingTargetMagazine, m_PendingInventoryLocation) );
- break;
- }
- case AT_WPN_DETACH_MAGAZINE:
- {
- Magazine mag = Magazine.Cast(m_PendingInventoryLocation.GetItem());
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventDetachMagazine(m_player, mag, m_PendingInventoryLocation) );
- break;
- }
- case AT_WPN_LOAD_BULLET:
- {
- m_WantContinue = false;
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventLoad1Bullet(m_player, m_PendingTargetMagazine) );
- break;
- }
- case AT_WPN_LOAD_MULTI_BULLETS_START:
- {
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventLoad1Bullet(m_player, m_PendingTargetMagazine) );
- break;
- }
- case AT_WPN_LOAD_MULTI_BULLETS_END:
- {
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventContinuousLoadBulletEnd(m_player) );
- break;
- }
- case AT_WPN_UNJAM:
- {
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventUnjam(m_player, NULL) );
- break;
- }
- case AT_WPN_EJECT_BULLET:
- {
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventMechanism(m_player, NULL) );
- break;
- }
- case AT_WPN_SET_NEXT_MUZZLE_MODE:
- {
- m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventSetNextMuzzleMode(m_player, NULL) );
- break;
- }
- default:
- m_InProgress = false;
- Error("unknown actionID=" + m_PendingWeaponAction);
- }
- m_IsEventSended = true;
- m_canEnd = false;
- }
-
- bool IsRunning()
- {
- return m_InProgress;
- }
-
- void SetRunning( bool value)
- {
- m_InProgress = value;
- }
-
- void Refresh()
- {
- OnWeaponActionEnd();
- }
-
- void Update( float deltaT )
- {
- if (m_WeaponInHand != m_player.GetItemInHands())
- {
- if( m_WeaponInHand )
- {
- m_SuitableMagazines.Clear();
- OnWeaponActionEnd();
- }
- m_WeaponInHand = Weapon_Base.Cast(m_player.GetItemInHands());
- if ( m_WeaponInHand )
- {
- m_MagazineInHand = null;
- //SET new magazine
- SetSutableMagazines();
- m_WeaponInHand.SetSyncJammingChance(0);
- }
- m_AnimationRefreshCooldown = 0;
- }
-
- if (m_WeaponInHand)
- {
- if(m_AnimationRefreshCooldown)
- {
- m_AnimationRefreshCooldown--;
-
- if( m_AnimationRefreshCooldown == 0)
- {
- RefreshAnimationState();
- }
- }
-
- if (!GetGame().IsMultiplayer())
- {
- m_WeaponInHand.SetSyncJammingChance(m_WeaponInHand.GetChanceToJam());
- }
- else
- {
- if ( m_NewJamChance >= 0)
- {
- m_WeaponInHand.SetSyncJammingChance(m_NewJamChance);
- m_NewJamChance = -1;
- m_WaitToSyncJamChance = false;
- }
- if (GetGame().IsServer() && !m_WaitToSyncJamChance )
- {
- float actual_chance_to_jam;
- actual_chance_to_jam = m_WeaponInHand.GetChanceToJam();
- if ( Math.AbsFloat(m_WeaponInHand.GetSyncChanceToJam() - m_WeaponInHand.GetChanceToJam()) > 0.001 )
- {
- DayZPlayerSyncJunctures.SendWeaponJamChance(m_player, m_WeaponInHand.GetChanceToJam());
- m_WaitToSyncJamChance = true;
- }
- }
- }
-
- if(m_readyToStart)
- {
- StartPendingAction();
- m_readyToStart = false;
- return;
- }
-
- if( !m_InProgress || !m_IsEventSended )
- return;
-
- if(m_canEnd)
- {
- if(m_WeaponInHand.IsIdle())
- {
- OnWeaponActionEnd();
- }
- else if(m_justStart)
- {
- m_InIronSight = m_player.IsInIronsights();
- m_InOptic = m_player.IsInOptics();
-
- if(m_InIronSight || m_InOptic)
- {
- //'RequestResetADSSync' can be called here, if ADS reset is desired
- m_player.ExitSights();
- }
-
- m_justStart = false;
- }
-
- }
- else
- {
- m_canEnd = true;
- m_justStart = true;
- }
- }
- else
- {
- if ( m_MagazineInHand != m_player.GetItemInHands() )
- {
- m_MagazineInHand = MagazineStorage.Cast(m_player.GetItemInHands());
- if ( m_MagazineInHand )
- {
- SetSutableMagazines();
- }
- }
-
-
- }
- }
- void OnWeaponActionEnd()
- {
- if ( !m_InProgress )
- return;
-
- if (!m_ControlAction)
- {
- if (GetGame().IsServer() && GetGame().IsMultiplayer())
- {
- if(m_PendingTargetMagazine)
- {
- GetGame().ClearJuncture(m_player,m_PendingTargetMagazine);
- }
- }
- else
- {
- InventoryLocation il = new InventoryLocation();
- il.SetHands(m_player,m_player.GetItemInHands());
- m_player.GetInventory().ClearInventoryReservationEx(m_player.GetItemInHands(),il);
-
- if( m_PendingTargetMagazine )
- {
- m_player.GetInventory().ClearInventoryReservationEx(m_PendingTargetMagazine, m_TargetInventoryLocation );
- }
-
- if( m_PendingInventoryLocation )
- {
- m_player.GetInventory().ClearInventoryReservationEx( m_PendingInventoryLocation.GetItem(), m_PendingInventoryLocation );
- }
- }
- }
-
- m_ControlAction = NULL;
- m_PendingWeaponAction = -1;
- m_PendingTargetMagazine = NULL;
- m_PendingInventoryLocation = NULL;
- m_TargetInventoryLocation = NULL;
- m_PendingWeaponActionAcknowledgmentID = -1;
- //m_WeaponInHand = NULL;
- m_InProgress = false;
- m_readyToStart = false;
- m_WantContinue = true;
-
- }
-
- void DelayedRefreshAnimationState(int delay)
- {
- if(m_WeaponInHand)
- {
- if(delay == 0)
- {
- RefreshAnimationState();
- }
- m_AnimationRefreshCooldown = delay;
- }
- }
-
- void RefreshAnimationState()
- {
- if(m_WeaponInHand)
- {
- WeaponStableState state = WeaponStableState.Cast( m_WeaponInHand.GetCurrentState() );
-
- if (state)
- {
- HumanCommandWeapons hcw = m_player.GetCommandModifier_Weapons();
- if (hcw)
- {
- hcw.SetInitState(state.m_animState);
- }
- }
- }
- }
-
- bool WantContinue()
- {
- return m_WantContinue;
- }
-
- Magazine GetPreparedMagazine()
- {
- Magazine mag;
- for (int i = 0; i < m_SuitableMagazines.Count(); i++)
- {
- mag = m_SuitableMagazines[i];
- if (!mag || mag.IsRuined() || (mag.GetHierarchyParent() && mag.GetHierarchyParent().IsWeapon()) )
- {
- m_SuitableMagazines.Remove(i);
- i--;
- continue;
- }
-
- if(!mag.GetHierarchyParent() || mag.GetHierarchyParent().GetInventory().AreChildrenAccessible())
- {
- if (mag.GetAmmoCount() > 0)
- return mag;
- }
- }
-
- return null;
- }
-
- Magazine GetNextPreparedMagazine( out int startIdx )
- {
- int count = m_SuitableMagazines.Count();
- Magazine mag;
- for (int i = startIdx; i < count; i++)
- {
- mag = m_SuitableMagazines[i];
- if (mag && mag.GetAmmoCount() > 0 && (!mag.GetHierarchyParent() || mag.GetHierarchyParent().GetInventory().AreChildrenAccessible()))
- {
- startIdx = i;
- return m_SuitableMagazines.Get(i);
- }
- }
- return null;
- }
-
- void OnMagazineInventoryEnter(Magazine mag)
- {
- if (mag)
- {
- Weapon_Base weapon = Weapon_Base.Cast(mag.GetHierarchyParent());
-
- if (weapon)
- return;
- }
-
- int i;
- MagazineStorage sMag = MagazineStorage.Cast(mag);
- if(sMag)
- {
- for(i = 0; i < m_MagazineStorageInInventory.Count(); i++ )
- {
- MagazineStorage s_mag_i = m_MagazineStorageInInventory[i];
- if(!s_mag_i)
- {
- m_MagazineStorageInInventory.RemoveOrdered(i);
- i--;
- continue;
- }
-
- if(CompareMagazinesSuitability(s_mag_i,sMag)<0)
- break;
- }
- m_MagazineStorageInInventory.InsertAt(sMag,i);
-
- SetSutableMagazines(); //TODO optimalize
- return;
- }
-
- if(mag)
- {
- for(i = 0; i < m_MagazinePilesInInventory.Count(); i++ )
- {
- Magazine mag_i = m_MagazinePilesInInventory[i];
- if(!mag_i)
- {
- m_MagazinePilesInInventory.RemoveOrdered(i);
- i--;
- continue;
- }
-
-
- if(CompareMagazinesSuitability(mag_i,mag)<0)
- {
- break;
- }
- }
- m_MagazinePilesInInventory.InsertAt(mag,i);
- SetSutableMagazines(); //TODO optimalize
- }
-
- }
-
- void OnMagazineInventoryExit(Magazine mag)
- {
- m_SuitableMagazines.RemoveItem(mag);
- MagazineStorage sMag = MagazineStorage.Cast(mag);
- if(sMag)
- {
- m_MagazineStorageInInventory.RemoveItem(sMag);
- return;
- }
-
- if(mag)
- {
- m_MagazinePilesInInventory.RemoveItem(mag);
- }
- }
-
- void OnMagazineAttach(Magazine mag)
- {
- OnMagazineInventoryExit(mag);
- }
-
- void OnMagazineDetach(Magazine mag)
- {
- OnMagazineInventoryEnter(mag);
- }
-
- int CompareMagazinesSuitability( Magazine mag1, Magazine mag2 )
- {
- return mag1.GetAmmoCount() - mag2.GetAmmoCount();
- }
-
- void SortMagazineAfterLoad()
- {
- array<MagazineStorage> magazines = new array<MagazineStorage>;
- array<Magazine> magazines_piles = new array<Magazine>;
- int low_mag1, high_mag1;
- int low_mag2, high_mag2;
- int i, j;
-
- for(i = 0; i < m_MagazineStorageInInventory.Count(); i++ )
- {
- MagazineStorage mag = m_MagazineStorageInInventory.Get(i);
- mag.GetNetworkID(low_mag1,high_mag1);
- for( j = 0; j < magazines.Count(); j++)
- {
- magazines.Get(j).GetNetworkID(low_mag2,high_mag2);
- if(low_mag1 > low_mag2)
- {
- break;
- }
- else if (low_mag1 == low_mag2)
- {
- if( high_mag1 > high_mag2 )
- {
- break;
- }
- }
- }
- magazines.InsertAt(mag,j);
- }
-
- m_MagazineStorageInInventory.Clear();
- m_MagazineStorageInInventory.Copy(magazines);
-
- for(i = 0; i < m_MagazinePilesInInventory.Count(); i++ )
- {
- Magazine pile = m_MagazinePilesInInventory.Get(i);
- pile.GetNetworkID(low_mag1,high_mag1);
- for( j = 0; j < magazines_piles.Count(); j++)
- {
- magazines_piles.Get(j).GetNetworkID(low_mag2,high_mag2);
- if(low_mag1 > low_mag2)
- {
- break;
- }
- else if (low_mag1 == low_mag2)
- {
- if( high_mag1 > high_mag2 )
- {
- break;
- }
- }
- }
- magazines_piles.InsertAt(pile,j);
- }
-
- m_MagazinePilesInInventory.Clear();
- m_MagazinePilesInInventory.Copy(magazines_piles);
-
- SetSutableMagazines();
- }
-
- void SetSutableMagazines()
- {
- m_SuitableMagazines.Clear();
- int i;
-
- if (m_WeaponInHand)
- {
- int mi = m_WeaponInHand.GetCurrentMuzzle();
-
- for (i = 0; i < m_MagazineStorageInInventory.Count(); i++ )
- {
- MagazineStorage s_mag = m_MagazineStorageInInventory[i];
-
- if (!s_mag)
- {
- m_MagazineStorageInInventory.RemoveOrdered(i);
- i--;
- continue;
- }
-
- if ( m_WeaponInHand.TestAttachMagazine(mi, s_mag, false, true))
- m_SuitableMagazines.Insert(s_mag);
- }
-
- for (i = 0; i < m_MagazinePilesInInventory.Count(); i++ )
- {
- Magazine mag = m_MagazinePilesInInventory[i];
- if (!mag)
- {
- m_MagazinePilesInInventory.RemoveOrdered(i);
- i--;
- continue;
- }
-
- if (m_WeaponInHand.CanChamberFromMag(mi, mag))
- m_SuitableMagazines.Insert(mag);
- }
- //TODO m_MagazineStorageInInventory and m_MagazinePilesInInventory always sort
- }
- else if (m_MagazineInHand)
- {
- for (i = 0; i < m_MagazinePilesInInventory.Count(); i++ )
- {
- Magazine m_mag = m_MagazinePilesInInventory[i];
- if (!m_mag)
- {
- m_MagazinePilesInInventory.RemoveOrdered(i);
- i--;
- continue;
- }
- if (m_MagazineInHand.IsCompatiableAmmo( m_mag ))
- m_SuitableMagazines.Insert(m_mag);
- }
- }
- else
- {
- m_PreparedMagazine = null;
- }
-
- }
-
- void OnLiftWeapon()
- {
- if( m_WeaponInHand )
- m_WeaponInHand.ResetBurstCount();
- }
-
- string GetCurrentModeName()
- {
- if( m_WeaponInHand )
- {
- int mi = m_WeaponInHand.GetCurrentMuzzle();
- return m_WeaponInHand.GetCurrentModeName(mi);
- }
- return "";
- }
-
- bool PrepareInventoryLocationForMagazineSwap( notnull Weapon_Base wpn, notnull Magazine new_mag, out InventoryLocation new_il )
- {
- int muzzleIndex = wpn.GetCurrentMuzzle();
- Magazine old_mag = Magazine.Cast(wpn.GetMagazine(muzzleIndex));
- InventoryLocation temp = new InventoryLocation();
-
- if (old_mag)
- {
- bool result = GameInventory.CanSwapEntitiesEx(new_mag, old_mag);
- if ( result )
- {
- new_mag.GetInventory().GetCurrentInventoryLocation(new_il);
- new_il.SetItem(old_mag);
- }
- else
- {
- result = GameInventory.CanForceSwapEntitiesEx(new_mag, null, old_mag, new_il);
- float dir[4];
- if ( !result )
- {
- new_il.SetGroundEx( old_mag, m_player.GetPosition(), dir);
- }
- }
- }
- return true;
- }
- }
|