Files
LonaRPG-Online-Games/ModScripts/99999999999999999994_MobHitBars.rb
T

247 lines
7.7 KiB
Ruby
Raw Normal View History

2026-10-29 23:02:28 +04:00
#==============================================================================
# Mob hit bars
# Shows stamina and health above an NPC after either value is reduced.
#==============================================================================
module MobHitBars
WIDTH = 42
INNER_WIDTH = WIDTH - 2
2026-10-31 03:07:31 +04:00
BITMAP_WIDTH = 160
2026-10-29 23:05:52 +04:00
BAR_X = (BITMAP_WIDTH - WIDTH) / 2
2026-10-31 03:15:48 +04:00
HEIGHT = 48
2026-10-29 23:02:28 +04:00
VISIBLE_FRAMES = 150
FADE_FRAMES = 30
Y_OFFSET = 7
BACK_COLOR = Color.new(0, 0, 0, 220)
BORDER_COLOR = Color.new(210, 210, 210, 220)
STAMINA_COLOR = Color.new(255, 220, 20, 255)
HEALTH_COLOR = Color.new(225, 35, 35, 255)
2026-10-29 23:09:57 +04:00
LABEL_FONT = "AuX DotBitC Xtra Bold"
2026-10-31 03:07:31 +04:00
LABEL_FONT_SIZE = 18
2026-10-31 03:15:48 +04:00
LEVEL_FONT_SIZE = 16
2026-10-31 03:07:31 +04:00
MAX_NPC_LEVEL = 100
YELLOW_NPC_LEVEL = 50
NORMAL_LEVEL_COLOR = Color.new(255, 255, 255, 255)
MEDIUM_LEVEL_COLOR = Color.new(255, 220, 20, 255)
MAX_LEVEL_COLOR = Color.new(255, 55, 55, 255)
2026-10-29 23:02:28 +04:00
def self.npc_actor(character)
return nil unless character.is_a?(Game_Event)
return nil unless character.respond_to?(:npc?) && character.npc?
actor = character.actor
return nil unless actor && actor.respond_to?(:battle_stat)
actor
rescue
nil
end
def self.stat(actor, name, type = nil)
if type.nil?
actor.battle_stat.get_stat(name).to_f
else
actor.battle_stat.get_stat(name, type).to_f
end
rescue
0.0
end
def self.ratio(value, maximum)
return 0.0 if maximum <= 0.0
[[value / maximum, 0.0].max, 1.0].min
end
2026-10-29 23:05:52 +04:00
def self.display_name(actor)
raw = actor.respond_to?(:npc_name) ? actor.npc_name.to_s : "NPC"
key = "DataNpcName:name/#{raw}"
2026-10-31 03:07:31 +04:00
translated = $game_text[key, true].to_s rescue ""
return translated unless translated.empty?
roles = [
[/Guard/i, "Стражник"],
[/(Bow|Archer)/i, "Лучник"],
[/Scout/i, "Разведчик"],
[/Warrior/i, "Воин"],
[/Raider/i, "Налётчик"],
[/Bandit/i, "Бандит"],
[/Miner/i, "Шахтёр"],
[/Shaman/i, "Шаман"],
[/(Mage|Witch)/i, "Маг"],
[/Slave/i, "Раб"],
[/Wolf/i, "Волк"],
[/Dog/i, "Собака"]
]
role = roles.find { |pair| raw =~ pair[0] }
return role[1] if role
race = actor.respond_to?(:race) ? actor.race.to_s : ""
race_name = $game_text["DataNpcName:race/#{race}", true].to_s rescue ""
race_name.empty? ? "NPC" : race_name
2026-10-29 23:05:52 +04:00
rescue
"NPC"
end
2026-10-31 03:15:48 +04:00
def self.fit_name(bitmap, npc_name)
2026-10-31 03:07:31 +04:00
name = npc_name.to_s
shortened = false
2026-10-31 03:15:48 +04:00
while name.length > 3 && bitmap.text_size("#{name}#{shortened ? "." : ""}").width > BITMAP_WIDTH - 8
2026-10-31 03:07:31 +04:00
name = name[0...-1]
shortened = true
end
2026-10-31 03:15:48 +04:00
"#{name}#{shortened ? "." : ""}"
2026-10-31 03:07:31 +04:00
rescue
2026-10-31 03:15:48 +04:00
"NPC"
2026-10-31 03:07:31 +04:00
end
2026-10-29 23:05:52 +04:00
def self.level(actor)
2026-10-31 03:07:31 +04:00
data = actor.respond_to?(:npc) ? actor.npc : nil
value = data && data.respond_to?(:combat) ? data.combat.to_i : 0
value = stat(actor, "combat").to_i if value <= 0
[[value, 1].max, MAX_NPC_LEVEL].min
2026-10-29 23:05:52 +04:00
rescue
1
end
2026-10-31 03:07:31 +04:00
def self.level_color(npc_level)
return MAX_LEVEL_COLOR if npc_level.to_i >= MAX_NPC_LEVEL
return MEDIUM_LEVEL_COLOR if npc_level.to_i >= YELLOW_NPC_LEVEL
NORMAL_LEVEL_COLOR
rescue
NORMAL_LEVEL_COLOR
end
2026-10-29 23:02:28 +04:00
end
class Sprite_Character < Sprite_Base
unless method_defined?(:mob_hit_bars_initialize_without)
alias mob_hit_bars_initialize_without initialize
end
def initialize(viewport, character = nil)
mob_hit_bars_initialize_without(viewport, character)
@mob_hit_bars_sprite = Sprite.new(viewport)
2026-10-29 23:05:52 +04:00
@mob_hit_bars_sprite.bitmap = Bitmap.new(MobHitBars::BITMAP_WIDTH, MobHitBars::HEIGHT)
@mob_hit_bars_sprite.ox = MobHitBars::BITMAP_WIDTH / 2
2026-10-29 23:02:28 +04:00
@mob_hit_bars_sprite.oy = MobHitBars::HEIGHT
@mob_hit_bars_sprite.visible = false
@mob_hit_bars_timer = 0
@mob_hit_bars_actor_id = nil
@mob_hit_bars_last_hp = nil
@mob_hit_bars_last_sta = nil
@mob_hit_bars_signature = nil
end
unless method_defined?(:mob_hit_bars_update_without)
alias mob_hit_bars_update_without update
end
def update
result = mob_hit_bars_update_without
update_mob_hit_bars
result
end
def update_mob_hit_bars
sprite = @mob_hit_bars_sprite
return unless sprite
actor = MobHitBars.npc_actor(@character)
unless actor
sprite.visible = false
reset_mob_hit_bars_tracking
return
end
actor_id = actor.object_id
hp = MobHitBars.stat(actor, "health")
sta = MobHitBars.stat(actor, "sta")
hp_max = MobHitBars.stat(actor, "health", ActorStat::MAX_STAT)
sta_max = MobHitBars.stat(actor, "sta", ActorStat::MAX_STAT)
if @mob_hit_bars_actor_id != actor_id
@mob_hit_bars_actor_id = actor_id
@mob_hit_bars_last_hp = hp
@mob_hit_bars_last_sta = sta
@mob_hit_bars_timer = 0
elsif (@mob_hit_bars_last_hp && hp < @mob_hit_bars_last_hp) ||
(@mob_hit_bars_last_sta && sta < @mob_hit_bars_last_sta)
@mob_hit_bars_timer = MobHitBars::VISIBLE_FRAMES
end
@mob_hit_bars_last_hp = hp
@mob_hit_bars_last_sta = sta
if @mob_hit_bars_timer <= 0
sprite.visible = false
return
end
2026-10-29 23:05:52 +04:00
npc_name = MobHitBars.display_name(actor)
npc_level = MobHitBars.level(actor)
signature = [hp, hp_max, sta, sta_max, npc_name, npc_level]
redraw_mob_hit_bars(sprite.bitmap, hp, hp_max, sta, sta_max, npc_name, npc_level) if signature != @mob_hit_bars_signature
2026-10-29 23:02:28 +04:00
@mob_hit_bars_signature = signature
sprite.x = self.x
sprite.y = self.y - (self.oy * self.zoom_y).to_i - MobHitBars::Y_OFFSET
sprite.z = self.z + 100
sprite.opacity = if @mob_hit_bars_timer < MobHitBars::FADE_FRAMES
@mob_hit_bars_timer * 255 / MobHitBars::FADE_FRAMES
else
255
end
sprite.visible = self.visible && !$hudForceHide
@mob_hit_bars_timer -= 1
rescue
@mob_hit_bars_sprite.visible = false if @mob_hit_bars_sprite
end
2026-10-29 23:05:52 +04:00
def redraw_mob_hit_bars(bitmap, hp, hp_max, sta, sta_max, npc_name, npc_level)
2026-10-29 23:02:28 +04:00
bitmap.clear
2026-10-29 23:09:57 +04:00
bitmap.font.name = MobHitBars::LABEL_FONT
bitmap.font.size = MobHitBars::LABEL_FONT_SIZE
bitmap.font.bold = false
2026-10-29 23:05:52 +04:00
bitmap.font.outline = false
2026-10-31 03:07:31 +04:00
bitmap.font.shadow = true
bitmap.font.color = MobHitBars.level_color(npc_level)
2026-10-31 03:15:48 +04:00
bitmap.font.size = MobHitBars::LEVEL_FONT_SIZE
bitmap.draw_text(0, 0, MobHitBars::BITMAP_WIDTH, 18, "Ур. #{npc_level}", 1)
bitmap.font.size = MobHitBars::LABEL_FONT_SIZE
label = MobHitBars.fit_name(bitmap, npc_name)
bitmap.draw_text(0, 17, MobHitBars::BITMAP_WIDTH, 21, label, 1)
2026-10-29 23:05:52 +04:00
x = MobHitBars::BAR_X
2026-10-31 03:15:48 +04:00
bitmap.fill_rect(x, 39, MobHitBars::WIDTH, 4, MobHitBars::BORDER_COLOR)
bitmap.fill_rect(x + 1, 40, MobHitBars::INNER_WIDTH, 2, MobHitBars::BACK_COLOR)
2026-10-29 23:02:28 +04:00
sta_width = (MobHitBars::INNER_WIDTH * MobHitBars.ratio(sta, sta_max)).round
2026-10-31 03:15:48 +04:00
bitmap.fill_rect(x + 1, 40, sta_width, 2, MobHitBars::STAMINA_COLOR) if sta_width > 0
2026-10-29 23:02:28 +04:00
2026-10-31 03:15:48 +04:00
bitmap.fill_rect(x, 44, MobHitBars::WIDTH, 4, MobHitBars::BORDER_COLOR)
bitmap.fill_rect(x + 1, 45, MobHitBars::INNER_WIDTH, 2, MobHitBars::BACK_COLOR)
2026-10-29 23:02:28 +04:00
hp_width = (MobHitBars::INNER_WIDTH * MobHitBars.ratio(hp, hp_max)).round
2026-10-31 03:15:48 +04:00
bitmap.fill_rect(x + 1, 45, hp_width, 2, MobHitBars::HEALTH_COLOR) if hp_width > 0
2026-10-29 23:02:28 +04:00
end
def reset_mob_hit_bars_tracking
@mob_hit_bars_actor_id = nil
@mob_hit_bars_last_hp = nil
@mob_hit_bars_last_sta = nil
@mob_hit_bars_signature = nil
@mob_hit_bars_timer = 0
end
unless method_defined?(:mob_hit_bars_dispose_without)
alias mob_hit_bars_dispose_without dispose
end
def dispose
if @mob_hit_bars_sprite
bitmap = @mob_hit_bars_sprite.bitmap
bitmap.dispose if bitmap && !bitmap.disposed?
@mob_hit_bars_sprite.dispose unless @mob_hit_bars_sprite.disposed?
@mob_hit_bars_sprite = nil
end
mob_hit_bars_dispose_without
end
end
puts "[MOB-HIT-BARS] stamina and health bars enabled" rescue nil