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 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 current_battery = 0 local state = "off" fpressed = 0 local timer = {} 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 flashlight_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 flashlight_main(e) 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) flashlight_batteries_txt = flashlight_batteries_txt - 1 current_battery = current_battery + 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) state = "off" end elseif state == "off" then 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 state = "off" SetFlashLight(0) end fpressed = 0 elseif fpressed == 3 then SetFlashLight(0) state = "off" fpressed = 0 end end TextCenterOnX(50,90,3,"spare batteries = "..flashlight_batteries_txt.." , current battery life = "..flashlight_battery_life[current_battery].."% , current drain per sec = 1% per "..flashlight_battery_drain_delay[current_battery].."s") end --main function flashlight_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