Add support for adventage, disadventage and emphasis rolls.
This commit is contained in:
		
							parent
							
								
									95122d2c99
								
							
						
					
					
						commit
						8884d047a6
					
				
					 3 changed files with 71 additions and 14 deletions
				
			
		| 
						 | 
					@ -1,13 +1,67 @@
 | 
				
			||||||
package com.pixelized.desktop.lwa.business
 | 
					package com.pixelized.desktop.lwa.business
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.pixelized.desktop.lwa.parser.arithmetic.Instruction
 | 
				
			||||||
 | 
					import kotlin.math.abs
 | 
				
			||||||
 | 
					import kotlin.math.max
 | 
				
			||||||
 | 
					import kotlin.math.min
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class RollUseCase {
 | 
					class RollUseCase {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun rollD100(): Int {
 | 
					    fun rollD100(): Int {
 | 
				
			||||||
        return roll(quantity = 1, faces = 100)
 | 
					        return roll(modifier = null, quantity = 1, faces = 100)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun roll(quantity: Int, faces: Int): Int {
 | 
					    fun roll(dice: Instruction.Dice): Int {
 | 
				
			||||||
        return sum(count = quantity) { (Math.random() * faces.toDouble() + 1).toInt() }
 | 
					        return roll(
 | 
				
			||||||
 | 
					            modifier = dice.modifier,
 | 
				
			||||||
 | 
					            quantity = dice.quantity,
 | 
				
			||||||
 | 
					            faces = dice.faces,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fun roll(modifier: Instruction.Dice.Modifier?, quantity: Int, faces: Int): Int {
 | 
				
			||||||
 | 
					        print("{")
 | 
				
			||||||
 | 
					        return sum(count = quantity) {
 | 
				
			||||||
 | 
					            when (modifier) {
 | 
				
			||||||
 | 
					                Instruction.Dice.Modifier.ADVANTAGE -> {
 | 
				
			||||||
 | 
					                    val roll1 = roll(faces = faces)
 | 
				
			||||||
 | 
					                    val roll2 = roll(faces = faces)
 | 
				
			||||||
 | 
					                    print("[$roll1,$roll2]")
 | 
				
			||||||
 | 
					                    max(roll1, roll2)
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                Instruction.Dice.Modifier.DISADVANTAGE -> {
 | 
				
			||||||
 | 
					                    val roll1 = roll(faces = faces)
 | 
				
			||||||
 | 
					                    val roll2 = roll(faces = faces)
 | 
				
			||||||
 | 
					                    print("[$roll1,$roll2]")
 | 
				
			||||||
 | 
					                    min(roll1, roll2)
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                Instruction.Dice.Modifier.EMPHASIS -> {
 | 
				
			||||||
 | 
					                    val roll1 = roll(faces = faces)
 | 
				
			||||||
 | 
					                    val roll2 = roll(faces = faces)
 | 
				
			||||||
 | 
					                    print("[$roll1,$roll2]")
 | 
				
			||||||
 | 
					                    val half = faces / 2
 | 
				
			||||||
 | 
					                    val roll1Abs = abs(half - roll1)
 | 
				
			||||||
 | 
					                    val roll2Abs = abs(half - roll2)
 | 
				
			||||||
 | 
					                    when {
 | 
				
			||||||
 | 
					                        roll1Abs == roll2Abs -> max(roll1, roll2)
 | 
				
			||||||
 | 
					                        roll1Abs < roll2Abs -> roll2
 | 
				
			||||||
 | 
					                        else -> roll1
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                null -> {
 | 
				
			||||||
 | 
					                    roll(faces = faces).also { print("$it") }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }.also {
 | 
				
			||||||
 | 
					            print("}:")
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private fun roll(faces: Int): Int {
 | 
				
			||||||
 | 
					        return (Math.random() * faces.toDouble() + 1).toInt()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private fun sum(count: Int, block: () -> Int): Int {
 | 
					    private fun sum(count: Int, block: () -> Int): Int {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,11 +53,9 @@ class SkillValueComputationUseCase(
 | 
				
			||||||
    ): Int { // TODO Roll detail instead of an simple Int.
 | 
					    ): Int { // TODO Roll detail instead of an simple Int.
 | 
				
			||||||
        print("Roll ->")
 | 
					        print("Roll ->")
 | 
				
			||||||
        return arithmeticParser.parse(roll).sumOf { instruction ->
 | 
					        return arithmeticParser.parse(roll).sumOf { instruction ->
 | 
				
			||||||
 | 
					            print(" ($instruction):")
 | 
				
			||||||
            val value = when (instruction) {
 | 
					            val value = when (instruction) {
 | 
				
			||||||
                is Instruction.Dice -> rollUseCase.roll(
 | 
					                is Instruction.Dice -> rollUseCase.roll(dice = instruction)
 | 
				
			||||||
                    quantity = instruction.quantity,
 | 
					 | 
				
			||||||
                    faces = instruction.faces
 | 
					 | 
				
			||||||
                )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                is Instruction.Flat -> instruction.value
 | 
					                is Instruction.Flat -> instruction.value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -68,10 +66,7 @@ class SkillValueComputationUseCase(
 | 
				
			||||||
                                .parse(sheet.damageBonus)
 | 
					                                .parse(sheet.damageBonus)
 | 
				
			||||||
                                .firstOrNull()
 | 
					                                .firstOrNull()
 | 
				
			||||||
                            if (damageBonusInstructions is Instruction.Dice) {
 | 
					                            if (damageBonusInstructions is Instruction.Dice) {
 | 
				
			||||||
                                rollUseCase.roll(
 | 
					                                rollUseCase.roll(dice = damageBonusInstructions)
 | 
				
			||||||
                                    quantity = damageBonusInstructions.quantity,
 | 
					 | 
				
			||||||
                                    faces = damageBonusInstructions.faces
 | 
					 | 
				
			||||||
                                )
 | 
					 | 
				
			||||||
                            } else {
 | 
					                            } else {
 | 
				
			||||||
                                0
 | 
					                                0
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
| 
						 | 
					@ -83,6 +78,7 @@ class SkillValueComputationUseCase(
 | 
				
			||||||
                                .firstOrNull()
 | 
					                                .firstOrNull()
 | 
				
			||||||
                            if (damageBonusInstructions is Instruction.Dice) {
 | 
					                            if (damageBonusInstructions is Instruction.Dice) {
 | 
				
			||||||
                                rollUseCase.roll(
 | 
					                                rollUseCase.roll(
 | 
				
			||||||
 | 
					                                    modifier = damageBonusInstructions.modifier,
 | 
				
			||||||
                                    quantity = damageBonusInstructions.quantity,
 | 
					                                    quantity = damageBonusInstructions.quantity,
 | 
				
			||||||
                                    faces = damageBonusInstructions.faces / 2,
 | 
					                                    faces = damageBonusInstructions.faces / 2,
 | 
				
			||||||
                                )
 | 
					                                )
 | 
				
			||||||
| 
						 | 
					@ -100,9 +96,9 @@ class SkillValueComputationUseCase(
 | 
				
			||||||
                        Instruction.Word.Type.CHA -> sheet.charisma
 | 
					                        Instruction.Word.Type.CHA -> sheet.charisma
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            } * instruction.sign
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            value.also { print(" ($instruction):$it") }
 | 
					            (value * instruction.sign).also { print("$it") }
 | 
				
			||||||
        }.also { println() }
 | 
					        }.also { println() }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,7 +18,14 @@ sealed class Instruction(
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        override fun toString(): String {
 | 
					        override fun toString(): String {
 | 
				
			||||||
            return "${sign.sign}${quantity}d${faces}"
 | 
					            return "${sign.sign}${
 | 
				
			||||||
 | 
					                when (modifier) {
 | 
				
			||||||
 | 
					                    Modifier.ADVANTAGE -> "a"
 | 
				
			||||||
 | 
					                    Modifier.DISADVANTAGE -> "d"
 | 
				
			||||||
 | 
					                    Modifier.EMPHASIS -> "e"
 | 
				
			||||||
 | 
					                    null -> ""
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }${quantity}d${faces}"
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue