I Have spent the past few hours getting a battle simulator finished and I am having a problem with it throwing an overflow error. The battle lasts way to many rounds and every time a type of troop dies, it gets replaced some how. This makes the attack a never-ending loop. If anyone can find the problem I would be grateful.
Below is the Battle Module.
Below is the Battle Module.
Option Explicit
Public Target As Integer
Public Round As Integer
Public DistToTarget
Public Pos(1 To 2, 1 To 10) As Integer
Dim Sent(1 To 10) As Integer
Dim Defenders(1 To 10) As Integer
Public Sub Send(ID, N)
Sent(ID) = N
End Sub
Public Sub Set_Defender(ID, N)
Defenders(ID) = N
End Sub
Public Sub Battle_Start()
Round = 1
Dim X
For X = 1 To 10
Pos(1, X) = 0 - TroopHandler.Get_Range(X)
Next X
For X = 1 To 10
Pos(2, X) = TroopHandler.Get_Range(X) + 50
Next X
PlayerAttack
End Sub
Public Sub PlayerAttack()
Dim T, R, C, S
For T = 1 To 10
If Sent(T) > 0 Then
'Troops found with ID T
'Get range
R = TroopHandler.Get_Range(T)
'get closest enemy
C = Get_ClosestEnemy
Debug.Print "Closest Enemy: " & C
'Check if closest is in range
If R <= Pos(2, C) Then
'Get stats
Dim PAtt, ELife, DefEff As Integer
Dim PCount, ECount As Integer
PAtt = TroopHandler.Get_Att(T)
ELife = TroopHandler.Get_Life(C)
PCount = Sent(T)
ECount = Defenders(C)
'Calculate
Dim AttEff
AttEff = PAtt * PCount
DefEff = (0.5 * ELife) * ECount
Debug.Print "Round: " & Round & " AttEff: " & AttEff
Debug.Print "DefEff: " & DefEff
Debug.Print "Att-DEf: " & AttEff - DefEff
Debug.Print "Def-Att: " & DefEff - AttEff
'ATTACK!
Debug.Print "Defenders: " & Defenders(C)
Debug.Print "Defense: " & (1 / 2 * ELife)
Debug.Print "Life: " & ELife
Debug.Print "Total Defense: " & (ECount * (1 / 2 * ELife))
Debug.Print "Killed: " & ((ECount * (1 / 2 * ELife) - AttEff))
'Defenders(C) = Defenders(C) - ((ECount * (1 / 2 * ELife) - AttEff))
Defenders(C) = (DefEff - AttEff)
Debug.Print "Lived:" & Defenders(C)
End If
End If
Next T
'Move forward
For T = 1 To 10
If Pos(1, T) < 0 Then
S = TroopHandler.Get_Speed(T)
If Abs(Pos(1, T)) < S Then
Pos(1, T) = 0
Else
Pos(1, T) = Pos(1, T) + S
End If
End If
Next T
Dim O
O = Check_Outcome
If O = 0 Then
Round = Round + 1
PCAttack
Else
MsgBox "Player Won."
Dim L
'Give reward
For L = 1 To 6
Call ResHandler.Set_Res(L, Map.Get_Reward(L))
Next L
End If
End Sub
Public Sub PCAttack()
Debug.Print "ENEMY ATTACK"
Dim T, R, C, S
For T = 1 To 10
If Defenders(T) > 0 Then
'Troops found with ID T
'Get range
R = TroopHandler.Get_Range(T)
Debug.Print "Range found"
'get closest enemy
C = Get_ClosestPlayer
Debug.Print "Closest: " & C
'Check if closest is in range
Debug.Print "Range: " & R
Debug.Print "Pos: " & Pos(1, C)
If R <= Abs(Pos(1, C)) Then
'Get stats
Dim EAtt, PLife, DefEff As Integer
Dim PCount, ECount As Integer
EAtt = TroopHandler.Get_Att(T)
PLife = TroopHandler.Get_Life(C)
PCount = Sent(T)
ECount = Defenders(C)
Debug.Print "EAtt: " & EAtt
Debug.Print "PCount: " & PCount
'Calculate
Dim AttEff
AttEff = EAtt * ECount
DefEff = (0.5 * PLife) * PCount
Debug.Print "AttEff: " & AttEff
Debug.Print "DefEff: " & DefEff
'ATTACK!
If Sent(C) <= 0 Then GoTo 1
Sent(C) = DefEff - AttEff
Debug.Print "Player's Troops: " & Sent(C)
'Sent(C) = ((PCount * (1 / 2 * PLife) - AttEff))
End If
End If
1:
Next T
'Move forward
For T = 1 To 10
If Pos(1, T) > 0 Then
S = TroopHandler.Get_Speed(T)
If Pos(1, T) < S Then
Pos(1, T) = 0
Else
Pos(1, T) = Pos(1, T) - S
End If
End If
Next T
Dim O
O = Check_Outcome
If O = 0 Then
Round = Round + 1
PlayerAttack
Else
MsgBox "Enemy Won."
End If
End Sub
Public Function Check_Etroops()
End Function
Public Function Check_Outcome()
Dim X
'Check Enemy
For X = 1 To 10
If Defenders(X) > 0 Then
GoTo CheckP
End If
Next X
Check_Outcome = 1 'Player won
Debug.Print "OUTCOME...PLAYER WON!"
Exit Function
CheckP:
For X = 1 To 10
If Sent(X) > 0 Then
Check_Outcome = 0 'None
Exit Function
End If
Next X
Check_Outcome = 2 'Enemy won
Debug.Print "OUTCOME...ENEMY WON!"
End Function
Public Function Get_ClosestEnemy()
Dim X
Dim Closest
Closest = 200
Dim C(1 To 10)
For X = 1 To 10
C(X) = Pos(2, X)
Next X
For X = 1 To 10
If C(X) < Closest Then
Closest = X
End If
Next X
Get_ClosestEnemy = Closest
End Function
Public Function Get_ClosestPlayer()
Dim X
Dim Closest
Closest = 200
Dim C(1 To 10)
For X = 1 To 10
C(X) = Pos(1, X)
Next X
For X = 1 To 10
If C(X) < Closest Then
Closest = X
End If
Next X
Get_ClosestPlayer = Closest
End Function