pressed = 0 if g_generator_parts == nil then g_generator_parts = 0 --number of repair parts player starts with end if g_fuel == nil then g_fuel = 0 --starting amount of g_fuel player starts with end local max_fuel = 100 --amount of fuel the generator can store when full (will start as soon as turned on if has any fuel though) local flicker_delay = 250 --amount of time in milliseconds to flicker power for when generator reaches low fuel (10%) local fuel_drain_delay = 5000 --amount of time in milliseconds to wait before fuel drains (by 1 - so if you wanted to do 1% you can cap max_fuel at 100) local refuel_amount = 1 --how much to refuel per session (less if you don't have enough fuel) local repair_time = 2500 --how long each part takes to repair/fit local refuel_time = 1250 --how long it takes to pour fuel into generator local new_flicker_delay = {} local new_fuel_drain_delay = {} local flicker_state = {} local fuel_state = {} generator_part = {} g_gen_parts_placed = {} g_generator_fuel = {} local old_activated = {} local last_state = {} local start_hp = {} g_state = {} local first_run = {} local new_refuel_time = {} --range to allow player use local interact_range = 150 --health = number of parts required to repair --name = generator number (for assigning doors and lights etc) --sound0 = repair / fit part --sound1 = start up --sound2 = pour fuel / refuel --sound3 = stutter (no fuel) --sound4 = engine loop function generator2_init_name(e,name) first_run[e] = nil weapon_name[e] = name if g_state[e] == nil then g_state[e] = "wait for repairs" end if g_gen_parts_placed[e] == nil then g_gen_parts_placed[e] = 0 --number of parts already repaired end if g_generator_fuel[e] == nil then g_generator_fuel[e] = 0 --starting amount of g_fuel in generator end end function generator2_main(e) if first_run[e] == nil then start_hp[e] = g_Entity[e]['health'] first_run[e] = 0 return elseif first_run[e] == 0 then first_run[e] = 1 for a = 1,9999 do if g_Entity[a] ~= nil and a ~= e then if weapon_name[a] ~= nil then if weapon_name[a] == weapon_name[e].." door" then SetActivated(a,-1) elseif weapon_name[a] == weapon_name[e].." light" then HideLight(a) end end end end return end if g_Entity[e]['health'] < start_hp[e] then SetEntityHealth(e,start_hp[e]) end if g_state[e] == "wait for repairs" then if PlayerLooking(e,interact_range,15) == 1 then if g_gen_parts_placed[e] < g_Entity[e]['health'] then if g_KeyPressE == 0 then PromptLocal(e,g_Entity[e]['health'] - g_gen_parts_placed[e].." Parts needed. (Hold 'E' to repair)") end if g_KeyPressE == 1 and pressed == 0 then if g_generator_parts > 0 then pressed = 1 --g_PlayerAngX seems to be causing problems so used 0 instead SetFreezeAngle(0,g_PlayerAngY,g_PlayerAngZ) SetFreezePosition(g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ) g_state[e] = "repairing" StartTimer(e) else PromptDuration("You don't have any parts to repair with",2000) end end else if g_KeyPressE == 0 then PromptLocal(e,"Turn generator on? (Press 'E')") end if g_KeyPressE == 1 and pressed == 0 then pressed = 1 if g_generator_fuel[e] > 0 then PlaySound(e,1) --start up sound SetAnimation(0) PlayAnimation(e) g_state[e] = "start up" else PromptDuration("It needs some fuel..",2000) PlaySound(e,3) --splutter sound g_state[e] = "need fuel" end return end end end elseif g_state[e] == "repairing" then if g_KeyPressE == 1 then pressed = 1 TransportToFreezePosition() if GetTimer(e) < repair_time then LoopSound(e,0) PromptLocal(e,"Repairing part "..round((GetTimer(e)/1000),1).."/"..round((repair_time/1000),1).."s") else StopSound(e,0) g_gen_parts_placed[e] = g_gen_parts_placed[e] + 1 g_generator_parts = g_generator_parts - 1 g_state[e] = "wait for repairs" end else StopSound(e,0) g_state[e] = "wait for repairs" end elseif g_state[e] == "start up" then new_fuel_drain_delay[e] = GetTimer(e)+fuel_drain_delay flicker_state[e] = "on" new_flicker_delay[e] = GetTimer(e)+flicker_delay --open doors? for a = 1,99999 do if g_Entity[a] ~= nil and a ~= e then if weapon_name[a] ~= nil then if weapon_name[a] == weapon_name[e].." door" then SetActivated(a,1) elseif weapon_name[a] == weapon_name[e].." light" then ShowLight(a) end end end if a >= 99999 then g_state[e] = "on" end end elseif g_state[e] == "on" then if GetTimer(e) > new_fuel_drain_delay[e] then new_fuel_drain_delay[e] = GetTimer(e)+fuel_drain_delay g_generator_fuel[e] = g_generator_fuel[e] - 1 end LoopSound(e,4) --running sound if g_generator_fuel[e] < 1 then g_state[e] = "need fuel" flicker_state[e] = "off" PlaySound(e,3) --stutter sound StopSound(e,4) for a = 1,99999 do if g_Entity[a] ~= nil and a ~= e then if weapon_name[a] ~= nil then if weapon_name[a] == weapon_name[e].." door" then StartTimer(a) SetAnimation(a,1) PlayAnimation(a) g_Entity[a]['animating'] = 1 CollisionOn(a) PlaySound(a,1) SetActivated(a,-1) elseif weapon_name[a] == weapon_name[e].." light" then HideLight(a) end end end end else if PlayerLooking(e,interact_range,15) == 1 then if g_KeyPressE == 0 then if g_generator_fuel[e] <= max_fuel*0.11 then fuel_state[e] = "low" elseif g_generator_fuel[e] < max_fuel*0.75 then fuel_state[e] = "ok" else fuel_state[e] = "good" end PromptLocal(e,"Current fuel = "..g_generator_fuel[e].."("..fuel_state[e].."), refuel the generator? (Hold E)") end if g_KeyPressE == 1 and pressed == 0 then pressed = 1 if g_fuel > 0 then new_refuel_time[e] = GetTimer(e)+refuel_time last_state[e] = "on" --g_PlayerAngX seems to be causing problems so used 0 instead SetFreezeAngle(0,g_PlayerAngY,g_PlayerAngZ) SetFreezePosition(g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ) g_state[e] = "refueling" else PromptDuration("You need fuel first",2000) end end end if g_generator_fuel[e] < max_fuel*0.1 then if GetTimer(e) > new_flicker_delay[e] then PlaySound(e,3) new_flicker_delay[e] = GetTimer(e)+flicker_delay if flicker_state[e] == "on" then flicker_state[e] = "off" for a = 1,99999 do if g_Entity[a] ~= nil and a ~= e then if weapon_name[a] ~= nil then if weapon_name[a] == weapon_name[e].." door" then old_activated[a] = g_Entity[a]['activated'] if g_Entity[a]['activated'] == 0 or g_Entity[a]['activated'] == 1 then SetActivated(a,-1) end elseif weapon_name[a] == weapon_name[e].." light" then HideLight(a) end end end end else flicker_state[e] = "on" for a = 1,99999 do if g_Entity[a] ~= nil and a ~= e then if weapon_name[a] ~= nil then if weapon_name[a] == weapon_name[e].." door" then SetActivated(a,old_activated[a]) elseif weapon_name[a] == weapon_name[e].." light" then ShowLight(a) end end end end end end else flicker_state[e] = "on" new_flicker_delay[e] = GetTimer(e)+flicker_delay end end elseif g_state[e] == "need fuel" then if PlayerLooking(e,interact_range,15) == 1 then if g_KeyPressE == 0 then PromptLocal(e,"Refuel the generator? (Hold E)") end if g_KeyPressE == 1 and pressed == 0 then pressed = 1 if g_fuel > 0 then new_refuel_time[e] = GetTimer(e)+refuel_time last_state[e] = "need fuel" --g_PlayerAngX seems to be causing problems so used 0 instead SetFreezeAngle(0,g_PlayerAngY,g_PlayerAngZ) SetFreezePosition(g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ) g_state[e] = "refueling" else PromptDuration("You need fuel first",2000) end end if g_generator_fuel[e] > 0 then g_state[e] = "wait for repairs" end end elseif g_state[e] == "refueling" then if g_KeyPressE == 1 then pressed = 1 TransportToFreezePosition() if GetTimer(e) < new_refuel_time[e] then LoopSound(e,2) PromptLocal(e,"Refuelling generator, generator fuel = "..g_generator_fuel[e]..", you have "..g_fuel.." fuel left. (Release E to stop filling)") else new_refuel_time[e] = GetTimer(e)+refuel_time if g_fuel >= refuel_amount then g_fuel = g_fuel - refuel_amount g_generator_fuel[e] = g_generator_fuel[e] + refuel_amount else g_generator_fuel[e] = g_generator_fuel[e] + g_fuel g_fuel = 0 end end else StopSound(e,2) g_state[e] = last_state[e] end end --g_state if g_KeyPressE == 0 then pressed = 0 end end --main 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 function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end