Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
pluginlifespan.c
Go to the documentation of this file.
1 //-----------------------------
2 // LIFESPAN plugin
3 //-----------------------------
4 /*
5 Lifespan plugin handles player facial hair, bloody hands, blood type in HUD
6 */
7 
9 {
10  CLEAN = 0,//clean needs to be 0
13  //--- ONLY LAST_INDEX BELLOW !!!
15 }
16 
17 enum LifeSpanState
18 {
19  BEARD_NONE = 0,
20  BEARD_MEDIUM = 1,
21  BEARD_LARGE = 2,
22  BEARD_EXTRA = 3,
23  COUNT = 4,
24 }
25 
26 class PluginLifespan extends PluginBase
27 {
28  protected static const int LIFESPAN_MIN = 0;
29  protected static const int LIFESPAN_MAX = 240; // value in minutes when player achieved maximum age in order to have full beard
30  protected int m_FakePlaytime;
31 
36 
37  //========================================
38  // GetInstance
39  //========================================
40  static PluginLifespan GetInstance()
41  {
42  return PluginLifespan.Cast( GetPlugin(PluginLifespan) );
43  }
44 
46  {
47  LoadFromCfg();
50  m_FakePlaytime = 0;
51  }
52 
53 //-----------------------------
54 // Player load from config
55 //-----------------------------
56  void LoadFromCfg()
57  {
60 
61  const int LIFESPAN_LENGTH = LIFESPAN_MAX - LIFESPAN_MIN;
62 
63  //cfg_name = 'CfgVehicles'
64  // config_name = 'CfgVehicles'
65  //cfg_class_count = 2348
66  // config_count = 2348
67  //cfg_class_name = 'SurvivorMale_Base'
68  // survivor_name = 'SurvivorMale_Base'
69  //cfg_class_fullname = 'CfgVehicles SurvivorMale_Base'
70  // survivor_path = 'CfgVehicles SurvivorMale_Base'
71  //cfg_class_member_count = 10
72  // survivor_lifespan_count = 10
73  //cfg_class_member_name = 'Lifespan'
74  // survivor_lifespan_name = 'Lifespan'
75  //cfg_class_member_fullname = 'CfgVehicles SurvivorMale_Base Lifespan';
76  // survivor_lifespan_path = 'CfgVehicles SurvivorMale_Base Lifespan';
77  //cfg_class_member_member_count = 1
78  // survivor_lifespan_beard_count = 1
79  //cfg_class_member_member_name = 'Beard'
80  // survivor_lifespan_beard_name = 'Beard'
81  //cfg_class_member_member_fullname = 'CfgVehicles SurvivorMale_Base Lifespan Beard'
82  // survivor_lifespan_beard_path = 'CfgVehicles SurvivorMale_Base Lifespan Beard'
83  //cfg_class_member_member_variable_name = 'mat'
84  // survivor_lifespan_beard_material_name = 'mat'
85  //cfg_class_member_member_variable_fullname = 'CfgVehicles SurvivorMale_Base Lifespan Beard mat'
86  // survivor_lifespan_beard_material_path = 'CfgVehicles SurvivorMale_Base Lifespan Beard mat'
87 
88  string config_name = "CfgVehicles";
89  int config_count = GetGame().ConfigGetChildrenCount( config_name );
90 
91  int i, j, k, l, m;
92 
93  //Print( "config_count: " + config_count );
94  for ( i = 0; i < config_count; i++ )
95  {
96  string survivor_name = "";
97  GetGame().ConfigGetChildName( config_name, i, survivor_name );
98 
99  if ( survivor_name != "" && survivor_name != "access" )
100  {
101  if ( GetGame().IsKindOf(survivor_name, "SurvivorMale_Base") || GetGame().IsKindOf(survivor_name, "SurvivorFemale_Base") )
102  {
103  string survivor_path = config_name + " " + survivor_name;
104  int survivor_lifespan_count = GetGame().ConfigGetChildrenCount( survivor_path );
105 
106  //Print( "survivor_path: " + survivor_path );
107  for ( j = 0; j < survivor_lifespan_count; j++ )
108  {
109  string survivor_lifespan_name = "";
110  GetGame().ConfigGetChildName( survivor_path, j, survivor_lifespan_name );
111 
112  string survivor_lifespan_path = survivor_path + " " + survivor_lifespan_name;
113 
114  if ( survivor_lifespan_name == "Lifespan" )
115  {
116  int survivor_lifespan_beard_count = GetGame().ConfigGetChildrenCount( survivor_lifespan_path );
117 
118  for ( k = 0; k < survivor_lifespan_beard_count; k++ )
119  {
120  string survivor_lifespan_beard_name = "";
121  GetGame().ConfigGetChildName( survivor_lifespan_path, k, survivor_lifespan_beard_name );
122 
123  string survivor_lifespan_beard_path = survivor_lifespan_path + " " + survivor_lifespan_beard_name;
124 
125  if ( survivor_lifespan_beard_name == "Beard" )
126  {
127  TStringArray materials = new TStringArray;
128  int cfg_class_member_member_variable_count = GetGame().ConfigGetChildrenCount( survivor_lifespan_beard_path );
129 
130  for ( l = 0; l < cfg_class_member_member_variable_count; l++ )
131  {
132  string survivor_lifespan_beard_material_name = "";
133  GetGame().ConfigGetChildName( survivor_lifespan_beard_path, l, survivor_lifespan_beard_material_name );
134  string survivor_lifespan_beard_material_path = survivor_lifespan_beard_path + " " + survivor_lifespan_beard_material_name;
135 
136  if ( survivor_lifespan_beard_material_name == "mat" )
137  {
138  GetGame().ConfigGetTextArray( survivor_lifespan_beard_material_path, materials );
139 
141 
142  int level_count = materials.Count() / 2;
143  for ( m = 0; m < level_count; m++ )
144  {
145  int tex = m*2;
146  int mat = ((m*2)+1);
147  if ( mat < materials.Count() )
148  {
149  float threshold = (((float)m / (float)(level_count - 1)) * (float)LIFESPAN_LENGTH) + (float)LIFESPAN_MIN;
150  lifespan_levels.Insert( new LifespanLevel(m, threshold, materials.Get(tex), materials.Get(mat)) );
151  //Print("material a textura a threshold: " + materials.Get(tex) + " " + materials.Get(mat) + " " + threshold );
152  }
153  }
154 
155  if ( lifespan_levels.Count() > 0 )
156  {
157  m_LifespanLevels.Set( survivor_name, lifespan_levels );
158  }
159  }
160  }
161  }
162  }
163  }
164  else if ( survivor_lifespan_name == "BloodyHands" )
165  {
166  string bloody_material, normal_material;
167  string path_normal = survivor_lifespan_path + " mat_normal";
168  string path_bloody = survivor_lifespan_path + " mat_blood";
169  GetGame().ConfigGetText(path_normal, normal_material);
170  GetGame().ConfigGetText(path_bloody, bloody_material);
171  m_BloodyHands.Set( survivor_name, new BloodyHands(normal_material, bloody_material) );
172  }
173  }
174  }
175  }
176  }
177 
178  /*for ( int cl = 0; cl < m_LifespanLevels.Count(); cl++ )
179  {
180  string class_name = m_LifespanLevels.GetKey( cl );
181  array<ref LifespanLevel> levels = m_LifespanLevels.GetElement( cl );
182 
183  Print( class_name );
184 
185  for ( int ll = 0; ll < levels.Count(); ll++)
186  {
187  Print( "lvl: " + levels.Get( ll ).GetLevel() );
188  Print( "treshold: " + levels.Get( ll ).GetThreshold() );
189  Print( "texture: " + levels.Get( ll ).GetTextureName() );
190  Print( "material: " + levels.Get( ll ).GetMaterialName() );
191  }
192  }*/
193  }
194 
195 //-----------------------------
196 // Support functions
197 //-----------------------------
198 
199  void SynchLifespanVisual( PlayerBase player, int beard_state_visible, bool bloody_hands_visible, bool blood_type_visible, int blood_type )
200  {
201  SynchBeardVisual( player, beard_state_visible );
202  SynchBloodyHandsVisual( player, bloody_hands_visible );
203  SynchShowBloodTypeVisual( player, blood_type_visible );
204  SynchShowBloodType( player, blood_type );
205  }
206 
207  void ChangeFakePlaytime( PlayerBase player, int change )
208  {
209  if ( !GetGame().IsMultiplayer() )
210  {
211  m_FakePlaytime = change;
212  UpdateLifespan( player, true );
213  }
214  }
215 
216 //-----------------------------
217 // Facial hair
218 //-----------------------------
219 
220  void UpdateLifespan(PlayerBase player, bool force_update = false)
221  {
222  if (player != null)
223  {
224  float playerPlaytime = m_FakePlaytime;
225 
226  if (GetGame().IsMultiplayer() && GetGame().IsServer())
227  playerPlaytime = player.StatGet(AnalyticsManagerServer.STAT_PLAYTIME);
228 
229  float playerBeard = playerPlaytime - player.GetLastShavedSeconds();
230  playerBeard = playerBeard / 60.0;
231 
232  UpdateLifespanLevel(player, playerBeard, force_update);
233  }
234  }
235 
236  protected void UpdateLifespanLevel( PlayerBase player, float player_beard, bool force_update = false )
237  {
238  if ( !player.IsAlive() )
239  return;
240 
241  if ( m_PlayerCurrentLevel.Contains(player) )
242  {
243  LifespanLevel current_level = m_PlayerCurrentLevel.Get( player );
244 
245  if ( player_beard > current_level.GetThreshold() || force_update )
246  {
247  LifespanLevel next_level = GetLifespanLevel( player.GetPlayerClass(), player_beard );
248 
249  if ( next_level != NULL )
250  {
251  SetPlayerLifespanLevel( player, next_level );
252  m_PlayerCurrentLevel.Set( player, next_level );
253  }
254  }
255  }
256  else
257  {
258  if ( m_LifespanLevels.Contains( player.GetPlayerClass() ) )
259  {
260  LifespanLevel level = GetLifespanLevel( player.GetPlayerClass(), player_beard );
261 
262  if ( level != NULL )
263  {
264  SetPlayerLifespanLevel( player, level );
265  m_PlayerCurrentLevel.Set( player, level );
266  }
267  }
268  }
269  }
270 
271  protected LifespanLevel GetLifespanLevel( string player_class, float age = 0 )
272  {
273  array<ref LifespanLevel> lifespan_levels = m_LifespanLevels.Get( player_class );
274 
275  if ( lifespan_levels != NULL )
276  {
277  for ( int i = lifespan_levels.Count() - 1; i >= 0; i-- )
278  {
279  LifespanLevel level = lifespan_levels.Get( i );
280 
281  if ( age >= level.GetThreshold() )
282  {
283  return level;
284  }
285  }
286  }
287 
288  return NULL;
289  }
290 
291  protected void SetPlayerLifespanLevel( PlayerBase player, LifespanLevel level )
292  {
293  if (player.m_CorpseState != 0)
294  return;
295  int slot_id = InventorySlots.GetSlotIdFromString("Head");
296  EntityAI players_head = player.GetInventory().FindPlaceholderForSlot( slot_id );
297 
298  if ( players_head )
299  {
300  switch (level.GetLevel())
301  {
302  case LifeSpanState.BEARD_NONE:
303  {
304  players_head.SetObjectTexture( 0, "");
305  players_head.SetObjectMaterial( 0, "");
306 
307  player.SetFaceTexture( level.GetTextureName() );
308  player.SetFaceMaterial( level.GetMaterialName() );
309 
310  player.SetLifeSpanStateVisible(LifeSpanState.BEARD_NONE);
311  //Print("LifeSpanState.BEARD_NONE");
312  break;
313  }
314  case LifeSpanState.BEARD_MEDIUM:
315  {
316  players_head.SetObjectTexture( 0, "");
317  players_head.SetObjectMaterial( 0, "");
318 
319  player.SetFaceTexture( level.GetTextureName() );
320  player.SetFaceMaterial( level.GetMaterialName() );
321 
322  player.SetLifeSpanStateVisible(LifeSpanState.BEARD_MEDIUM);
323  //Print("LifeSpanState.BEARD_MEDIUM");
324  break;
325  }
326 
327  case LifeSpanState.BEARD_LARGE:
328  {
329  players_head.SetObjectTexture( 0, "");
330  players_head.SetObjectMaterial( 0, "");
331 
332  player.SetFaceTexture( level.GetTextureName() );
333  player.SetFaceMaterial( level.GetMaterialName() );
334 
335  player.SetLifeSpanStateVisible(LifeSpanState.BEARD_LARGE);
336  //Print("LifeSpanState.BEARD_LARGE");
337  break;
338  }
339 
340  case LifeSpanState.BEARD_EXTRA:
341  {
342  players_head.SetObjectTexture( 0, level.GetTextureName() );
343  players_head.SetObjectMaterial( 0, level.GetMaterialName() );
344 
345  array< ref LifespanLevel> lifespan_levels = m_LifespanLevels.Get( player.GetPlayerClass() );
346  LifespanLevel prev_level = lifespan_levels.Get(LifeSpanState.BEARD_LARGE);
347 
348  player.SetFaceTexture( prev_level.GetTextureName() );
349  player.SetFaceMaterial( prev_level.GetMaterialName() );
350 
351  player.SetLifeSpanStateVisible(LifeSpanState.BEARD_EXTRA);
352  //Print("LifeSpanState.BEARD_EXTRA");
353  break;
354  }
355 
356  default:
357  {
358  Print("Lifespan state missing");
359  break;
360  }
361  }
362  }
363  }
364 
365  void SynchBeardVisual( PlayerBase player, int state )
366  {
367  float player_beard;
368 
369  switch (state)
370  {
371  case LifeSpanState.BEARD_NONE:
372  {
373  // first out of 4 states
374  player_beard = LIFESPAN_MIN;
375  break;
376  }
377  case LifeSpanState.BEARD_MEDIUM:
378  {
379  // second out of 4 states
380  player_beard = LIFESPAN_MAX/3;
381  break;
382  }
383 
384  case LifeSpanState.BEARD_LARGE:
385  {
386  // third out of 4 states
387  player_beard = LIFESPAN_MAX/1.5;
388  break;
389  }
390 
391  case LifeSpanState.BEARD_EXTRA:
392  {
393  // fourth out of 4 states
394  player_beard = LIFESPAN_MAX;
395  break;
396  }
397 
398  default:
399  {
400  Print("Lifespan state missing");
401  break;
402  }
403  }
404 
405  UpdateLifespanLevel( player, player_beard, true );
406  }
407 
408 //-----------------------------
409 // Bloody hands
410 //-----------------------------
411 
413  {
414  if ( CanMakeHandsBloody( player ) )
415  {
416  SetBloodyHandsEx( player, type );
417  }
418  }
419 
420  void UpdateBloodyHandsVisibility( PlayerBase player, bool show )
421  {
422  if ( CanMakeHandsBloody( player ) )
423  {
424  SetBloodyHands( player, show );
425  }
426  }
427 
428  void SynchBloodyHandsVisual( PlayerBase player, bool has_bloody_hands )
429  {
430  UpdateBloodyHandsVisibility( player, has_bloody_hands );
431  }
432 
433 
435  {
436  player.SetBloodyHandsEx( type );
437 
438  if ( type )
439  {
440  SetHandsMaterial( player, BloodyHands.MATERIAL_TYPE_BLOODY );
441  }
442  else
443  {
444  SetHandsMaterial( player, BloodyHands.MATERIAL_TYPE_NORMAL );
445  }
446 
447  }
448 
449  void SetBloodyHands( PlayerBase player, bool show )
450  {
451  player.SetBloodyHands( show );
452 
453  if ( show )
454  {
455  SetHandsMaterial( player, BloodyHands.MATERIAL_TYPE_BLOODY );
456  }
457  else
458  {
459  SetHandsMaterial( player, BloodyHands.MATERIAL_TYPE_NORMAL );
460  }
461  }
462 
463  protected void SetHandsMaterial( PlayerBase player, int material_type )
464  {
465  string player_class = player.GetPlayerClass();
466  int slot_id;
467  EntityAI eai;
468 
469  if ( m_BloodyHands.Contains(player_class))
470  {
471  slot_id = InventorySlots.GetSlotIdFromString("Gloves");
472  eai = player.GetInventory().FindPlaceholderForSlot( slot_id );
473 
474  if (eai)
475  {
476  eai.SetObjectMaterial( 0, m_BloodyHands.Get(player_class).GetMaterial(material_type) );
477  }
478  }
479  else
480  {
481  //Print("Error! Player class <" + player_class + "> does not contain valid configuration for bloody hands!");
482  }
483  }
484 
486  {
487  return !player.FindAttachmentBySlotName( "Gloves" ) );
488  }
489 
490 //-----------------------------
491 // Blood type in HUD
492 //-----------------------------
493 
494  void UpdateBloodTypeVisibility( PlayerBase player, bool show )
495  {
496  player.SetBloodTypeVisible( show );
497 
498  if ( player.m_Hud )
499  {
500  player.m_Hud.UpdateBloodName();
501  }
502  }
503 
504  void UpdateBloodType( PlayerBase player, int blood_type )
505  {
506  player.SetBloodType( blood_type );
507  }
508 
509  void SynchShowBloodTypeVisual( PlayerBase player, bool show )
510  {
511  UpdateBloodTypeVisibility( player, show );
512  }
513 
514  void SynchShowBloodType( PlayerBase player, int blood_type )
515  {
516  UpdateBloodType( player, blood_type );
517  }
518 
519 //-----------------------------
520 // Getters for other systems
521 //-----------------------------
522 
524  {
525  if ( m_PlayerCurrentLevel.Contains(player) )
526  {
527  LifespanLevel current_level = m_PlayerCurrentLevel.Get( player );
528  return current_level.GetTextureName();
529  }
530  return "";
531  }
532 
534  {
535  if ( m_PlayerCurrentLevel.Contains(player) )
536  {
537  LifespanLevel current_level = m_PlayerCurrentLevel.Get( player );
538  return current_level.GetMaterialName();
539  }
540  return "";
541  }
542 }
GetCurrentHeadTexture
string GetCurrentHeadTexture(PlayerBase player)
Definition: pluginlifespan.c:523
GetGame
proto native CGame GetGame()
CLEAN
@ CLEAN
Definition: pluginlifespan.c:10
SetBloodyHands
void SetBloodyHands(PlayerBase player, bool show)
Definition: pluginlifespan.c:449
GetCurrentHeadMaterial
string GetCurrentHeadMaterial(PlayerBase player)
Definition: pluginlifespan.c:533
UpdateBloodType
void UpdateBloodType(PlayerBase player, int blood_type)
Definition: pluginlifespan.c:504
InventorySlots
provides access to slot configuration
Definition: inventoryslots.c:5
TStringArray
array< string > TStringArray
Definition: enscript.c:685
AnalyticsManagerServer
Definition: analyticsmanagerserver.c:1
SetPlayerLifespanLevel
protected void SetPlayerLifespanLevel(PlayerBase player, LifespanLevel level)
Definition: pluginlifespan.c:291
m_FakePlaytime
protected int m_FakePlaytime
Definition: pluginlifespan.c:30
eBloodyHandsTypes
eBloodyHandsTypes
Definition: pluginlifespan.c:8
JUST_BLOOD
@ JUST_BLOOD
Definition: pluginlifespan.c:12
Print
proto void Print(void var)
Prints content of variable to console/log.
m_BloodyHands
protected ref map< string, ref BloodyHands > m_BloodyHands
Definition: pluginlifespan.c:34
PluginBase
Definition: pluginadminlog.c:1
UpdateLifespanLevel
protected void UpdateLifespanLevel(PlayerBase player, float player_beard, bool force_update=false)
Definition: pluginlifespan.c:236
SynchBeardVisual
void SynchBeardVisual(PlayerBase player, int state)
Definition: pluginlifespan.c:365
m_BloodType
protected ref map< PlayerBase, int > m_BloodType
Definition: pluginlifespan.c:35
GetPlugin
PluginBase GetPlugin(typename plugin_type)
Definition: pluginmanager.c:316
SetBloodyHandsEx
void SetBloodyHandsEx(PlayerBase player, eBloodyHandsTypes type)
Definition: pluginlifespan.c:434
m_LifespanLevels
protected ref map< string, ref array< ref LifespanLevel > > m_LifespanLevels
Definition: pluginlifespan.c:33
PlayerBase
Definition: playerbaseclient.c:1
map
map
Definition: controlsxboxnew.c:3
SynchBloodyHandsVisual
void SynchBloodyHandsVisual(PlayerBase player, bool has_bloody_hands)
Definition: pluginlifespan.c:428
ChangeFakePlaytime
void ChangeFakePlaytime(PlayerBase player, int change)
Definition: pluginlifespan.c:207
PluginLifespan
void PluginLifespan()
Definition: pluginlifespan.c:45
SynchShowBloodTypeVisual
void SynchShowBloodTypeVisual(PlayerBase player, bool show)
Definition: pluginlifespan.c:509
UpdateBloodyHandsVisibilityEx
void UpdateBloodyHandsVisibilityEx(PlayerBase player, eBloodyHandsTypes type)
Definition: pluginlifespan.c:412
CanMakeHandsBloody
bool CanMakeHandsBloody(PlayerBase player)
Definition: pluginlifespan.c:485
LAST_INDEX
@ LAST_INDEX
Definition: pluginlifespan.c:14
LoadFromCfg
void LoadFromCfg()
Definition: pluginlifespan.c:56
BloodyHands
Definition: bloodyhands.c:1
BEARD_EXTRA
enum eBloodyHandsTypes BEARD_EXTRA
GetLifespanLevel
protected LifespanLevel GetLifespanLevel(string player_class, float age=0)
Definition: pluginlifespan.c:271
SynchLifespanVisual
void SynchLifespanVisual(PlayerBase player, int beard_state_visible, bool bloody_hands_visible, bool blood_type_visible, int blood_type)
Definition: pluginlifespan.c:199
UpdateBloodyHandsVisibility
void UpdateBloodyHandsVisibility(PlayerBase player, bool show)
Definition: pluginlifespan.c:420
array< string >
m_PlayerCurrentLevel
protected ref map< PlayerBase, ref LifespanLevel > m_PlayerCurrentLevel
Definition: pluginlifespan.c:32
UpdateBloodTypeVisibility
void UpdateBloodTypeVisibility(PlayerBase player, bool show)
Definition: pluginlifespan.c:494
UpdateLifespan
void UpdateLifespan(PlayerBase player, bool force_update=false)
Definition: pluginlifespan.c:220
COUNT
enum eBloodyHandsTypes COUNT
SALMONELA
@ SALMONELA
Definition: pluginlifespan.c:11
LIFESPAN_MIN
enum eBloodyHandsTypes LIFESPAN_MIN
LifespanLevel
Definition: lifespanlevel.c:1
SetHandsMaterial
protected void SetHandsMaterial(PlayerBase player, int material_type)
Definition: pluginlifespan.c:463
BEARD_NONE
enum eBloodyHandsTypes BEARD_NONE
BEARD_MEDIUM
enum eBloodyHandsTypes BEARD_MEDIUM
BEARD_LARGE
enum eBloodyHandsTypes BEARD_LARGE
SynchShowBloodType
void SynchShowBloodType(PlayerBase player, int blood_type)
Definition: pluginlifespan.c:514
EntityAI
Definition: building.c:5
float
Definition: enconvert.c:96