Sign in to follow this  
Followers 0
Toady

Combat simulator

3 posts in this topic

#1 ·  Posted (edited)

I wrote this script a long time ago. Its a basic fight simulator that displays a player vs. player combat in action. Each player has equal health, chance to crit, and change to dodge or parry. I lost interest in making a way to allow the user to customize their player's stats. Maybe someone else reading this will find it worth while. Oh well, Enjoy.

Shows each players health bars change during combat. Also displays a combat log to see who hit who and for how much damage.

#include <guiconstants.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <ProgressConstants.au3>

$gui = GUICreate("Fight simulation",380,400,-1,-1)
$button = GUICtrlCreateButton("Fight!",120,10,150)
$log = GUICtrlCreateList("",10,100,360,300,Bitor($WS_BORDER, $WS_VSCROLL))
$hpbar1 = GUICtrlCreateProgress(20,50,160,25,$PBS_SMOOTH)
$hpbar2 = GUICtrlCreateProgress(200,50,160,25,$PBS_SMOOTH)
GUICtrlSetData($hpbar1,100)
GUICtrlSetData($hpbar2,100)
$pname1 = GUICtrlCreateLabel("Player 1",20,80)
$pname2 = GUICtrlCreateLabel("Player 2",200,80)
$hptext1 = GUICtrlCreateLabel("1000",150,80)
$hptext2 = GUICtrlCreateLabel("1000",330,80)
GUISetState()

Dim $logtext = ""
Dim $player1[9]
Dim $player2[9]

While 1
    $msg = GUIGetMsg()
    Select
    case $msg = -3 
        Exit
    Case $msg = $button
        fight()
    EndSelect
WEnd

Func fight()
    GUICtrlSetState($button,$GUI_DISABLE)
    GUICtrlSetData($log,"")
    ;both players have same weapon and skills and same base health
    ;Both players have 1000 health, 15% chance to crit, 150 base weapon damage, 20% chance to dodge or block
    setStats($player1,"Player1",1000,15,150,$hpbar1,$hptext1,10,10)
    setStats($player2,"Player2",1000,15,150,$hpbar2,$hptext2,10,10)
    GUICtrlSetData($pname1,$player1[0])
    GUICtrlSetData($pname2,$player2[0])
    GUICtrlSetData($hptext1,$player1[1])
    GUICtrlSetData($hptext2,$player2[1])
    While 1
        attack($player1,$player2)
        sleep(Random(200,1000,1)) ;random sleep to make fight appear more real, lol no really.
        If $player2[1] = 0 Then ExitLoop ;if health = 0 then player 1 won
        attack($player2,$player1)
        sleep(Random(200,1000,1))
        If $player1[1] = 0 Then ExitLoop
    WEnd
    If $player1[1] = 0 Then 
        GUICtrlSetData($log,$player2[0] & " defeats " & $player1[0] & "!")
    EndIf
    If $player2[1] = 0 Then 
        GUICtrlSetData($log,$player1[0] & " defeats " & $player2[0] & "!")
    EndIf
    GUICtrlSetState($button,$GUI_ENABLE)
EndFunc

Func attack($self,ByRef $enemy)
    Local $event
    Local $dmg = Random($self[3]*0.70,$self[3],1) ;each player can mitigate up to 30% damage
    Local $bCrit = False
    Local $dodge = Random(0,100,1)
    Local $miss = Random(0,100,1)
    Local $block = Random(0,100,1)
    Local $chance = 10 ;chance to miss (10%)
    If $dodge <= $enemy[7] Or $miss <= $chance Or $block <= $enemy[8] Then
        If $miss <= $chance Then
            $event = $self[0] & " misses"
        ElseIf $dodge <= $enemy[7] Then
            $event = $enemy[0] & " dodges " & $self[0] & "'s attack"
        ElseIf $block <= $enemy[8] Then
            $event = $enemy[0] & " blocks " & $self[0] & "'s attack"
        EndIf
    Else
        If Random(0,100,1) <= $self[2] Then
            $dmg = Round($dmg*1.5,0) ;crit damage (+50%)
            $bCrit = True
        EndIf
        $enemy[1] -= $dmg
        $event = $self[0] & " hits " & $enemy[0] & " for " & $dmg
        If $bCrit Then
            $event &= " (critical)"
        Endif
    EndIf
    $event &= "|"
    GUICtrlSetData($log,Guictrlread($log) & $event)
    GUICtrlSetData($enemy[4],100*($enemy[1]/$enemy[6]))
    If $enemy[1] < 0 Then $enemy[1] = 0
    GUICtrlSetData($enemy[5],$enemy[1])
    _GUICtrlListBox_SetCaretIndex($log,_GUICtrlListBox_GetCount($log)-1)
EndFunc

Func setStats(ByRef $player,$name,$health,$critchance,$bonusdmg,$hpbar,$hptext,$dodge,$block)
    $player[0] = $name
    $player[1] = $health ;current health
    $player[2] = $critchance
    $player[3] = $bonusdmg
    $player[4] = $hpbar
    $player[5] = $hptext
    $player[6] = $health ;base health
    $player[7] = $dodge ;chance to dodge
    $player[8] = $block ;chance to block
EndFunc
Edited by Toady

www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Share this post


Link to post
Share on other sites



Very nice script. Player1 is very strong. ;)


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Yeah, a little too strong perhaps. Going first is a definite advantage! ;)

Maybe randomise who goes first? Or randomise who hits who at any given point? (Maybe that could be part of a 'Speed' or 'Initiative' stat?)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0