#===============================================================================
# Class Antilag Settings
#-------------------------------------------------------------------------------
# 防止事件过多卡机脚本
# 设置CPU线程
#===============================================================================
class Antilag_Settings
attr_accessor :event
attr_accessor :max_cpu_utilization
attr_accessor :cpu_tolerance
#-----------------------------------------------------------------------------
# 初始化对象
#-----------------------------------------------------------------------------
def initialize
@event = true
@high_priority = true
@max_cpu_utilization = 85
#这个是控制优先级。
@cpu_tolerance = 20
@SetPriorityClass = Win32API.new('kernel32', 'SetPriorityClass',
['p', 'i'], 'i')
@GetProcessTimes = Win32API.new('kernel32', 'GetProcessTimes',
['i','p','p','p','p'], 'i')
end
#-----------------------------------------------------------------------------
# 更改优先级 on(true)/off(false)
#-----------------------------------------------------------------------------
def high_priority=(value)
@high_priority = value
if @high_priority
@SetPriorityClass.call(-1, 0x00000080) # 高质量
# 设置CPU优先级80为高。110为实时。20为标准。
else
@SetPriorityClass.call(-1, 0x00000020) # 标准优先
end
end
#-----------------------------------------------------------------------------
# 返回现在的CPU使用率
#-----------------------------------------------------------------------------
def get_cpu_utilization
# 使用API获取内核用户调用时间
creation_time = '0' * 10
exit_time = '0' * 10
kernel_time = '0' * 10
user_time = '0' * 10
@GetProcessTimes.call(-1, creation_time, exit_time, kernel_time, user_time)
kernel_time = kernel_time.unpack('l2')
user_time = user_time.unpack('l2')
kernel_time = kernel_time[0] + kernel_time[1]
user_time = user_time[0] + user_time[1]
# 计算不同的CPU使用率
if @old_time != nil
timer_difference = Time.new - @old_timer
time_difference = kernel_time + user_time - @old_time
result = time_difference / timer_difference / 100000
else
result = $antilag.max_cpu_utilization
end
# 节约CPU使用(还是计算)
@old_timer = Time.new
@old_time = kernel_time + user_time
return result
end
end
$antilag = Antilag_Settings.new
#==============================================================================
# ■ Scene_Map
#------------------------------------------------------------------------------
# 处理地图画面的类。
#==============================================================================
class Scene_Map
#-----------------------------------------------------------------------------
# 刷新模块, 增加平滑帧数
#-----------------------------------------------------------------------------
alias f0tzis_anti_lag_scene_map_update update
def update
f0tzis_anti_lag_scene_map_update
if Graphics.frame_count % 20 == 0 and $antilag.max_cpu_utilization <= 100
# 计算CPU最大限度利用和当前的利用率
abs = $antilag.max_cpu_utilization - $antilag.get_cpu_utilization
# 如果改变帧速率的差异
if abs.abs >= $antilag.max_cpu_utilization * $antilag.cpu_tolerance/100.0
Graphics.frame_rate = [[10, Graphics.frame_rate + abs / 2].max, 40].min
end
end
end
end
#咳咳少了几行代码
$antilag.max_cpu_utilization = 85
$antilag.cpu_tolerance = 20
$antilag.high_priority = true
$antilag.event = true
#自动回滚标准
if $antilag.max_cpu_utilization>=100
$antilag.max_cpu_utilization=85
end # 分歧结束。