123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636 |
- class SoftSkillsManager
- {
- protected PlayerBase m_Player;
- protected float m_SpecialtyLevel;
- protected float m_RoughLevel;
- protected float m_PreciseLevel;
-
- protected bool m_IsLinear;
- protected bool m_IsActive;
- protected bool m_IsCoolDown;
- protected int m_UserActionsCounter;
- static protected const int DEFAULT_EFFICIENCY = 0;
- static protected const float PRECISE_WEIGHT_LIMIT = -1;
- static protected const float ROUGH_WEIGHT_LIMIT = 1;
- static protected const float COOLDOWN_TIMER = 5; //default 5,
- protected ref Timer m_CoolDownTimer = new Timer();
- protected bool m_IsDebugMode;
- protected float m_CoolDownValue;
- protected float m_LastUAValue;
- protected float m_ComponentBonusBefore;
- protected float m_ComponentBonusAfter;
- protected float m_GeneralBonusBefore;
- protected float m_GeneralBonusAfter;
- protected ref SoftSkillManagerDebug m_DebugWindow;
- protected ref Timer m_SynchTimer;
- void SoftSkillsManager( PlayerBase player )
- {
- m_Player = player;
- m_IsCoolDown = false;
- m_IsLinear = false;
- m_IsActive = true;
- m_IsDebugMode = false;
- }
- void InitSpecialty( float specialty_level )
- {
- SetSpecialtyLevel( specialty_level );
- SynchSpecialtyLevel();
- }
- void ~SoftSkillsManager()
- {
- delete m_CoolDownTimer;
- }
- // ----------------------------------------------------------------------------------------
- float AddLinearPrecise( float specialty_weight )
- {
- m_SpecialtyLevel += specialty_weight;
- SetLastUAValue( specialty_weight );
- m_SpecialtyLevel = Math.Clamp( m_SpecialtyLevel, PRECISE_WEIGHT_LIMIT, ROUGH_WEIGHT_LIMIT );
- return m_SpecialtyLevel;
- }
- // ----------------------------------------------------------------------------------------
- float AddLinearRough( float specialty_weight )
- {
- m_SpecialtyLevel += specialty_weight;
- SetLastUAValue( specialty_weight );
- m_SpecialtyLevel = Math.Clamp( m_SpecialtyLevel, PRECISE_WEIGHT_LIMIT, ROUGH_WEIGHT_LIMIT );
- return m_SpecialtyLevel;
- }
- // ----------------------------------------------------------------------------------------
- float AddExponentialPrecise( float specialty_weight )
- {
- m_UserActionsCounter -= 1;
- if( m_UserActionsCounter == 0)
- {
- m_UserActionsCounter = -1;
- m_SpecialtyLevel = 0;
- }
-
- float adjusted_weight = specialty_weight / Math.Sqrt( Math.AbsInt( m_UserActionsCounter ) );
-
- SetLastUAValue( adjusted_weight );
- m_SpecialtyLevel += adjusted_weight;
- m_SpecialtyLevel = Math.Clamp( m_SpecialtyLevel, PRECISE_WEIGHT_LIMIT, ROUGH_WEIGHT_LIMIT );
- return m_SpecialtyLevel;
- }
-
- // ----------------------------------------------------------------------------------------
- float AddExponentialRough( float specialty_weight )
- {
- m_UserActionsCounter += 1;
-
- if( m_UserActionsCounter == 0)
- {
- m_UserActionsCounter = 1;
- m_SpecialtyLevel = 0;
- }
-
- float adjusted_weight = specialty_weight / Math.Sqrt( Math.AbsInt( m_UserActionsCounter ) );
- SetLastUAValue( adjusted_weight );
- m_SpecialtyLevel += adjusted_weight;
- m_SpecialtyLevel = Math.Clamp( m_SpecialtyLevel, PRECISE_WEIGHT_LIMIT, ROUGH_WEIGHT_LIMIT );
- return m_SpecialtyLevel;
- }
- // ----------------------------------------------------------------------------------------
- void AddSpecialty( float specialty_weight )
- {
- if( GetSoftSkillsState() )
- {
- if( !IsCoolDown() )
- {
- if( specialty_weight < 0 )
- {
- if( IsLinear() )
- {
- SetSpecialtyLevel( AddLinearPrecise( specialty_weight ) );
- }
- else
- {
- SetSpecialtyLevel( AddExponentialPrecise( specialty_weight ) );
- }
- m_Player.GetStatSpecialty().Set( m_SpecialtyLevel );
- StartCoolDownTimer( Math.AbsFloat( ( specialty_weight * 100 ) ) * COOLDOWN_TIMER );
- SynchSpecialtyLevel();
- }
- else if( specialty_weight > 0 )
- {
- if( IsLinear() )
- {
- SetSpecialtyLevel( AddLinearRough( specialty_weight ) );
- }
- else
- {
- SetSpecialtyLevel( AddExponentialRough( specialty_weight ) );
- }
- m_Player.GetStatSpecialty().Set( m_SpecialtyLevel );
- StartCoolDownTimer( Math.AbsFloat( ( specialty_weight * 100 ) ) * COOLDOWN_TIMER );
- SynchSpecialtyLevel();
- }
- else
- {
- //if the specialty weight for a recipe or UA is set to 0, it will increase neither specialty, nor UA counter
- return;
- }
- }
- else
- {
- StartCoolDownTimer( Math.AbsFloat( ( specialty_weight * 100 ) ) * COOLDOWN_TIMER );
- }
- }
- }
- // ----------------------------------------------------------------------------------------
- // Use AddSpecialtyBonus if you want to increase a value, based on the specialty level of the player. e.g. more harvested material
- // limit_efficiency sets the max amount of bonus, the soft skill can provide: limit_efficiency = 2 is 50% bonus, limit_efficiency = 3 is 33% bonus etc
- float AddSpecialtyBonus( float base_value, float specialty_weight, bool is_cacomponent = false, float limit_efficiency = 2 )
- {
- if ( specialty_weight == 0 )
- {
- return base_value;
- }
-
- SetBonusBefore( is_cacomponent, base_value);
- float adjusted_value;
-
- GetPreciseRoughLevels();
- if ( limit_efficiency != 0 )
- {
- if ( specialty_weight < 0 )
- {
- adjusted_value = base_value + ( ( base_value * m_PreciseLevel ) / limit_efficiency );
- }
- else
- {
- adjusted_value = base_value + ( ( base_value * m_RoughLevel ) / limit_efficiency );
- }
- }
- else
- {
- if ( specialty_weight < 0 )
- {
- adjusted_value = base_value + ( ( base_value * m_PreciseLevel ) );
- }
- else
- {
- adjusted_value = base_value + ( ( base_value * m_RoughLevel ) );
- }
- }
- SetBonusAfter( is_cacomponent, adjusted_value );
- return adjusted_value;
- }
-
- // ----------------------------------------------------------------------------------------
- // Use SubtractSpecialtyBonus if you want to decrease a value, based on the specialty level of the player. e.g. faster harvesting time
- // limit_efficiency sets the max amount of bonus, the soft skill can provide: limit_efficiency = 2 is 50% bonus, limit_efficiency = 3 is 33% bonus etc
- float SubtractSpecialtyBonus( float base_value, float specialty_weight, bool is_cacomponent = false, float limit_efficiency = 2 )
- {
- if ( specialty_weight == 0 )
- {
- return base_value;
- }
-
- SetBonusBefore( is_cacomponent, base_value);
- float adjusted_value;
-
- GetPreciseRoughLevels();
- if ( limit_efficiency != 0 )
- {
- if ( specialty_weight < 0 )
- {
- adjusted_value = base_value - ( ( base_value * m_PreciseLevel ) / limit_efficiency );
- }
- else
- {
- adjusted_value = base_value - ( ( base_value * m_RoughLevel ) / limit_efficiency );
- }
- }
- else
- {
- if ( specialty_weight < 0 )
- {
- adjusted_value = base_value - ( ( base_value * m_PreciseLevel ) );
- }
- else
- {
- adjusted_value = base_value - ( ( base_value * m_RoughLevel ) );
- }
- }
- SetBonusAfter( is_cacomponent, adjusted_value );
- return adjusted_value;
- }
- // ----------------------------------------------------------------------------------------
- // Use AdjustCraftingTime if you want to decrease time for recipe crafting
- // limit_efficiency sets the max amount of bonus, the soft skill can provide: limit_efficiency = 2 is 50% bonus, limit_efficiency = 3 is 33% bonus etc
- float AdjustCraftingTime( float base_time, float specialty_weight, float limit_efficiency = 2 )
- {
- if ( specialty_weight == 0 )
- {
- return base_time;
- }
-
- SetBonusBefore( false, base_time);
- float adjusted_time;
- GetPreciseRoughLevels();
- if ( specialty_weight < 0 )
- {
- adjusted_time = base_time - ( ( base_time * m_PreciseLevel ) / limit_efficiency );
- }
- else
- {
- adjusted_time = base_time - ( ( base_time * m_RoughLevel ) / limit_efficiency );
- }
- SetBonusAfter( false, adjusted_time );
- return adjusted_time;
- }
- // ----------------------------------------------------------------------------------------
- PlayerBase GetSoftSkillsPlayer()
- {
- return m_Player;
- }
-
- // ----------------------------------------------------------------------------------------
- void SetSpecialtyLevel( float specialty_level )
- {
- m_SpecialtyLevel = specialty_level;
- }
-
- // ----------------------------------------------------------------------------------------
- float GetSpecialtyLevel()
- {
- return m_SpecialtyLevel;
- }
- // ----------------------------------------------------------------------------------------
- void SynchSpecialtyLevel()
- {
- #ifdef SERVER
- Param1<float> specialty_level = new Param1<float>( m_SpecialtyLevel );
- GetGame().RPCSingleParam( m_Player, ERPCs.RPC_SOFT_SKILLS_SPECIALTY_SYNC, specialty_level, true, m_Player.GetIdentity() );
- #endif
- }
- // ----------------------------------------------------------------------------------------
- void SetSoftSkillsState( bool state )
- {
- m_IsActive = state;
- }
- // ----------------------------------------------------------------------------------------
- bool GetSoftSkillsState()
- {
- return m_IsActive;
- }
- // ----------------------------------------------------------------------------------------
- void SetLinearState( bool model )
- {
- m_IsLinear = model;
- }
- // ----------------------------------------------------------------------------------------
- bool IsLinear()
- {
- return m_IsLinear;
- }
- // ----------------------------------------------------------------------------------------
- void GetPreciseRoughLevels()
- {
- if ( m_SpecialtyLevel > 0)
- {
- m_RoughLevel = m_SpecialtyLevel;
- m_PreciseLevel = DEFAULT_EFFICIENCY;
- }
- else if ( m_SpecialtyLevel < 0)
- {
- m_RoughLevel = DEFAULT_EFFICIENCY;
- m_PreciseLevel = Math.AbsFloat( m_SpecialtyLevel );
- }
- else
- {
- m_RoughLevel = DEFAULT_EFFICIENCY;
- m_PreciseLevel = DEFAULT_EFFICIENCY;
- }
- }
- // ----------------------------------------------------------------------------------------
- void StartCoolDownTimer( float cooldown_value )
- {
- SetCoolDown( true );
- SetCoolDownValue( cooldown_value );
- m_CoolDownTimer.Run( cooldown_value, this, "SetCoolDown", new Param1<bool>( false ) );
- }
- // ----------------------------------------------------------------------------------------
- bool IsCoolDown()
- {
- return m_IsCoolDown;
- }
- // ----------------------------------------------------------------------------------------
- void SetCoolDown( bool cool_down )
- {
- m_IsCoolDown = cool_down;
- }
-
- // ----------------------------------------------------------------------------------------
- // SoftSkillManagerDebug support
- // ----------------------------------------------------------------------------------------
- void CreateDebugWindow( bool create )
- {
- if ( create )
- {
- m_DebugWindow = new SoftSkillManagerDebug( this );
- SetIsDebug( create );
- }
- else
- {
- ResetDebugWindow();
- SetIsDebug( create );
- delete m_DebugWindow;
- }
- }
-
- // ----------------------------------------------------------------------------------------
- void SynchDebugStats()
- {
- if ( GetGame().IsServer() && GetGame().IsMultiplayer() )
- {
- Param5<float, float, float, float, bool> debug_stats = new Param5<float, float, float, float, bool>( m_GeneralBonusBefore, m_GeneralBonusAfter, m_LastUAValue, m_CoolDownValue, m_IsCoolDown );
- GetGame().RPCSingleParam( m_Player, ERPCs.RPC_SOFT_SKILLS_STATS_SYNC, debug_stats, true, m_Player.GetIdentity() );
- }
- }
- // ----------------------------------------------------------------------------------------
- void SetIsDebug( bool is_debug )
- {
- m_IsDebugMode = is_debug;
- }
- // ----------------------------------------------------------------------------------------
- bool IsDebug()
- {
- return m_IsDebugMode;
- }
-
- // ----------------------------------------------------------------------------------------
- void SetCoolDownValue( float cooldown_value )
- {
- m_CoolDownValue = cooldown_value;
- }
- // ----------------------------------------------------------------------------------------
- float GetCoolDownValue()
- {
- return m_CoolDownValue;
- }
-
- // ----------------------------------------------------------------------------------------
- float GetLastUAValue()
- {
- return m_LastUAValue;
- }
- // ----------------------------------------------------------------------------------------
- void SetLastUAValue( float last_ua_value )
- {
- m_LastUAValue = last_ua_value;
- }
- // ----------------------------------------------------------------------------------------
- void SetBonusBefore( bool is_cacomponent, float base_value)
- {
- if ( IsDebug() )
- {
- if ( is_cacomponent )
- {
- SetComponentBonusBefore(base_value);
- }
- else
- {
- SetGeneralBonusBefore(base_value);
- }
- }
- }
- // ----------------------------------------------------------------------------------------
- void SetBonusAfter( bool is_cacomponent, float adjusted_value )
- {
- if ( IsDebug() )
- {
- if ( is_cacomponent )
- {
- SetComponentBonusAfter(adjusted_value);
- }
- else
- {
- SetGeneralBonusAfter(adjusted_value);
- }
- }
- }
- // ----------------------------------------------------------------------------------------
- float GetComponentBonusBefore()
- {
- return m_ComponentBonusBefore;
- }
- // ----------------------------------------------------------------------------------------
- void SetComponentBonusBefore( float component_bonus_before )
- {
- m_ComponentBonusBefore = component_bonus_before;
- }
- // ----------------------------------------------------------------------------------------
- float GetComponentBonusAfter()
- {
- return m_ComponentBonusAfter;
- }
- // ----------------------------------------------------------------------------------------
- void SetComponentBonusAfter( float component_bonus_after )
- {
- m_ComponentBonusAfter = component_bonus_after;
- }
- // ----------------------------------------------------------------------------------------
- float GetGeneralBonusBefore()
- {
- return m_GeneralBonusBefore;
- }
- // ----------------------------------------------------------------------------------------
- void SetGeneralBonusBefore( float general_bonus_before )
- {
- m_GeneralBonusBefore = general_bonus_before;
- }
- // ----------------------------------------------------------------------------------------
- float GetGeneralBonusAfter()
- {
- return m_GeneralBonusAfter;
- }
- // ----------------------------------------------------------------------------------------
- void SetGeneralBonusAfter( float general_bonus_after )
- {
- m_GeneralBonusAfter = general_bonus_after;
- }
- // ----------------------------------------------------------------------------------------
- void StartSynchTimer()
- {
- SetIsDebug( true );
- m_SynchTimer = new Timer;
- m_SynchTimer.Run( 2, this, "SynchDebugStats", NULL, true );
- }
- // ----------------------------------------------------------------------------------------
- void StopSynchTimer()
- {
- SetIsDebug( false );
-
- if ( m_SynchTimer )
- {
- m_SynchTimer.Stop();
- delete m_SynchTimer;
- }
- }
-
- // ----------------------------------------------------------------------------------------
- void ResetDebugWindow()
- {
- SetSpecialtyLevel( 0 );
- SetLastUAValue( 0 );
- SetComponentBonusBefore( 0 );
- SetComponentBonusAfter( 0 );
- SetGeneralBonusBefore( 0 );
- SetGeneralBonusAfter( 0 );
- SetCoolDownValue( 0 );
-
- SynchSpecialtyLevel();
- }
- }
- // ----------------------------------------------------------------------------------------
- // SoftSkillManagerDebug
- // ----------------------------------------------------------------------------------------
- class SoftSkillManagerDebug
- {
- SoftSkillsManager m_SoftSkillManager;
- ref Widget m_PanelSoftSkills;
- TextWidget SpecialtyTotal;
- TextWidget SpecialtyChange;
- TextWidget ComponentBonusBefore;
- TextWidget ComponentBonusAfter;
- TextWidget GeneralBonusBefore;
- TextWidget GeneralBonusAfter;
- TextWidget CoolDown;
- TextWidget IsCoolDown;
- void SoftSkillManagerDebug( SoftSkillsManager softskill_manager )
- {
- m_SoftSkillManager = softskill_manager;
- m_PanelSoftSkills = GetGame().GetWorkspace().CreateWidgets("gui/layouts/debug/day_z_hud_debug_softskills.layout");
-
- GetGame().GetUpdateQueue(CALL_CATEGORY_SYSTEM).Insert(this.OnUpdate);
- Class.CastTo(SpecialtyTotal, m_PanelSoftSkills.FindWidget("SpecialtyTotal"));
- Class.CastTo(SpecialtyChange, m_PanelSoftSkills.FindWidget("SpecialtyChange"));
- Class.CastTo(ComponentBonusBefore, m_PanelSoftSkills.FindWidget("ComponentBonusBefore"));
- Class.CastTo(ComponentBonusAfter, m_PanelSoftSkills.FindWidget("ComponentBonusAfter"));
- Class.CastTo(GeneralBonusBefore, m_PanelSoftSkills.FindWidget("GeneralBonusBefore"));
- Class.CastTo(GeneralBonusAfter, m_PanelSoftSkills.FindWidget("GeneralBonusAfter"));
- Class.CastTo(CoolDown, m_PanelSoftSkills.FindWidget("CoolDown"));
- Class.CastTo(IsCoolDown, m_PanelSoftSkills.FindWidget("IsCoolDown"));
-
- m_PanelSoftSkills.Show( true );
- SpecialtyTotal.Show( true );
- SpecialtyChange.Show( true );
- ComponentBonusBefore.Show( true );
- ComponentBonusAfter.Show( true );
- GeneralBonusBefore.Show( true );
- GeneralBonusAfter.Show( true );
- CoolDown.Show( true );
- IsCoolDown.Show( true );
- }
-
- void ~SoftSkillManagerDebug()
- {
- if ( GetGame() )
- {
- GetGame().GetUpdateQueue(CALL_CATEGORY_SYSTEM).Remove(this.OnUpdate);
- }
-
- delete m_PanelSoftSkills;
- }
- SoftSkillsManager GetActiveSoftSkillManager()
- {
- return m_SoftSkillManager;
- }
-
- void OnUpdate()
- {
- if ( GetActiveSoftSkillManager().GetSoftSkillsPlayer().IsAlive() )
- {
- float speciality = GetActiveSoftSkillManager().GetSpecialtyLevel();
- speciality = speciality * 100;
- speciality = Math.Round( speciality );
- speciality = speciality * 0.01;
-
- SpecialtyTotal.SetText( "Specialty level: " + speciality.ToString() );
- SpecialtyChange.SetText( "Specialty change: " + GetActiveSoftSkillManager().GetLastUAValue() );
- ComponentBonusBefore.SetText( "Component/craft default: " + GetActiveSoftSkillManager().GetComponentBonusBefore() );
- ComponentBonusAfter.SetText( "Component/craft with bonus: " + GetActiveSoftSkillManager().GetComponentBonusAfter() );
- GeneralBonusBefore.SetText( "General default: " + GetActiveSoftSkillManager().GetGeneralBonusBefore() );
- GeneralBonusAfter.SetText( "General with bonus: " + GetActiveSoftSkillManager().GetGeneralBonusAfter() );
- CoolDown.SetText( "CoolDown value: " + GetActiveSoftSkillManager().GetCoolDownValue() );
- IsCoolDown.SetText( "Cooldown active: " + GetActiveSoftSkillManager().IsCoolDown() );
- }
- else
- {
- delete this;
- }
- }
- }
|