In the last section I managed to create a widget that would set up my fights by calculating all the relevant stats and abilities and gear. Now, I actually need to play out the fight turn-by-turn. In Lone Wolf this is done by calculating the difference between the two Combat Skill values, then ‘rolling’ a random number between 0 and 9, then reading off the chart for how much Endurance both Lone Wolf and his opponent lose. Here's the chart....
Now, let me say in advance, this is not how I would handle this. The chart is neat, I like how it gives you both health totals at once - so you're both attacking at the same time instead of typical D&D “I swing, wait for you to swing, I swing, wait, yawn, etc...” But, having 4 values (column, row, player health, enemy health) all on the same table looks like it's going to be a little bit of a headache to code. Let's see...
-time passes-
Okay, I've been showing some of my mistakes along the way here - and with this code I made a lot of them. So many, which took me so long to untangle, that I'm actually going to skip showing you the bad parts and focus on what I got to work. I do want to show you one thing though, a screenshot from some of the bad code...
If you read each line, you'll notice that the math doesn't add up. That was one of the big problems I was having (after fixing the wall of red error text). I fixed most all of those problems by spitting things up, which is where I want to pick up talking about my code.
Widgets are great, they are re-usable so that you can write one block of code just one time and call it over and over down the road. Another really cool thing about widgets - they can call other widgets, and they can even call themselves. My Inventory widget from a few sections back - I was using it to print out the inventory after it had just changed the inventory. A widget (or ‘function’ in JavaScript parlance) calling itself is called ‘recursive’ (I believe) - and that's another great way you can re-use code to cut down on the typing.
The combat system has 3 connected widgets. <<CombatTurn>> calls <<Attack>> which calls <<TurnEnd>> and if everybody's still alive then <<TurnEnd>> calls <<Attack>> and continues the cycle until somebody falls and the combat is over.
Let's start with <<CombatTurn>>
/*
* <<CombatTurn $enemy $player CanEscape>>
*/
<<widget "CombatTurn">> <<nobr>>
<<set $lwEnd = $args[1].Endurance>>
<<set $eEnd = $args[0].Endurance>>
<<set $CSdiff = $args[1].CombatSkill - $args[0].CombatSkill>>
Your CS difference is $CSdiff
<br> You attack your opponent...
<<Attack>>
<</nobr>> <</widget>>
This just sets up the two main stats of CombatSkill and Endurance. Now, I wrote this as a little stub to test the turn-by-turn combat system; in the final story this would actually be a part of the <<CombatSetup>> widget that I spent the last section working on. The next widget, <<Attack>> does the really heavy lifting...
<<widget "Attack">> <<nobr>>
<<set _atkroll = random (0, 9)>>
<<switch Math.clamp($CSdiff, -100, 100)>>
<<case -11 -12 -13 -14>>
<<if _atkroll == 1>> <<set $eDmg = 0>> <<set $lwDmg = 100>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 0>> <<set $lwDmg = 100>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 0>> <<set $lwDmg = 8>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 0>> <<set $lwDmg = 8>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 1>> <<set $lwDmg = 7>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 2>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 5>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 6>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -10 -9>>
<<if _atkroll == 1>> <<set $eDmg = 0>> <<set $lwDmg = 100>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 0>> <<set $lwDmg = 8>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 0>> <<set $lwDmg = 7>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 1>> <<set $lwDmg = 7>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 2>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 3>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 4>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 7>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -8 -7>>
<<if _atkroll == 1>> <<set $eDmg = 0>> <<set $lwDmg = 8>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 0>> <<set $lwDmg = 7>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 1>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 2>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 4>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 8>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -6 -5>>
<<if _atkroll == 1>> <<set $eDmg = 0>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 1>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 2>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 8>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 9>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -4 -3>>
<<if _atkroll == 1>> <<set $eDmg = 1>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 2>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 8>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 9>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 10>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -2 -1>>
<<if _atkroll == 1>> <<set $eDmg = 2>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 9>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 10>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 11>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 0>>
<<if _atkroll == 1>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 9>> <<set $lwDmg = 1>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 10>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 11>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<case 1 2>>
<<if _atkroll == 1>> <<set $eDmg = 4>> <<set $lwDmg = 5>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 7>> <<set $lwDmg = 3>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 9>> <<set $lwDmg = 2>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 10>> <<set $lwDmg = 1>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 11>> <<set $lwDmg = 0>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 14>> <<set $lwDmg = 0>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<case 3 4>>
<<if _atkroll == 1>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 7>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 9>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 11>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 14>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 16>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 5 6>>
<<if _atkroll == 1>> <<set $eDmg = 6>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 7>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 8>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 9>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 11>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 14>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 16>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 18>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 7 8>>
<<if _atkroll == 1>> <<set $eDmg = 7>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 8>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 9>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 11>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 12>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 14>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 16>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 18>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 9 10>>
<<if _atkroll == 1>> <<set $eDmg = 8>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 9>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 11>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 12>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 14>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 16>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 18>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 11 12 13 14>>
<<if _atkroll == 1>> <<set $eDmg = 9>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 11>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 12>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 14>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 16>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 18>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<</switch>>
<</nobr>> <</widget>>
<<set _atkroll = random (0, 9)>>
<<switch Math.clamp($CSdiff, -100, 100)>>
<<case -11 -12 -13 -14>>
<<if _atkroll == 1>> <<set $eDmg = 0>> <<set $lwDmg = 100>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 0>> <<set $lwDmg = 100>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 0>> <<set $lwDmg = 8>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 0>> <<set $lwDmg = 8>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 1>> <<set $lwDmg = 7>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 2>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 5>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 6>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -10 -9>>
<<if _atkroll == 1>> <<set $eDmg = 0>> <<set $lwDmg = 100>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 0>> <<set $lwDmg = 8>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 0>> <<set $lwDmg = 7>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 1>> <<set $lwDmg = 7>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 2>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 3>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 4>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 7>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -8 -7>>
<<if _atkroll == 1>> <<set $eDmg = 0>> <<set $lwDmg = 8>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 0>> <<set $lwDmg = 7>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 1>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 2>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 4>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 8>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -6 -5>>
<<if _atkroll == 1>> <<set $eDmg = 0>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 1>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 2>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 8>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 9>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -4 -3>>
<<if _atkroll == 1>> <<set $eDmg = 1>> <<set $lwDmg = 6>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 2>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 8>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 9>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 10>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case -2 -1>>
<<if _atkroll == 1>> <<set $eDmg = 2>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 9>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 10>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 11>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 0>>
<<if _atkroll == 1>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 9>> <<set $lwDmg = 1>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 10>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 11>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<case 1 2>>
<<if _atkroll == 1>> <<set $eDmg = 4>> <<set $lwDmg = 5>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 7>> <<set $lwDmg = 3>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 9>> <<set $lwDmg = 2>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 10>> <<set $lwDmg = 1>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 11>> <<set $lwDmg = 0>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 14>> <<set $lwDmg = 0>> <br> case 1, 2 roll _atkroll <<TurnEnd>> <</if>>
<<case 3 4>>
<<if _atkroll == 1>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 7>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 9>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 11>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 14>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 16>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 5 6>>
<<if _atkroll == 1>> <<set $eDmg = 6>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 7>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 8>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 9>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 11>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 14>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 16>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 18>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 7 8>>
<<if _atkroll == 1>> <<set $eDmg = 7>> <<set $lwDmg = 4>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 8>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 9>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 11>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 12>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 14>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 16>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 18>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 9 10>>
<<if _atkroll == 1>> <<set $eDmg = 8>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 9>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 11>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 12>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 14>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 16>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 18>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<case 11 12 13 14>>
<<if _atkroll == 1>> <<set $eDmg = 9>> <<set $lwDmg = 3>> <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 10>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 11>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 12>> <<set $lwDmg = 2>> <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 14>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 16>> <<set $lwDmg = 1>> <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 18>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 100>> <<set $lwDmg = 0>> <<TurnEnd>> <</if>>
<</switch>>
<</nobr>> <</widget>>
Yeah, this took a lot more code that I'd like - it's another one I'm having a hard time figuring out how to simplify.
The first thing I need is to take the difference between the player's and enemy's Combat Skills so I can random-roll the right table. That's easy, I just used a <<switch>>, the new part is the variable that <<switch>> is evaluating- Math.clamp($CSdiff, -100, 100).
$CSdiff is my variable. One of the errors I kept running into though was that SugarCube was reading it as a string of text and not as a number. That was confusing <<switch>>. The function Math.clamp forces that variable to be treated as a number, and it's arguments are the variable, the minimum value and maximum value (both of which are far higher that I'll need).
With the correct Combat Skill difference, now I need a random number and to look up that result - I'm going to take out one section, for the $CSdiff == 0 or an even fight...
<<case 0>>
<<if _atkroll == 1>> <<set $eDmg = 3>> <<set $lwDmg = 5>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 2>> <<set $eDmg = 4>> <<set $lwDmg = 4>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 3>> <<set $eDmg = 5>> <<set $lwDmg = 4>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 4>> <<set $eDmg = 6>> <<set $lwDmg = 3>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 5>> <<set $eDmg = 7>> <<set $lwDmg = 2>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 6>> <<set $eDmg = 8>> <<set $lwDmg = 2>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 7>> <<set $eDmg = 9>> <<set $lwDmg = 1>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 8>> <<set $eDmg = 10>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 9>> <<set $eDmg = 11>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
<<if _atkroll == 0>> <<set $eDmg = 12>> <<set $lwDmg = 0>> <br> case 0 roll _atkroll <<TurnEnd>> <</if>>
So <<case 0>> of my <<switch>> means both characters have the same Combat Skill value. I roll up a random number, then I <<if>> two variables for how much damage the enemy takes $eDmg and how much damage our hero “Lone Wolf” takes $lwDmg. Then there is a weird line that prints the case # and random roll #. That is debugging code. After fixing a ton of errors I was getting funky math, like my screenshot above, so I started printing this line out to see where the widget was getting it's numbers from. Which showed me something else about ‘splitting things up’ - when I had 2 or more combats on the same Passage the math went haywire. As soon as I moved them to different Passages, everything worked the right way. This was unexpected. It's also not a problem - I was running a ton of fights on the same Passage as a quick way to test the system, in the actual story there would only be one combat on each Passage. So I don't think this is a big deal, but I'm still not sure exactly why it happens. JavaScript/ SugarCube is a little weird about exactly how and when things load, from what reading I have been able to understand, but since this doesn't seem like it will be a problem outside of testing I'm not worried about it. I also wonder if I should put this code in a Passage instead of a Widget - maybe that would fix the issue? Again, I don't plan on running this down at the moment but I am going to be careful about watching for it to happen again.
Now, this is only 1 <<case>>, there are 13 total - so this is a really long block of code, and the reason why I'd rather resolve combat some other way. It's a lot of <<switch/ if>>ing that was a pain to type.
Once I know how much damage everybody takes, then I call the next widget, <<TurnEnd>>...
<<widget "TurnEnd">> <<nobr>>
<<set $lwEnd = $lwEnd - $lwDmg>>
<<set $eEnd = $eEnd - $eDmg>>
<br> Damage: Foe $eDmg Lone Wolf $lwDmg
<br> Endurance: Foe $eEnd Lone Wolf $lwEnd
<<if $lwEnd <= 0>>
<br> You have been killed, your quest ends here !
<<elseif $eEnd <= 0>>
<br> You have defeated your foe!
<br> You have $lwEnd Endurance remaining !
<<else>>
<<linkreplace "Attack">> <<Attack>> <</linkreplace>>
<</if>>
<</nobr>> <</widget>>
This is pretty straight-forward. First I take both characters' Endurance and subtract the damage they took, then I display that (so that there is something on the screen, my last message was debug, not for the player to see).<<set $lwEnd = $lwEnd - $lwDmg>>
<<set $eEnd = $eEnd - $eDmg>>
<br> Damage: Foe $eDmg Lone Wolf $lwDmg
<br> Endurance: Foe $eEnd Lone Wolf $lwEnd
<<if $lwEnd <= 0>>
<br> You have been killed, your quest ends here !
<<elseif $eEnd <= 0>>
<br> You have defeated your foe!
<br> You have $lwEnd Endurance remaining !
<<else>>
<<linkreplace "Attack">> <<Attack>> <</linkreplace>>
<</if>>
<</nobr>> <</widget>>
Then I check if the player is dead. If not I check if the enemy is dead. If not I print a link to the <<Attack>> widget for the next round. This section needs some work. If the player is killed I need to send them back to the beginning of the fight somehow so they can try it again, or try to avoid it (I really don't want to make the player start over from the beginning, that seems a bit too harsh (and yeah, I know it's “old school,” I started gaming back in the “old school” - but being historical doesn't automatically make it good)). Also, in some fights the player has the choice to escape - which needs to be a link here after the Attack; and is another thing I don't like about this system, I think it's a little harsh to forbid the player from trying to escape, except sometimes. I'd like it to always be an option. Escape should be easy to add, it's just another attack round, but Lone Wolf can't do any damage, he can only take damage. If he survives the fight ends. So if I copy the <<TurnEnd>> widget and just remove the line that deals damage to the enemy, I pretty much have it done already.
Still, despite missing a few pieces, I do have a working system...
So now I am at a strange place.
I'm not actually done. I have a lot of pieces of scattered code that all need to be brought together, missing parts need to be added, everything needs to be double-checked, and it all needs to be made pretty and put into an actual story - not the current several dozen un-related testing passages.
But, this isn't my story, and these aren't my rules. All of this work has been on Joe Dever's fantastic setting and rules, and sadly he passed away - so I can't even ask him for permission to do this (granted, I could try to get in touch with his estate, somehow). That's why I've been limiting the book material to the dry rules and not any of the story or art. So far this has been a mental exercise and learning experience, not actually creating a game.
So I'm hesitant to go on working with this code. I've put a lot of hours into this so far, and a whole lot of frustration, and I want something that is going to be mine, and more importantly something that I can share with you, gentle reader.
Which means, despite the fact that Lone Wolf's story is not quite done, it's time for this project to end. I have learned so much, and it has been great to re-visit something I loved from my youth, now it is time to go forward. So my next posting is going to be a new project - what you ask? Well, I'm not sure yet, still doing some research into a few ideas. As soon as I decide on the next steps to take, I will let you know.
Look forward to seeing you then :)
No comments:
Post a Comment