renpy/doc/cookbook/Point-Based Endings
From Ren'Py
Point-Based Endings
Let's say that we want to base the ending on how the user did on six variables: strength, wisdom, intelligence, dexterity, constitution, and charisma.
First, we'll need a convenience function. You don't need to understand it, just copy it exactly:
init: python: def max_points(*values): return [ i for i, v in enumerate(values) if v == max(values) ]
Second, define your variables. Let's assume that they're named like this:
label start: $ strength_points = 0 $ wisdom_points = 0 $ intelligence_points = 0 $ dexterity_points = 0 $ constitution_points = 0 $ charisma_points = 0
Then, at the end, figuring out which ending the player has gotten is done in two steps. In Step 1, we use the function we defined above to get a list of the winning points:
$ winners = max_points(strength_points, wisdom_points, intelligence_points, dexterity_points, constitution_points, charisma_points)
Finally, you compare the array of winners to the various possibilities. The array which you get back has a list of the indices which have the most points, as you passed them in. In the above case, strength_points has index 0, wisdom_points has index 1, and so on.
if winners == [0]: # strength_points wins elif winners == [1]: # wisdom_points wins elif winners == [2]: # intelligence_points wins elif winners == [3]: # dexterity_points wins elif winners == [4]: # constitution_points wins elif winners == [5]: # charisma_points wins elif winners == [0,1]: # strength_points and wisdom_points tie for first place elif winners == [0,2]: # strength_points and intelligence_points tie for first place elif winners == [0,3]: # strength points and dexterity_points tie for first place elif winners == [0,4]: # strength_points and constitution_points tie for first place elif winners == [0,5]: # strength_points and charisma_points tie for first place elif winners == [1,2]: # wisdom_points ties with intelligence_points for first elif winners == [1,3]: # wisdom_points ties with dexterity_points for first elif winners == [1,4]: # wisdom_points ties with constitution_points for first elif winners == [1,5]: # wisdom_points ties with charisma_points for first elif winners == [2,3]: # intelligence ties with dexterity for first elif winners == [2,4]: # intelligence ties with constitution for first elif winners == [2,5]: # intelligence ties with charisma for first elif winners == [3,4]: # dexterity ties with consitution elif winners == [3,5]: # dexterity ties with charisma elif winners == [4,5]: # consitution ties with charisma elif winners == [0,1,2]: # strength ties with wisdom and intelligence elif winners == [0,1,3]: # strength ties with wisdom and dexterity elif winners == [0,1,4]: # strength ties with wisdom and constitution elif winners == [0,1,5]: # strength ties with wisdom and charisma elif winners == [0,2,3]: # strength ties with intelligence and dexterity elif winners == [0,2,4]: # strength ties with intelligence and constitution elif winners == [0,2,5]: # strength ties with intelligence and charisma elif winners == [0,3,4]: # strength ties with dexterity and constitution elif winners == [0,3,5]: # strength ties with dexterity and charisma elif winners == [0,4,5]: # strength ties with constitution and charisma elif winners == [1,2,3]: # wisdom ties with intelligence and dexterity elif winners == [1,2,4]: # wisdom ties with intelligence and constitution elif winners == [1,2,5]: # wisdom ties with intelligence and charisma elif winners == [1,3,4]: # wisdom ties with dexterity and constitution elif winners == [1,3,5]: # wisdom ties with dexterity and charisma elif winners == [1,4,5]: # wisdom ties with constitution and charisma elif winners == [2,3,4]: # intelligence ties with dexterity and constitution elif winners == [2,3,5]: # intelligence ties with dexterity and charisma elif winners == [2,4,5]: # intelligence ties with constitution and charisma elif winners == [3,4,5]: # dexterity ties with constitution and charisma elif winners == [0,1,2,3]: # strength ties with wisdom, intelligence, and dexterity elif winners == [0,1,2,4]: # strength ties with wisdom, intelligence, and constitution elif winners == [0,1,2,5]: # strength ties with wisdom, intelligence, and charisma elif winners == [0,1,3,4]: # strength ties with wisdom, dexterity, and constitution elif winners == [0,1,3,5]: # strength ties with wisdom, dexterity and charisma elif winners == [0,1,4,5]: # strength ties with wisdom, constitution and charisma elif winners == [0,2,3,4]: # strength ties with intelligence, dexterity, and constitution elif winners == [0,2,3,5]: # strength ties with intelligence, dexterity, and charisma # and so on else: # they're all equal
Basically, you have to provide cases for the power set of all possible choices. This is, obviously, a nightmare for large numbers of variables. The power set contains 2**N elements where N is the number of elements in your set. In this case we have 6 variables, so the power set contains 64 conditions (really 63, since you can't have no indices in the set of maximums).
It's much more manageable, especially for large numbers of variables, to only handle individual winners, and then have a single ending for all ties, or at least most ties. You do this by simply stopping before we did and end with an "else" block.
