DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
PluginLifespan.c
Go to the documentation of this file.
1//-----------------------------
2// LIFESPAN plugin
3//-----------------------------
4/*
5Lifespan 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
17enum LifeSpanState
18{
23 COUNT = 4,
24}
25
26class 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 //========================================
41 {
43 }
44
46 {
51 }
52
53//-----------------------------
54// Player load from config
55//-----------------------------
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 // NEW STATS API
225 if ( GetGame().IsMultiplayer() && GetGame().IsServer() )
226 {
227 float player_playtime = player.StatGet("playtime");
228 }
229 else
230 {
231 player_playtime = m_FakePlaytime;
232 }
233
234 float player_beard = player_playtime - player.GetLastShavedSeconds();
235 player_beard = player_beard / 60.0;
236
237 UpdateLifespanLevel( player, player_beard, force_update );
238 }
239 }
240
241 protected void UpdateLifespanLevel( PlayerBase player, float player_beard, bool force_update = false )
242 {
243 if ( !player.IsAlive() )
244 return;
245
246 if ( m_PlayerCurrentLevel.Contains(player) )
247 {
248 LifespanLevel current_level = m_PlayerCurrentLevel.Get( player );
249
250 if ( player_beard > current_level.GetThreshold() || force_update )
251 {
252 LifespanLevel next_level = GetLifespanLevel( player.GetPlayerClass(), player_beard );
253
254 if ( next_level != NULL )
255 {
256 SetPlayerLifespanLevel( player, next_level );
257 m_PlayerCurrentLevel.Set( player, next_level );
258 }
259 }
260 }
261 else
262 {
263 if ( m_LifespanLevels.Contains( player.GetPlayerClass() ) )
264 {
265 LifespanLevel level = GetLifespanLevel( player.GetPlayerClass(), player_beard );
266
267 if ( level != NULL )
268 {
269 SetPlayerLifespanLevel( player, level );
270 m_PlayerCurrentLevel.Set( player, level );
271 }
272 }
273 }
274 }
275
276 protected LifespanLevel GetLifespanLevel( string player_class, float age = 0 )
277 {
278 array<ref LifespanLevel> lifespan_levels = m_LifespanLevels.Get( player_class );
279
280 if ( lifespan_levels != NULL )
281 {
282 for ( int i = lifespan_levels.Count() - 1; i >= 0; i-- )
283 {
284 LifespanLevel level = lifespan_levels.Get( i );
285
286 if ( age >= level.GetThreshold() )
287 {
288 return level;
289 }
290 }
291 }
292
293 return NULL;
294 }
295
296 protected void SetPlayerLifespanLevel( PlayerBase player, LifespanLevel level )
297 {
298 if (player.m_CorpseState != 0)
299 return;
300 int slot_id = InventorySlots.GetSlotIdFromString("Head");
301 EntityAI players_head = player.GetInventory().FindPlaceholderForSlot( slot_id );
302
303 if ( players_head )
304 {
305 switch (level.GetLevel())
306 {
307 case LifeSpanState.BEARD_NONE:
308 {
309 players_head.SetObjectTexture( 0, "");
310 players_head.SetObjectMaterial( 0, "");
311
312 player.SetFaceTexture( level.GetTextureName() );
313 player.SetFaceMaterial( level.GetMaterialName() );
314
315 player.SetLifeSpanStateVisible(LifeSpanState.BEARD_NONE);
316 //Print("LifeSpanState.BEARD_NONE");
317 break;
318 }
319 case LifeSpanState.BEARD_MEDIUM:
320 {
321 players_head.SetObjectTexture( 0, "");
322 players_head.SetObjectMaterial( 0, "");
323
324 player.SetFaceTexture( level.GetTextureName() );
325 player.SetFaceMaterial( level.GetMaterialName() );
326
327 player.SetLifeSpanStateVisible(LifeSpanState.BEARD_MEDIUM);
328 //Print("LifeSpanState.BEARD_MEDIUM");
329 break;
330 }
331
332 case LifeSpanState.BEARD_LARGE:
333 {
334 players_head.SetObjectTexture( 0, "");
335 players_head.SetObjectMaterial( 0, "");
336
337 player.SetFaceTexture( level.GetTextureName() );
338 player.SetFaceMaterial( level.GetMaterialName() );
339
340 player.SetLifeSpanStateVisible(LifeSpanState.BEARD_LARGE);
341 //Print("LifeSpanState.BEARD_LARGE");
342 break;
343 }
344
345 case LifeSpanState.BEARD_EXTRA:
346 {
347 players_head.SetObjectTexture( 0, level.GetTextureName() );
348 players_head.SetObjectMaterial( 0, level.GetMaterialName() );
349
350 array< ref LifespanLevel> lifespan_levels = m_LifespanLevels.Get( player.GetPlayerClass() );
351 LifespanLevel prev_level = lifespan_levels.Get(LifeSpanState.BEARD_LARGE);
352
353 player.SetFaceTexture( prev_level.GetTextureName() );
354 player.SetFaceMaterial( prev_level.GetMaterialName() );
355
356 player.SetLifeSpanStateVisible(LifeSpanState.BEARD_EXTRA);
357 //Print("LifeSpanState.BEARD_EXTRA");
358 break;
359 }
360
361 default:
362 {
363 Print("Lifespan state missing");
364 break;
365 }
366 }
367 }
368 }
369
370 void SynchBeardVisual( PlayerBase player, int state )
371 {
372 float player_beard;
373
374 switch (state)
375 {
376 case LifeSpanState.BEARD_NONE:
377 {
378 // first out of 4 states
379 player_beard = LIFESPAN_MIN;
380 break;
381 }
382 case LifeSpanState.BEARD_MEDIUM:
383 {
384 // second out of 4 states
385 player_beard = LIFESPAN_MAX/3;
386 break;
387 }
388
389 case LifeSpanState.BEARD_LARGE:
390 {
391 // third out of 4 states
392 player_beard = LIFESPAN_MAX/1.5;
393 break;
394 }
395
396 case LifeSpanState.BEARD_EXTRA:
397 {
398 // fourth out of 4 states
399 player_beard = LIFESPAN_MAX;
400 break;
401 }
402
403 default:
404 {
405 Print("Lifespan state missing");
406 break;
407 }
408 }
409
410 UpdateLifespanLevel( player, player_beard, true );
411 }
412
413//-----------------------------
414// Bloody hands
415//-----------------------------
416
418 {
419 if ( CanMakeHandsBloody( player ) )
420 {
421 SetBloodyHandsEx( player, type );
422 }
423 }
424
425 void UpdateBloodyHandsVisibility( PlayerBase player, bool show )
426 {
427 if ( CanMakeHandsBloody( player ) )
428 {
429 SetBloodyHands( player, show );
430 }
431 }
432
433 void SynchBloodyHandsVisual( PlayerBase player, bool has_bloody_hands )
434 {
435 UpdateBloodyHandsVisibility( player, has_bloody_hands );
436 }
437
438
440 {
441 player.SetBloodyHandsEx( type );
442
443 if ( type )
444 {
446 }
447 else
448 {
450 }
451
452 }
453
454 void SetBloodyHands( PlayerBase player, bool show )
455 {
456 player.SetBloodyHands( show );
457
458 if ( show )
459 {
461 }
462 else
463 {
465 }
466 }
467
468 protected void SetHandsMaterial( PlayerBase player, int material_type )
469 {
470 string player_class = player.GetPlayerClass();
471 int slot_id;
472 EntityAI eai;
473
474 if ( m_BloodyHands.Contains(player_class))
475 {
476 slot_id = InventorySlots.GetSlotIdFromString("Gloves");
477 eai = player.GetInventory().FindPlaceholderForSlot( slot_id );
478
479 if (eai)
480 {
481 eai.SetObjectMaterial( 0, m_BloodyHands.Get(player_class).GetMaterial(material_type) );
482 }
483 }
484 else
485 {
486 //Print("Error! Player class <" + player_class + "> does not contain valid configuration for bloody hands!");
487 }
488 }
489
491 {
492 return !player.FindAttachmentBySlotName( "Gloves" ) );
493 }
494
495//-----------------------------
496// Blood type in HUD
497//-----------------------------
498
499 void UpdateBloodTypeVisibility( PlayerBase player, bool show )
500 {
501 player.SetBloodTypeVisible( show );
502
503 if ( player.m_Hud )
504 {
505 player.m_Hud.UpdateBloodName();
506 }
507 }
508
509 void UpdateBloodType( PlayerBase player, int blood_type )
510 {
511 player.SetBloodType( blood_type );
512 }
513
514 void SynchShowBloodTypeVisual( PlayerBase player, bool show )
515 {
516 UpdateBloodTypeVisibility( player, show );
517 }
518
519 void SynchShowBloodType( PlayerBase player, int blood_type )
520 {
521 UpdateBloodType( player, blood_type );
522 }
523
524//-----------------------------
525// Getters for other systems
526//-----------------------------
527
529 {
530 if ( m_PlayerCurrentLevel.Contains(player) )
531 {
532 LifespanLevel current_level = m_PlayerCurrentLevel.Get( player );
533 return current_level.GetTextureName();
534 }
535 return "";
536 }
537
539 {
540 if ( m_PlayerCurrentLevel.Contains(player) )
541 {
542 LifespanLevel current_level = m_PlayerCurrentLevel.Get( player );
543 return current_level.GetMaterialName();
544 }
545 return "";
546 }
547}
protected int m_FakePlaytime
void UpdateBloodyHandsVisibility(PlayerBase player, bool show)
void SynchShowBloodTypeVisual(PlayerBase player, bool show)
protected void SetPlayerLifespanLevel(PlayerBase player, LifespanLevel level)
enum eBloodyHandsTypes COUNT
void UpdateLifespan(PlayerBase player, bool force_update=false)
void SynchBloodyHandsVisual(PlayerBase player, bool has_bloody_hands)
void SetBloodyHandsEx(PlayerBase player, eBloodyHandsTypes type)
protected ref map< string, ref BloodyHands > m_BloodyHands
void SynchLifespanVisual(PlayerBase player, int beard_state_visible, bool bloody_hands_visible, bool blood_type_visible, int blood_type)
void SynchBeardVisual(PlayerBase player, int state)
void UpdateBloodTypeVisibility(PlayerBase player, bool show)
void UpdateBloodType(PlayerBase player, int blood_type)
void PluginLifespan()
static protected const int LIFESPAN_MAX
eBloodyHandsTypes
@ JUST_BLOOD
@ CLEAN
@ LAST_INDEX
@ SALMONELA
enum eBloodyHandsTypes BEARD_MEDIUM
enum eBloodyHandsTypes BEARD_NONE
void ChangeFakePlaytime(PlayerBase player, int change)
string GetCurrentHeadMaterial(PlayerBase player)
enum eBloodyHandsTypes BEARD_LARGE
enum eBloodyHandsTypes LIFESPAN_MIN
string GetCurrentHeadTexture(PlayerBase player)
protected void UpdateLifespanLevel(PlayerBase player, float player_beard, bool force_update=false)
enum eBloodyHandsTypes BEARD_EXTRA
protected ref map< PlayerBase, ref LifespanLevel > m_PlayerCurrentLevel
protected LifespanLevel GetLifespanLevel(string player_class, float age=0)
protected ref map< PlayerBase, int > m_BloodType
void SetBloodyHands(PlayerBase player, bool show)
bool CanMakeHandsBloody(PlayerBase player)
protected void SetHandsMaterial(PlayerBase player, int material_type)
void UpdateBloodyHandsVisibilityEx(PlayerBase player, eBloodyHandsTypes type)
protected ref map< string, ref array< ref LifespanLevel > > m_LifespanLevels
void SynchShowBloodType(PlayerBase player, int blood_type)
PluginBase GetPlugin(typename plugin_type)
const int MATERIAL_TYPE_BLOODY
Definition BloodyHands.c:6
const int MATERIAL_TYPE_NORMAL
Definition BloodyHands.c:5
proto native int ConfigGetChildrenCount(string path)
Get count of subclasses in config class on path.
proto bool ConfigGetText(string path, out string value)
Get string value from config on path.
proto native void ConfigGetTextArray(string path, out TStringArray values)
Get array of strings from config on path.
proto bool ConfigGetChildName(string path, int index, out string name)
Get name of subclass in config class on path.
provides access to slot configuration
static proto native int GetSlotIdFromString(string slot_name)
converts string to slot_id
float GetThreshold()
string GetMaterialName()
string GetTextureName()
static PluginCameraTools GetInstance()
proto native CGame GetGame()
proto void Print(void var)
Prints content of variable to console/log.
array< string > TStringArray
Definition EnScript.c:685