-- LUA Script - precede every function and global member with lowercase name of script + '_main' -- global array to hold frame value of door g_my_doorautolegacy = {} function my_doorautolegacy_init(e) -- set entity for manual frame control CharacterControlLimbo(e) g_my_doorautolegacy[e] = {frame = 0} end function my_doorautolegacy_main(e) if not scheduler(e,20) then return end local Ent = g_Entity[e] local Door = g_my_doorautolegacy[e] -- detect player proximity if CloserThan(60, Ent) and g_PlayerHealth > 0 then -- only if door visible to player GetEntityPlayerVisibility(e) if Ent.plrvisible == 1 then -- trigger door to open if Ent.activated == 0 then Door.frame = 0 SetAnimationFrame(e, Door.frame) SetActivated(e, 1) ActivateIfUsed(e) PlaySound(e, 0) end end else -- when out of range, trigger door to close if Ent.activated == 2 then SetActivated(e, 3) PlaySound(e, 1) CollisionOn(e) end end -- handle door opening frame animation if Ent.activated == 1 then if Door.frame >= GetEntityAnimationFinish(e, 0) then CollisionOff(e) SetActivated(e, 2) else Door.frame = Door.frame + 1 end SetAnimationFrame(e, Door.frame) end -- handle door closing frame animation if Ent.activated == 3 then if Door.frame <= GetEntityAnimationStart(e,0) then SetActivated(e, 0) else Door.frame = Door.frame - 1 end SetAnimationFrame(e, Door.frame) end end function CloserThan(dist, Ent) dist = dist or 100 local DX = g_PlayerPosX - Ent.x local DY = g_PlayerPosY - Ent.y local DZ = g_PlayerPosZ - Ent.z return (DX*DX + DY*DY + DZ*DZ) <= (dist * dist) end ---------------------------------------------------------------- -- Scheduler, this nifty little function keeps track of time. -- -- The main routine should be called every frame and returns -- -- a flag indicating whether the passed in time has expired -- -- since the last time it was called. -- -- If no time period is specified 100ms is used by default, -- -- i.e. 1/10th of a second. -- -- The caller can either ignore the return flag or use it to -- -- trigger time sensitive functionality. -- -- A global value giving the frames per second count is also -- -- generated, this can be used in script to make animations -- -- independent of frames. -- -- The reason this function is tied to an entity rather than -- -- being global is so that each entity can be triggered at a -- -- different time rather than all being triggered in the same -- -- frame. ---------------------------------------------------------------- g_scheduler = {} function scheduler(e, period) period = period or 100 -- defaults to tenths of a second (100ms) if g_Time == nil then return false end if g_scheduler[e] == nil then g_scheduler[e] = {frames_per_second = 60, period_accumulated = math.random(0, period), accumulated_time = 0, timer_value_last_frame = g_Time, frame_counter = 0}; -- 'test' mode sometimes doesn't clear Lua globals so if -- this appears to be the case initialise everything elseif g_scheduler[e].accumulated_time > 2000 then g_scheduler[e].frames_per_second = 60 g_scheduler[e].period_accumulated = math.random(0, period) g_scheduler[e].accumulated_time = 0 g_scheduler[e].timer_value_last_frame = g_Time g_scheduler[e].frame_counter = 0 end local entry = g_scheduler[e] local do_this_frame_flag = false entry.frame_counter = entry.frame_counter + 1 local time_since_last_frame = g_Time - entry.timer_value_last_frame if (entry.period_accumulated + time_since_last_frame) > period then entry.period_accumulated = (entry.period_accumulated + time_since_last_frame) - period; do_this_frame_flag = true else entry.period_accumulated = entry.period_accumulated + time_since_last_frame end if (entry.accumulated_time + time_since_last_frame) > 1000 then -- more than a second passed? entry.accumulated_time = (entry.accumulated_time + time_since_last_frame) - 1000; entry.frames_per_second = entry.frame_counter entry.frame_counter = 0 else entry.accumulated_time = entry.accumulated_time + time_since_last_frame end entry.timer_value_last_frame = entry.timer_value_last_frame + time_since_last_frame return do_this_frame_flag end