PHP Code:
if (isDedicated) exitWith {}; //This script won't run on dedicated servers (Thanks Xeno)
/*
TPW HOUSELIGHTS v 1.03
This script causes flickering lights to automatically come on in enterable buildings around the player. It will work on any map with ALICE2 compatible enterable buildings, and will simply ignore unenterable ALICE buildings.
Please feel free to modify and improve this script, on the proviso that you post your improvements for others to benefit from.
Thanks:
* CarlGustaffa for the azimuth code
* Rydygier for the inspiration for the flickering code
* RogueTrooper for the improved code for determining enterable buildings
TPW 20120519
*/
///////////
//VARIABLES
///////////
//Distance player has to move for houses to be rescanned. Player must move greater than this distance to rescan houses.
_movedistance = 50;
//Distance around player to scan for houses to light. There's no point specifying a really large value since the engine won't reliably display lights past 100m or so.
_housedistance = 100;
//Percentage of houses to receive lights
_houseperc = 100;
/*ARRAY OF LIGHT VALUES
You can add or remove lights to taste.
Each individual light array consists of:
[red,green,blue,maximum brightness,minimum brightness,%chance of flickering,%brightness to flicker]
red,green,blue,max and min are all in the range 0 - 255
Flickering %chance and %brightness are in the range 0-100
Flickering uses CPU, so if you want none, set all %chance of flickering values to 0.
*/
_lightsarray = [
[255,127,24,40,20,100,10],//warm yellow/orange (candle or fire)
[255,200,100,40,20,80,5], //yellow/white (incandesecent)
[100,200,255,40,20,100,10]]; //Blue/white (television or fluorescent
//////////////////////////////////////////
//PROCESS LIGHT ARRAYS INTO USEABLE VALUES
//////////////////////////////////////////
_proclightsarray = [];
{
_red = _x select 0; _red = _red /255;
_green = _x select 1; _green = _green/255;
_blue = _x select 2; _blue = _blue/255;
_max = _x select 3;
_min = _x select 4;
_flperc = _x select 5;
_flvar = _x select 6; _flvar = _flvar/100;
_proclight = [_red,_green,_blue,_max,_min,_flperc,_flvar];
_proclightsarray = _proclightsarray + [_proclight];
} foreach _lightsarray;
//////////////////
//FLICKER FUNCTION
//////////////////
tpw_flicker = call compile format ["%1","{
_lt0 = _this select 0;
_br = _this select 1;
_fv = _this select 2;
_rng = _br * _fv;
_brmax = (_br + _rng);
_brmin = (_br - _rng);
_op = 1;
_inc = _rng/(random 10);
_slp = random 0.1;
_br2 = _br;
while {not (isNull _lt0)} do
{
_br2 = _br2 + (_op * _inc);
_lt0 setLightBrightness _br2;
if (_br2 > _brmax) then {_op = -1;_brmax = _br + (random _rng);_slp = random 0.2;_inc = _rng/((random 10) + 1);};
if (_br2 < _brmin) then {_op = 1;_brmin = _br - (random _rng);_slp = random 0.2;_inc = _rng/((random 10) + 1);};
sleep _slp;
};
};"
];
////////////
//MAIN LOOP
////////////
// Initial dummy player position
_lastpos = [0,0,0];
while {true} do
{
//Only recompute houses to light if player has moved > set distsance
if (player distance _lastpos > _movedistance) then
{
//GET ANGLE OF SUN - ADAPTED FROM CARLGUSTAFFA
_lat = -1 * getNumber(configFile >> "CfgWorlds" >> worldName >> "latitude");
_day = 360 * (dateToNumber date);
_hour = (daytime / 24) * 360;
_sunangle = ((12 * cos(_day) - 78) * cos(_lat) * cos(_hour)) - (24 * sin(_lat) * cos(_day));
//Recalculate player's position
_lastpos = (position player);
//Only bother if it's dark
if (_sunangle < 0) then
{
// Delete lights from previously lit houses
{deletevehicle _x;} foreach tpw_lithouses; tpw_lithouses = [];
//Find houses within set distance of player
_houses = nearestObjects [getpos player, ["House"], _housedistance];
{
//ONLY THE CHOSEN PERCENTAGE OF ENTERABLE HOUSES WITH MULTIPLE BUILDINGPOSITIONS WILL ACTUALLY LIGHT UP
if ((((_x buildingpos 0) select 0) != 0) and ((random 100) < _houseperc)) then
{
_pos = getpos _x;
_lightnum = floor (random (count _proclightsarray));_light = _proclightsarray select _lightnum; //Select light type
_r = _light select 0;_g = _light select 1;_b = _light select 2;_col = [_r,_g,_b]; //Light colour values
_max = _light select 3;_min = _light select 4;//Light brightness range
_flperc = _light select 5;_flvar = _light select 6;//Light flickering parameters
_br = 0.1 * ((_min + (random (_max - _min)))/255); //Random brightness between specified range
_lp = "#lightpoint" createVehicleLocal _pos; //Create light
_lp setLightBrightness _br; // Set its brightness
_lp setLightColor _col; // Set its colour
_lp lightAttachObject [_x, [random 2,random 2,1]]; // place it at a random position within house
if ((random 100) < _flperc) then {[_lp,_br,_flvar] spawn tpw_flicker}; // Set appropriate flicker for light type
tpw_lithouses = tpw_lithouses + [_lp]; // Add it to the lit houses array so it can be removed later
};
} foreach _houses;
};
};
sleep 10;
};