Wednesday, August 23, 2017

Converting Lone Wolf to Twine and SugarCube - Part 8 - Combat Setup


The Lone Wolf Combat System

     This is it, the last of the major game rules I need to implement. Once I finish this I'll pretty much have everything coded to track and do (that is, all my variables and widgets) - I'll just need to clean things up, make them pretty, and put them in with the text. I'm finally getting close to being finished with this project (I can't describe that feeling well enough).

     So here are the book's combat rules...


Rules for Combat


There will be occasions on your adventure when you have to fight an enemy. The enemy’s COMBAT SKILL and ENDURANCE points are given in the text. Lone Wolf’s aim in the combat is to kill the enemy by reducing his ENDURANCE points to zero while losing as few ENDURANCE points as possible himself.
At the start of a combat, enter Lone Wolf’s and the enemy’s ENDURANCE points in the appropriate boxes on the Combat Record section of your Action Chart.
The sequence for combat is as follows.

1. Add any extra points gained through your Kai Disciplines to your current COMBAT SKILL total.
2. Subtract the COMBAT SKILL of your enemy from this total. The result is your Combat Ratio. Enter it on the Action Chart.

Example

Lone Wolf (COMBAT SKILL 15) is ambushed by a Winged Devil (COMBAT SKILL 20). He is not given the opportunity to evade combat, but must stand and fight as the creature swoops down on him. Lone Wolf has the Kai Discipline of Mindblast, so he adds 2 points to his COMBAT SKILL, giving a total COMBAT SKILL of 17.

He subtracts the Winged Devil’s COMBAT SKILL from his own, giving a Combat Ratio of −3 (17 − 20 = −3). −3 is noted on the Action Chart as the Combat Ratio.


3. When you have your Combat Ratio, pick a number from the Random Number Table.
4. Turn to the Combat Results Table. Along the top of the chart are shown the Combat Ratio numbers. Find the number that is the same as your Combat Ratio and cross-reference it with the random number that you have picked (the random numbers appear on the side of the chart). You now have the number of ENDURANCE points lost by both Lone Wolf and his enemy in this round of combat. (E represents points lost by the enemy; LW represents points lost by Lone Wolf.)

Example

The Combat Ratio between Lone Wolf and Winged Devil has been established as -3. If the number taken from the Random Number Table is a 6, then the result of the first round of combat is:

• Lone Wolf loses 3 ENDURANCE points
• Winged Devil loses 6 ENDURANCE points


• On the Action Chart, mark the changes in ENDURANCE points to the participants in the combat.
• Unless otherwise instructed, or unless you have an option to evade, the next round of combat now starts.
• Repeat the sequence from Stage 3.


This process of combat continues until the ENDURANCE points of either the enemy or Lone Wolf are reduced to zero or below, at which point that combatant is declared dead. If Lone Wolf is dead, the adventure is over. If the enemy is dead, Lone Wolf proceeds but with his ENDURANCE points possibly reduced.
A summary of Combat Rules appears in the back of this book.

Evasion of Combat


During your adventure you may be given the chance to evade combat. If you have already engaged in a round of combat and decide to evade, calculate the combat for that round in the usual manner. All points lost by the enemy as a result of that round are ignored, and you make your escape. Only Lone Wolf may lose ENDURANCE points during that round, but then that is the risk of running away! You may only evade if the text of the particular section allows you to do so.



    So, while this is kind of simple, it's also fairly complex. Yeah, that made a lot of sense :) Okay, while this looks simple, it's actually fairly complex. One of the first hurdles is the combat table. Once you find the difference between the Combat Skills of Lone Wolf and the Enemy, then you use that number as a reference on this combat table...


    Along the top you find the CS difference (in this case it is negative, therefore the Enemy is higher than LW). Then you pick a random number from 0 to 9, and cross-reference the box to find out how much Endurance both LW and the Enemy lose. That's one turn, keep picking new random numbers on the same column until somebody's dead.
    So I'm going to need some kind of <<if>> or <<switch>> to get the difference; then within that a <<random>> to get the results.

    Now, there are only 2 stats: Combat Skill and Endurance. Endurance is easy, it only goes down during the fight and doesn't matter at all until it reaches 0. A simple variable.
    Combat Skill is more complicated. First, there is the basic combat skill that we got at the beginning of the character creation. Then, add 2 if the player chose Weaponskill and if the player has the correct weapon. Add 2 more if the player has Mindblast and if the Enemy is not immune to it. Subtract 2 if the Enemy has Mindblast and if the player does not have Mindshield. Add 2 each if the player has a chainmail waistcoat and/ or helmet. Also, if the player has a Strength Potion and if they want to use it, add 2 more. All of which has to be figured out before the fight starts to get that fight's CS - which may be different in the next fight (due to lost/ gained equipment) so needs to be re-calculated each fight.
    I see a lot of <<if>> macros in my future.

    Also, we need to track if you can and want to escape, who's alive at the end of every round (and what to do if the player dies? send them back a Passage? re-start the book?), and according to the rules Health Potions can only be used at the end of a fight, so we'll need to check and offer that as well.

    So far I've got a workable system (I kind of hesitate to call it “good”) for tracking everything the player has - but I'm also going to need to track some things the enemies can have. So how do I organize their stats?


I, Object


    At the beginning I made an Object, my first list of Disciplines. Then I figured out it would be easier to just track disciplines in an Array. But, now I need to track a bunch of different stats for my monsters - and here is where I think an Object will work perfectly. Something like this...

<<set $enemy = {
CS: #,
End: #,
Mindblast: true/false,
Mindshield: true/false,
}>>

    And there's my little generic enemy. Their combat stats, plus if they have mindblast and/ or mindshield (which I'll apply to the player when I check before combat). All I have to do is copy this little block and fill in the specific names and numbers.
    Now, to reference the numbers you use the variable name and a “.” like - $enemy.CS or $enemy.Mindblast - which makes it easy to get to the values. If you pass the enemy as a argument (let's say $args[1]) you do the exact same thing - $args[1].CS or $args[1].Mindblast - so that's easy too.


Let's get ready to ruummmbbbbllllleeeeee........


    Okay, I need to get my stats together and check for all that stuff, so let's make another handy widget that we can call as needed...

<<widget CombatSetup>> <<nobr>> You prepare for combat...

// pass the enemy variable as $args[0]

/* player base CS */
<<set _cbtCS = $CombatSkill>>

/* weaponskill */
<<if $Disciplines.contains ("Weaponskill")>>
<<if $weapons.contains ($WpnProf)>>
<<set _cbtCS += 2>> <br> Having the weapon you're trained in will give you an edge!
<</if>>
<</if>>

/* no weapon */
<<if $weapons[0] == “-empty-”>>
<<if $weapons[1] == “-empty-">>
<<set _cbtCS -= 4>> <br> Not having any weapons will make the fight harder!
<</if>>
<</if>>

/* player Mindblast */
<<if $Disciplines.contains ("Mindblast")>>
<<if $args[0].Mindshield == “false”>>
<<set _cbtCS += 2>> <br> Your Mindblast staggers your foe!
<<elseif $args[0].Mindshield == “true”>>
<br> Your foe is immune to your Mindblast!
<</if>>
<</if>>

/* enemy Mindblast */
<<if $args[0].Mindblast == “true”>>
<<if $Disciplines.contains ("Mindshield")>>
<br> Your foe tries to use Mindblast on you, but you resist with Mindshield !
<<else>>
<<set _cbtCS -= 2>> <br> Your foe's Mindblast makes it difficult to fight!
<</if>>
<</if>>

/* player items (armor, helmet) */
<<if $specialitems.contains ("Chainmail Waistcoat")>>
<<set _cbtCS += 2>> <br> Your Chainmail Waistcoat will help protect you!
<</if>>
<<if $specialitems.contains ("Helmet")>>
<<set _cbtCS += 2>> <br> Your Helmet will help protect you!
<</if>>

/* player does have and wants to use potion */
//check the whole container to offer option
<<if $backpack.contains ("Strength Potion")>>
<br> Do you want to drink one of your Strength Potions to help with the fight? <br>
<span id="usestr">
// yes
<<link “ Yes”>>
//check each slot to determin which to use
<<if $backpack[0] == “Strength Potion”
<<set _cbtCS += 2>> <<set $backpack[0] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<</replace>>
<<elseif $backpack[1] == “Strength Potion”
<<set _cbtCS += 2>> <<set $backpack[1] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<</replace>>
<<elseif $backpack[2] == “Strength Potion”
<<set _cbtCS += 2>> <<set $backpack[2] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<</replace>>
<<elseif $backpack[3] == “Strength Potion”
<<set _cbtCS += 2>> <<set $backpack[3] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<</replace>>
<<elseif $backpack[4] == “Strength Potion”
<<set _cbtCS += 2>> <<set $backpack[4] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<</replace>>
<<elseif $backpack[5] == “Strength Potion”
<<set _cbtCS += 2>> <<set $backpack[5] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<</replace>>
<<elseif $backpack[6] == “Strength Potion”
<<set _cbtCS += 2>> <<set $backpack[6] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<</replace>>
<<elseif $backpack[7] == “Strength Potion”
<<set _cbtCS += 2>> <<set $backpack[7] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<</replace>>
<</if>> <</link>>

//no
<<link “ No”>>
<<replace #usestr>> You decide that you don't need any help defeating this enemy!
<</replace>> <</link>>

</span>
<</if>>

The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The Enemy's Combat Skill is $args[0].CS
<<if _cbtCS > $args[0].CS>> <<set _diff = _cbtCS - $args[0].CS>>
<br> You are stronger than your enemy! (+ _diff)
<<elseif _cbtCS < $args[0].CS>> <<set _diff = $args[0].CS - _cbtCS>>
<br> You are weaker than your enemy! (- _diff)
<<else>>
<br> You are evenly matched with your enemy!
<</if>>
<br> Your Endurance is $Endurance
<br> The Enemy's Endurance is $args[0].End

<</nobr>> <</widget>>

    And all that beautiful code above? Doesn't work.



     When I ran it I got all sorts of “unexpected token” errors in my <<if>> statements. What does that mean? Darned if I know. The error documentation is, well, as far as I can tell, non-existent. It seemed like at least some of the problem was from accessing the $enemy object. So I decided to break my code into two parts, the first part would just be trying to read everything from the player side. After some fiddling I came up with this...

<<nobr>>
<<set $CombatSkill to 10>>
<<set $Endurance to 20>>

<<set $Disciplines to []>>
<<set $WpnProf = "none">>

<<set $gold = 0>>
<<set $weapons = [ "-empty-", "-empty-" ]>>
<<set $backpack = [ "-empty-", "-empty-", "-empty-", "-empty-", "-empty-", "-empty-", "-empty-", "-empty-" ]>>
<<set $specialitems = []>>


<<widget CombatSetup>> <<nobr>> You prepare for combat...

<<set _cbtCS = $CombatSkill>>

<<if $Disciplines.includes ("Weaponskill")>>
<<if $weapons.includes ($WpnProf)>>
<<set _cbtCS += 2>> <br> Having the weapon you're trained in will give you an edge!
<</if>>
<</if>>

<<if $weapons.includes ("-empty-", 1)>>
<<if $weapons.includes ("-empty-", 0)>>
<<set _cbtCS -= 4>> <br> Not having any weapons will make the fight harder!
<</if>>
<</if>>

<<if $Disciplines.includes ("Mindblast")>>
<<set _cbtCS += 2>> <br> Your Mindblast staggers your foe!
<</if>>

<<if $Disciplines.includes ("Mindshield")>>
<br> Your foe tries to use Mindblast on you, but you resist with Mindshield !
<</if>>

<<if $specialitems.contains ("Chainmail Waistcoat")>>
<<set _cbtCS += 2>> <br> Your Chainmail Waistcoat will help protect you!
<</if>>

<<if $specialitems.contains ("Helmet")>>
<<set _cbtCS += 2>> <br> Your Helmet will help protect you!
<</if>>

<<if $backpack.contains ("Strength Potion")>>
<br> Do you want to drink one of your Strength Potions to help with the fight? <br>
<<print $backpack>>
<span id="usestr">
<<link "Yes">>
<<if $backpack.includes ("Strength Potion", 7)>>
<<set _cbtCS += 2>> <<set $backpack[7] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<<print $backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>>
<<elseif $backpack.includes ("Strength Potion", 6)>>
<<set _cbtCS += 2>> <<set $backpack[6] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<<print $backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>>
<<elseif $backpack.includes ("Strength Potion", 5)>>
<<set _cbtCS += 2>> <<set $backpack[5] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<<print $backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>>
<<elseif $backpack.includes ("Strength Potion", 4)>>
<<set _cbtCS += 2>> <<set $backpack[4] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<<print $backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>>
<<elseif $backpack.includes ("Strength Potion", 3)>>
<<set _cbtCS += 2>> <<set $backpack[3] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<<print $backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>>
<<elseif $backpack.includes ("Strength Potion", 2)>>
<<set _cbtCS += 2>> <<set $backpack[2] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<<print $backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>>
<<elseif $backpack.includes ("Strength Potion", 1)>>
<<set _cbtCS += 2>> <<set $backpack[1] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<<print $backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>>
<<elseif $backpack.includes ("Strength Potion", 0)>>
<<set _cbtCS += 2>> <<set $backpack[0] = "-empty-">>
<<replace #usestr>> You feel the potion energize you!
<<print $backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>>
<</if>> <</link>>

<<link "No">>
<<replace #usestr>> You decide that you don't need any help defeating this enemy!
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</replace>> <</link>>

</span>
<<else>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> Your Endurance is $Endurance
<</if>>

<</nobr>> <</widget>>
<</nobr>>

    Instead of splitting the code over two Passages, I dumped it all into one, and then started testing things one scenario at a time, here are all the tests I built:

Let's start testing...

No weapons
<<CombatSetup>>

Weapons, no skill
<<set $weapons = [ "Sword", "Axe" ]>>
<<CombatSetup>>

Skill, not for weapons carried
<<set $Disciplines to ["Weaponskill"]>> <<set $WpnProf = "Dagger">>
<<CombatSetup>>

Skill and weapon in slot 1
<<set $WpnProf = "Axe">>
<<CombatSetup>>

Skill and weapon in slot 0
<<set $WpnProf = "Sword">>
<<CombatSetup>>

Skilled, equipped, Mindblast
<<set $Disciplines to ["Weaponskill", "Mindblast"]>>
<<CombatSetup>>

Skilled, equipped, Mindblast, Mindshield
<<set $Disciplines to ["Weaponskill", "Mindblast", "Mindshield"]>>
<<CombatSetup>>

Skilled, equipped, Mindblast, Mindshield, Armor
<<set $specialitems = ["Chainmail Waistcoat"]>>
<<CombatSetup>>

Skilled, equipped, Mindblast, Mindshield, Armor, Helmet
<<set $specialitems = ["Chainmail Waistcoat", "Helmet"]>>
<<CombatSetup>>

Skilled, equipped, Mindblast, Mindshield, Armor, Helmet, Potion in slot 2
<<set $backpack = [ "-empty-", "Strength Potion", "-empty-", "-empty-", "-empty-", "-empty-", "-empty-", "-empty-" ]>>
<<CombatSetup>>







    Finally, I got all of this to work right. So I know how to access the player, no errors. Now I just needed to be able to get the enemy information...

<<set $enemy1 = {
CS: 10,
End: 10,
Mindblast: "true",
Mindshield: "true",
}>>

<<set $enemy2 = {
CS: 15,
End: 15,
Mindblast: "false",
Mindshield: "true",
}>>

<<set $enemy3 = {
CS: 10,
End: 20,
Mindblast: "false",
Mindshield: "false",
}>>

<<widget "EnemyStats">> <<nobr>>

<br> The enemy's Combat Skill is $args[0].CS

<br> The enemy's Endurance is $args[0].End

<<if $args[0].Mindblast == "true">> <br> The enemy has Mindblast
<<elseif $args[0].Mindblast == "false">> <br> The enemy does not have Mindblast
<</if>>

<<if $args[0].Mindshield == "true">> <br> The enemy has Mindshield
<<elseif $args[0].Mindshield == "false">> <br> The enemy does not have Mindshield
<</if>>

<</nobr>> <</widget>>




    And that worked just fine for the enemies. But I got to thinking, instead of making separate variables for the enemy's Mindshield/ Mindblast - could I just give the enemy Object an Array like I do for the player? So I decided to try it...

<<set $enemy4 = {
CS: 44,
End: 44,
Disciplines: [],
}>>

<<set $enemy5 = {
CS: 55,
End: 55,
Disciplines: ["Mindblast"],
}>>

<<set $enemy6 = {
CS: 66,
End: 66,
Disciplines: ["Mindblast", "Mindshield"],
}>>


<<widget "EnemyStatsArray">> <<nobr>>

<br> The enemy's Combat Skill is $args[0].CS

<br> The enemy's Endurance is $args[0].End

<<if $args[0].Disciplines.includes ("Mindblast")>> <br> The enemy has Mindblast
<<else>> <br> The enemy does not have Mindblast
<</if>>

<<if $args[0].Disciplines.includes ("Mindshield")>> <br> The enemy has Mindshield
<<else>> <br> The enemy does not have Mindshield
<</if>>

<</nobr>> <</widget>>

    And hot diggidy dog it actually worked! That's a much nicer way to handle it, and it lets my monsters have the same options as the players, which is not a big deal for this Lone Wolf project but will be great when I spin off this code into my own project. While I was adding the disciplines I also realized that I could add just about anything, including the name and description of the enemy to make things more customized (instead of “enemy”/ "foe" everything).

<<set $enemy7 = {
CS: 66,
End: 66,
Disciplines: ["Mindblast", "Mindshield"],
Name: "Giak",
Description: "A short, foul, twisted servent of the Darklords who delights in murder and mayhem.",
}>>


<<widget "EnemyStatsFull">> <<nobr>>

<br> Your enemy is a $args[0].Name

<br> $args[0].Description

<br> The $args[0].Name's Combat Skill is $args[0].CS

<br> The $args[0].Name's Endurance is $args[0].End

<<if $args[0].Disciplines.includes ("Mindblast")>> <br> The $args[0].Name has Mindblast
<<else>> <br> The $args[0].Name does not have Mindblast
<</if>>

<<if $args[0].Disciplines.includes ("Mindshield")>> <br> The $args[0].Name has Mindshield
<<else>> <br> The $args[0].Name does not have Mindshield
<</if>>

<</nobr>> <</widget>>


    I've got two separate pieces for the player and enemy stats, so now for the combined widget, the one that finally gets all the right info to setup the fight...
 
/*
*   <<CombatSetup $enemy $player>>
*/
<<widget CombatSetup>> <<nobr>>
<br> You prepare for combat...
<br> Your enemy is a $args[0].Name
<br> $args[0].Description

<<set _cbtCS = $args[1].CombatSkill>>

<<if $args[1].Disciplines.includes ("Weaponskill")>>
<<if $args[1].weapons.includes ($args[1].WpnProf)>>
<<set _cbtCS += 2>> <br> Having the weapon you're trained in will give you an edge!
<</if>>
<</if>>

<<if $args[1].weapons.includes ("-empty-", 1)>>
<<if $args[1].weapons.includes ("-empty-", 0)>>
<<set _cbtCS -= 4>> <br> Not having any weapons will make the fight harder!
<</if>>
<</if>>

<<if $args[1].Disciplines.includes ("Mindblast")>>
<<if $args[0].Disciplines.includes ("Mindshield")>> <br> The $args[0].Name resists with Mindshield !
<<else>>
<<set _cbtCS += 2>> <br> Your Mindblast staggers your foe!
<</if>>
<</if>>

<<if $args[0].Disciplines.includes ("Mindblast")>>
<br> The $args[0].Name has Mindblast
<<if $args[1].Disciplines.includes ("Mindshield")>>
, but you resist with Mindshield !
<<else>>  <<set _cbtCS -= 2>>
<br> You struggle to fight through the mental assault!
<</if>>
<</if>>

<<if $args[1].specialitems.contains ("Chainmail Waistcoat")>>
<<set _cbtCS += 2>> <br> Your Chainmail Waistcoat will help protect you!
<</if>>

<<if $args[1].specialitems.contains ("Helmet")>>
<<set _cbtCS += 2>> <br> Your Helmet will help protect you!
<</if>>

<<if $args[1].backpack.contains ("Strength Potion")>>
<br> Do you want to drink one of your Strength Potions to help with the fight? <br>
<<print $args[1].backpack>> <br>
<span id="usestr">
<<link "Yes">>
<<if $args[1].backpack.includes ("Strength Potion", 7)>>
  <<set _cbtCS += 2>> <<set $args[1].backpack[7] = "-empty-">>
  <<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
  <</replace>>
<<elseif $args[1].backpack.includes ("Strength Potion", 6)>>
  <<set _cbtCS += 2>> <<set $args[1].backpack[6] = "-empty-">>
  <<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
  <</replace>>
<<elseif $args[1].backpack.includes ("Strength Potion", 5)>>
  <<set _cbtCS += 2>> <<set $args[1].backpack[5] = "-empty-">>
  <<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
  <</replace>>
<<elseif $args[1].backpack.includes ("Strength Potion", 4)>>
  <<set _cbtCS += 2>> <<set $args[1].backpack[4] = "-empty-">>
  <<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
  <</replace>>
<<elseif $args[1].backpack.includes ("Strength Potion", 3)>>
  <<set _cbtCS += 2>> <<set $args[1].backpack[3] = "-empty-">>
  <<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
  <</replace>>
<<elseif $args[1].backpack.includes ("Strength Potion", 2)>>
  <<set _cbtCS += 2>> <<set $args[1].backpack[2] = "-empty-">>
  <<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
  <</replace>>
<<elseif $args[1].backpack.includes ("Strength Potion", 1)>>
  <<set _cbtCS += 2>> <<set $args[1].backpack[1] = "-empty-">>
  <<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
  <</replace>>
<<elseif $args[1].backpack.includes ("Strength Potion", 0)>>
  <<set _cbtCS += 2>> <<set $args[1].backpack[0] = "-empty-">>
  <<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
  <</replace>>
 <</if>> <</link>>

<<link "No">>
<<replace #usestr>>
<br> You decide that you don't need any help defeating this enemy!
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
<</replace>> <</link>>

</span>
<<else>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
<</if>>

<</nobr>> <</widget>>


    Here are what the player and enemy Objects look like...

<<set $player8 = {
CombatSkill: 10,
Endurance: 20,
Disciplines: ["Weaponskill", "Mindblast", "Mindshield"],
WpnProf: "Sword",
gold: 0,
weapons: [ "Sword", "Axe" ],
backpack: [ "-empty-", "Strength Potion", "-empty-", "Strength Potion", "-empty-", "-empty-", "-empty-", "-empty-" ],
specialitems: ["Helmet", "Chainmail Waistcoat"],
}>>

<<set $enemy3 = {
CS: 10,
End: 20,
Disciplines: ["Mindshield", "Mindblast"],
Name: "Giak",
Description: "A short, foul, twisted servent of the Darklords who delights in murder and mayhem.",
}>>



Let me go over some of that code to talk about what it does...

/*
* <<CombatSetup $enemy $player>>
*/
<<widget CombatSetup>> <<nobr>>
<br> You prepare for combat...
<br> Your enemy is a $args[0].Name
<br> $args[0].Description

<<set _cbtCS = $args[1].CombatSkill>>

    Above I introduced the enemy and set a local variable that will be the final Combat Skill score. I made it local, expecting to add to this widget so it would handle all of the combat system - but in hindsight I should have made it global since I'm actually going to end up using several widgets together for the combat system.

<<if $args[1].Disciplines.includes ("Weaponskill")>>
<<if $args[1].weapons.includes ($args[1].WpnProf)>>
<<set _cbtCS += 2>> <br> Having the weapon you're trained in will give you an edge!
<</if>>
<</if>>

    An easy check, does the player's Array of disciplines have the “Weaponskill” ability, and if so does their weapons Array have the weapon they are skilled with - it was a much better idea to use a separate variable to hold the skilled weapon; if you remember that far back I originally made 9 different ‘weaponskill’ disciplines, which was clunky. I am still mixing cases a lot (some properties are upper-case and others lower) which I really need to smooth out. It's confusing enough writing this stuff, I have to pick a rule for upper or lower and stick with it (having a list of the variables to copy-and-paste helps though).
    Using the Objects is great, the ObjectName.ArrayName.includes ("thing") syntax is easy to remember, works great, and you can include the index number if you want a specific slot/ spot or leave it out to search the whole Array. It also ties each inventory to each character (pc or npc) so I can easily make and track multiple characters (in a future project, always looking ahead). I can also build monsters just like players, giving them a base skill and adding gear or disciplines, to make a varity of challenges. This was one of the better ideas I've had on this project. (which I am proud of until I think about how few of my ideas so far have been good :)

<<if $args[1].weapons.includes ("-empty-", 1)>>
<<if $args[1].weapons.includes ("-empty-", 0)>>
<<set _cbtCS -= 4>> <br> Not having any weapons will make the fight harder!
<</if>>
<</if>>

    If you don't have any weapons at all (and here is where I like that I used “-empty-” to initialize the inventory slots - it's easy to check for that phrase and it makes the code easy for me to read) then you have a penalty to your combat skill score.

<<if $args[1].Disciplines.includes ("Mindblast")>>
<<if $args[0].Disciplines.includes ("Mindshield")>> <br> The $args[0].Name resists with Mindshield !
<<else>>
<<set _cbtCS += 2>> <br> Your Mindblast staggers your foe!
<</if>>
<</if>>

    Do you, $args[1] since the player is the second variable passed, have mindblast and if so does the enemy $args[0] have mindshield?

<<if $args[0].Disciplines.includes ("Mindblast")>>
<br> The $args[0].Name has Mindblast
<<if $args[1].Disciplines.includes ("Mindshield")>>
, but you resist with Mindshield !
<<else>> <<set _cbtCS -= 2>>
<br> You struggle to fight through the mental assault!
<</if>>
<</if>>

    And the same question of mindblast/shield but in reverse.

<<if $args[1].specialitems.contains ("Chainmail Waistcoat")>>
<<set _cbtCS += 2>> <br> Your Chainmail Waistcoat will help protect you!
<</if>>

<<if $args[1].specialitems.contains ("Helmet")>>
<<set _cbtCS += 2>> <br> Your Helmet will help protect you!
<</if>>

    Then there are the two armor pieces, which go in the ‘special items’ Array.

<<if $args[1].backpack.contains ("Strength Potion")>>
<br> Do you want to drink one of your Strength Potions to help with the fight? <br>
<<print $args[1].backpack>> <br>
<span id="usestr">

    Now, the Strength Potion is a little tricky. First I just check to see if you have one. The <<print>> was for debugging, I wanted to be able to see the player's inventory before and after to make sure the potion was being seen and removed correctly.
    The following code I'm abbreviating, it's very repetative. First there's the link if you want to use your potion (you don't have to after all). Then for each backpack slot I have an <<if>> that looks for a potion. It starts looking at backpack[7] - the last slot - first, counting down, because it acted funny when I tried counting up from 0. Once the potion is applied the <<replace>> overwrites the link to use the potion (I'm trying not to write any more infinite potion glitches). Then, there is the final summary of the stats involved, which ends what this widget was designed to do (the actual fight will be next). It is really repetative since I had to duplicate a lot of code for each of the 8 backpack slots - I'm sure there's a better way to do this but I haven't figured it out yet (will look for one though, I'm sick of writing massive walls of code).

<<link "Yes">>
<<if $args[1].backpack.includes ("Strength Potion", 7)>>
<<set _cbtCS += 2>> <<set $args[1].backpack[7] = "-empty-">>
<<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
<</replace>>

<<elseif $args[1].backpack.includes ("Strength Potion", 6)>>
<<set _cbtCS += 2>> <<set $args[1].backpack[6] = "-empty-">>
<<replace #usestr>>
<br> You feel the potion energize you!
<br> <<print $args[1].backpack>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
<</replace>>
<<elseif $args[1].backpack.includes ("Strength Potion", 5)>>
<<set _cbtCS += 2>> <<set $args[1].backpack[5] = "-empty-">>
<<replace #usestr>> .... ..... ....

    Finally, if you didn't want to use a potion or if you didn't have one, the widget prints out your final combat stats.

<<link "No">>
<<replace #usestr>>
<br> You decide that you don't need any help defeating this enemy!
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
<</replace>> <</link>>

</span>
<<else>>
<br> The battle begins!
<br> Your Combat Skill for this fight is _cbtCS
<br> The $args[0].Name's Combat Skill is $args[0].CS
<br> Your Endurance is $args[1].Endurance
<br> The $args[0].Name's Endurance is $args[0].End
<</if>>

<</nobr>> <</widget>>


    Now that we can set up the fight - next we actually need each round of combat...


No comments:

Post a Comment