First the script:
#===============================================================================
# ** AntiLag Script
#-------------------------------------------------------------------------------
#
# f0tz!baerchen
# 0.5
# 30.12.2006
#
#-------------------------------------------------------------------------------
#
# Credits:
# Chaosg1 (for testing ;) )
# NearFantastica (for the Event AntiLag I used and improved)
#
#-------------------------------------------------------------------------------
#
# Features:
# - Event AntiLag: Event (and their Sprites) which are not on the screen are
# not updated except they run on "Autostart" or "Parallel Process" or they
# have an empty comment in the first line
# - High Priority: Game can be run on high priority
# - Smooth Antilag: the Event AntiLag does only work fine if the events are
# distributed over the whole map, but when there are many events at the same
# place it lags again. If the script notices lag it will reduce the frame rate
# to prevent this lag. Maybe the game will run slower, too..
#
#-------------------------------------------------------------------------------
#
# Options:
# can be changed during the game, but once high_priority is true you
# cannot disable it anymore
$antilag_factor = 1.0 # if you make this value smaller the game will be smoother
# but eventually slower, too. (you can also increase it)
# (only if $antilag_switch is true)
$antilag_switch = false # set this to true if you want the game to be smoother
# (but perhaps slower) instead of lagging
$antilag_high_priority = true # set this to true if you want the game to run on
# high priority
$antilag = true # set this to true to enable normal anti-lag
#
#===============================================================================
#-------------------------------------------------------------------------------
# * SDK Log Script
#-------------------------------------------------------------------------------
SDK.log('AntiLag', 'f0tz!baerchen', 0.5, '30.12.06')
#-------------------------------------------------------------------------------
# Begin SDK Enabled Check
#-------------------------------------------------------------------------------
if SDK.state('AntiLag') == true
#===============================================================================
# FrameRateTest Class
# used for some things with the Frame Rate etc..
#===============================================================================
class FrameRateTest
attr_reader :frame_count_start
#-----------------------------------------------------------------------------
# initialize
#-----------------------------------------------------------------------------
def initialize
@time_fps = Time.new
@frame_count_fps = Graphics.frame_count
end
#-----------------------------------------------------------------------------
# fps: returns the Frames past since the last call
# so you should use it once per second..
# if show is set to true it prints the Frames
#-----------------------------------------------------------------------------
def fps(show = false)
if show
time = Time.new
p ((Graphics.frame_count - @frame_count_fps) / (time - @time_fps)).round
time_2 = Time.new
@time_fps += time_2-time
else
return ((Graphics.frame_count - @frame_count_fps) /
(Time.new - @time_fps)).round
end
@time_fps = Time.new
@frame_count_fps = Graphics.frame_count
end
end
#===============================================================================
# Scene_Map class
#===============================================================================
class Scene_Map
#-----------------------------------------------------------------------------
# main method, initialising of the FrameRateTest-Object added
#-----------------------------------------------------------------------------
alias f0tzis_anti_lag_scene_map_main main
def main
$antilag = FrameRateTest.new
f0tzis_anti_lag_scene_map_main
end
#-----------------------------------------------------------------------------
# update method, antilag has been added, frame rate is changed to the
# real frame rate, so everything slows down if the program cannot reach the
# frame rate anymore
#-----------------------------------------------------------------------------
alias f0tzis_anti_lag_scene_map_update update
def update
f0tzis_anti_lag_scene_map_update
if $antilag_switch
if Graphics.frame_count % Graphics.frame_rate == 0
Graphics.frame_rate = ($antilag.fps ** 2) * $antilag_factor / 40
end
elsif Graphics.frame_rate != 40
Graphics.frame_rate = 40
end
end
end
#===============================================================================
# Game_Event Class
#===============================================================================
class Game_Event
#-----------------------------------------------------------------------------
# for Anti_Lag, decides, if an event is on the screen or not.
#-----------------------------------------------------------------------------
def in_range?
# returns true if $antilag is false or the event is an
# Autostart/Parallel Process event or it has an empty
# comment in the first line
if not $antilag or (@trigger == 3 or @trigger == 4 or
(@list!=nil and @list[0].code == 108 and @list[0].parameters == ['']))
return true
end
screne_x = $game_map.display_x
screne_x -= 256
screne_y = $game_map.display_y
screne_y -= 256
screne_width = $game_map.display_x
screne_width += 2816
screne_height = $game_map.display_y
screne_height += 2176
return (@real_x > screne_x and @real_x < screne_width and
@real_y > screne_y and @real_y < screne_height)
end
#-----------------------------------------------------------------------------
# update method
#-----------------------------------------------------------------------------
alias f0tzis_anti_lag_game_event_update update
def update
return if not self.in_range?
f0tzis_anti_lag_game_event_update
end
end
#===============================================================================
# Sprite_Character Class
#===============================================================================
class Sprite_Character < RPG::Sprite
#-----------------------------------------------------------------------------
# update method, parameters added for Loop_Map, rebuild for 8dirs
#-----------------------------------------------------------------------------
alias f0tzis_anti_lag_sprite_char_update update
def update
return if @character.is_a?(Game_Event) and not @character.in_range?
f0tzis_anti_lag_sprite_char_update
end
end
#===============================================================================
# High Priority?
#===============================================================================
if $antilag_high_priority
@SetCurrentProcess = Win32API.new('kernel32','GetCurrentProcess',[],'i').call
@SetPriorityClass = Win32API.new('kernel32','SetPriorityClass',['p', 'i'],'i')
@SetPriorityClass.call(@GetCurrentProcess, 0x00000080)
end
end
Features:
- Event AntiLag: Event (and their Sprites) which are not on the screen are
not updated except they run on "Autostart" or "Parallel Process" or they
have an empty comment in the first line
- High Priority: Game can be run on high priority
- Smooth Antilag: the Event AntiLag does only work fine if the events are
distributed over the whole map, but when there are many events at the same
place it lags again. If the script notices lag it will reduce the frame rate
to prevent this lag. Maybe the game will run slower, too..
Just place it under the SDK and above the rest of your scripts.
CREDITS: f0tz!baerchen
Chaosg1 (for testing )
NearFantastica (for the Event AntiLag I used and improved)(original thread:
http://www.hbgames.org/forums/showthread.php?t=11639 )