# SubRoutine not fully Exicuting

• ### Question

• Hello.  I'm learning Small Basic and trying to run a program that has a number of Subroutines. The Subroutines only are running up to their First 2 Questions (Num inserts).

The Program is supposed to subtract to inserted numbers then, looking at the difference set off a specific IF-THEN. Within the IF-THEN is a Math.GetRandomNumber that is then compared to another IF-THEN. For those who are familiar this is a variant of the WEGD6 Ship Damage Charts.

I figure the problem is simple, I'm just not seeing it.

Thank You For Reading and Responding

```TextWindow.WriteLine ("Welcome to Ship Damage Calculator v1.0 [c] by David Gipe")
TextWindow.WriteLine ("Would you like to use the Expanded Charts? [Y(1) / N(2)]")
IF Question1 = 2 THEN
Standard()
ELSE
Extended()
ENDIF

Sub Standard
TextWindow.WriteLine ("Note to End Program Enter Zorn in Damage Field")
TextWindow.Writeline ("What is the damage roll?")
IF Damage = "Zorn" THEN
TextWindow.Writeline ("Thank You For Using SDC v1.0. Good Gaming!")
ENDIF
TextWindow.Writeline("What is the Resistance Roll?")
Dtotal = Damage-Resist
IF Dtotal <= 3 THEN
TextWindow.Writeline("Shields Blown OR Controls Ionized if Shield Arc was not Hit")
IF Dtotal > 4 < 9 THEN
TextWindow.Writeline("Vessel has been Lightly Damaged!")
STDLightDamage()
IF Dtotal >= 9 <= 12 THEN
TextWindow.Writeline("Vessel has been Heavily Damaged!")
STDHeavyDamage()
IF Dtotal >= 13 <= 15 THEN
TextWindow.Writeline("Vessel has been Severely Damaged!!")
STDSevereDamage()
IF Dtotal >= 16 THEN
TextWindow.Writeline("Vessel has been Destroyed!!!")
EndIf
EndIf
EndIf
EndIf
EndIf

EndSub

Sub Extended
TextWindow.Writeline("Note: to End Program Enter Zorn for Damage")
TextWindow.Writeline ("What is the damage roll?")
IF Damage = "Zorn" THEN
TextWindow.Writeline ("Thank You For Using SDC v1.0. Good Gaming!")
ENDIF
TextWindow.Writeline("What is the Resistance Roll?")
Dtotal = Damage-Resist
IF Dtotal < 3 THEN
TextWindow.Writeline("Shields Blown OR Controls Ionized if Shield Arc was not Hit")
IF Dtotal >= 4 <= 8 THEN
TextWindow.Writeline("Vessel has been Lightly Damaged!")
EXDLightDamage()
IF Dtotal >= 9 <= 12 THEN
TextWindow.Writeline("Vessel has been Heavily Damaged!")
EXDHeavyDamage()
IF Dtotal >= 13 <= 15 THEN
TextWindow.Writeline("Vessel has been Severly Damaged!!")
EXDSevereDamage()
IF Dtotal >= 16 THEN
TextWindow.Writeline("Vessel has been Destroyed!!!")
ENDIF
EndIf
EndIf
EndIf
EndIf

EndSub

Sub STDLightDamage
LightD = Math.GetRandomNumber (6)
IF LightD = 1 THEN
TextWindow.Writeline("-1D From Maneuverability. If already at Zero then -1 from Movement")
IF LightD = 2 THEN
TextWindow.Writeline("One On-Board Weapon is Hit and Destroyed!")
IF LightD = 3 THEN
TextWindow.Writeline("One On-Board Weapon is Hit and Lightly Damaged.")
IF LightD = 4 THEN
TextWindow.Writeline("Hyperdrive Engines Damaged. Double normal Astrogation times. If making a 1 Round Jump use standard difficulty rules [is x2 difficulty] and add 10")
IF LightD = 5 THEN
TextWindow.Writeline("-1D from Shields. If Shields are 0D then 1 hit of Controls are Ionized")
IF LightD = 6 THEN
TextWindow.Writeline("-1 from Movement")
ENDIF
EndIf
EndIf
EndIf
EndIf
EndIf

ENDSUB

Sub STDHeavyDamage
HeavyD = Math.GetRandomNumber (6)
IF HeavyD = 1 THEN
TextWindow.Writeline("-2D From Maneuverability. If already at Zero then -2 from Movement")
IF HeavyD = 2 THEN
TextWindow.Writeline("Lose a Weapon in one Fire-Arc")
IF HeavyD = 3 THEN
TextWindow.Writeline("All Weapons of One type in a   Single Fire-Arc is Destroyed!")
IF HeavyD = 4 THEN
TextWindow.Writeline("Hyperdrive Engines Damaged add 10 to all Astrogation difficulties")
IF HeavyD = 5 THEN
TextWindow.Writeline("-2D from Shields. If Shields are 0D then 2 hits of Controls are Ionized")
IF HeavyD = 6 THEN
TextWindow.Writeline("-2 from Movement")
ENDIF
EndIf
EndIf
EndIf
EndIf
EndIf

ENDSUB

Sub STDSevereDamage
SevereD = Math.GetRandomNumber (6)
IF SevereD = 1 THEN
TextWindow.Writeline("Dead in Space All Engine and Maneuvering Systems Destroyed!!")
IF SevereD = 2 THEN
IF SevereD = 3 THEN
TextWindow.Writeline("Hyperdrive Disabled.")
IF SevereD = 4 THEN
TextWindow.Writeline("All Weapons Lose Power and are")
WeaponDamage = Math.GetRandomNumber (6)
IF WeaponDamage <= 4 THEN
TextWindow.Writeline ("All Weapons are Severely Damaged!")
ELSE
TextWindow.Writeline ("All Weapons are Destroyed!!!")
IF SevereD = 5 THEN
TextWindow.Writeline("Structural Damage vessel Disintegrates in 1D6 Rounds!!!")
IF SevereD = 6 THEN
TextWindow.Writeline("Vessel Explodes in a ball of Fire!!")
ENDIF
ENDIF
EndIf
EndIf
EndIf
EndIf
EndIf

ENDSUB

Sub EXDLightDamage
LightD = Math.GetRandomNumber (18)
IF LightD = 1 THEN
TextWindow.Writeline("-1D From Maneuverability. If already at Zero then -1 from 	Movement")
IF LightD = 2 THEN
TextWindow.Writeline("One On-Board Weapon is Hit and Destroyed!")
IF LightD = 3 THEN
TextWindow.Writeline("One On-Board Weapon is Hit and Lightly Damaged.")
IF LightD = 4 THEN
TextWindow.Writeline("Hyperdrive Engines Damaged. Double normal Astrogation times. If 	making a 1 Round Jump use standard difficulty rules [is x2 difficulty] and add 10")
IF LightD = 5 THEN
TextWindow.Writeline(" -1D from Shields. If Shields are 0D then 1 hit of Controls are 	Ionized")
IF LightD = 6 THEN
TextWindow.Writeline("-1 from Movement")
IF LightD = 7 THEN
TextWindow.Writeline("Power disruption -1D to 1 system and crew in area take 2D damage")
IF LightD = 8 THEN
TextWindow.Writeline("Hyperdrive damage +10 to rolls for one round jumps")
IF LightD = 9 THEN
TextWindow.Writeline("Shield grid damaged -1D to shield operations Easy Repair")
IF LightD = 10 THEN
TextWindow.Writeline("Communications damaged, communications requires a difficulty roll of 	Meoderate or +5 to existing difficulties")
IF LightD = 11 THEN
TextWindow.Writeline("Random Gun lightly damaged -1pip from damage")
IF LightD = 12 THEN
TextWindow.Writeline("Holds are Hit")
Hold = Math.GetRandomNumber (2)
IF Hold = 1 THEN
TextWindow.Writeline ("Lose Half of your Cargo OR 3D6 Items")
TextWindow.Writeline ("Lose Half of your Consumables")
IF LightD = 13 THEN
TextWindow.Writeline("Ship losses 1D6 Maneuverability")
IF LightD = 14 THEN
TextWindow.Writeline("Random Weapon out of alignment -1d6 Fire Control and gunner takes 2D6 damage")
IF LightD = 15 THEN
TextWindow.Writeline("Shield Grid surge one shield Arc disabled, Easy repair.")
IF LightD = 16 THEN
TextWindow.Writeline("Shield grid damaged Shields -1 pip, Easy Repair")
IF LightD = 17 THEN
TextWindow.Writeline("Sensor Ssytem Damaged -1D to sensors")
IF LightD = 18 THEN
TextWindow.Writeline("Engine power damaged “Terrain” difficulties increased by 5")
ENDIF
ENDIF
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf

ENDSUB

Sub EXDHeavyDamage
HeavyD = Math.GetRandomNumber (18)
IF HeavyD = 1 THEN
TextWindow.Writeline("-2D From Maneuverability. If already at Zero then -2 from Movement")
IF HeavyD = 2 THEN
TextWindow.Writeline("Lose a Weapon in one Fire-Arc")
IF HeavyD = 3 THEN
TextWindow.Writeline("All Weapons of One type in a   Single Fire-Arc is Destroyed!")
IF HeavyD = 4 THEN
TextWindow.Writeline("Hyperdrive Engines Damaged add 10 to all Astrogation difficulties")
IF HeavyD = 5 THEN
TextWindow.Writeline("-2D from Shields. If Shields are 0D then 2 hits of Controls are Ionized")
IF HeavyD = 6 THEN
TextWindow.Writeline("-2 from Movement")
IF HeavyD = 7 THEN
TextWindow.Writeline("Random Weapon takes Damage, Moderate Repair. Damage Reduced by 1D6 Gunner Takes 3D damage")
IF HeavyD = 8 THEN
TextWindow.Writeline("Sensors Severely Damaged and Off-Line, Moderate Repair")
IF HeavyD = 9 THEN
TextWindow.Writeline("Hyperdrive Damage all Astrogation difficulties increased by 15")
IF HeavyD = 10 THEN
TextWindow.Writeline("Medical Equipment Damage IF N\A then Life Support damaged, Moderate Repair Or Fire Control -1D")
IF HeavyD = 11 THEN
TextWindow.Writeline("All Weapons lose power takes 5 rounds to repair in combat at very easy decrease rounds by one for every difficulty above very easy")
IF HeavyD = 12 THEN
TextWindow.Writeline("Navigational Computer Damaged one round Jumps not possible.")
IF HeavyD = 13 THEN
TextWindow.Writeline("Piloting controls damages, -2D to Pilot Skills")
IF HeavyD = 14 THEN
TextWindow.Writeline("Shield Grid damaged. Shields -1D. Repair Difficulty increased by 1 level.")
IF HeavyD = 15 THEN
TextWindow.Writeline("Hull fracuted. Hull is reduced by 1 pip, Difficult Repair")
IF HeavyD = 16 THEN
TextWindow.Writeline("Heavy Power surge all Weapons lightly damaged. Reduce damage by 2 pips")
IF HeavyD = 17 THEN
TextWindow.Writeline("Movement -1 and Maneuverability -2 pips")
IF HeavyD = 18 THEN
TextWindow.Writeline("Random weapon takes damage. Weapon damage is reduced by 1D and gunner takes 4D6 damage")
ENDIF
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf

ENDSUB

Sub EXDSevereDamage
SevereD = Math.GetRandomNumber (18)
IF SevereD = 1 THEN
TextWindow.Writeline("Dead in Space All Engine and Maneuvering Systems Destroyed!!")
IF SevereD = 2 THEN
IF SevereD = 3 THEN
TextWindow.Writeline("Hyperdrive Disabled.")
IF SevereD = 4 THEN
TextWindow.Writeline("All Weapons Lose Power and are")
WeaponDamage = Math.GetRandomNumber (6)
IF WeaponDamage <= 4 THEN
TextWindow.Writeline ("All Weapons are Severely Damaged!")
ELSE
TextWindow.Writeline ("All Weapons are Destroyed!!!")
IF SevereD = 5 THEN
TextWindow.Writeline("Structural Damage vessel Disintegrates in 1D6 Rounds!!!")
IF SevereD = 6 THEN
TextWindow.Writeline("Vessel Explodes in a ball of Fire!!")
IF SevereD = 7 THEN
TextWindow.Writeline("Power Core is Disabled, Difficult repair")
IF SevereD = 8 THEN
IF SevereD = 9 THEN
TextWindow.Writeline("Massive power surge in hit Arc , crew in area takes 6D6 damage")
IF SevereD = 10 THEN
TextWindow.Writeline("Sub-Light drives Destroyed, Movement 0")
IF SevereD = 11 THEN
TextWindow.Writeline("Communications Destroyed, Difficult repair")
IF SevereD = 12 THEN
TextWindow.Writeline("Holds Breached!")
Breach = Math.GetRandomNumber (6)
IF Breach <= 4 THEN
TextWindow.Writeline ("All Cargo Lost. If Capital then 75% Lost")
ELSE
TextWindow.Writeline ("All Consumables Lost. If Capital 75% Lost")
IF SevereD = 13 THEN
TextWindow.Writeline("Sensor System Destroyed. If Capital Heroic to Repair for Half Function")
IF SevereD = 14 THEN
TextWindow.Writeline("Shield Generator Hit")
ShieldHit = Math.GetRandomNumber (6)
IF ShieldHit <= 4 THEN
TextWindow.Writeline ("Disabled, Difficult Repair")
ELSE
TextWindow.Writeline ("Destroyed")
IF SevereD = 15 THEN
TextWindow.Writeline("FCS Computer damaged, Fire Control Reduced to 0D6")
IF SevereD = 16 THEN
TextWindow.Writeline("Ship losses 3 Movement")
IF SevereD = 17 THEN
TextWindow.Writeline("Generator Surging last used system Disabled, Moderate Repair")
IF SevereD = 18 THEN
TextWindow.Writeline("Piloting controls Damaged -4D to Piloting Skills")
ENDIF
ENDIF
ENDIF
ENDIF
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf

ENDSUB```

Thursday, June 6, 2013 8:21 PM

• Methedor,

Here's an example from your code.  You can change Dtotal to any value and will get the "Heavily Damaged" message when Dtotal = 9 through 12.  Basically  I just added "And".  So the logic is if Dtotal is greater than or equal to 9 "And" Dtotal is Less than or equal to 12 then print the line.

Dtotal=9
IF Dtotal >= 9 And Dtotal <= 12 THEN
TextWindow.Writeline("Vessel has been Heavily Damaged!")
endif

JR

JR

Friday, June 7, 2013 1:13 AM
• Wow that's a lot of nested if statements - consider using the ElseIf command or even an array of results.

EDIT:  The example below - in you code if SevereD is 16 it never enters the check for 16 (highlighted code), since it is not 15 and doesn't even enter the first If (underlined code).

For example:

```IF SevereD = 15 THEN
TextWindow.Writeline("FCS Computer damaged, Fire Control Reduced to 0D6")
IF SevereD = 16 THEN
TextWindow.Writeline("Ship losses 3 Movement")
IF SevereD = 17 THEN
TextWindow.Writeline("Generator Surging last used system Disabled, Moderate Repair")
IF SevereD = 18 THEN
TextWindow.Writeline("Piloting controls Damaged -4D to Piloting Skills")
ENDIF
ENDIF
ENDIF
ENDIF```

Should be

```IF SevereD = 15 THEN
TextWindow.Writeline("FCS Computer damaged, Fire Control Reduced to 0D6")
ELSEIF SevereD = 16 THEN
TextWindow.Writeline("Ship losses 3 Movement")
ELSEIF SevereD = 17 THEN
TextWindow.Writeline("Generator Surging last used system Disabled, Moderate Repair")
ELSEIF SevereD = 18 THEN
TextWindow.Writeline("Piloting controls Damaged -4D to Piloting Skills")
ENDIF
```

Or

```TextSevereD[15] = "FCS Computer damaged, Fire Control Reduced to 0D6"
TextSevereD[16] = "Ship losses 3 Movement"
TextSevereD[17] = "Generator Surging last used system Disabled, Moderate Repair"
TextSevereD[18] = "Piloting controls Damaged -4D to Piloting Skills"

TextWindow.Writeline(TextSevereD[SevereD])```
What data do we input and for that input what do expect the result to be?
Thursday, June 6, 2013 8:56 PM
• You can also use the following code (as the ranges are connected):

```If d<4 Then
TextWindow.Writeline("Shields Blown OR Controls Ionized if Shield Arc was not Hit")
ElseIf d<9 Then
TextWindow.Writeline("Vessel has been Lightly Damaged!")
ElseIf d<13 Then
TextWindow.Writeline("Vessel has been Heavily Damaged!")
ElseIf d<16 Then
TextWindow.Writeline("Vessel has been Severely Damaged!!")
Else
TextWindow.Writeline("Vessel has been Destroyed!!!")
EndIf
```

Jan [ WhTurner ] The Netherlands

• Edited by Saturday, June 8, 2013 1:12 PM
• Proposed as answer by Tuesday, June 11, 2013 8:53 AM
• Marked as answer by Sunday, June 16, 2013 5:58 PM
Saturday, June 8, 2013 1:09 PM

### All replies

• Wow that's a lot of nested if statements - consider using the ElseIf command or even an array of results.

EDIT:  The example below - in you code if SevereD is 16 it never enters the check for 16 (highlighted code), since it is not 15 and doesn't even enter the first If (underlined code).

For example:

```IF SevereD = 15 THEN
TextWindow.Writeline("FCS Computer damaged, Fire Control Reduced to 0D6")
IF SevereD = 16 THEN
TextWindow.Writeline("Ship losses 3 Movement")
IF SevereD = 17 THEN
TextWindow.Writeline("Generator Surging last used system Disabled, Moderate Repair")
IF SevereD = 18 THEN
TextWindow.Writeline("Piloting controls Damaged -4D to Piloting Skills")
ENDIF
ENDIF
ENDIF
ENDIF```

Should be

```IF SevereD = 15 THEN
TextWindow.Writeline("FCS Computer damaged, Fire Control Reduced to 0D6")
ELSEIF SevereD = 16 THEN
TextWindow.Writeline("Ship losses 3 Movement")
ELSEIF SevereD = 17 THEN
TextWindow.Writeline("Generator Surging last used system Disabled, Moderate Repair")
ELSEIF SevereD = 18 THEN
TextWindow.Writeline("Piloting controls Damaged -4D to Piloting Skills")
ENDIF
```

Or

```TextSevereD[15] = "FCS Computer damaged, Fire Control Reduced to 0D6"
TextSevereD[16] = "Ship losses 3 Movement"
TextSevereD[17] = "Generator Surging last used system Disabled, Moderate Repair"
TextSevereD[18] = "Piloting controls Damaged -4D to Piloting Skills"

TextWindow.Writeline(TextSevereD[SevereD])```
What data do we input and for that input what do expect the result to be?
Thursday, June 6, 2013 8:56 PM
• Thanks for the Alternative Commands. I'll Switch over to ElseIf to conserve space (As I'm not sure how to use Arrays). The data is coming from the difference of the Damage and Resist

```TextWindow.Writeline ("What is the damage roll?")
```TextWindow.Writeline("What is the Resistance Roll?")
Dtotal = Damage-Resist```

The Result (Dtotal) is then compared to This:

```IF Dtotal <= 3 THEN
TextWindow.Writeline("Shields Blown OR Controls Ionized if Shield Arc was not Hit")
IF Dtotal > 4 < 9 THEN
TextWindow.Writeline("Vessel has been Lightly Damaged!")
STDLightDamage()
IF Dtotal >= 9 <= 12 THEN
TextWindow.Writeline("Vessel has been Heavily Damaged!")
STDHeavyDamage()
IF Dtotal >= 13 <= 15 THEN
TextWindow.Writeline("Vessel has been Severely Damaged!!")
STDSevereDamage()
IF Dtotal >= 16 THEN
TextWindow.Writeline("Vessel has been Destroyed!!!")```

Depending on the result One of the several nested SubRoutines should go off. In these SubRoutines are the random damage results.

----

IE: Damage = 12 Resist = 2 so Dtotal = 10 [12 subtract 2] That Triggers (Assuming we use the Standard Chart) STDHeavyDamage()

From STDHeavyDamage() we go into the RandomNumber (6)

The Number will then trigger one of the corresponding IF statements to display the Text Result.

----

All I get to is the Questions of Damage and Resistance, it then ends the program. However the Zorn string does trigger the closing If-Then.

Thursday, June 6, 2013 9:14 PM
• Did you see my edited change to post above where I think the problems are - switching to elseif will probably solve or at least make the code easier to read - also use the format option - right click inside the code and select 'Format Program'.
Thursday, June 6, 2013 9:30 PM
• Ok Using ELSEIF has solved part of the issue. Thanks!

The only issue I'm having now is determining how to state a Range. Like "When a number from 1-4 happens" Right now I'm using >= # <= # and so I have it triggering all responses. I'll see if I can find a basic command list.

Friday, June 7, 2013 12:39 AM
• Methedor,

Here's an example from your code.  You can change Dtotal to any value and will get the "Heavily Damaged" message when Dtotal = 9 through 12.  Basically  I just added "And".  So the logic is if Dtotal is greater than or equal to 9 "And" Dtotal is Less than or equal to 12 then print the line.

Dtotal=9
IF Dtotal >= 9 And Dtotal <= 12 THEN
TextWindow.Writeline("Vessel has been Heavily Damaged!")
endif

JR

JR

Friday, June 7, 2013 1:13 AM
• You can also use the following code (as the ranges are connected):

```If d<4 Then
TextWindow.Writeline("Shields Blown OR Controls Ionized if Shield Arc was not Hit")
ElseIf d<9 Then
TextWindow.Writeline("Vessel has been Lightly Damaged!")
ElseIf d<13 Then
TextWindow.Writeline("Vessel has been Heavily Damaged!")
ElseIf d<16 Then
TextWindow.Writeline("Vessel has been Severely Damaged!!")
Else
TextWindow.Writeline("Vessel has been Destroyed!!!")
EndIf
```

Jan [ WhTurner ] The Netherlands

• Edited by Saturday, June 8, 2013 1:12 PM
• Proposed as answer by Tuesday, June 11, 2013 8:53 AM
• Marked as answer by Sunday, June 16, 2013 5:58 PM
Saturday, June 8, 2013 1:09 PM