flashlight_battery_life = {} flashlight_battery_drain_delay = {} local new_flashlight_battery_drain_delay = {} local new_flicker_delay_off = nil local new_flicker_delay_on = nil local new_regen_delay = 0 local replace_battery_delay = 3 --how many seconds til battery reloading is complete flashlight_batteries = 0 --number of batteries player starts with (can be 0, forcing player to have no flashlight until a battery is found *note, the flashlight will still have a battery inside it so remember to set regen_delay to a very high number) max_flashlight_batteries = 5 --maximum batteries player can hold at one time local flickering_on = 1 --set to 1 for flickering effect, 0 for constant on (when state is on) local flickering_perc_start = 40 --what % of battery life the flickering will begin local flicker_delay_off = math.random(1,3) --how many seconds before flashlight can go off again while flickering local flicker_delay_on = 0.2 --how long til light comes back on while flickering local regen_while_off = 0 --set to 0 to turn battery automatically restore power while off local regen_delay = 10 --this is actually a modifier and multiplies the drain speed by this to get the value (so a drain of 0.5s becomes 2s to restore 1% at 4) local current_battery = 0 local state = "off" fpressed = 0 local timer = {} local show_battery_icon = 1 --set to 0 if you aren't using spare batteries and don't want the sprite icon to appear on screen local fl_i = LoadImage("scriptbank\\images\\flashlight\\fl_icon.png") --path to the flash light icon images\\flashlight\\fl_icon local fl_sprite_posx = 13 --x position of the flashlight image on screen (% of screen) local fl_sprite_posy = 86 --y position of the flashlight image on screen (% of screen) local fl_sprite_sizex = 10 --x size of the flashlight image on screen (% of screen) local fl_sprite_sizey = 13 --y size of the flashlight image on screen (% of screen) local flb_i = LoadImage("scriptbank\\images\\flashlight\\flb_icon.png") --path the the battery image local flb_sprite_posx = 23 local flb_sprite_posy = 92 local flb_sprite_sizex = 5 local flb_sprite_sizey = 7 flashlight_batteries_txt = flashlight_batteries --sound at slot 0 is for replacing batteries --sound at slot 1 is for clicking on/off --set entity always active & use with flashlight_battery.lua for extra batteries function flashlight2_init(e) flashlight_battery_life[0] = 0 flashlight_battery_drain_delay[0] = 0 if flashlight_batteries > 0 then for a = 1,flashlight_batteries do flashlight_battery_life[a] = 100 --starting amount of life for the batteries flashlight_battery_drain_delay[a] = 0.2 --how fast the batteries drain (in seconds) end end timer[e] = 0 end function flashlight2_main(e) if fl_sprite == nil and fl_i ~= nil then fl_sprite = CreateSprite(fl_i) SetSpriteSize(fl_sprite,fl_sprite_sizex,fl_sprite_sizey) if show_battery_icon == 1 then if flb_sprite == nil and flb_i ~= nil then flb_sprite = CreateSprite(flb_i) SetSpriteSize(flb_sprite,flb_sprite_sizex,flb_sprite_sizey) end end else SetFlashLightKeyEnabled(0) if g_KeyPressF == 1 and fpressed < 3 then fpressed = 2 if os.time() > timer[e] + (replace_battery_delay * 0.35) then if current_battery < flashlight_batteries then TextCenterOnX(50,85,3,"Replacing flash light batteries...") if os.time() > timer[e] + replace_battery_delay then PlaySoundIfSilent(e,1) current_battery = current_battery + 1 flashlight_batteries_txt = flashlight_batteries_txt - 1 timer[e] = os.time() fpressed = 3 end else TextCenterOnX(50,85,3,"No spare batteries remaining...") end end end if state == "on" then if flashlight_battery_life[current_battery] > 0 then if new_flashlight_battery_drain_delay[current_battery] == nil then new_flashlight_battery_drain_delay[current_battery] = g_Time + (flashlight_battery_drain_delay[current_battery]*1000) end if flickering_on == 1 then if flashlight_battery_life[current_battery] <= flickering_perc_start then if new_flicker_delay_off == nil then new_flicker_delay_off = g_Time + (flicker_delay_off*1000) new_flicker_delay_on = g_Time + (flicker_delay_off*1000) end if g_Time > new_flicker_delay_off then SetFlashLight(0) new_flicker_delay_on = g_Time + (flicker_delay_on*1000) new_flicker_delay_off = g_Time + (math.random(flicker_delay_off*0.8,flicker_delay_off*1.2)*1000) end if g_Time > new_flicker_delay_on then SetFlashLight(1) end end end if g_Time > new_flashlight_battery_drain_delay[current_battery] then flashlight_battery_life[current_battery] = flashlight_battery_life[current_battery] - 1 new_flashlight_battery_drain_delay[current_battery] = g_Time + (flashlight_battery_drain_delay[current_battery]*1000) end else SetFlashLight(0) new_regen_delay = g_Time + regen_delay state = "off" end elseif state == "off" then if regen_while_off == 1 then if g_Time > new_regen_delay then new_regen_delay = g_Time + ((flashlight_battery_drain_delay[current_battery]*1000)*regen_delay) if flashlight_battery_life[current_battery] < 100 then flashlight_battery_life[current_battery] = flashlight_battery_life[current_battery] + 1 end end end end if g_KeyPressF == 0 then timer[e] = os.time() if fpressed == 2 then PlaySound(e,0) if state == "off" then state = "on" SetFlashLight(1) else new_regen_delay = g_Time + ((flashlight_battery_drain_delay[current_battery]*1000)*regen_delay) state = "off" SetFlashLight(0) end fpressed = 0 elseif fpressed == 3 then new_regen_delay = g_Time + ((flashlight_battery_drain_delay[current_battery]*1000)*regen_delay) SetFlashLight(0) state = "off" fpressed = 0 end end SetSpritePosition(fl_sprite,fl_sprite_posx,fl_sprite_posy) PasteSprite(fl_sprite) TextCenterOnX(fl_sprite_posx+(fl_sprite_sizex/1.5),fl_sprite_posy+(fl_sprite_sizey/2),3," "..flashlight_battery_life[current_battery].."%") SetSpritePosition(fl_sprite,200,200) if show_battery_icon == 1 then SetSpritePosition(flb_sprite,flb_sprite_posx,flb_sprite_posy) PasteSprite(flb_sprite) TextCenterOnX(flb_sprite_posx+(flb_sprite_sizex/1.5),flb_sprite_posy+(flb_sprite_sizey/2),3," x "..flashlight_batteries_txt) SetSpritePosition(flb_sprite,200,200) end --TextCenterOnX(50,90,3,"spare batteries = "..remaining_batteries.." , current battery life = "..flashlight_battery_life[current_battery].."% , current drain per sec = 1% per "..flashlight_battery_drain_delay[current_battery].."s") end end --main function flashlight2_exit(e) end function PlayerLooking(e,dis,v) if g_Entity[e] ~= nil then if dis == nil then dis = 3000 end if v == nil then v = 0.5 end if GetPlayerDistance(e) <= dis then local destx = g_Entity[e]['x'] - g_PlayerPosX local destz = g_Entity[e]['z'] - g_PlayerPosZ local angle = math.atan2(destx,destz) angle = angle * (180.0 / math.pi) if angle <= 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end while g_PlayerAngY < 0 or g_PlayerAngY > 360 do if g_PlayerAngY <= 0 then g_PlayerAngY = 360 + g_PlayerAngY elseif g_PlayerAngY > 360 then g_PlayerAngY = g_PlayerAngY - 360 end end local L = angle - v local R = angle + v if L <= 0 then L = 360 + L elseif L > 360 then L = L - 360 end if R <= 0 then R = 360 + R elseif R > 360 then R = R - 360 end if (L < R and math.abs(g_PlayerAngY) > L and math.abs(g_PlayerAngY) < R) then return 1 elseif (L > R and (math.abs(g_PlayerAngY) > L or math.abs(g_PlayerAngY) < R)) then return 1 else return 0 end else return 0 end end end