Code:
# * KLastDefeat XP * #
# Scripter : Kyonides
# 2025-08-21
# * THIS IS NOT A PLUG & PLAY SCRIPT * #
# This scriptlet allows you to automatically clone a template event that
# will be used to mark the last place where you were killed by mobs.
# The KLastDefeat module includes several CONSTANTS that you should edit to let
# the scriptlet find and clone your template event whenever it is needed.
# By using a script call, you can retrieve the EXP you had lost there.
# * Script Calls * #
# - Set a custom Lose EXP Percent Before Battle starts
# $game_temp.lose_exp_percent = 25
# - Let the party retrieve the EXP they had lost on that map.
# $game_temp.remove_last_spot
module KLastDefeat
START_LOSE_EXP_PERCENT = 1
EVENT_MAP_ID = 1
EVENT_ID = 5
CLONE_EVENT_ID = 1000
class EventData
def initialize(map_id, event_id)
@map_id = map_id
@id = event_id
end
attr_accessor :map_id, :id
end
extend self
def setup_event_data
EventData.new(EVENT_MAP_ID, EVENT_ID)
end
def remove_event_id?(event_id)
CLONE_EVENT_ID == event_id
end
end
class Game_Temp
alias :kyon_last_dft_gm_tmp_init :initialize
def initialize
kyon_last_dft_gm_tmp_init
@last_spot_event = KLastDefeat.setup_event_data
@lose_exp_percent = KLastDefeat::START_LOSE_EXP_PERCENT
clear_last_spot
end
def clear_last_spot
@last_spot_map_id = 0
@last_spot_xy = []
@lost_exp = []
end
def last_spot?
@last_spot_map_id > 0 and @player_new_map_id == @last_spot_map_id
end
def remove_last_spot
lost_exp = @lost_exp.map {|exp| exp.abs }
$game_party.actors_change_exp(lost_exp)
clear_last_spot
end
attr_accessor :last_spot_event, :last_spot_map_id, :last_spot_xy
attr_accessor :lose_exp_percent, :lost_exp
end
class Game_Party
def actors_exp_percent(percent)
@actors.map {|actor| actor.exp * percent / 100 }
end
def actors_change_exp(exp)
if exp.is_a?(Numeric)
@actors.each {|actor| actor.exp += exp }
else
@actors.each_with_index {|actor, n| actor.exp += exp[n] }
end
end
end
class Game_Map
alias :kyon_last_dft_gm_map_stp :setup
def setup(map_id)
kyon_last_dft_gm_map_stp(map_id)
setup_last_spot if $game_temp.last_spot_map_id == @map_id
end
def setup_last_spot
mx, my = $game_temp.last_spot_xy
event = $game_temp.last_spot_event
if event.map_id == @map_id
all_events = @map.events
else
all_events = load_data(sprintf("Data/Map%03d.rxdata", event.map_id))
end
event = all_events[event.id].dup
event.id = KLastDefeat::CLONE_EVENT_ID
event.x = mx
event.y = my
@events[event.id] = Game_Event.new(@map_id, event)
end
def delete_last_spot
@events.delete(KLastDefeat::CLONE_EVENT_ID)
end
end
class Game_Character
def xy
[@x, @y]
end
end
class Interpreter
alias :kyon_last_dft_inter_transfer_player :command_201
alias :kyon_last_dft_inter_command_end :command_end
def command_end
kyon_last_dft_inter_command_end
$game_map.delete_last_spot if KLastDefeat.remove_event_id?(@event_id)
end
def command_201
result = kyon_last_dft_inter_transfer_player
$game_map.setup_last_spot if $game_temp.last_spot?
result
end
end
class Scene_Battle
alias :kyon_last_dft_scn_btl_btl_end :battle_end
def process_last_spot
percent = -$game_temp.lose_exp_percent
lost_exp = $game_party.actors_exp_percent(percent)
$game_party.actors_change_exp(lost_exp)
$game_temp.lost_exp = lost_exp
$game_temp.last_spot_map_id = $game_map.map_id
$game_temp.last_spot_xy = $game_player.xy
end
def battle_end(result)
process_last_spot if result == 2
kyon_last_dft_scn_btl_btl_end(result)
end
end