12 float m_LifetimeStart;
13 float m_LifetimeEnd = -1;
14 float m_FadeOutTime = -1;
15 float m_FadeInTime = -1;
19 float m_BrightnessPulse;
20 float m_BrightnessPulseSpeed;
21 float m_BrightnessPulseAmplitudeMax;
22 float m_BrightnessPulseAmplitudeMin;
23 float m_BrightnessTarget;
24 float m_BrightnessSpeedOfChange = 1;
25 float m_RadiusSpeedOfChange = 1;
26 float m_OptimizeShadowsRadius = 0;
28 float m_DancingShadowsAmplitude;
29 float m_DancingShadowsSpeed;
31 float m_BlinkingSpeed;
33 bool m_IsDebugEnabled =
false;
38 vector m_DancingShadowsLocalPos;
42 static ref set<ScriptedLightBase> m_NightTimeOnlyLights =
new set<ScriptedLightBase>();
47 m_LifetimeStart =
GetGame().GetTime();
55 if (m_NightTimeOnlyLights)
57 int index = m_NightTimeOnlyLights.Find(
this);
60 m_NightTimeOnlyLights.Remove(index);
67 if (!IsVisibleDuringDaylight())
70 if (player && player.m_UndergroundPresence)
71 SetVisibleDuringDaylight(
true);
72 m_NightTimeOnlyLights.Insert(
this);
76 override bool IsScriptedLight()
87 item.GetInventory().GetCurrentInventoryLocation( il );
93 else if (il.GetSlot() != -1)
101 private void UpdateLightMode(
string slotName);
104 private void DeleteLightWithDelay()
113 m_DeleteTimer.Run( 0.03 ,
this,
"DeleteLightNow", NULL,
true);
119 private void DeleteLightNow()
125 void AttachOnObject(
Object parent,
vector local_pos =
"0 0 0",
vector local_ori =
"0 0 0")
147 SetOrientation(local_ori);
149 parent.AddChild(
this, -1);
160 void AttachOnMemoryPoint(
Object parent,
string memory_point_start,
string memory_point_target =
"")
162 if (parent.MemoryPointExists(memory_point_start))
164 m_LocalPos = parent.GetMemoryPointPos(memory_point_start);
167 if (memory_point_target !=
"" )
169 if (parent.MemoryPointExists(memory_point_target))
171 vector target_pos = parent.GetSelectionPositionLS(memory_point_target);
173 local_ori = target_pos.VectorToAngles();
177 ErrorEx(
"memory point 'memory_point_target' not found when attaching light");
180 AttachOnObject(parent,
m_LocalPos, local_ori);
185 ErrorEx(
"memory point 'memory_point_start' not found when attaching light");
190 void DetachFromParent()
199 if ( !
m_Parent.ToDelete() && !ToDelete() )
210 static ScriptedLightBase CreateLightAtObjMemoryPoint(
typename name, notnull
Object target,
string memory_point_start,
string memory_point_target =
"",
vector global_pos =
"0 0 0",
float fade_in_time_in_s = 0)
213 if (target.MemoryPointExists(memory_point_start))
216 light.AttachOnMemoryPoint(target, memory_point_start, memory_point_target);
232 Error(
"Error! Light entity of name " +
name.ToString() +
" cannot be spawned! This name is incorrect or not inherited from ScriptedLightBase." );
236 if (fade_in_time_in_s != 0)
238 light_instance.FadeIn(fade_in_time_in_s);
245 Error(
"An instance of ScriptedLightBase was attempted to spawn on server side! Lights are CLIENT SIDE ONLY!");
249 return light_instance;
253 void SetBrightnessTo(
float value)
255 m_Brightness = value;
256 m_BrightnessTarget = value;
257 SetBrightness(m_Brightness * m_BrightnessPulse);
258 CorrectLightPulseDuringDaylight();
262 void CorrectLightPulseDuringDaylight()
264 if (m_Brightness < 100)
266 float v = m_Brightness * 0.01;
270 float brightness_compesation = 1 / v;
271 float compenset_brightness = (m_Brightness * m_BrightnessPulse) * brightness_compesation;
272 SetBrightness(compenset_brightness);
283 void FadeBrightnessTo(
float value,
float time_in_s )
285 m_BrightnessTarget = value;
289 m_BrightnessSpeedOfChange = 9999;
293 m_BrightnessSpeedOfChange =
Math.AbsFloat(m_Brightness - m_BrightnessTarget) / time_in_s;
298 void SetRadiusTo(
float value)
301 m_RadiusTarget = value;
306 void FadeRadiusTo(
float value,
float time_in_s )
308 m_RadiusTarget = value;
312 m_RadiusSpeedOfChange = 9999;
316 m_RadiusSpeedOfChange =
Math.AbsFloat(
m_Radius - m_RadiusTarget) / time_in_s;
326 DeleteLightWithDelay();
332 void SetLifetime(
float life_in_s)
335 m_LifetimeEnd =
GetGame().GetTime() + life_in_s * 1000;
339 void SetFadeOutTime(
float time_in_s)
341 m_FadeOutTime = time_in_s * 1000;
345 void FadeOut(
float time_in_s = -1)
347 float time_in_ms = time_in_s * 1000;
351 float kill_time_in_s = m_FadeOutTime*0.001;
353 FadeBrightnessTo(0, kill_time_in_s);
354 FadeRadiusTo(0, kill_time_in_s);
355 SetLifetime(kill_time_in_s);
359 FadeBrightnessTo(0, time_in_s);
360 FadeRadiusTo(0, time_in_s);
361 SetLifetime(time_in_s);
366 void FadeIn(
float time_in_s)
368 float brightness = m_Brightness;
370 FadeBrightnessTo(brightness, time_in_s);
374 void AddLifetime(
float life_in_s)
376 m_LifetimeEnd += life_in_s * 1000;
380 void OnFrameLightSource(
IEntity other,
float timeSlice)
389 int current_time =
GetGame().GetTime();
391 if ( CheckLifetime(current_time) )
400 HandleFlickering(current_time - m_LifetimeStart, timeSlice);
401 HandleDancingShadows(current_time - m_LifetimeStart, timeSlice);
402 CheckFadeOut(current_time);
403 HandleBrightnessFadeing(timeSlice);
404 HandleRadiusFadeing(timeSlice);
406 CheckIfParentIsInCargo();
407 TryShadowOptimization();
408 OnFrameLightSource(other, timeSlice);
410 HandleBlinking(current_time);
414 void SetDancingShadowsAmplitude(
float max_deviation_in_meters)
416 m_DancingShadowsAmplitude =
Math.AbsFloat(max_deviation_in_meters);
420 void SetDancingShadowsMovementSpeed(
float speed_in_meters_per_frame)
422 m_DancingShadowsSpeed =
Math.AbsFloat(speed_in_meters_per_frame);
426 float GetDancingShadowsAmplitude()
428 return m_DancingShadowsAmplitude;
432 float GetDancingShadowsMovementSpeed()
434 return m_DancingShadowsSpeed;
440 m_IsDebugEnabled = state;
444 void HandleDancingShadows(
float time,
float timeSlice)
446 if (m_DancingShadowsAmplitude > 0)
448 for (
int i = 0; i < 3; i++ )
450 m_DancingShadowsLocalPos[i] = m_DancingShadowsLocalPos[i] + (
Math.RandomFloat(-m_DancingShadowsSpeed,m_DancingShadowsSpeed) * timeSlice) ;
452 if (m_DancingShadowsLocalPos[i] > m_DancingShadowsAmplitude)
453 m_DancingShadowsLocalPos[i] = m_DancingShadowsAmplitude;
455 if (m_DancingShadowsLocalPos[i] < -m_DancingShadowsAmplitude)
456 m_DancingShadowsLocalPos[i] = -m_DancingShadowsAmplitude;
471 if (m_IsDebugEnabled)
479 m_DancingShadowsLocalPos =
Vector(0,0,0);
484 void HandleFlickering(
float time,
float timeSlice)
486 if (m_BrightnessPulseAmplitudeMax > 0)
488 m_BrightnessPulse += (
Math.RandomFloat(-m_BrightnessPulseSpeed, m_BrightnessPulseSpeed) ) * timeSlice;
490 if (m_BrightnessPulse < m_BrightnessPulseAmplitudeMin + 1)
491 m_BrightnessPulse = m_BrightnessPulseAmplitudeMin + 1;
493 if (m_BrightnessPulse > m_BrightnessPulseAmplitudeMax + 1)
494 m_BrightnessPulse = m_BrightnessPulseAmplitudeMax + 1;
499 m_BrightnessPulse = 1;
504 void SetFlickerSpeed(
float speed)
506 m_BrightnessPulseSpeed = speed;
510 void SetFlickerAmplitude(
float coef)
512 m_BrightnessPulseAmplitudeMax =
Math.AbsFloat(coef);
513 m_BrightnessPulseAmplitudeMin = -
Math.AbsFloat(coef);
516 void SetFlickerAmplitudeMax(
float coef)
518 m_BrightnessPulseAmplitudeMax = coef;
521 void SetFlickerAmplitudeMin(
float coef)
523 m_BrightnessPulseAmplitudeMin = coef;
527 float GetFlickerSpeed()
529 return m_BrightnessPulseSpeed;
533 float GetFlickerAmplitudeCoefMax()
535 return m_BrightnessPulseAmplitudeMax;
539 float GetFlickerAmplitudeCoefMin()
541 return m_BrightnessPulseAmplitudeMin;
545 void TryShadowOptimization()
547 if (m_OptimizeShadowsRadius > 0)
551 if (distance_to_camera < m_OptimizeShadowsRadius)
553 SetCastShadow(
false);
563 void SetDisableShadowsWithinRadius(
float radius_in_m)
565 m_OptimizeShadowsRadius = radius_in_m;
569 float GetDisableShadowsWithinRadius()
571 return m_OptimizeShadowsRadius;
574 void CheckIfParentIsInCargo()
588 bool is_in_cargo = GI.IsInCargo();
592 EntityAI parent2 = parent_EAI.GetHierarchyParent();
594 if (parent2 && parent2.GetInventory())
596 is_in_cargo = parent2.GetInventory().IsInCargo();
614 private bool CheckLifetime(
int current_time)
616 if ( current_time > m_LifetimeEnd && m_LifetimeEnd != -1 )
626 private void CheckFadeOut(
int current_time)
629 if ( m_FadeOutTime != -1 && m_LifetimeEnd != -1 && current_time > m_LifetimeEnd - m_FadeOutTime )
633 float time_left_in_s = (m_LifetimeEnd - current_time) * 0.001;
634 FadeBrightnessTo(0, time_left_in_s);
635 FadeRadiusTo(0, time_left_in_s);
640 private void HandleBrightnessFadeing(
float timeSlice)
642 if ( m_Brightness != m_BrightnessTarget )
644 float brightness_difference = m_Brightness - m_BrightnessTarget;
646 if (brightness_difference > m_BrightnessSpeedOfChange*timeSlice)
647 brightness_difference = m_BrightnessSpeedOfChange*timeSlice;
649 if (brightness_difference < -m_BrightnessSpeedOfChange*timeSlice)
650 brightness_difference = -m_BrightnessSpeedOfChange*timeSlice;
652 m_Brightness -= brightness_difference;
654 if ( m_Brightness > 0 || m_BrightnessTarget > 0)
656 SetBrightness(m_Brightness * m_BrightnessPulse);
657 CorrectLightPulseDuringDaylight();
667 SetBrightness(m_Brightness * m_BrightnessPulse);
668 CorrectLightPulseDuringDaylight();
673 private void HandleRadiusFadeing(
float timeSlice)
677 float radius_difference =
m_Radius - m_RadiusTarget;
679 if (radius_difference > m_RadiusSpeedOfChange*timeSlice)
680 radius_difference = m_RadiusSpeedOfChange*timeSlice;
682 if (radius_difference < -m_RadiusSpeedOfChange*timeSlice)
683 radius_difference = -m_RadiusSpeedOfChange*timeSlice;
687 if (
m_Radius > 0 || m_RadiusTarget > 0)
704 void SetBlinkingSpeed(
float _speed)
706 m_BlinkingSpeed = _speed;
710 float GetBlinkingSpeed()
712 return m_BlinkingSpeed;
716 private void HandleBlinking(
float time)
718 if ( m_BlinkingSpeed <= 0 )
723 multiplier =
Math.Sin(time * 0.001 * m_BlinkingSpeed);
724 multiplier = (multiplier + 1)/2;
726 multiplier =
Math.Round(multiplier);
727 SetBrightness(m_Brightness * multiplier);