Diceroll on table using a 2D-Array

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
Pankrazius
Newbie
Posts: 5
Joined: Wed Oct 08, 2014 3:35 pm
Projects: Torwelten - Karil (Promo-Game)
Deviantart: Pankrazius
Location: Bavaria
Contact:

Diceroll on table using a 2D-Array

#1 Post by Pankrazius »

This code is used within my training project.
It's a try to implement my P&P-gamingrules (Regelwerck/Ruleworx) used in my P&P-Project "Torwelten".

So there may be easier ways to accomplish the same task (determining damage and other effects within the game). But this is how the rules work in my game ;)

First you need an array. In other words, a list full of lists. Looks funny but works.

Code: Select all

    ewt = [[3, 3, 3, 2, 2, 2, 2, 2, 2, 2],      # 0 -7 
        [3, 3, 3, 2, 2, 2, 2, 2, 2, 1],         # 1 -6
        [3, 3, 2, 2, 2, 2, 2, 2, 1, 1],         # 2 -5
        [3, 3, 2, 2, 2, 2, 2, 1, 1, 1],         # 3 -4
        [3, 3, 2, 2, 2, 2, 1, 1, 1, 1],         # 4 -3
        [3, 2, 2, 2, 2, 1, 1, 1, 1, 0],         # 5 -2
        [3, 2, 2, 2, 1, 1, 1, 1, 0, 0],         # 6 -1
        [3, 2, 2, 2, 1, 1, 1, 0, 0, 0],         # 7  0
        [3, 2, 2, 2, 1, 1, 0, 0, 0, 0],         # 8 +1
        [3, 2, 2, 1, 1, 0, 0, 0, 0, 0],         # 9 +2
        [2, 2, 1, 1, 0, 0, 0, 0, 0, 0],         #10 +3
        [2, 1, 1, 0, 0, 0, 0, 0, 0, 0],         #11 +4
        [2, 1, 0, 0, 0, 0, 0, 0, 0, 0],         #12 +5
        [2, 0, 0, 0, 0, 0, 0, 0, 0, 0],         #13 +6
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]         #14 +7

# Würfelergebnis = -1 (1 = 0, 2 = 1 ... 10 = 9
You may use only one line for the whole thing - but so its simpler to read and understand.
Effect-Dicerolls in Regelwerk are shown like this:

"Weapon XY, Dmg (2/-1)"

The first Value in the brackets determine how many d10 you may use. The more, the better. Second Value is the modifier. the lower the better. It determines on wich row of the table you roll and check the results.

In the normal game i would throw 2 ten-sided-dice and check the table for the acummulated effect.

In programmingterms this act looks like:

Code: Select all

    def EWTCheck(dice, mod):
        """
        dice = Integer, Anzahl Würfelwürfe
        mod = Integer (-7 - +7)
        return = Float; Berechneter Effekt in ganzen und halben Punkten.
        """
        if mod < -7:
            return (10.0)     # Zu viel Schaden. Schiffsgeschütz gegen ungerüsteten Menschen.
        elif mod > 7:
            return (0.0)      # Zu wenig Schaden. Faustfeuerwaffe gegen Schiffspanzerung.
        else:
            pass
        checkmod = mod +7     # Modifikator auf die 0-15-Tabelle anpassen.
        
        ew = 0.0
        while dice >= 0:
            diceresult = random.randint(0,9) # W10-Wurf, gleich -1 zur Anpassung auf die 0-9-Tabelle.
            result = ewt[checkmod][diceresult]
            if result == 0:
                dice =- 1
                pass
            elif result == 1:
                dice =- 1
                ew = ew + 0.5
            elif result == 2:
                dice =- 1
                ew = ew + 1.0
            elif result == 3:
                ew = ew + 1.0
                dice = dice + 1
            else:
                pass
        return(ew)
Simple?
Ok... i will explain.
At first the function checks, if the modifier is out of range. The maximum difference is 7. -7 is like using a ship gun against an unarmored civilian. The target is reduced to cosmic dust. +7 is like the try to pierce ship armor with a handgun. So no effect.
Within theese range (else) you may roll your dice.

"checkmod" a temporary variable will be generated. It contains the "mod" from above and add 7 Points. This, because your lists starting at 0 and don't know negative values.

"ew" contains the result of your complete dicethrow(s) as a float. (Because the P&P game calculate with "half points of damage" for grazehots and scratches) It starts with 0.0 (to define it als float)

The while-loop uses the dice-argument as base.
The following randomizer generates a result between 0 and 9 (like a ten sided-dice) and add its result to the "diceresult"-variable.

Now there comes the magic.

the "result"-variable "looks" for the List of Lists - the 2DArray called ewt. and ther for the [X] list and the [Y] listentry.

So a diceroll of 5 at -1 mod would look on list [6], listentry [5] and therfore result in a "1".

The rest of the Code is a serie of 4 if-clauses wich adding the ammount of effect to "ew" and reduce the remaining "dice" by one. Excluding the last one (if result == 3)
this is a special case in my game. its a lucky hit. You do one point of damage and may reroll, thus the dice-variable isn't reduced.

Ok... I explained much things about my game in this. But basically you may find the code usefull.

Post Reply

Who is online

Users browsing this forum: No registered users