VB Magic

2012/02/10

Added a generator tool (Windows) in VB.net for TyranntMicro

Filed under: .NET, Fez Spider, Gadgeteer, Learning, VB.NET, Windows Forms — Tags: , , , — vbmagic @ 7:56 pm

Hi,

Added a new generator tool. So far it can just generate items:

This tool will store the items in “|” delimited text files which can easily be turned into objects using the split(“|”c) or split(‘|’) string function.

An example of the above “Arrows” item is:

1.0|Arrows||A Quiver of arrows. Holds a maximum of 10 arrows|arrowico.gif|arrow.gif|ammo|10|0|False|False|False

I was planning to do this on the Spider but it just makes more sense to do this on a desktop machine.

So Character Generator is almost finished. (Starting items are all that is required)
Item generator done
Next when I get spare time I’ll start working on the Maze Generator in the generator tool and a “Play Game” screen system on the Spider.

2011/04/19

Rolling the Dice

Filed under: .NET, VB.NET — Tags: , , , , — vbmagic @ 9:46 pm

Doing a game that is an RPG, one thing that I will always need to do is roll a dice. There may be better ways of introducing randomisation but rolling the dice is my preferred method. It was the best part of playing games like Dungeons and Dragons and Middle Earth Role Playing as a kid. We always used a certain way of writing down the dice formula to complete a certain action, for example:

2d6+1

Would mean that you would roll two six sided dice and then add one to the result. This formula has also stuck with me when I end up trying to write an RPG game. I once used a PICK operating system in one of my very early jobs to write a character generator in PICK Basic (Or Data Basic as it was also known) This piece of code seems to have followed me to multiple different operating systems and languages. I thought I would post the latest incarnation. A quick note, the FIELD function used was available in the PICK Basic Language it was used for manipulating strings, I did a .net version to make things easier for me 🙂


Module support

    Public Function RollDice(value As String) As Integer
        ' setup working variables to make things easier
        Dim tmp As String = ""
        Dim modType As String = ""
        Dim numberOfDice As Integer = 0
        Dim numberOfSidesPerDice As Integer = 0
        Dim modifier As Integer = 0
        ' First we check to see if there is a "d" in the string
        If value.Contains("d") = True Then
            ' There is so first we need to get the value infront of the d
            tmp = Field(value, "d"c, 1).Trim
            ' and convert it to an integer if it's a number, errors are silently
            ' ignored for now. 
            If Integer.TryParse(tmp, numberOfDice) = False Then
                numberOfDice = 0
            End If
            ' Now look at the value after the d
            tmp = Field(value, "d"c, 2).Trim
            ' does it contain a + or a -?
            If tmp.Contains("+") = True Then
                modType = "+"
            End If
            If tmp.Contains("-") = True Then
                modType = "-"
            End If
            ' if does not contain a + or a -, then there is no modifer
            If modType = "" Then
                ' and we take the right side of the d as the number of sides
                ' of the dice
                If Integer.TryParse(tmp, numberOfSidesPerDice) = False Then
                    numberOfSidesPerDice = 0
                End If
            Else
                ' there is a + or a - so we need to extract the number on the left
                ' side of the +/-
                Dim bit As String = Field(tmp, CChar(modType), 1).Trim
                If Integer.TryParse(bit, numberOfSidesPerDice) = False Then
                    numberOfSidesPerDice = 0
                End If
                ' now we take the right side of the +/- and set that to the modifier
                bit = Field(tmp, CChar(modType), 2).Trim
                If Integer.TryParse(bit, modifier) = False Then
                    modifier = 0
                End If
            End If
        Else
            ' Ah so there is no d so we assume it's not a forumlar, just a number
            numberOfDice = 0
            numberOfSidesPerDice = 0
            If Integer.TryParse(value, 0) = True Then
                modifier = 0
            Else
                modifier = 0
            End If
        End If


        ' Now comes time to roll the dice
        Dim lp As Integer
        Dim total As Integer = 0
        ' Set up a random object randomised by the syystem date and time
        Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
        ' loop through the number of dice
        For lp = 1 To numberOfDice
            ' add each roll to the total
            total = total + CInt(objRandom.Next(numberOfSidesPerDice) + 1)
        Next
        ' now modify the total if needed
        If modType = "+" Then
            total += modifier
        ElseIf modType = "-" Then
            total -= modifier
        End If
        ' we have the results of the dice roll
        Return total
    End Function

    ' Using the delimiter to split the string into chunks, return the pos chunk
    ' e.g. Field("1d6+1","d",2) would return "6+1"
    Public Function Field(ByVal sourceString As String, ByVal delimiter As Char, ByVal pos As Integer) As String
        Dim parts() As String = sourceString.Split(delimiter)
        If pos > parts.Length Then
            Return ""
        Else
            Return parts(pos - 1)
        End If
    End Function

End Module

2011/04/18

Quick post about XML Literals

Filed under: .NET, VB.NET — Tags: , , — vbmagic @ 9:41 pm

Just a quick post as I’ve decided to write a simplified standalone version of a game to get some game mechanics designed. As a way of storing information that would have been stored I though I would use XML for the “Static” information which brings me to the great XML literals addition to vb.NET. Makes the code so much more readable and it also allows auto complete and error checking while you type it.

Below is a snippet of code to give an example of how the data is generated.


        Dim classes As XElement =
            <classes>
                <class description="A Character trained in weapons and armour">Fighter</class>
                <class description="A Character trained in Ranged and close quarters combat">Rogue</class>
                <class description="A Character trained in the arcane arts of combat">Mage</class>
                <class description="A Character trained in the arcane arts of healing and defence">Priest</class>
            </classes>

        Dim mobs As XElement =
            <mobs>
                <mob strength="1d4+2" constitution="1d6" dexterity="1d4" intelligence="3"
                    weapon="Rats Teeth">Giant Rat</mob>
            </mobs>

        Dim items As XElement =
            <items>
                <item type="weapon" value="1d4" action="stabs">Dagger</item>
                <item type="weapon" value="1d4+1" action="strikes">Staff</item>
                <item type="weapon" value="1d6" action="Shoots">Bow</item>
                <item type="weapon" value="1d6" action="swings">Short Sword</item>
                <item type="defence" value="1" action="">Robes</item>
                <item type="defence" value="2" action="">Leather Armour</item>
                <item type="defence" value="1d4-2" action="">Shield</item>
                <item type="potion" value="1d4" action="quaffs" stat="mana">Mana Potion</item>
                <item type="potion" value="1d4" action="quaffs" stat="hp">Health Potion</item>

                <item type="weapon" value="1d4-1" action="bites">Rats Teeth</item>
            </items>

        Dim spells As XElement =
            <spells>
                <spell></spell>
            </spells>

        Dim maps As XElement =
            <maps>
                <map name="test" rows="10" cols="10">
                    <row number="0">##########</row>
                    <row number="1">#*#f...g>#</row>
                    <row number="2">#.#.######</row>
                    <row number="3">#a#.#....#</row>
                    <row number="4">#+#.#.##.#</row>
                    <row number="5">#.#..e##.#</row>
                    <row number="6">#.######.#</row>
                    <row number="7">#.#....+d#</row>
                    <row number="8">#b+c...###</row>
                    <row number="9">##########</row>
                    <event type="message" description="As you were walking along the field, the ground shook and split appart and you landed in what looks to be an old mine tunnel">*</event>
                    <event type="message" description="You notice bones on the floor">a</event>
                    <event type="message" description="You hear scratching noises through the door">b</event>
                    <event type="fight" description="Giant rats run at you as you enter the room" mob="Giant Rat" mobcount="1d4">c</event>
                    <event type="item" description="You find a health potion on the floor" item="Health Potion" itemcount="1">d</event>
                    <event type="fight" description="Giant Rats swarm round the corrner of the tunnel" mob="Giant Rat" mobcount="1d4+4">e</event>
                    <event type="message" description="You feel a breeze from the end of the corridor">f</event>
                    <event type="message" description="You see a ladder ahead leading out of the tunnel">g</event>
                </map>
            </maps>
« Newer Posts

Blog at WordPress.com.