Script needed to turn back time in a battle

Hey guys, so I need a script that is able to reverse time a set amount of turns in a battle, allow me to elaborate.

   In my game a specific character has the ability to turn back time, and I'd like to implement that into the battle system of my game as a skill. All damage taken stause effects items used etc done within those turns would be reset.

  Example by turn 4 character B is dead and character C had taken a lot of damage character A uses this skill and returns to turn 2 of the battle all dead enemies and character return potions used are given back to the player etc, however character A who cast the spell receives a set amount of damage and is unable to move for the amount of turns reset plus one. If possible I'd like there to be three variants to the skill one that returns 2 turns 4 turns and 6 turns. ( the skill will be unavailable until a minimum of three turns have passed) Edit: So in order for the player to reset 2 turns 3 must have passed, for 4, 5 must have passed etc.

I am using victor's animated battle and actor battler scripts. both of which can be found here https://victorscripts.wordpress.com  (not sure if this matters but I figure it's best to mention it)

I personally have very little experience writing scripts so I don't know how difficult this will be, so I apologize if I am asking for too much to be done, especially for free.

If you need anymore information, other scripts I am using, anything at all really just ask.

Thank you in advance 😀
Turn back time, eh?  Somebody played Life Is Strange recently, hmmmm?

Actually, time travel sounds like an awesome game mechanic.  I love Star Trek, and their episodes involving time travel are among my favorites.  I don't know if I can pull it off, but I want to write a space epic RPG (it won't be Star Trek though, it'll be my own universe and characters, don't want any copyright or trademark problems).  I want to include some time travel elements in it, but I'm not sure how to pull it off yet.

Unless you limit your game to just a few locations, and two three time periods, you'd have to multiply the number of levels by a ridiculous number.  For a true feeling that you're time traveling in the game, you'd need at least 5 versions of each city and location: current, past, far past, future, and far future.  Even more if you want your time machine or spaceship to be able to select a specific year from, say, 1800 to 2200 or so.  The number of levels you'd need to truly simulate time travel would be insane!  And then aging your NPCs and keeping track of when you had conversations with each: go to year 1900 and have a conversation with NPC X.  Your next interaction with NPC X would be different depending on whether you go back before 1900 or after 1900.
Good timing. I plan to script a time leap effect in battle. If there isn't already something for that you can

wait until i finish it.
There's at least 8-9 classes involved to make this skill works. So it can turn out to be a long script.

Going to need a databank/array to record informations that's getting bigger and bigger each turn whether the skill is used or not.

Well, since graphic isn't involved, the worry about memory low crash isn't there, but still going to get bigger and bigger the longer the battle goes.

Because this has to be done with EVERY battle and recording starting from turn 1 and EVERY turn after that, just to get ready to make the skill works.

Do-able, but not code wise imo.. Changing the trigger to cast skill, and 3 turns later, time reverts might save you a lot of resources.

Well, since this is a new concept. I bet some script maker out there will want to give it a try.
@meow I am sure when they make the script it will allow for a setting in a skill that is a fixed amount of turns.

Say a skill "Time Reverse" could reverse 3 turns if turn >= 3.

This way it can start disposing information after 3 turns, thus information would hardly stack.

Pretty straight forward. Definitely possible but a time-consuming script to make for sure.
@MeowFace Most of the information needed can be accessed through @subject in the Scene_Battle script (specifically the method process_action part). My skill usage script records what skills were used in a similar manner to how I think this script would be written.
@Stapleton, yup, a turn delayed skill can work to save the trouble of saving data each turn. But if he wants it to trigger right away and any time in any battle. The recorder has to start from turn 0-1 to get ready for the skill cast.

@Sarlecc, Damage is disposed or rather, reset to 0 after it had done its job. So you will need to tap into that part to save each damage, and sum it up each turn into the array. Items usage need to be recorded by "each" turn to enable the "revert" of time. So like it or not the stack gets bigger when the battle is prolonged. Array can be easily cleared when the battle ends. You can't keep and overwrites the information as "current", else there won't be a "past"  to return to.

Edit:

Sorry Stapleton, misread what you meant. Yes, that can be done when we know how many turns the revert is. So the hash can be kept at 6 turns record(if the biggest revert a skill has is only 6 turns) and overwrite the old datas pushing out the oldest.
I said similar manner to my script 😉 . My script uses multidimensional arrays to store data since it didn't need turn information I did not store that. @subject has information on damage if I remember correctly (use @subject.inspect to see all available variables). Which would be available at the time the skill/item was used. However it may be easier to do it on the health left at the end of the turn. So basically you would have an array look something like this (note this is an extremely basic one):

a = [["Eric", 1, 50,], ["Eric", 2, 40]]#Name, turn, hp#All of this data can be obtained from @subject except turn which you can increment yourselfIn fact the data I would store would be the subject's name, health, buffs and items used (to refund them when this skill is cast) and of course the turn number. No need to know which skills were cast that I can see (unless they are limited use skills).
Why would you store the damage at all? Or item usage for that matter? Or even bother with making complicated arrays?


You can simply record the enemies and actors (the whole instance of them, so you save their HP/states/etc and even custom variables from custom scripts) and the party's items, and basically that's it. Record it for a maximum of 7 turns (since the OP mentioned a max of 6 turns reversed). The newer ones replace the older ones.


At the end of the battle, get rid of everything if needed.


When the skill is used, replace the current battle data with a stored data, call a refresh on every graphic element, put in a fade in effect or something for show, do the damage on the user, and at the end, seal the skill for X turns.
Just had a thought but would you even need to record information from every turn? Like couldn't you record the information every 7th turn?

@sixth I just like overthinking problems too much sometimes. 😛 But you are right with this script saving the entire instance would probably be a whole lot easier. 
@Sarlecc If you recorded only using variables of seven, what would happen if the player chose to only reset two turns there would be no stored data for turns 2, 3 ,4, 5,6,8 etc.
Just had a thought but would you even need to record information from every turn? Like couldn't you record the information every 7th turn?

@sixth I just like overthinking problems too much sometimes. 😛 But you are right with this script saving the entire instance would probably be a whole lot easier. 
Every turn is needed to "get ready" for a skill that can be cast any time in a battle. Recording every 7th turn will not work.

eg. Skill used at the 8th turn can only return 1 turn to the "past".

But like what i mentioned. If it's a skill with delayed effect, then yes, only start recording when the skill is activated and do whatever revert a few turns later. And no, it doesn't have to be 7 too, if the skill max is 6, 6 will do.

 

Why would you store the damage at all? Or item usage for that matter? Or even bother with making complicated arrays?
Certain compatibility issues with custom scripts is the problem Sixth, what you suggested is storing 2x7 sets of library data in a "simple" array. But in fact, it's the same as Sarlecc's method if we break them down further, because each library still has its params that we can tab into. Sarlecc is only storing the needed information, memory wise.

Imagine a game with instant items, or a battle system that allows you to change equipment during battle. 

The battle will end up storing a huge amount of unwanted datas for a game that generate items of their own. 

And restoring everything in a huge dump won't work for battle that allows the change of equipment during battle, the player will either end up with extra or missing item. So in the end, it  will still require to restore "part" of the data by calling the params of each "library". So nope, Sarlecc's method will proves to be easier and much efficient in the long run.
The problem with storing just the needed information is that the array will be a bit harder to work with. Plus the method argument is a lot longer and loses readability. Whereas the the problem with storing the entire @subject object is like you said storing everything you need plus everything you don't (as some of it doesn't even pertain to battle like the actor's nickname). However it's method argument will be much, much shorter and has the advantage of being highly readable.

#Examples@array = []@array2 = []#edit: also remembered you could do it like this# def add_certain_data(subject)# @array.push([subject.name, subject.hp, subject.mp, subject.tp, subject.buffs, etc])# enddef add_certain_data(name, hp, mp, tp, buffs, etc) @array.push([name, hp, mp, tp, buffs, etc])end#to get information from @array you would do something likeputs @array[0][0] #to get the name#vsdef add_instance_data(subject) @array2.push(subject)end#to get information from @array2 you would do thisputs @array2[0].name #to get the name
@Sarlecc If you recorded only using variables of seven, what would happen if the player chose to only reset two turns there would be no stored data for turns 2, 3 ,4, 5,6,8 etc
Every turn is needed to "get ready" for a skill that can be cast any time in a battle. Recording every 7th turn will not work.

eg. Skill used at the 8th turn can only return 1 turn to the "past".

But like what i mentioned. If it's a skill with delayed effect, then yes, only start recording when the skill is activated and do whatever revert a few turns later. And no, it doesn't have to be 7 too, if the skill max is 6, 6 will do.
If you were to start the recording at turn 1 and say the player decided on turn 3 they didn't like what they did they could use the skill to revert to turn 1. Then the skill might have a cool down of x turns so the next time that they could use the skill would be at a turn greater than x. Which when used would send them back to turn x. This is of course assuming that there is no player input that decides which turn they want to end up on. (hopefully I make sense 😛 )
Well, what Sixth said about recording the current and restoring it "as is" should work without the need of too much math with the damages and such. So your method should be able to extract the informations easily. Haven't been using @subject and not having any time to look it up for now, i am not sure how it works. But i am just going to assume it calls for game_troop/game_party for informations.

So something like this can work

$game_party.battle_members[N].hp

$game_party.battle_members[N].tp

And depending on the available battle members (normally max 4, 0~3), the data needed isn't much i guess.

Haven't tried it before, but i am wondering if it's possible to revert a state's timer? (Replacing a state and makes it start half way)

So the database format saved will be something like this for each turn?

[[+- accumulative item's id used],[[hp,mp,tp,[states],[state's timers]],[[hp,mp,tp,[states],[state's timers]],[[hp,mp,tp,[states],[state's timers]],[[hp,mp,tp,[states],[state's timers]]]

And i am not sure if there's a need to revert the time on the enemies, it there is.. the data will be much larger.

But those still can be handled and access by:

$game_troop.members[N].hp

$game_troop.members[N].mp

etc.

As for your save turn suggestion... can't get a grasp of what you are trying to achieve there.

Do you mean since the skill has a cool down timer, it can be done by simply saving the turns based on the cool down gaps? Well, you can do that i guess, but will need need to store the data starting from turn 1.

But then again, OP is asking for a "few" skills that revert different turns.. that won't work unless all time revert skills share the same cool down timer.
Haven't tried it before, but i am wondering if it's possible to revert a state's timer? (Replacing a state and makes it start half way)
Same haven't tried it before though I am thinking it's possible.

Haven't been using @subject
Spoiler
#everything from @subject.inspect#<Game_Actor:0x8d6d1f4 @battler_name="", @battler_hue=0, @actions=[#<Game_Action:0x8f8b9b8 @subject=#<Game_Actor:0x8d6d1f4 ...>, @forcing=false, @item=#<Game_BaseItem:0x8f8b9a4 @class=RPG::Skill, @item_id=1>, @target_index=0, @value=0>], @speed=516.0, @result=#<Game_ActionResult:0x8d6d1b8 @battler=#<Game_Actor:0x8d6d1f4 ...>, @used=false, @missed=false, @evaded=false, @critical=false, @success=false, @hp_damage=0, @mp_damage=0, @tp_damage=0, @hp_drain=0, @mp_drain=0, @added_states=[], @removed_states=[], @added_buffs=[], @added_debuffs=[], @removed_buffs=[]>, @last_target_index=0, @guarding=false, @animation_id=0, @animation_mirror=false, @sprite_effect_type=nil, @tp=0.36030310737648064, @mp=861, @hp=8788, @hidden=false, @param_plus=[0, 0, 0, 0, 0, 0, 0, 0], @states=[], @state_turns={}, @state_steps={}, @buffs=[0, 0, 0, 0, 0, 0, 0, 0], @buff_turns={}, @actor_id=2, @name="Natalie", @nickname="Thunder Fist", @character_name="Actor4", @character_index=1, @face_name="Actor4", @face_index=1, @class_id=2, @level=99, @exp={2=>2547133}, @equips=[#<Game_BaseItem:0x8d6ccb8 @class=RPG::Weapon, @item_id=7>, #<Game_BaseItem:0x8d6cca4 @class=nil, @item_id=0>, #<Game_BaseItem:0x8d6cc90 @class=nil, @item_id=0>, #<Game_BaseItem:0x8d6cc7c @class=RPG::Armor, @item_id=1>, #<Game_BaseItem:0x8d6cc68 @class=nil, @item_id=0>], @skills=[85, 86, 87, 88, 89], @action_input_index=0, @last_skill=#<Game_BaseItem:0x8d52bd8 @class=nil, @item_id=0>>
As you can see you can call tons of information from it. It's one of the most helpful variables I have found. 🙂

Yea pretty much. Basically this can be done by doing something like this:

if $game_troop.turn_count % x === 0 || $game_troop.turn_count == 1endEither way the biggest problem I am having is how many turns should be stored. Should they all be stored then remove the data when the skill is used? Should I store a certain number of turns say 6 for example? Or should I work it off storing only certain turns like the above example?

Edit: Btw I'm having fun working on this 😀
Either way the biggest problem I am having is how many turns should be stored. Should they all be stored then remove the data when the skill is used? Should I store a certain number of turns say 6 for example? Or should I work it off storing only certain turns like the above example?

Edit: Btw I'm having fun working on this 😀
6 should work since the max revert is 6 turns. so the other skills that revert 2 and 4 is within range.

Recording based on cool down gaps can only work when there's 1 skill with 1 revert time & cool down time.

But when it comes to 3 skills... the OP requested 3 skills with 2, 4, 6 turns reverted.. and the cool down is on 3, 5 turns...

I can't see how it's possible to do that without recording every turns... well.. might be able to pull this off on every 2 turns if the cool down timer isn't singular... edit: you still need to record items used every turn though..

And yup! Fun is the the most important factor to start writing a script! I am glad you are having fun with it! 😉
Initially, what I intended to do is to allow a skill to "lay" a Time Point at the turn which it's used.

Then, the player can use a Time Leap skill to return at that Point.

If @sarlecc is working on it then i'll pass.
@MeowFace I forgot about the item usage lol. 😛 Though if I could record say the party inventory vs items used I could just restore it to that.

@Lecode so with the way you were thinking of doing it would be to have two skills? One to record the turn information and another one to go to that turn? (Wonders what type of options I should include in the config now).
The reason to avoid storing the whole party inventory is already explained somewhere above i think...

The inventory is the biggest data set compared to all, and if the game is using instant item, that will mean it can easily exceed hundreds or even thousands of items, each with their own unique id. No fun storing/restoring that.

And if the game enables the equip change in battle.. you know how vx ace works with its inventory.. a revert there will be disastrous unless you save the equip informations as well and restore them along with the item dump restore. So you need to check with the OP if those 2 scripts are used.

And if those aren't being use, you might even be able to reduce the size by storing items by category/tag, it's up to you to implement that. 😉

Edit: You can even restrict the items to be used in a battle. Somewhere under class RPG::UsableItem < RPG::BaseItem

Just thought you might want to publish the script elsewhere for public use. Because if that's the case, you will need those 2 (instant items/battle equip) planned into how you store/restore the informations.

------

Simply storing Item used isn't hard i guess, just record the ids of the item used each turn. And the max IDs to be stored each turn is 4 max anyway 😉

During restore you will need to return the items by ids in each turns being restored to the player's inventory.
I had implemented reverse time logic before, but I don't think it was very flexible.


First I used a stack to store all of the actions that occurred via Action History.


Then I used this stack to "reverse" time, where my definition of reversing time was simply reversing all of the actions that took place. This makes it pretty simple to manipulate time since all you have to do is adjust where in the stack you currently are, and adjust the battle accordingly.


Of course, now that I think about the problem again, the implementation was severely lacking.

Spoiler
1. An action history makes sense. Storing it with each battler, does not. This is something that should probably be stored as a separate object, perhaps maintained by the battle manager.
2. It would be better to abstract each "Action". For example, you have


- "use skill" actions. This is recorded when you use a skill.


- "use item" actions. This is recorded when you use an item.


You could then define custom actions as needed. For example


- "change equip" actions.


3. Each action would store the "results" of the action, much like the current Game_Action and Game_ActionResult setup. However, now you will need to figure out what "results" you want to store, since changing equips is something you're going to have to handle based on the scripts you're using.


4. Once an abstract action object is defined, you could then provide an interface method called "reverse" or something and then simply implement the logic for each type of action.


For example, if Eric uses a Bomb on a Slime and it died, you will note that


-Eric used 5 TP


-Eric consumed a Bomb


-Slime lost 97413 HP


-Slime died (not through state add, but through HP loss)


You would then proceed to reverse each of them by recovering Eric's 5 TP, giving Eric back his bomb, recovering Slime's 97413 HP, and making sure it is now alive again.


5. My reverse time does not handle event processing. You can have an event do arbitrarily anything, and this is...pretty hard to "reverse" in a general way. I don't know what you would do here besides forcing developers to provide a second, "reverse event" that basically does the opposite of what they did.
However, the idea above isn't "reversing time", it's simply reversing each action that was performed.


The easiest way I can think of is to just take snapshots of every moment that you would allow people to go to, and then just use reset the game based on the snapshot. It's pretty much like what emulators are doing when you save snapshots (which I think would be a really cool script to have as well) . You don't need to worry about *how* things happened; you only need to worry about *what* things were like at that point in time.


If the snapshot captures everything, it should support all scripts.


And I don't think it would be that bad on memory consumption, especially if you compress the data.

Original thread: https://forums.rpgmakerweb.com/threads/script-needed-to-turn-back-time-in-a-battle.44889/