Troop Placeholders

troopPlaceholders1

This script allows you to create troop “placeholders”. These are special types of enemies that will be replaced with an actual enemy when they are
encountered, allowing you to create random troops while managing them using the troop editor.

Each troop contains a set of “placeholder rules”. These rules are used to determine how the placeholders can be populated. The rules come with a number
of options, and more options can be added to it if necessary.

By creating a set of placeholder rules, along with the use of placeholders, you can effectively produce random encounters.

Download

Script – Demo

Installation

Place this script below Materials and above Main

Description

A placeholder enemy consists of a sprite, which is used as a visual reference in the troop editor, and a placeholder name.

Placeholder rules determine whether an enemy can be placed in a placeholder spot or not based on the placeholder name. You can use placeholder tags to restrict certain enemies from certain spots.

For example, if you have three placeholders whose names are

-Small
-Medium
-Large

And you have a rule that says an enemy can only be placed in “Large” placeholders, then it cannot be placed in any other placeholder spot.

Usage

Create placeholders

To create a placeholder enemy, notetag it with

<placeholder: name>

The name can be anything you want. You should avoid using very long names
You should also include a graphic for your placeholder enemy.

troopPlaceholders2

Setup Troop Placeholders

In the troop editor, you can visually place the placeholders.

troopPlaceholders3

You can choose to mix enemies with placeholders if you want certain enemies
to always appear. Only placeholder enemies will be replaced.

troopPlaceholders5

Setup Placeholder Rules

In the troop event editor, create a comment of the form

<placeholder rule: id>
  weight: 50; tags: small, medium
</placeholder rule>

Where id is the ID of the enemy this rule applies to. You can set the ID to 0 if you want a chance to replace the placeholder with no enemy.

Notice that the rule options come in pairs of the form

option: value
option: value1, value2
option1: value1; option2: value2

And are separated by semi-colons.
See the reference section for information on each option

troopPlaceholders4

Reference

This is the list of options available and what they mean.

Weight

Probability that this enemy will be selected. This weight is compared
against weights of all other rules

Tags

Which placeholders this enemy can be placed in. The enemy can only be placed in placeholders whose name matches one of these tags. You can have multiple tags for a single rule.

You may also like...

17 Responses

  1. naseweiss says:

    So its intended to generate empty battles?
    I was just wondering, i dont want to offend you. 😉

    • Hime says:

      If none of the conditions are met then there won't be any enemies.

      • naseweiss says:

        Mh, probably ok.
        Got the hang of it after a few tries and it seems to work fine so far. Saved a lot of work and database entries, really like your script. Good work!

        Judging from a quick view on the source code it should be compatible with every script ive seen/ use. Great. 🙂

  2. naseweiss says:

    The problem is, your script can generate empty battles. In an clean project it instantly goes to victory, but which Yanfly Scripts (suppose its Battle Engine) i get the divison by zero error.
    Heres an pictureset showing the problem:
    http://abload.de/image.php?img=errordescriptionguyi4.png

    Maybe you could use a Bool to track if you have only empty battlers so far. And if you reach the last spawnbox the script has to choose an id which is not 0.
    But i am not sure, because i did not look into your code yet.

    • Hime says:

      You should get yanfly's engine fixed because no enemy means you win by default. The script shouldn't just pick a random enemy even though none of them can be added.

  3. naseweiss says:

    Theres a Bug if id 0 is to high you might get an "empty" battle which results in a division by 0 error and game crash.

  4. RogueDeus says:

    Thanks! You are good at what you do.

    I had not actually tried to use the ID:0 as I had not seen an update stating it was added and had not thought about trying it before. Thanks for correcting me.

  5. Tsukihime says:

    I have tested it and found that the script already supports ID 0 for no enemy.

    There is actually a bug with the replacement though, and this is probably why you’re getting the results you’re getting: once the placeholders have been randomized once, it is permanently changed. So this pretty much explains why the randomization is not working correctly: it’s not even being randomized. I have updated the script to address this.

    The algorithm is pretty simple

    1: Total = Add up all the weights
    2: Value = random number between 0 and Total
    3: For each *valid* rule, if its weight is less than value, then return the enemy ID of that rule. Otherwise, subtract the weight from the value and continue

    This means that if you have weights of A is 30 and B is 15, then there’s 66.6% chance of getting A and 33.3% chance of getting B. I tested it with some sample runs across a hundred million trials (in a single run) and the outcome was about that.

  6. RogueDeus says:

    Before I forget, and hopefully I am not abusing your attention. But could you add in the option for ID:0 = empty member, as I talked to you a few weeks ago? If not, I will leave it alone. 🙂

    Thanks again.

  7. RogueDeus says:

    Just ran into an issue.

    It seems that using this tag format (option: value1, value2), causes issues. Either it wont fill the placeholder at all (in a clean project), or it causes this error:
    Script ‘Window_Selectable’ line 61: ZeroDivisionError occurred.

    Using the semicolon separated (option1: value1; option2: value2) functions 100%.

    100% Example: weight: 33; tags: medium; tags: small
    Error Prone Example: weight: 33; tags: medium, small

    Unless I am doing something wrong. 🙂

    • Tsukihime says:

      It was a bug. I have addressed the issue and updated the script. Thanks for the report.

      • RogueDeus says:

        Thanks Hime! You are so much more responsive than most other prolific scripters in the community. And so much more polite to newbies like myself.

        I hope I am not bothering you, but I have another apparent anomaly to report.

        Can you possibly take a moment to explain to me the logic behind your weight math?
        I don’t pretend to understand 100% of what I am seeing in the code, but the results in practice seem as though it will often get stuck on one replacement over another.

        For example: A 4 member troop, with a single ‘medium’ slot (in position #1), and 3 ‘small’ slots (in position 2,3,4) with the following rules.
        ID 2: weight: 30; tags: small, medium
        ID 6: weight: 15; tags: medium

        In game, if I get an ID 6 the first time the troop is called, I get it 100% of the time (at least for several minutes) in position 1, despite the fact that is is 2 times more likely that ID 2 should be there instead. And visa verse.

        In troop ‘Battle Test’ mode, it randomizes normally. (Or so it appears) Of course, its also completely starting and stopping the game loop each time.

        This leads me to think it has to do with the random seed.
        Am I mistaken?

  8. RogueDeus says:

    Amazing work!

    Have you considered combining this with the functionality of your ‘Encounter Conditions’ script?
    Also, is there a possibility to have a NULL troop member result?

    I have to say the diversity of your work is astounding!

    • Tsukihime says:

      I believe encounter conditions can be used on placeholders as well since they are just regular troop members (that just happen to get replaced with a different enemy).

      You can have a placeholder replaced with no enemy if none of the rules are satisfied. For example, if there are only “large” slots left, and none of your “large” enemies can be added, then no enemy will be selected.

      However at the moment there are constraints that would result in this behavior. Maybe when I add different types of constraints (such as enemies always appearing in pairs, or certain enemies not appearing if another enemy exists)

      • RogueDeus says:

        Thanks for the quick reply.

        Doh. Seems I asked the first question about the ‘Encounter Conditions’ script before adequate testing…. I had all the test flags set, so every troop member showed up regardless. You saying that prompted me to double check and found the issue.

        Though I am not sure I asked the second question correctly.

        I am wondering if I might have a weighted placeholder rule that simply replaces the holder-member with nothing. A simple way may be using an ID of 0, to denote an empty member.

        Thus I can have a 3 placeholder troop of the same tag, that always has 1 member, sometimes has 2 members, and rarely has 3… etc… If that’s what you already described I must be misunderstanding your answer. 🙂

        Either way, I can jimmy together the ‘Encounter Conditions’ to get the same effect now!

        Again, your scripts are really helping me out more than you can know. I am still having trouble wrapping my head around a dynamic type language like Ruby. I cut my teeth on Java and C#. :p

Leave a Reply

Your email address will not be published. Required fields are marked *