Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
softskillsmanager.c
Go to the documentation of this file.
2 {
3  protected PlayerBase m_Player;
4  protected float m_SpecialtyLevel;
5  protected float m_RoughLevel;
6  protected float m_PreciseLevel;
7 
8  protected bool m_IsLinear;
9  protected bool m_IsActive;
10  protected bool m_IsCoolDown;
11 
12  protected int m_UserActionsCounter;
13 
14  static protected const int DEFAULT_EFFICIENCY = 0;
15  static protected const float PRECISE_WEIGHT_LIMIT = -1;
16  static protected const float ROUGH_WEIGHT_LIMIT = 1;
17  static protected const float COOLDOWN_TIMER = 5; //default 5,
18 
19  protected ref Timer m_CoolDownTimer = new Timer();
20 
21  protected bool m_IsDebugMode;
22  protected float m_CoolDownValue;
23  protected float m_LastUAValue;
24  protected float m_ComponentBonusBefore;
25  protected float m_ComponentBonusAfter;
26  protected float m_GeneralBonusBefore;
27  protected float m_GeneralBonusAfter;
28 
29  protected ref SoftSkillManagerDebug m_DebugWindow;
30 
31  protected ref Timer m_SynchTimer;
32 
33  void SoftSkillsManager( PlayerBase player )
34  {
35  m_Player = player;
36  m_IsCoolDown = false;
37  m_IsLinear = false;
38  m_IsActive = true;
39  m_IsDebugMode = false;
40  }
41 
42  void InitSpecialty( float specialty_level )
43  {
44  SetSpecialtyLevel( specialty_level );
45  SynchSpecialtyLevel();
46  }
47 
48  void ~SoftSkillsManager()
49  {
50  delete m_CoolDownTimer;
51  }
52 
53  // ----------------------------------------------------------------------------------------
54  float AddLinearPrecise( float specialty_weight )
55  {
56  m_SpecialtyLevel += specialty_weight;
57  SetLastUAValue( specialty_weight );
58  m_SpecialtyLevel = Math.Clamp( m_SpecialtyLevel, PRECISE_WEIGHT_LIMIT, ROUGH_WEIGHT_LIMIT );
59 
60  return m_SpecialtyLevel;
61  }
62 
63  // ----------------------------------------------------------------------------------------
64  float AddLinearRough( float specialty_weight )
65  {
66  m_SpecialtyLevel += specialty_weight;
67  SetLastUAValue( specialty_weight );
68  m_SpecialtyLevel = Math.Clamp( m_SpecialtyLevel, PRECISE_WEIGHT_LIMIT, ROUGH_WEIGHT_LIMIT );
69 
70  return m_SpecialtyLevel;
71  }
72 
73  // ----------------------------------------------------------------------------------------
74  float AddExponentialPrecise( float specialty_weight )
75  {
76  m_UserActionsCounter -= 1;
77 
78  if( m_UserActionsCounter == 0)
79  {
80  m_UserActionsCounter = -1;
81  m_SpecialtyLevel = 0;
82  }
83 
84  float adjusted_weight = specialty_weight / Math.Sqrt( Math.AbsInt( m_UserActionsCounter ) );
85 
86  SetLastUAValue( adjusted_weight );
87 
88  m_SpecialtyLevel += adjusted_weight;
89  m_SpecialtyLevel = Math.Clamp( m_SpecialtyLevel, PRECISE_WEIGHT_LIMIT, ROUGH_WEIGHT_LIMIT );
90 
91  return m_SpecialtyLevel;
92  }
93 
94  // ----------------------------------------------------------------------------------------
95  float AddExponentialRough( float specialty_weight )
96  {
97  m_UserActionsCounter += 1;
98 
99  if( m_UserActionsCounter == 0)
100  {
101  m_UserActionsCounter = 1;
102  m_SpecialtyLevel = 0;
103  }
104 
105  float adjusted_weight = specialty_weight / Math.Sqrt( Math.AbsInt( m_UserActionsCounter ) );
106 
107  SetLastUAValue( adjusted_weight );
108 
109  m_SpecialtyLevel += adjusted_weight;
110  m_SpecialtyLevel = Math.Clamp( m_SpecialtyLevel, PRECISE_WEIGHT_LIMIT, ROUGH_WEIGHT_LIMIT );
111 
112  return m_SpecialtyLevel;
113  }
114 
115  // ----------------------------------------------------------------------------------------
116  void AddSpecialty( float specialty_weight )
117  {
118  if( GetSoftSkillsState() )
119  {
120  if( !IsCoolDown() )
121  {
122  if( specialty_weight < 0 )
123  {
124  if( IsLinear() )
125  {
126  SetSpecialtyLevel( AddLinearPrecise( specialty_weight ) );
127  }
128  else
129  {
130  SetSpecialtyLevel( AddExponentialPrecise( specialty_weight ) );
131  }
132 
133  m_Player.GetStatSpecialty().Set( m_SpecialtyLevel );
134  StartCoolDownTimer( Math.AbsFloat( ( specialty_weight * 100 ) ) * COOLDOWN_TIMER );
135  SynchSpecialtyLevel();
136  }
137  else if( specialty_weight > 0 )
138  {
139  if( IsLinear() )
140  {
141  SetSpecialtyLevel( AddLinearRough( specialty_weight ) );
142  }
143  else
144  {
145  SetSpecialtyLevel( AddExponentialRough( specialty_weight ) );
146  }
147 
148  m_Player.GetStatSpecialty().Set( m_SpecialtyLevel );
149  StartCoolDownTimer( Math.AbsFloat( ( specialty_weight * 100 ) ) * COOLDOWN_TIMER );
150  SynchSpecialtyLevel();
151  }
152  else
153  {
154  //if the specialty weight for a recipe or UA is set to 0, it will increase neither specialty, nor UA counter
155  return;
156  }
157  }
158  else
159  {
160  StartCoolDownTimer( Math.AbsFloat( ( specialty_weight * 100 ) ) * COOLDOWN_TIMER );
161  }
162  }
163  }
164 
165  // ----------------------------------------------------------------------------------------
166  // Use AddSpecialtyBonus if you want to increase a value, based on the specialty level of the player. e.g. more harvested material
167  // 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
168  float AddSpecialtyBonus( float base_value, float specialty_weight, bool is_cacomponent = false, float limit_efficiency = 2 )
169  {
170  if ( specialty_weight == 0 )
171  {
172  return base_value;
173  }
174 
175  SetBonusBefore( is_cacomponent, base_value);
176 
177  float adjusted_value;
178 
179  GetPreciseRoughLevels();
180 
181  if ( limit_efficiency != 0 )
182  {
183  if ( specialty_weight < 0 )
184  {
185  adjusted_value = base_value + ( ( base_value * m_PreciseLevel ) / limit_efficiency );
186  }
187  else
188  {
189  adjusted_value = base_value + ( ( base_value * m_RoughLevel ) / limit_efficiency );
190  }
191  }
192  else
193  {
194  if ( specialty_weight < 0 )
195  {
196  adjusted_value = base_value + ( ( base_value * m_PreciseLevel ) );
197  }
198  else
199  {
200  adjusted_value = base_value + ( ( base_value * m_RoughLevel ) );
201  }
202  }
203 
204  SetBonusAfter( is_cacomponent, adjusted_value );
205 
206  return adjusted_value;
207  }
208 
209  // ----------------------------------------------------------------------------------------
210  // Use SubtractSpecialtyBonus if you want to decrease a value, based on the specialty level of the player. e.g. faster harvesting time
211  // 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
212  float SubtractSpecialtyBonus( float base_value, float specialty_weight, bool is_cacomponent = false, float limit_efficiency = 2 )
213  {
214  if ( specialty_weight == 0 )
215  {
216  return base_value;
217  }
218 
219  SetBonusBefore( is_cacomponent, base_value);
220 
221  float adjusted_value;
222 
223  GetPreciseRoughLevels();
224 
225  if ( limit_efficiency != 0 )
226  {
227  if ( specialty_weight < 0 )
228  {
229  adjusted_value = base_value - ( ( base_value * m_PreciseLevel ) / limit_efficiency );
230  }
231  else
232  {
233  adjusted_value = base_value - ( ( base_value * m_RoughLevel ) / limit_efficiency );
234  }
235  }
236  else
237  {
238  if ( specialty_weight < 0 )
239  {
240  adjusted_value = base_value - ( ( base_value * m_PreciseLevel ) );
241  }
242  else
243  {
244  adjusted_value = base_value - ( ( base_value * m_RoughLevel ) );
245  }
246  }
247 
248  SetBonusAfter( is_cacomponent, adjusted_value );
249 
250  return adjusted_value;
251  }
252 
253  // ----------------------------------------------------------------------------------------
254  // Use AdjustCraftingTime if you want to decrease time for recipe crafting
255  // 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
256  float AdjustCraftingTime( float base_time, float specialty_weight, float limit_efficiency = 2 )
257  {
258  if ( specialty_weight == 0 )
259  {
260  return base_time;
261  }
262 
263  SetBonusBefore( false, base_time);
264 
265  float adjusted_time;
266 
267  GetPreciseRoughLevels();
268 
269  if ( specialty_weight < 0 )
270  {
271  adjusted_time = base_time - ( ( base_time * m_PreciseLevel ) / limit_efficiency );
272  }
273  else
274  {
275  adjusted_time = base_time - ( ( base_time * m_RoughLevel ) / limit_efficiency );
276  }
277 
278  SetBonusAfter( false, adjusted_time );
279 
280  return adjusted_time;
281  }
282 
283  // ----------------------------------------------------------------------------------------
284  PlayerBase GetSoftSkillsPlayer()
285  {
286  return m_Player;
287  }
288 
289  // ----------------------------------------------------------------------------------------
290  void SetSpecialtyLevel( float specialty_level )
291  {
292  m_SpecialtyLevel = specialty_level;
293  }
294 
295  // ----------------------------------------------------------------------------------------
296  float GetSpecialtyLevel()
297  {
298  return m_SpecialtyLevel;
299  }
300 
301  // ----------------------------------------------------------------------------------------
302  void SynchSpecialtyLevel()
303  {
304  #ifdef SERVER
305  Param1<float> specialty_level = new Param1<float>( m_SpecialtyLevel );
306  GetGame().RPCSingleParam( m_Player, ERPCs.RPC_SOFT_SKILLS_SPECIALTY_SYNC, specialty_level, true, m_Player.GetIdentity() );
307  #endif
308  }
309 
310  // ----------------------------------------------------------------------------------------
311  void SetSoftSkillsState( bool state )
312  {
313  m_IsActive = state;
314  }
315 
316  // ----------------------------------------------------------------------------------------
317  bool GetSoftSkillsState()
318  {
319  return m_IsActive;
320  }
321 
322  // ----------------------------------------------------------------------------------------
323  void SetLinearState( bool model )
324  {
325  m_IsLinear = model;
326  }
327 
328  // ----------------------------------------------------------------------------------------
329  bool IsLinear()
330  {
331  return m_IsLinear;
332  }
333 
334  // ----------------------------------------------------------------------------------------
335  void GetPreciseRoughLevels()
336  {
337  if ( m_SpecialtyLevel > 0)
338  {
339  m_RoughLevel = m_SpecialtyLevel;
340  m_PreciseLevel = DEFAULT_EFFICIENCY;
341  }
342  else if ( m_SpecialtyLevel < 0)
343  {
344  m_RoughLevel = DEFAULT_EFFICIENCY;
345  m_PreciseLevel = Math.AbsFloat( m_SpecialtyLevel );
346  }
347  else
348  {
349  m_RoughLevel = DEFAULT_EFFICIENCY;
350  m_PreciseLevel = DEFAULT_EFFICIENCY;
351  }
352  }
353 
354  // ----------------------------------------------------------------------------------------
355  void StartCoolDownTimer( float cooldown_value )
356  {
357  SetCoolDown( true );
358  SetCoolDownValue( cooldown_value );
359  m_CoolDownTimer.Run( cooldown_value, this, "SetCoolDown", new Param1<bool>( false ) );
360  }
361 
362  // ----------------------------------------------------------------------------------------
363  bool IsCoolDown()
364  {
365  return m_IsCoolDown;
366  }
367 
368  // ----------------------------------------------------------------------------------------
369  void SetCoolDown( bool cool_down )
370  {
371  m_IsCoolDown = cool_down;
372  }
373 
374 // ----------------------------------------------------------------------------------------
375 // SoftSkillManagerDebug support
376 // ----------------------------------------------------------------------------------------
377  void CreateDebugWindow( bool create )
378  {
379  if ( create )
380  {
381  m_DebugWindow = new SoftSkillManagerDebug( this );
382  SetIsDebug( create );
383  }
384  else
385  {
386  ResetDebugWindow();
387  SetIsDebug( create );
388  delete m_DebugWindow;
389  }
390  }
391 
392  // ----------------------------------------------------------------------------------------
393  void SynchDebugStats()
394  {
395  if ( GetGame().IsServer() && GetGame().IsMultiplayer() )
396  {
397  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 );
398  GetGame().RPCSingleParam( m_Player, ERPCs.RPC_SOFT_SKILLS_STATS_SYNC, debug_stats, true, m_Player.GetIdentity() );
399  }
400  }
401 
402  // ----------------------------------------------------------------------------------------
403  void SetIsDebug( bool is_debug )
404  {
405  m_IsDebugMode = is_debug;
406  }
407 
408  // ----------------------------------------------------------------------------------------
409  bool IsDebug()
410  {
411  return m_IsDebugMode;
412  }
413 
414  // ----------------------------------------------------------------------------------------
415  void SetCoolDownValue( float cooldown_value )
416  {
417  m_CoolDownValue = cooldown_value;
418  }
419 
420  // ----------------------------------------------------------------------------------------
421  float GetCoolDownValue()
422  {
423  return m_CoolDownValue;
424  }
425 
426  // ----------------------------------------------------------------------------------------
427  float GetLastUAValue()
428  {
429  return m_LastUAValue;
430  }
431 
432  // ----------------------------------------------------------------------------------------
433  void SetLastUAValue( float last_ua_value )
434  {
435  m_LastUAValue = last_ua_value;
436  }
437 
438  // ----------------------------------------------------------------------------------------
439  void SetBonusBefore( bool is_cacomponent, float base_value)
440  {
441  if ( IsDebug() )
442  {
443  if ( is_cacomponent )
444  {
445  SetComponentBonusBefore(base_value);
446  }
447  else
448  {
449  SetGeneralBonusBefore(base_value);
450  }
451  }
452  }
453 
454  // ----------------------------------------------------------------------------------------
455  void SetBonusAfter( bool is_cacomponent, float adjusted_value )
456  {
457  if ( IsDebug() )
458  {
459  if ( is_cacomponent )
460  {
461  SetComponentBonusAfter(adjusted_value);
462  }
463  else
464  {
465  SetGeneralBonusAfter(adjusted_value);
466  }
467  }
468  }
469 
470  // ----------------------------------------------------------------------------------------
471  float GetComponentBonusBefore()
472  {
473  return m_ComponentBonusBefore;
474  }
475 
476  // ----------------------------------------------------------------------------------------
477  void SetComponentBonusBefore( float component_bonus_before )
478  {
479  m_ComponentBonusBefore = component_bonus_before;
480  }
481 
482  // ----------------------------------------------------------------------------------------
483  float GetComponentBonusAfter()
484  {
485  return m_ComponentBonusAfter;
486  }
487 
488  // ----------------------------------------------------------------------------------------
489  void SetComponentBonusAfter( float component_bonus_after )
490  {
491  m_ComponentBonusAfter = component_bonus_after;
492  }
493 
494  // ----------------------------------------------------------------------------------------
495  float GetGeneralBonusBefore()
496  {
497  return m_GeneralBonusBefore;
498  }
499 
500  // ----------------------------------------------------------------------------------------
501  void SetGeneralBonusBefore( float general_bonus_before )
502  {
503  m_GeneralBonusBefore = general_bonus_before;
504  }
505 
506  // ----------------------------------------------------------------------------------------
507  float GetGeneralBonusAfter()
508  {
509  return m_GeneralBonusAfter;
510  }
511 
512  // ----------------------------------------------------------------------------------------
513  void SetGeneralBonusAfter( float general_bonus_after )
514  {
515  m_GeneralBonusAfter = general_bonus_after;
516  }
517 
518  // ----------------------------------------------------------------------------------------
519  void StartSynchTimer()
520  {
521  SetIsDebug( true );
522  m_SynchTimer = new Timer;
523  m_SynchTimer.Run( 2, this, "SynchDebugStats", NULL, true );
524  }
525 
526  // ----------------------------------------------------------------------------------------
527  void StopSynchTimer()
528  {
529  SetIsDebug( false );
530 
531  if ( m_SynchTimer )
532  {
533  m_SynchTimer.Stop();
534  delete m_SynchTimer;
535  }
536  }
537 
538  // ----------------------------------------------------------------------------------------
539  void ResetDebugWindow()
540  {
541  SetSpecialtyLevel( 0 );
542  SetLastUAValue( 0 );
543  SetComponentBonusBefore( 0 );
544  SetComponentBonusAfter( 0 );
545  SetGeneralBonusBefore( 0 );
546  SetGeneralBonusAfter( 0 );
547  SetCoolDownValue( 0 );
548 
549  SynchSpecialtyLevel();
550  }
551 }
552 
553 // ----------------------------------------------------------------------------------------
554 // SoftSkillManagerDebug
555 // ----------------------------------------------------------------------------------------
556 
558 {
569 
570  void SoftSkillManagerDebug( SoftSkillsManager softskill_manager )
571  {
572  m_SoftSkillManager = softskill_manager;
573  m_PanelSoftSkills = GetGame().GetWorkspace().CreateWidgets("gui/layouts/debug/day_z_hud_debug_softskills.layout");
574 
575  GetGame().GetUpdateQueue(CALL_CATEGORY_SYSTEM).Insert(this.OnUpdate);
576 
577  Class.CastTo(SpecialtyTotal, m_PanelSoftSkills.FindWidget("SpecialtyTotal"));
578  Class.CastTo(SpecialtyChange, m_PanelSoftSkills.FindWidget("SpecialtyChange"));
579  Class.CastTo(ComponentBonusBefore, m_PanelSoftSkills.FindWidget("ComponentBonusBefore"));
580  Class.CastTo(ComponentBonusAfter, m_PanelSoftSkills.FindWidget("ComponentBonusAfter"));
581  Class.CastTo(GeneralBonusBefore, m_PanelSoftSkills.FindWidget("GeneralBonusBefore"));
582  Class.CastTo(GeneralBonusAfter, m_PanelSoftSkills.FindWidget("GeneralBonusAfter"));
583  Class.CastTo(CoolDown, m_PanelSoftSkills.FindWidget("CoolDown"));
584  Class.CastTo(IsCoolDown, m_PanelSoftSkills.FindWidget("IsCoolDown"));
585 
586  m_PanelSoftSkills.Show( true );
587  SpecialtyTotal.Show( true );
588  SpecialtyChange.Show( true );
589  ComponentBonusBefore.Show( true );
590  ComponentBonusAfter.Show( true );
591  GeneralBonusBefore.Show( true );
592  GeneralBonusAfter.Show( true );
593  CoolDown.Show( true );
594  IsCoolDown.Show( true );
595  }
596 
598  {
599  if ( GetGame() )
600  {
601  GetGame().GetUpdateQueue(CALL_CATEGORY_SYSTEM).Remove(this.OnUpdate);
602  }
603 
604  delete m_PanelSoftSkills;
605  }
606 
608  {
609  return m_SoftSkillManager;
610  }
611 
612  void OnUpdate()
613  {
614  if ( GetActiveSoftSkillManager().GetSoftSkillsPlayer().IsAlive() )
615  {
616  float speciality = GetActiveSoftSkillManager().GetSpecialtyLevel();
617  speciality = speciality * 100;
618  speciality = Math.Round( speciality );
619  speciality = speciality * 0.01;
620 
621  SpecialtyTotal.SetText( "Specialty level: " + speciality.ToString() );
622  SpecialtyChange.SetText( "Specialty change: " + GetActiveSoftSkillManager().GetLastUAValue() );
623  ComponentBonusBefore.SetText( "Component/craft default: " + GetActiveSoftSkillManager().GetComponentBonusBefore() );
624  ComponentBonusAfter.SetText( "Component/craft with bonus: " + GetActiveSoftSkillManager().GetComponentBonusAfter() );
625  GeneralBonusBefore.SetText( "General default: " + GetActiveSoftSkillManager().GetGeneralBonusBefore() );
626  GeneralBonusAfter.SetText( "General with bonus: " + GetActiveSoftSkillManager().GetGeneralBonusAfter() );
627  CoolDown.SetText( "CoolDown value: " + GetActiveSoftSkillManager().GetCoolDownValue() );
628  IsCoolDown.SetText( "Cooldown active: " + GetActiveSoftSkillManager().IsCoolDown() );
629  }
630  else
631  {
632  delete this;
633  }
634  }
635 
636 }
GetGame
proto native CGame GetGame()
CALL_CATEGORY_SYSTEM
const int CALL_CATEGORY_SYSTEM
Definition: tools.c:8
SpecialtyTotal
TextWidget SpecialtyTotal
Definition: softskillsmanager.c:561
ComponentBonusBefore
TextWidget ComponentBonusBefore
Definition: softskillsmanager.c:563
SoftSkillsManager
Definition: softskillsmanager.c:1
SpecialtyChange
TextWidget SpecialtyChange
Definition: softskillsmanager.c:562
IsCoolDown
TextWidget IsCoolDown
Definition: softskillsmanager.c:568
PlayerBase
Definition: playerbaseclient.c:1
GeneralBonusBefore
TextWidget GeneralBonusBefore
Definition: softskillsmanager.c:565
m_PanelSoftSkills
ref Widget m_PanelSoftSkills
Definition: softskillsmanager.c:560
m_IsActive
bool m_IsActive
Definition: modifierbase.c:20
TextWidget
Definition: enwidgets.c:219
m_SoftSkillManager
class SoftSkillsManager m_SoftSkillManager
OnUpdate
void OnUpdate()
Definition: softskillsmanager.c:612
m_Player
DayZPlayer m_Player
Definition: hand_events.c:42
~SoftSkillManagerDebug
void ~SoftSkillManagerDebug()
Definition: softskillsmanager.c:597
ComponentBonusAfter
TextWidget ComponentBonusAfter
Definition: softskillsmanager.c:564
GetActiveSoftSkillManager
SoftSkillsManager GetActiveSoftSkillManager()
Definition: softskillsmanager.c:607
ERPCs
ERPCs
Definition: erpcs.c:1
Timer
Definition: dayzplayerimplement.c:62
SoftSkillManagerDebug
void SoftSkillManagerDebug(SoftSkillsManager softskill_manager)
Definition: softskillsmanager.c:570
Widget
Definition: enwidgets.c:189
Math
Definition: enmath.c:6
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
GeneralBonusAfter
TextWidget GeneralBonusAfter
Definition: softskillsmanager.c:566
CoolDown
TextWidget CoolDown
Definition: softskillsmanager.c:567