#============================================================================== # LonaCraft - 160_Rest.rb (board R12 + R14, research docs/RESEARCH_REST.md, # owner decisions 2026-10-06: all 12 choices as recommended). # How well Lona sleeps depends on what she sleeps on, and a Bed inside walls # can give the inn's meal and bath from a chest and a tub inside the walls. # What it does: # * Three tiers, decided when the nap starts (research 2.1): # GROUND a nap with no bed (the nap skill anywhere): stamina gain x # "ground_sta_mult" 0.6 and never above "ground_sta_cap_pct" 80 % # of max, health GAIN x "ground_health_mult" 0.5 (hunger damage # unchanged), mood -"ground_mood" 10, dirt +"ground_dirt" 15, # "ground_sick_chance" 10 % for one FeelsSick stack, and # WildDangerous +"ground_danger" 10 before the danger roll (kept). # BEDROLL the Bedroll's Sleep, the nap skill on a Bedroll, the game's # own camp bed (PlayerBed), a Bed outside the walls: the plain nap. # BED a Bed / Dark Bed / Blue Bed whose own tile is inside the walls # (Bases.inside_tiles) or in an owned home: health and stamina to # max before the nap math (as the inn, NoerTavern_Nap.rb:131-132), # mood +"bed_mood" 10, the danger roll sees WildDangerous x # "bed_roll_mult" 0.75 (this roll only, the cut is put back). # R10 (Config/Bases.json "difficulty", 120_Raids.rb diff; hard changes # nothing): "ground_danger" + "ground_danger_add" (hell 15, doom 20), # "ground_sick_chance" + "ground_sick_add" (15 / 20 %), "bed_roll_mult" # + "bed_roll_add" (0.85 / 0.90). The level comes from the config file # only, read once at load, never from the game's mode. # * Meal (BED only, "food_on_bedroll" false): cooked dishes (DISHES, the dark # pot's food recipe outputs in Effects/Alchemy/2000_* and 6000_*) in # storage pieces inside the walls, never Lona's bag. One meal = "meal_sat" # 30 points: the smallest single dish worth that much, else dishes from the # largest down until 30; fewer than 30 points there = nothing taken, no # refill (research 2.2 "Not enough"). Eaten: satiety to max before the nap # math (as NoerTavern_Nap.rb:133); the dish's own effects do not apply. # Mystery and human dishes only with "mystery_dishes" / "human_dishes". # * Bath (BED only, "bath_on_bedroll" false): a piece with the Bathe action # (Water Trough, Wash Tub) inside the walls: the inn's 4 x # batch_take_bath(0.5) (0.25 cuffed) and dirt 0 (NoerTavern_Nap.rb:134-135), # no scene, no EXP. # * Where: base maps and plain field maps (OnRegionMap), owned homes (the # whole house map is inside walls); not while captured or in a rape loop; # the rented inn room is left alone (Homes.nap_keys_active); the Deep Sewer # keeps its own roll (C9). # * Hooks: the furniture Sleep action (lonacraft_furniture_action alias) # notes the bed; Game_Map#call_nap_event and GIM_OM#handleNap note the tier # (the interpreter still runs the bed's event there); GIM_ADDON# # region_map_wildness_nap_stats adds the ground danger / the bed cut; # Game_Actor#process_nap_change (Lona only) does the meal, bath, restore # before and scales the deltas after, around every other mod's nap math. # * Popups after waking (Homes.notice): "Lona ate a meal from the chest.", # "Lona washed before bed.", the ground and bed lines. Console lines give # the numbers. # * Nothing is saved; nothing runs on other maps or for other actors. #============================================================================== module LonaCraft module Rest TEXT = "LonaCraft:Bases:" BED_KINDS = ["Bed", "BedDark", "BedBlue"] BEDROLL_KINDS = ["PottedPlant"] # the Bedroll (90_Furniture.rb KINDS) CAMP_BED_EVENT = "PlayerBed" # the game's camp bed (Action_BuildCamp.rb) FRESH_FRAMES = 1800 # a noted tier is valid this long # item_name => [satiety, group] (research 1.7; read again from the item # at run time when it has a "sat" effect) DISHES = { "ItemMeatSalad" => [80, :plain], "ItemSopGood" => [32, :plain], "ItemSmokedMeat" => [26, :plain], "ItemSopMeat" => [16, :plain], "ItemSopPlant" => [16, :plain], "ItemDryFood" => [16, :plain], "ItemSopGoodMystery" => [24, :mystery], "ItemSmokedMeatMystery" => [18, :mystery], "ItemSopMeatMystery" => [12, :mystery], "ItemSopGoodHuman" => [32, :human], "ItemSmokedMeatHuman" => [32, :human], "ItemSopMeatHuman" => [16, :human], "ItemSopGoodHumanMystery" => [24, :human], "ItemSmokedMeatHumanMystery" => [18, :human], "ItemSopMeatHumanMystery" => [12, :human] } MEAT_SALAD_ID = 153 # food_SF_MeatSalad.json:2 has "ItemOil" for "item_name" (game data typo) @pending = nil # the bed whose Sleep was picked @ctx = nil # the tier of the nap about to happen def self.say(text) p "LonaCraft Rest: #{text}" end def self.text_key(key) "#{TEXT}#{key}" end def self.cfg LonaCraft::Raids.cfg["rest"] end def self.enabled? defined?(LonaCraft::Raids) && defined?(LonaCraft::Furniture) && cfg["enabled"] ? true : false end def self.bases LonaCraft::Bases end def self.fur LonaCraft::Furniture end def self.homes defined?(LonaCraft::Homes) ? LonaCraft::Homes : nil end #---------------------------------------------------------------------- # Where tiers apply #---------------------------------------------------------------------- def self.owned_home?(map) h = homes return false if h.nil? || !h.enabled? id = h.home_at_map?(map) !id.nil? && h.owned?(id) rescue false end def self.scope_ok?(map) return false if map.nil? || map.isOverMap return false if $story_stats["Captured"].to_i != 0 || $story_stats["RapeLoop"].to_i != 0 return false if homes && homes.nap_keys_active return true if $story_stats["OnRegionMap"].to_i >= 1 return true if bases.record_for_map(map) owned_home?(map) end # {[x, y] => true} of the tiles inside the walls of +map+, :all for an # owned home, {} otherwise def self.inside(map) return :all if owned_home?(map) rec = bases.record_for_map(map) return {} if rec.nil? rm = map.instance_variable_get(:@region_map) bases.inside_tiles(map, rec, rm.is_a?(Array) ? rm : []) end def self.inside?(ins, x, y) ins == :all || (ins.is_a?(Hash) && ins[[x.to_i, y.to_i]]) end def self.records_on(map) fur.records.select{|f| f.is_a?(Hash) && f["map_id"] == map.map_id } end #---------------------------------------------------------------------- # The tier #---------------------------------------------------------------------- # 90_Furniture.rb Sleep on a placed piece def self.note_sleep(rec, kind) return if rec.nil? || kind.nil? @pending = {:map => $game_map.map_id_rec, :x => $game_player.x, :y => $game_player.y, :frame => Graphics.frame_count, :uid => rec["uid"].to_i, :key => kind[:key].to_s} end def self.fresh?(h) h.is_a?(Hash) && h[:map] == $game_map.map_id_rec && h[:x] == $game_player.x && h[:y] == $game_player.y && Graphics.frame_count - h[:frame].to_i <= FRESH_FRAMES end # :ground, :bedroll or :bed for a nap started by +interp+ now; nil = untouched def self.detect(interp) map = $game_map return nil unless enabled? && scope_ok?(map) pend = fresh?(@pending) ? @pending : nil @pending = nil if pend return :bedroll if BEDROLL_KINDS.include?(pend[:key]) if BED_KINDS.include?(pend[:key]) rec = fur.record(pend[:uid]) return :bedroll if rec.nil? return inside?(inside(map), rec["x"], rec["y"]) ? :bed : :bedroll end end ev = (interp && interp.respond_to?(:event_id)) ? map.events[interp.event_id] : nil return :bedroll if ev && ev.respond_to?(:name) && ev.name.to_s == CAMP_BED_EVENT px = $game_player.x py = $game_player.y on_roll = records_on(map).any?{|f| k = fur.kind_for(f["item_name"]) k && BEDROLL_KINDS.include?(k[:key]) && fur.footprint(k, f["x"].to_i, f["y"].to_i).include?([px, py]) } on_roll ? :bedroll : :ground rescue => e say("tier error #{e.class} #{e.message}") nil end # call_nap_event / handleNap: note the tier unless one is noted already def self.capture(interp) return if fresh?(@ctx) tier = detect(interp) @ctx = tier ? {:tier => tier, :map => $game_map.map_id_rec, :x => $game_player.x, :y => $game_player.y, :frame => Graphics.frame_count} : nil say("nap tier #{tier}") if tier end def self.tier fresh?(@ctx) ? @ctx[:tier] : nil end def self.clear @ctx = nil end #---------------------------------------------------------------------- # The danger roll (GIM_ADDON alias below) #---------------------------------------------------------------------- def self.before_roll case tier when :ground add = cfg["ground_danger"].to_i + LonaCraft::Raids.diff("ground_danger_add").to_i # R10 $story_stats["WildDangerous"] += add say("ground nap: WildDangerous +#{add} -> #{$story_stats["WildDangerous"]}") nil when :bed pre = $story_stats["WildDangerous"].to_f mult = [cfg["bed_roll_mult"].to_f + LonaCraft::Raids.diff("bed_roll_add").to_f, 0.0].max # R10 cut = pre * (1.0 - mult) $story_stats["WildDangerous"] = pre - cut say("bed nap: WildDangerous #{pre.round(1)} x#{mult} for this roll") cut end end def self.after_roll(cut) $story_stats["WildDangerous"] += cut if cut end #---------------------------------------------------------------------- # Dishes #---------------------------------------------------------------------- def self.dish_name(obj) return "ItemMeatSalad" if obj.is_a?(RPG::Item) && obj.id == MEAT_SALAD_ID n = obj.respond_to?(:item_name) ? obj.item_name.to_s : "" DISHES.key?(n) ? n : nil end def self.dish_ok?(name) row = DISHES[name] return false if row.nil? case row[1] when :mystery then cfg["mystery_dishes"] ? true : false when :human then cfg["human_dishes"] ? true : false else true end end def self.satiety_of(obj, name) if obj.respond_to?(:use_item_effect) && obj.use_item_effect.is_a?(Array) le = obj.use_item_effect.find{|e| e && e.respond_to?(:attr) && e.attr.to_s == "sat" && e.stats_key.nil? } return le.adjust.to_i if le && le.respond_to?(:adjust) && le.adjust.to_f > 0 end DISHES[name][0] rescue DISHES[name][0] end # [[obj, name, sat, box id], ...] one row per unit, chests in record order def self.dishes_inside(map, ins) out = [] records_on(map).each{|f| k = fur.kind_for(f["item_name"]) next unless k && k[:storage] && inside?(ins, f["x"], f["y"]) box = fur.box_contents(f["box"].to_i) next unless box.is_a?(Hash) box.each{|obj, n| name = dish_name(obj) next if name.nil? || !dish_ok?(name) sat = satiety_of(obj, name) n.to_i.times{ out << [obj, name, sat, f["box"].to_i] } } } out end # the units that make one meal of +need+ points, or nil (research 2.2) def self.pick_meal(units, need) single = units.select{|u| u[2] >= need }.min_by{|u| u[2] } return [single] if single taken = [] total = 0 units.each_with_index.sort_by{|u, i| [-u[2], i] }.each{|u, i| break if total >= need taken << u total += u[2] } total >= need ? taken : nil end def self.bath_inside?(map, ins) records_on(map).any?{|f| k = fur.kind_for(f["item_name"]) k && (k[:actions] || []).include?(:bathe) && inside?(ins, f["x"], f["y"]) } end def self.notice(key) homes.notice(text_key(key)) if homes end #---------------------------------------------------------------------- # The nap (Game_Actor#process_nap_change alias below) #---------------------------------------------------------------------- # before the nap math; returns the state for after_nap, or nil def self.before_nap(actor) t = tier clear return nil if t.nil? || !enabled? return nil if homes && homes.nap_keys_active # the inn room: its own rest map = $game_map st = {:tier => t, :sta => actor.sta.to_f, :health => actor.health.to_f} if t == :bed dim = actor.attr_dimensions actor.health = dim["health"][2] actor.sta = dim["sta"][2] end fed = t == :bed || cfg["food_on_bedroll"] wash = t == :bed || cfg["bath_on_bedroll"] if t != :ground && (fed || wash) ins = inside(map) if fed units = dishes_inside(map, ins) meal = pick_meal(units, cfg["meal_sat"].to_i) if meal meal.each{|obj, name, sat, box| $game_boxes.remove_item(obj, 1, box, false) } actor.sat = actor.attr_dimensions["sat"][2] notice("Rest/ate") say("meal from the chest: #{meal.map{|u| u[1] }.join(", ")} (#{meal.inject(0){|s, u| s + u[2] }} points)") else say("no meal: #{units.inject(0){|s, u| s + u[2] }} points of dishes inside the walls") end end if wash && bath_inside?(map, ins) scale = actor.respond_to?(:is_cuffed?) && actor.is_cuffed? ? 0.25 : 0.5 4.times{ actor.batch_take_bath(scale) } actor.dirt = 0 notice("Rest/bathed") say("washed before bed") end end st rescue => e say("before nap error #{e.class} #{e.message}") nil end def self.after_nap(actor, st) return if st.nil? case st[:tier] when :ground dim = actor.attr_dimensions gain = actor.sta.to_f - st[:sta] if gain > 0 cap = dim["sta"][2].to_f * cfg["ground_sta_cap_pct"].to_f / 100.0 actor.sta = [st[:sta] + gain * cfg["ground_sta_mult"].to_f, [cap, st[:sta]].max].min.round end hgain = actor.health.to_f - st[:health] actor.health = (st[:health] + hgain * cfg["ground_health_mult"].to_f).round if hgain > 0 actor.mood -= cfg["ground_mood"].to_i actor.dirt += cfg["ground_dirt"].to_i sick = rand(100) < cfg["ground_sick_chance"].to_i + LonaCraft::Raids.diff("ground_sick_add").to_i # R10 actor.add_state("FeelsSick") if sick notice("Rest/ground") say("ground: stamina #{st[:sta].round} -> #{actor.sta.round} (gain #{gain.round} x#{cfg["ground_sta_mult"]}), health #{st[:health].round} -> #{actor.health.round}#{sick ? ", feels sick" : ""}") when :bed actor.mood += cfg["bed_mood"].to_i notice("Rest/bed") say("bed: health #{actor.health.round}, stamina #{actor.sta.round}, satiety #{actor.sat.round}") else say("bedroll: a plain nap") end rescue => e say("after nap error #{e.class} #{e.message}") end end end #============================================================================== # The hooks #============================================================================== class Game_Interpreter # the furniture Sleep action notes the bed (90_Furniture.rb:1123-1127) if method_defined?(:lonacraft_furniture_action) && !method_defined?(:lonacraft_furniture_action_rest) alias_method :lonacraft_furniture_action_rest, :lonacraft_furniture_action def lonacraft_furniture_action(action, kind, rec) begin LonaCraft::Rest.note_sleep(rec, kind) if action == :sleep rescue => e p "LonaCraft Rest sleep note error #{e.class} #{e.message}" end lonacraft_furniture_action_rest(action, kind, rec) end end end module GIM_OM if method_defined?(:handleNap) && !method_defined?(:handleNap_lonacraft_rest) alias_method :handleNap_lonacraft_rest, :handleNap def handleNap(*args) begin LonaCraft::Rest.capture(self) rescue => e p "LonaCraft Rest capture error #{e.class} #{e.message}" end begin handleNap_lonacraft_rest(*args) ensure LonaCraft::Rest.clear end end end end class Game_Map if method_defined?(:call_nap_event) && !method_defined?(:call_nap_event_lonacraft_rest) alias_method :call_nap_event_lonacraft_rest, :call_nap_event def call_nap_event begin LonaCraft::Rest.capture(interpreter) unless LonaCraft::Raids.nap_refused? rescue => e p "LonaCraft Rest capture error #{e.class} #{e.message}" end call_nap_event_lonacraft_rest end end end module GIM_ADDON if method_defined?(:region_map_wildness_nap_stats) && !method_defined?(:lonacraft_rest_nap_stats) alias_method :lonacraft_rest_nap_stats, :region_map_wildness_nap_stats def region_map_wildness_nap_stats cut = nil begin cut = LonaCraft::Rest.before_roll rescue => e p "LonaCraft Rest roll error #{e.class} #{e.message}" end begin lonacraft_rest_nap_stats ensure LonaCraft::Rest.after_roll(cut) end end end end class Game_Actor if method_defined?(:process_nap_change) && !method_defined?(:process_nap_change_lonacraft_rest) alias_method :process_nap_change_lonacraft_rest, :process_nap_change def process_nap_change(*args) st = nil st = LonaCraft::Rest.before_nap(self) if $game_player && equal?($game_player.actor) result = process_nap_change_lonacraft_rest(*args) LonaCraft::Rest.after_nap(self, st) if st result end end end