Files
LonaRPG-Online-Games/ModScripts/multiplayer/31_night_day.rb
T
2026-10-29 21:59:06 +04:00

539 lines
15 KiB
Ruby

# encoding: UTF-8
module MPNightDayTransition
TRANSITION_FRAMES = 60
@map_shadow_profiles = {}
@map_shadow_profile_sources = {}
@parsed_shadow_cache = {}
class << self
def normalize_event_script_path(path)
path.to_s.tr("\\", "/").sub(/\A\.\//, "")
rescue
path.to_s
end
def event_script_path?(path)
normalized = normalize_event_script_path(path).downcase
normalized.include?("data/hcgframes/event/")
rescue
false
end
def entry_shadow_script?(path)
name = File.basename(normalize_event_script_path(path)).downcase
return true if name =~ /(?:^|_)(?:bios(?:_begin)?|begin|wakeup|capturedbegin)\.rb$/
false
rescue
false
end
def absolute_event_script_path(path)
normalized = normalize_event_script_path(path)
return nil if normalized == ""
absolute = File.expand_path(normalized, Dir.pwd)
return absolute if File.exist?(absolute)
return normalized if File.exist?(normalized)
absolute
rescue
nil
end
def duplicate_shadow_profile(profile)
return nil unless profile.is_a?(Hash)
result = {}
profile.each do |mode, arr|
result[mode] = arr.is_a?(Array) ? arr.dup : arr
end
result
rescue
nil
end
def parse_shadow_profile_cached(path)
file_path = absolute_event_script_path(path)
return nil if file_path.nil? || file_path == ""
return nil unless File.exist?(file_path)
@parsed_shadow_cache ||= {}
current_mtime = begin
File.mtime(file_path).to_i
rescue
0
end
cached = @parsed_shadow_cache[file_path]
if cached.is_a?(Hash) && cached[:mtime] == current_mtime
return duplicate_shadow_profile(cached[:profile])
end
parsed = parse_shadow_profile(file_path)
@parsed_shadow_cache[file_path] = {
:mtime => current_mtime,
:profile => duplicate_shadow_profile(parsed)
}
duplicate_shadow_profile(parsed)
rescue
nil
end
def shadow_profile_has_mode?(map_id, mode = nil)
profile = shadow_profile_for_map(map_id)
return false if profile.nil?
return true if mode.nil?
target = profile[mode]
target.is_a?(Array) && target.size >= 4 && !target.any?(&:nil?)
rescue
false
end
def merge_shadow_profile_for_map(map_id, parsed_profile)
return if parsed_profile.nil? || parsed_profile.empty?
@map_shadow_profiles ||= {}
current = (@map_shadow_profiles[map_id.to_i] || {}).dup
[:day, :night].each do |mode|
next unless parsed_profile[mode].is_a?(Array) && parsed_profile[mode].size >= 4
current[mode] = parsed_profile[mode].dup
end
@map_shadow_profiles[map_id.to_i] = current unless current.empty?
rescue
end
def remember_shadow_source_for_map(map_id, path)
return if map_id.to_i <= 0
file_path = absolute_event_script_path(path)
return if file_path.nil? || file_path == ""
@map_shadow_profile_sources ||= {}
@map_shadow_profile_sources[map_id.to_i] ||= []
list = @map_shadow_profile_sources[map_id.to_i]
list << file_path unless list.include?(file_path)
rescue
end
def shadow_sources_for_map(map_id)
@map_shadow_profile_sources ||= {}
list = @map_shadow_profile_sources[map_id.to_i]
return [] if list.nil?
list.dup
rescue
[]
end
def entry_shadow_candidate_paths_for_map(game_map)
return [] if game_map.nil?
bases = []
begin
map_name = game_map.name.to_s.strip
bases << map_name unless map_name == ""
rescue
end
begin
add_data = game_map.respond_to?(:add_data) ? game_map.add_data : nil
event_tag = (add_data.is_a?(Hash) ? add_data["event"] : nil)
if event_tag.is_a?(String)
event_tag.split(",").each do |name|
normalized = name.to_s.strip
bases << normalized unless normalized == ""
end
end
rescue
end
bases = bases.map { |name| name.to_s.strip }.reject { |name| name == "" }.uniq
return [] if bases.empty?
suffixes = [
"_Begin.rb",
"_begin.rb",
"_BIOS_Begin.rb",
"_BIOS_begin.rb",
"_WakeUp.rb",
"_wakeup.rb",
"_CapturedBegin.rb",
"_capturedbegin.rb"
]
candidates = []
bases.each do |base|
suffixes.each do |suffix|
path = File.expand_path("Data/HCGframes/event/#{base}#{suffix}", Dir.pwd)
candidates << path if File.exist?(path)
end
end
candidates.uniq
rescue
[]
end
def ensure_shadow_profile_for_map(game_map, mode = nil)
return false if game_map.nil?
map_id = begin
game_map.map_id.to_i
rescue
0
end
return false if map_id <= 0
return true if shadow_profile_has_mode?(map_id, mode)
sources = shadow_sources_for_map(map_id)
entry_shadow_candidate_paths_for_map(game_map).each do |path|
sources << path
end
sources = sources.map { |path| absolute_event_script_path(path) }.
reject { |path| path.nil? || path == "" }.
uniq
merged_any = false
sources.each do |path|
remember_shadow_source_for_map(map_id, path)
parsed = parse_shadow_profile_cached(path)
next if parsed.nil? || parsed.empty?
merge_shadow_profile_for_map(map_id, parsed)
merged_any = true
break if shadow_profile_has_mode?(map_id, mode)
end
shadow_profile_has_mode?(map_id, mode) || merged_any
rescue
false
end
def shadow_profile_for_map(map_id)
@map_shadow_profiles ||= {}
@map_shadow_profiles[map_id.to_i]
rescue
nil
end
def shadow_target_for_map(map_id, mode)
profile = shadow_profile_for_map(map_id)
return nil if profile.nil?
target = profile[mode]
return nil unless target.is_a?(Array) && target.size >= 4
[target[0].to_i, target[1].to_i, target[2].to_i, target[3].to_i]
rescue
nil
end
def script_context_name(ctx)
return "" if ctx.nil?
begin
name = ctx.name.to_s if ctx.respond_to?(:name)
return name unless name.nil? || name == ""
rescue
end
begin
ev = ctx.instance_variable_get(:@event)
return ev.name.to_s if ev && ev.respond_to?(:name)
rescue
end
""
end
def map_entry_context?(ctx, path = nil)
name = script_context_name(ctx)
return true if name =~ /(BIOS|WakeUp|StartPoint)/i
return true if entry_shadow_script?(path)
false
rescue
false
end
def bios_context?(ctx)
return false if ctx.nil?
script_context_name(ctx).to_s == "BIOS"
end
def record_shadow_profile_for_current_map(path, ctx = nil)
return unless event_script_path?(path)
return unless map_entry_context?(ctx, path)
map_id = begin
$game_map ? $game_map.map_id.to_i : 0
rescue
0
end
return if map_id <= 0
file_path = absolute_event_script_path(path)
return if file_path.nil? || file_path == ""
remember_shadow_source_for_map(map_id, file_path)
parsed = parse_shadow_profile_cached(file_path)
return if parsed.nil? || parsed.empty?
merge_shadow_profile_for_map(map_id, parsed)
rescue
end
def current_day_night_mode
return :day if $game_date && $game_date.respond_to?(:day?) && $game_date.day?
return :night if $game_date && $game_date.respond_to?(:night?) && $game_date.night?
nil
rescue
nil
end
def current_shadow_snapshot
shadow = begin
$game_map ? $game_map.shadows : nil
rescue
nil
end
return nil if shadow.nil?
r = shadow.instance_variable_get(:@r) rescue nil
g = shadow.instance_variable_get(:@g) rescue nil
b = shadow.instance_variable_get(:@b) rescue nil
a = shadow.instance_variable_get(:@a) rescue nil
return nil if [r, g, b, a].any?(&:nil?)
[r.to_i, g.to_i, b.to_i, a.to_i]
rescue
nil
end
def record_live_shadow_for_current_map(path, ctx = nil)
return unless event_script_path?(path)
return unless map_entry_context?(ctx, path)
map_id = begin
$game_map ? $game_map.map_id.to_i : 0
rescue
0
end
return if map_id <= 0
remember_shadow_source_for_map(map_id, path)
mode = current_day_night_mode
return if mode.nil?
snapshot = current_shadow_snapshot
return if snapshot.nil?
@map_shadow_profiles ||= {}
current = (@map_shadow_profiles[map_id] || {}).dup
current[mode] = snapshot
@map_shadow_profiles[map_id] = current unless current.empty?
rescue
end
def parse_shadow_profile(path)
return nil unless File.exist?(path)
lines = Ruby310Compat.read_text_file(path).to_s.split(/\r?\n/)
profile = {}
fallback = [nil, nil, nil, nil]
mode = nil
mode_stack = []
branch_stack = []
lines.each do |raw_line|
line = raw_line.to_s.sub(/#.*$/, "").strip
next if line == ""
inline_mode = nil
inline_mode = :day if line =~ /\bif\s+\$game_date\.day\?\s*(?:then)?\s*$/
inline_mode = :night if line =~ /\bif\s+\$game_date\.night\?\s*(?:then)?\s*$/
if line =~ /^\s*if\s+\$game_date\.day\?\s*$/
mode_stack << mode
branch_stack << :day_if
mode = :day
next
elsif line =~ /^\s*if\s+\$game_date\.night\?\s*$/
mode_stack << mode
branch_stack << :night_if
mode = :night
next
elsif line =~ /^\s*if\b/
mode_stack << mode
branch_stack << :other_if
next
elsif line =~ /^\s*elsif\s+\$game_date\.day\?\s*$/
mode = :day
next
elsif line =~ /^\s*elsif\s+\$game_date\.night\?\s*$/
mode = :night
next
elsif line =~ /^\s*elsif\b/
mode = mode_stack.empty? ? mode : mode_stack.last
next
elsif line =~ /^\s*else\s*$/
current_branch = branch_stack.empty? ? nil : branch_stack.last
if current_branch == :day_if
mode = :night
elsif current_branch == :night_if
mode = :day
else
mode = mode_stack.empty? ? mode : mode_stack.last
end
next
elsif line == "end"
mode = mode_stack.empty? ? nil : mode_stack.pop
branch_stack.pop unless branch_stack.empty?
next
end
target_mode = inline_mode || mode
target = target_mode.nil? ? fallback : (profile[target_mode] ||= [nil, nil, nil, nil])
if line =~ /(?:\$game_map\.)?shadows\.set_color\(\s*(-?\d+)\s*,\s*(-?\d+)\s*,\s*(-?\d+)\s*\)/
target[0] = $1.to_i
target[1] = $2.to_i
target[2] = $3.to_i
elsif line =~ /(?:\$game_map\.)?shadows\.set_opacity\(\s*(-?\d+)\s*\)/
target[3] = $1.to_i
end
end
if fallback.any? { |value| !value.nil? }
[:day, :night].each do |target_mode|
target = (profile[target_mode] ||= [nil, nil, nil, nil])
4.times do |i|
target[i] = fallback[i] if target[i].nil?
end
end
end
profile.delete_if { |_k, arr| !arr.is_a?(Array) || arr.any?(&:nil?) }
profile
rescue
nil
end
end
end
class Object
unless private_instance_methods.include?(:mp_night_day_transition__orig_load_script)
alias mp_night_day_transition__orig_load_script load_script
end
def load_script(path)
result = mp_night_day_transition__orig_load_script(path)
MPNightDayTransition.record_shadow_profile_for_current_map(path, self)
MPNightDayTransition.record_live_shadow_for_current_map(path, self)
result
end
private :load_script
end
class Game_Date
unless method_defined?(:mp_server_day_night__orig_day?)
alias mp_server_day_night__orig_day? day?
end
unless method_defined?(:mp_server_day_night__orig_night?)
alias mp_server_day_night__orig_night? night?
end
def mp_server_day_night_mode
return nil unless defined?(WebSocket)
return nil unless WebSocket.respond_to?(:server_day_night_mode)
WebSocket.server_day_night_mode
rescue
nil
end
def mp_pvp_arena_force_day?
return false unless defined?(MP_ArenaFlag) && MP_ArenaFlag.respond_to?(:arena_active?) && MP_ArenaFlag.arena_active?
return false unless $game_map
$game_map.map_id.to_i == 90
rescue
false
end
def day?
return true if mp_pvp_arena_force_day?
mode = mp_server_day_night_mode
return true if mode == :day
return false if mode == :night
mp_server_day_night__orig_day?
end
def night?
return false if mp_pvp_arena_force_day?
mode = mp_server_day_night_mode
return true if mode == :night
return false if mode == :day
mp_server_day_night__orig_night?
end
end
class Game_Map
def mp_pvp_arena_force_day?
return false unless defined?(MP_ArenaFlag) && MP_ArenaFlag.respond_to?(:arena_active?) && MP_ArenaFlag.arena_active?
@map_id.to_i == 90
rescue
false
end
def mp_night_day_shadow_target(mode)
mode = :day if mp_pvp_arena_force_day? && (mode == :day || mode == :night)
if mode == :day || mode == :night
MPNightDayTransition.ensure_shadow_profile_for_map(self, mode)
end
custom = MPNightDayTransition.shadow_target_for_map(@map_id, mode)
return custom unless custom.nil?
case mode
when :day
if isOverMap
[125, 125, 125, 80]
else
[150, 130, 100, 30]
end
when :night
if isOverMap
[80, 90, 160, 200]
else
[40, 40, 120, 170]
end
when :underground
tmp_basic = 230
tmp_scoutcraft_fix = [$game_player.actor.scoutcraft_trait * 3, 75].min
[0, 5, 10, tmp_basic - tmp_scoutcraft_fix]
else
nil
end
end
def mp_night_day_transition_frames(mode)
return 0 unless mode == :day || mode == :night
return 0 unless @mp_night_day_last_map_id == @map_id
return 0 unless @mp_night_day_last_mode == :day || @mp_night_day_last_mode == :night
return 0 if @mp_night_day_last_mode == mode
MPNightDayTransition::TRANSITION_FRAMES
end
def mp_night_day_apply_shadow(mode, instant = false)
return if shadows.nil?
target = mp_night_day_shadow_target(mode)
return if target.nil?
frames = instant ? 0 : mp_night_day_transition_frames(mode)
shadows.set_color(target[0], target[1], target[2], frames)
shadows.set_opacity(target[3], frames)
@mp_night_day_last_mode = mode
@mp_night_day_last_map_id = @map_id
end
def setup_dayLight
prp "$game_map.setup dayLight", 3
mp_night_day_apply_shadow(:day)
end
def setup_nightLight
prp "$game_map.setup nightLIght", 3
mp_night_day_apply_shadow(mp_pvp_arena_force_day? ? :day : :night)
end
def set_underground_light
prp "$game_map.setup nightLIght", 3
mp_night_day_apply_shadow(:underground, true)
end
end