Print Page | Close Window

Advanced NPC’s - Revised

Printed From: Mirage Source
Category: Tutorials
Forum Name: Submitted Tutorials
Forum Discription: Tutorial submissions for MSE are posted here, waiting for approval
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=100
Printed Date: 20 December 2006 at 6:03pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: Advanced NPC’s - Revised
Posted By: KiteMinase
Subject: Advanced NPC’s - Revised
Date Posted: 11 February 2006 at 1:34am
Advanced Npc's - Revised:

Tested on Both 3.03 and MSE Build1! Works on both :P

Coding Difficulty - 2/5
Understanding Difficulty - 4/5 (only if you don't know where stuff goes)

Part1:

Ok, re-did what i had done from scratch. I had done a large part of the code but i found a very big bug in the very foundation of the whole thing. This meant i either started again or un-did a lot of my work.

I started again and decided to get rid of some of the original Npc stuff (such as drop item, etc.) you'll see what later.

Anyway, im gonna make it less long winded and just give you the code, coz i can't be bothered being vague at the moment. Any n00b should get this, copy and paste, pretty much.

Features:

Npc Inventory and eqiupable items.
Last time the inventory was equiped, now there are four equiped items and 5 dropped items. The npc does not drop his equipment (unless you make the first 4 inv slots the same as his equipment)

Drops their inventory when they die.
Drop only their inventory, not equiped items.

Choosing how often the NPC walks (from almost always to never (good for shop keepers and hard bosses))
No change

Allows them to attack certain players no matter what (unless there dead oO) (ie, pkers and those of a different class)
No change

And, in part 3 i will post how to make npc's use Spells! this is probably the biggest code i've ever embarked on, so dont hate me if i dont post it for a while =3
No Change

And of course... the spell frequency!
No Change

Code:

Client and Server:

Code:

Public Const MAX_NPC_INV = 5
Public Const MAX_NPC_SPELLS = 5


Code:

Type NpcInvRec
    Num As Byte
    Value As Long
    dur As Integer
End Type

Type NpcSpellRec
    Num As Byte
    Freq As Long
End Type

Type NpcRec
   
    'original
    name As String * NAME_LENGTH
    Sprite As Integer
    SpawnSecs As Long
    Range As Byte
    STR  As Byte
    DEF As Byte
    SPEED As Byte
    MAGI As Byte
    Behavior As Byte
   
    'kite's advanced
    WeaponNum As Byte
    ShieldNum As Byte
    HelmetNum As Byte
    ArmorNum As Byte
    WalkFreq As Long
    PKPref As Boolean
    ClassPref As Long
    Inv(1 To MAX_NPC_INV) As NpcInvRec
    Spell(1 To MAX_NPC_SPELLS) As NpcSpellRec
End Type


Code:

Public NpcInv(1 To MAX_NPC_INV) As NpcInvRec
Public NpcSpell(1 To MAX_NPC_SPELLS) As NpcSpellRec


Client:

Items:

Form: frmNpcEditor.

Delete:

All the drop item pieces.
npcSay piece.

add:

(name/type : caption/text/min|max)

scrlWeapon : 0|255
lblWeapon: 0
scrlShield : 0|255
lblWeapon : 0
scrlHelmet : 0|255
lblHelmet : 0
scrlArmor : 0|255
lblArmor : 0

have as many inventory slots as you have:

scrlInv(slotnum)Num : 0|255
lblInv(slotnum)Num : 0
scrlInv(slotnum)Val : 0|255
lblInv(slotnum)Val : 0

scrlWalkFreq : 0|4
lblWalkFreq : 0

optPkPref : False
optClassPref : False
scrlClassPref : 0|(you max classes)
lblClassPref : 0

Have as many spell slots you have:

scrlSpell(slotnum)Num : 0|255
lblSpell(slotNum)Num : 0
scrllSpell(slotnum)Freq : 0|255
lblSpell(slotnum_Freq : 0

Anyway, it should end up something like this:



DoubleClick anywhere on the form or just click the script button. This is the entire forms code:

Code:

Option Explicit

Private Sub optClassPref_Click()
If optClassPref.Value = False Then
    scrlClassPref.Enabled = False
Else
    scrlClassPref.Enabled = True
End If
End Sub

Private Sub scrlClassPref_Change()
If optClassPref.Value = 0 Then
    scrlClassPref.Enabled = False
Else
    scrlClassPref.Enabled = True
End If
lblClassPref.Caption = scrlClassPref.Value
End Sub

Private Sub scrlSpell1Num_Change()
lblSpell1Num.Caption = scrlSpell1Num.Value
End Sub

Private Sub scrlSpell2Num_Change()
lblSpell2Num.Caption = scrlSpell2Num.Value
End Sub

Private Sub scrlSpell3Num_Change()
lblSpell3Num.Caption = scrlSpell3Num.Value
End Sub

Private Sub scrlSpell4Num_Change()
lblSpell4Num.Caption = scrlSpell4Num.Value
End Sub

Private Sub scrlSpell5Num_Change()
lblSpell5Num.Caption = scrlSpell5Num.Value
End Sub

Private Sub scrlSpell1Freq_Change()
lblSpell1Freq.Caption = scrlSpell1Freq.Value
End Sub

Private Sub scrlSpell2Freq_Change()
lblSpell2Freq.Caption = scrlSpell2Freq.Value
End Sub

Private Sub scrlSpell3Freq_Change()
lblSpell3Freq.Caption = scrlSpell3Freq.Value
End Sub

Private Sub scrlSpell4Freq_Change()
lblSpell4Freq.Caption = scrlSpell4Freq.Value
End Sub

Private Sub scrlSpell5Freq_Change()
lblSpell5Freq.Caption = scrlSpell5Freq.Value
End Sub

Private Sub scrlSprite_Change()
    lblSprite.Caption = STR(scrlSprite.Value)
End Sub

Private Sub scrlRange_Change()
    lblRange.Caption = STR(scrlRange.Value)
End Sub

Private Sub scrlSTR_Change()
    lblSTR.Caption = STR(scrlSTR.Value)
    lblStartHP.Caption = STR(scrlSTR.Value * scrlDEF.Value)
    lblExpGiven.Caption = STR(scrlSTR.Value * scrlDEF.Value * 2)
End Sub

Private Sub scrlDEF_Change()
    lblDEF.Caption = STR(scrlDEF.Value)
    lblStartHP.Caption = STR(scrlSTR.Value * scrlDEF.Value)
    lblExpGiven.Caption = STR(scrlSTR.Value * scrlDEF.Value * 2)
End Sub

Private Sub scrlSPEED_Change()
    lblSPEED.Caption = STR(scrlSPEED.Value)
    lblExpGiven.Caption = STR(scrlSTR.Value * scrlDEF.Value * 2)
End Sub

Private Sub scrlMAGI_Change()
    lblMAGI.Caption = STR(scrlMAGI.Value)
End Sub

Private Sub cmdOk_Click()
    Call NpcEditorOk
End Sub

Private Sub scrlWalkFreq_Change()
Select Case scrlWalkFreq.Value
    Case 0
    lblWalkFreq.Caption = "none"
    Case 1
    lblWalkFreq.Caption = "sometimes..."
    Case 2
    lblWalkFreq.Caption = "Average"
    Case 3
    lblWalkFreq.Caption = "Most Of The Time"
    Case 4
    lblWalkFreq.Caption = "ALWAYS!!!"
End Select
End Sub

Private Sub tmrSprite_Timer()
    Call NpcEditorBltSprite
End Sub

Private Sub cmdCancel_Click()
    Call NpcEditorCancel
End Sub

Private Sub scrlWeapon_Change()
lblWeapon.Caption = scrlWeapon.Value
End Sub

Private Sub scrlArmor_Change()
lblArmor.Caption = scrlArmor.Value
End Sub

Private Sub scrlShield_Change()
lblShield.Caption = scrlShield.Value
End Sub

Private Sub scrlHelmet_Change()
lblHelmet.Caption = scrlHelmet.Value
End Sub

Private Sub scrlInv1Num_Change()
lblInv1Num.Caption = scrlInv1Num.Value
End Sub

Private Sub scrlInv2Num_Change()
lblInv2Num.Caption = scrlInv2Num.Value
End Sub

Private Sub scrlInv3Num_Change()
lblInv3Num.Caption = scrlInv3Num.Value
End Sub

Private Sub scrlInv4Num_Change()
lblInv4Num.Caption = scrlInv4Num.Value
End Sub

Private Sub scrlInv5Num_Change()
lblInv5Num.Caption = scrlInv5Num.Value
End Sub

Private Sub scrlInv1Val_Change()
lblInv1Val.Caption = scrlInv1Val.Value
End Sub

Private Sub scrlInv2Val_Change()
lblInv2Val.Caption = scrlInv2Val.Value
End Sub

Private Sub scrlInv3Val_Change()
lblInv3Val.Caption = scrlInv3Val.Value
End Sub

Private Sub scrlInv4Val_Change()
lblInv4Val.Caption = scrlInv4Val.Value
End Sub

Private Sub scrlInv5Val_Change()
lblInv5Val.Caption = scrlInv5Val.Value
End Sub


Ok, now find:

NpceditorInit

and change that and npceditorok to:

Code:

Public Sub NpcEditorInit()
On Error Resume Next
   
    frmNpcEditor.picSprites.Picture = LoadPicture(App.Path & "\sprites.bmp")
   
    frmNpcEditor.txtName.Text = Trim(Npc(EditorIndex).name)
    frmNpcEditor.scrlSprite.Value = Npc(EditorIndex).Sprite
    frmNpcEditor.txtSpawnSecs.Text = STR(Npc(EditorIndex).SpawnSecs)
    frmNpcEditor.cmbBehavior.ListIndex = Npc(EditorIndex).Behavior
    frmNpcEditor.scrlRange.Value = Npc(EditorIndex).Range
    frmNpcEditor.scrlSTR.Value = Npc(EditorIndex).STR
    frmNpcEditor.scrlDEF.Value = Npc(EditorIndex).DEF
    frmNpcEditor.scrlSPEED.Value = Npc(EditorIndex).SPEED
    frmNpcEditor.scrlMAGI.Value = Npc(EditorIndex).MAGI
    frmNpcEditor.scrlWeapon.Value = Npc(EditorIndex).WeaponNum
    frmNpcEditor.scrlShield.Value = Npc(EditorIndex).ShieldNum
    frmNpcEditor.scrlHelmet.Value = Npc(EditorIndex).HelmetNum
    frmNpcEditor.scrlArmor.Value = Npc(EditorIndex).ArmorNum
    frmNpcEditor.scrlWalkFreq.Value = Npc(EditorIndex).WalkFreq
    frmNpcEditor.chkPkPref.Value = Npc(EditorIndex).PKPref
    If Npc(EditorIndex).ClassPref <> -1 Then
        frmNpcEditor.scrlClassPref.Value = Npc(EditorIndex).ClassPref
        frmNpcEditor.optClassPref.Value = True
        frmNpcEditor.scrlClassPref.Enabled = True
    Else
        frmNpcEditor.scrlClassPref.Value = 0
        frmNpcEditor.optClassPref.Value = False
        frmNpcEditor.scrlClassPref.Enabled = False
    End If
    frmNpcEditor.scrlInv1Num.Value = Npc(EditorIndex).Inv(1).Num
    frmNpcEditor.scrlInv2Num.Value = Npc(EditorIndex).Inv(2).Num
    frmNpcEditor.scrlInv3Num.Value = Npc(EditorIndex).Inv(3).Num
    frmNpcEditor.scrlInv4Num.Value = Npc(EditorIndex).Inv(4).Num
    frmNpcEditor.scrlInv5Num.Value = Npc(EditorIndex).Inv(5).Num
    frmNpcEditor.scrlInv1Val.Value = Npc(EditorIndex).Inv(1).Value
    frmNpcEditor.scrlInv2Val.Value = Npc(EditorIndex).Inv(2).Value
    frmNpcEditor.scrlInv3Val.Value = Npc(EditorIndex).Inv(3).Value
    frmNpcEditor.scrlInv4Val.Value = Npc(EditorIndex).Inv(4).Value
    frmNpcEditor.scrlInv5Val.Value = Npc(EditorIndex).Inv(5).Value
    frmNpcEditor.scrlSpell1Num.Value = Npc(EditorIndex).Spell(1).Num
    frmNpcEditor.scrlSpell2Num.Value = Npc(EditorIndex).Spell(2).Num
    frmNpcEditor.scrlSpell3Num.Value = Npc(EditorIndex).Spell(3).Num
    frmNpcEditor.scrlSpell4Num.Value = Npc(EditorIndex).Spell(4).Num
    frmNpcEditor.scrlSpell5Num.Value = Npc(EditorIndex).Spell(5).Num
    frmNpcEditor.scrlSpell1Freq.Value = Npc(EditorIndex).Spell(1).Freq
    frmNpcEditor.scrlSpell2Freq.Value = Npc(EditorIndex).Spell(2).Freq
    frmNpcEditor.scrlSpell3Freq.Value = Npc(EditorIndex).Spell(3).Freq
    frmNpcEditor.scrlSpell4Freq.Value = Npc(EditorIndex).Spell(4).Freq
    frmNpcEditor.scrlSpell5Freq.Value = Npc(EditorIndex).Spell(5).Freq
   
    frmNpcEditor.Show vbModal
End Sub

Public Sub NpcEditorOk()
    Npc(EditorIndex).name = frmNpcEditor.txtName.Text
    Npc(EditorIndex).Sprite = frmNpcEditor.scrlSprite.Value
    Npc(EditorIndex).SpawnSecs = Val(frmNpcEditor.txtSpawnSecs.Text)
    Npc(EditorIndex).Behavior = frmNpcEditor.cmbBehavior.ListIndex
    Npc(EditorIndex).Range = frmNpcEditor.scrlRange.Value
    Npc(EditorIndex).STR = frmNpcEditor.scrlSTR.Value
    Npc(EditorIndex).DEF = frmNpcEditor.scrlDEF.Value
    Npc(EditorIndex).SPEED = frmNpcEditor.scrlSPEED.Value
    Npc(EditorIndex).MAGI = frmNpcEditor.scrlMAGI.Value
    Npc(EditorIndex).WeaponNum = frmNpcEditor.scrlWeapon.Value
    Npc(EditorIndex).ShieldNum = frmNpcEditor.scrlShield.Value
    Npc(EditorIndex).HelmetNum = frmNpcEditor.scrlHelmet.Value
    Npc(EditorIndex).ArmorNum = frmNpcEditor.scrlArmor.Value
    Npc(EditorIndex).WalkFreq = frmNpcEditor.scrlWalkFreq.Value
    Npc(EditorIndex).PKPref = frmNpcEditor.chkPkPref.Value
    If frmNpcEditor.optClassPref.Value = True Then
        Npc(EditorIndex).ClassPref = frmNpcEditor.optClassPref.Value
    Else
        Npc(EditorIndex).ClassPref = -1
    End If
    Npc(EditorIndex).Inv(1).Num = frmNpcEditor.scrlInv1Num.Value
    Npc(EditorIndex).Inv(2).Num = frmNpcEditor.scrlInv2Num.Value
    Npc(EditorIndex).Inv(3).Num = frmNpcEditor.scrlInv3Num.Value
    Npc(EditorIndex).Inv(4).Num = frmNpcEditor.scrlInv4Num.Value
    Npc(EditorIndex).Inv(5).Num = frmNpcEditor.scrlInv5Num.Value
    Npc(EditorIndex).Inv(1).Value = frmNpcEditor.scrlInv1Val.Value
    Npc(EditorIndex).Inv(2).Value = frmNpcEditor.scrlInv2Val.Value
    Npc(EditorIndex).Inv(3).Value = frmNpcEditor.scrlInv3Val.Value
    Npc(EditorIndex).Inv(4).Value = frmNpcEditor.scrlInv4Val.Value
    Npc(EditorIndex).Inv(5).Value = frmNpcEditor.scrlInv5Val.Value
    Npc(EditorIndex).Spell(1).Num = frmNpcEditor.scrlSpell1Num.Value
    Npc(EditorIndex).Spell(2).Num = frmNpcEditor.scrlSpell2Num.Value
    Npc(EditorIndex).Spell(3).Num = frmNpcEditor.scrlSpell3Num.Value
    Npc(EditorIndex).Spell(4).Num = frmNpcEditor.scrlSpell4Num.Value
    Npc(EditorIndex).Spell(5).Num = frmNpcEditor.scrlSpell5Num.Value
    Npc(EditorIndex).Spell(1).Freq = frmNpcEditor.scrlSpell1Freq.Value
    Npc(EditorIndex).Spell(2).Freq = frmNpcEditor.scrlSpell2Freq.Value
    Npc(EditorIndex).Spell(3).Freq = frmNpcEditor.scrlSpell3Freq.Value
    Npc(EditorIndex).Spell(4).Freq = frmNpcEditor.scrlSpell4Freq.Value
    Npc(EditorIndex).Spell(5).Freq = frmNpcEditor.scrlSpell5Freq.Value
   
    Call SendSaveNpc(EditorIndex)
    InNpcEditor = False
    Unload frmNpcEditor
End Sub



Ok, now to fix some of the packets we fu... screwed smile

So, we'll have them in this order (because its neat):

Number
Name
Sprite
Spawnsecs
behavior
range
str
def
speed
magi
weaponnum
shieldnum
helmetnum
armornum
walkfreq
pkpref
classpref
inv
spells

smile

so find:

SAVENPC

and change the whole sub to look like this:

Code:

Sub SendSaveNpc(ByVal NpcNum As Long)
Dim Packet As String
   
    Packet = "SAVENPC" & SEP_CHAR & NpcNum & SEP_CHAR & Trim(Npc(NpcNum).name) & SEP_CHAR & Npc(NpcNum).Sprite & SEP_CHAR & Npc(NpcNum).SpawnSecs & SEP_CHAR & Npc(NpcNum).Behavior & SEP_CHAR & Npc(NpcNum).Range & SEP_CHAR & Npc(NpcNum).STR & SEP_CHAR & Npc(NpcNum).DEF & SEP_CHAR & Npc(NpcNum).SPEED & SEP_CHAR & Npc(NpcNum).MAGI & SEP_CHAR & Npc(NpcNum).WeaponNum & SEP_CHAR & Npc(NpcNum).ShieldNum & SEP_CHAR & Npc(NpcNum).HelmetNum & SEP_CHAR & Npc(NpcNum).ArmorNum & SEP_CHAR & Npc(NpcNum).WalkFreq & SEP_CHAR & Npc(NpcNum).PKPref & SEP_CHAR & Npc(NpcNum).ClassPref & SEP_CHAR & Npc(NpcNum).Inv(1).Num & SEP_CHAR & Npc(NpcNum).Inv(2).Num & SEP_CHAR & Npc(NpcNum).Inv(3).Num & SEP_CHAR & Npc(NpcNum).Inv(4).Num
    Packet = Packet & SEP_CHAR & Npc(NpcNum).Inv(5).Num & SEP_CHAR & Npc(NpcNum).Inv(1).Val & SEP_CHAR & Npc(NpcNum).Inv(2).Val & SEP_CHAR & Npc(NpcNum).Inv(3).Val & SEP_CHAR & Npc(NpcNum).Inv(4).Val & SEP_CHAR & Npc(NpcNum).Inv(5).Val & SEP_CHAR & Npc(NpcNum).Spell(1).Num & SEP_CHAR & Npc(NpcNum).Spell(2).Num & SEP_CHAR & Npc(NpcNum).Spell(3).Num
    Packet = Packet & SEP_CHAR & Npc(NpcNum).Spell(4).Num & SEP_CHAR & Npc(NpcNum).Spell(5).Num & SEP_CHAR & Npc(NpcNum).Spell(1).Freq & SEP_CHAR & Npc(NpcNum).Spell(2).Freq & SEP_CHAR & Npc(NpcNum).Spell(3).Freq & SEP_CHAR & Npc(NpcNum).Spell(4).Freq & SEP_CHAR & Npc(NpcNum).Spell(5).Freq & SEP_CHAR & END_CHAR
    Call SendData(Packet)
End Sub


Ok, now in the server find:

' update the npc

and change that bit to:

Code:

' Update the npc
        Npc(n).Name = Parse(2)
        Npc(n).Sprite = Val(Parse(3))
        Npc(n).SpawnSecs = Val(Parse(4))
        Npc(n).Behavior = Val(Parse(5))
        Npc(n).Range = Val(Parse(6))
        Npc(n).STR = Val(Parse(7))
        Npc(n).DEF = Val(Parse(8))
        Npc(n).SPEED = Val(Parse(9))
        Npc(n).MAGI = Val(Parse(10))
        Npc(n).WeaponNum = Val(Parse(11))
        Npc(n).ShieldNum = Val(Parse(12))
        Npc(n).HelmetNum = Val(Parse(13))
        Npc(n).ArmorNum = Val(Parse(14))
        Npc(n).WalkFreq = Val(Parse(15))
        Npc(n).PKPref = Val(Parse(16))
        Npc(n).ClassPref = Val(Parse(17))
        Npc(n).Inv(1).Num = Val(Parse(18))
        Npc(n).Inv(2).Num = Val(Parse(19))
        Npc(n).Inv(3).Num = Val(Parse(20))
        Npc(n).Inv(4).Num = Val(Parse(21))
        Npc(n).Inv(5).Num = Val(Parse(22))
        Npc(n).Inv(1).Val = Val(Parse(23))
        Npc(n).Inv(2).Val = Val(Parse(24))
        Npc(n).Inv(3).Val = Val(Parse(25))
        Npc(n).Inv(4).Val = Val(Parse(26))
        Npc(n).Inv(5).Val = Val(Parse(27))
        Npc(n).Spell(1).Num = Val(Parse(28))
        Npc(n).Spell(2).Num = Val(Parse(29))
        Npc(n).Spell(3).Num = Val(Parse(30))
        Npc(n).Spell(4).Num = Val(Parse(31))
        Npc(n).Spell(5).Num = Val(Parse(32))
        Npc(n).Spell(1).Freq = Val(Parse(33))
        Npc(n).Spell(2).Freq = Val(Parse(34))
        Npc(n).Spell(3).Freq = Val(Parse(35))
        Npc(n).Spell(4).Freq = Val(Parse(36))
        Npc(n).Spell(5).Freq = Val(Parse(37))


Now find:

Update npc packet

and change it to:

Code:

' Update the item
        Npc(n).name = Parse(2)
        Npc(n).Sprite = Val(Parse(3))
        Npc(n).SpawnSecs = 0
        Npc(n).Behavior = 0
        Npc(n).Range = 0
        Npc(n).STR = 0
        Npc(n).DEF = 0
        Npc(n).SPEED = 0
        Npc(n).MAGI = 0
        Npc(n).WeaponNum = 0
        Npc(n).ShieldNum = 0
        Npc(n).HelmetNum = 0
        Npc(n).ArmorNum = 0
        Npc(n).walkpref = 0
        Npc(n).PKPref = False
        Npc(n).ClassPref = 0
        Npc(n).Inv(1).Num = 0
        Npc(n).Inv(2).Num = 0
        Npc(n).Inv(3).Num = 0
        Npc(n).Inv(4).Num = 0
        Npc(n).Inv(5).Num = 0
        Npc(n).Inv(1).Val = 0
        Npc(n).Inv(2).Val = 0
        Npc(n).Inv(3).Val = 0
        Npc(n).Inv(4).Val = 0
        Npc(n).Inv(5).Val = 0
        Npc(n).Spell(1).Num = 0
        Npc(n).Spell(2).Num = 0
        Npc(n).Spell(3).Num = 0
        Npc(n).Spell(4).Num = 0
        Npc(n).Spell(5).Num = 0
        Npc(n).Spell(1).Freq = 0
        Npc(n).Spell(2).Freq = 0
        Npc(n).Spell(3).Freq = 0
        Npc(n).Spell(4).Freq = 0
        Npc(n).Spell(5).Freq = 0


and find:

edit npc packet

change to:

Code:

' Update the npc
        Npc(n).Name = Parse(2)
        Npc(n).Sprite = Val(Parse(3))
        Npc(n).SpawnSecs = Val(Parse(4))
        Npc(n).Behavior = Val(Parse(5))
        Npc(n).Range = Val(Parse(6))
        Npc(n).STR = Val(Parse(7))
        Npc(n).DEF = Val(Parse(8))
        Npc(n).SPEED = Val(Parse(9))
        Npc(n).MAGI = Val(Parse(10))
        Npc(n).WeaponNum = Val(Parse(11))
        Npc(n).ShieldNum = Val(Parse(12))
        Npc(n).HelmetNum = Val(Parse(13))
        Npc(n).ArmorNum = Val(Parse(14))
        Npc(n).WalkFreq = Val(Parse(15))
        Npc(n).PKPref = Val(Parse(16))
        Npc(n).ClassPref = Val(Parse(17))
        Npc(n).Inv(1).Num = Val(Parse(18))
        Npc(n).Inv(2).Num = Val(Parse(19))
        Npc(n).Inv(3).Num = Val(Parse(20))
        Npc(n).Inv(4).Num = Val(Parse(21))
        Npc(n).Inv(5).Num = Val(Parse(22))
        Npc(n).Inv(1).Val = Val(Parse(23))
        Npc(n).Inv(2).Val = Val(Parse(24))
        Npc(n).Inv(3).Val = Val(Parse(25))
        Npc(n).Inv(4).Val = Val(Parse(26))
        Npc(n).Inv(5).Val = Val(Parse(27))
        Npc(n).Spell(1).Num = Val(Parse(28))
        Npc(n).Spell(2).Num = Val(Parse(29))
        Npc(n).Spell(3).Num = Val(Parse(30))
        Npc(n).Spell(4).Num = Val(Parse(31))
        Npc(n).Spell(5).Num = Val(Parse(32))
        Npc(n).Spell(1).Freq = Val(Parse(33))
        Npc(n).Spell(2).Freq = Val(Parse(34))
        Npc(n).Spell(3).Freq = Val(Parse(35))
        Npc(n).Spell(4).Freq = Val(Parse(36))
        Npc(n).Spell(5).Freq = Val(Parse(37))


And change these subs to these:

Code:

Sub SendUpdateNpcToAll(ByVal NpcNum As Long)
Dim Packet As String

    Packet = "UPDATENPC" & SEP_CHAR & NpcNum & SEP_CHAR & Trim(Npc(NpcNum).Name) & SEP_CHAR & Npc(NpcNum).Sprite & SEP_CHAR & END_CHAR
    Call SendDataToAll(Packet)
End Sub

Sub SendUpdateNpcTo(ByVal Index As Long, ByVal NpcNum As Long)
Dim Packet As String

    Packet = "UPDATENPC" & SEP_CHAR & NpcNum & SEP_CHAR & Trim(Npc(NpcNum).Name) & SEP_CHAR & Npc(NpcNum).Sprite & SEP_CHAR & END_CHAR
    Call SendDataTo(Index, Packet)
End Sub

Sub SendEditNpcTo(ByVal Index As Long, ByVal NpcNum As Long)
Dim Packet As String

    Packet = "EDITNPC" & SEP_CHAR & NpcNum & SEP_CHAR & Trim(Npc(NpcNum).Name) & SEP_CHAR & Npc(NpcNum).Sprite & SEP_CHAR & Npc(NpcNum).SpawnSecs & SEP_CHAR & Npc(NpcNum).Behavior & SEP_CHAR & Npc(NpcNum).Range & SEP_CHAR & Npc(NpcNum).STR & SEP_CHAR & Npc(NpcNum).DEF & SEP_CHAR & Npc(NpcNum).SPEED & SEP_CHAR & Npc(NpcNum).MAGI & SEP_CHAR & Npc(NpcNum).WeaponNum & SEP_CHAR & Npc(NpcNum).ShieldNum & SEP_CHAR & Npc(NpcNum).HelmetNum & SEP_CHAR & Npc(NpcNum).ArmorNum & SEP_CHAR & Npc(NpcNum).WalkFreq & SEP_CHAR & Npc(NpcNum).PKPref & SEP_CHAR & Npc(NpcNum).ClassPref
    Packet = Packet & SEP_CHAR & Npc(NpcNum).Inv(1).Num & SEP_CHAR & Npc(NpcNum).Inv(2).Num & SEP_CHAR & Npc(NpcNum).Inv(3).Num & SEP_CHAR & Npc(NpcNum).Inv(4).Num & SEP_CHAR & Npc(NpcNum).Inv(5).Num & SEP_CHAR & Npc(NpcNum).Inv(1).Value & SEP_CHAR & Npc(NpcNum).Inv(2).Value & SEP_CHAR & Npc(NpcNum).Inv(3).Value & SEP_CHAR & Npc(NpcNum).Inv(4).Value & SEP_CHAR & Npc(NpcNum).Inv(5).Value & SEP_CHAR & Npc(NpcNum).Spell(1).Num & SEP_CHAR & Npc(NpcNum).Spell(2).Num & SEP_CHAR & Npc(NpcNum).Spell(3).Num & SEP_CHAR & Npc(NpcNum).Spell(4).Num & SEP_CHAR & Npc(NpcNum).Spell(5).Num
    Packet = Packet & SEP_CHAR & Npc(NpcNum).Spell(1).Freq & SEP_CHAR & Npc(NpcNum).Spell(2).Freq & SEP_CHAR & Npc(NpcNum).Spell(3).Freq & SEP_CHAR & Npc(NpcNum).Spell(4).Freq & SEP_CHAR & Npc(NpcNum).Spell(5).Freq
    Call SendDataTo(Index, Packet)
End Sub


Now save and load npcs:

Code:

Sub SaveNpc(ByVal NpcNum As Long)
Dim FileName As String

    FileName = App.Path & "\npcs.ini"
   
    Call PutVar(FileName, "NPC" & NpcNum, "Name", Trim(Npc(NpcNum).Name))
    Call PutVar(FileName, "NPC" & NpcNum, "AttackSay", Trim(Npc(NpcNum).AttackSay))
    Call PutVar(FileName, "NPC" & NpcNum, "Sprite", Trim(Npc(NpcNum).Sprite))
    Call PutVar(FileName, "NPC" & NpcNum, "SpawnSecs", Trim(Npc(NpcNum).SpawnSecs))
    Call PutVar(FileName, "NPC" & NpcNum, "Behavior", Trim(Npc(NpcNum).Behavior))
    Call PutVar(FileName, "NPC" & NpcNum, "Range", Trim(Npc(NpcNum).Range))
    Call PutVar(FileName, "NPC" & NpcNum, "DropChance", Trim(Npc(NpcNum).DropChance))
    Call PutVar(FileName, "NPC" & NpcNum, "DropItem", Trim(Npc(NpcNum).DropItem))
    Call PutVar(FileName, "NPC" & NpcNum, "DropItemValue", Trim(Npc(NpcNum).DropItemValue))
    Call PutVar(FileName, "NPC" & NpcNum, "STR", Trim(Npc(NpcNum).STR))
    Call PutVar(FileName, "NPC" & NpcNum, "DEF", Trim(Npc(NpcNum).DEF))
    Call PutVar(FileName, "NPC" & NpcNum, "SPEED", Trim(Npc(NpcNum).SPEED))
    Call PutVar(FileName, "NPC" & NpcNum, "MAGI", Trim(Npc(NpcNum).MAGI))
    Call PutVar(FileName, "NPC" & NpcNum, "weaponnum", Trim(Npc(NpcNum).WeaponNum))
    Call PutVar(FileName, "NPC" & NpcNum, "shieldnum", Trim(Npc(NpcNum).ShieldNum))
    Call PutVar(FileName, "NPC" & NpcNum, "helmetnum", Trim(Npc(NpcNum).HelmetNum))
    Call PutVar(FileName, "NPC" & NpcNum, "armornum", Trim(Npc(NpcNum).ArmorNum))
    Call PutVar(FileName, "NPC" & NpcNum, "walkfreq", Trim(Npc(NpcNum).WalkFreq))
    Call PutVar(FileName, "NPC" & NpcNum, "pkpref", Trim(Npc(NpcNum).PKPref))
    Call PutVar(FileName, "NPC" & NpcNum, "classpref", Trim(Npc(NpcNum).ClassPref))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv1Num", Trim(Npc(NpcNum).Inv(1).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv2Num", Trim(Npc(NpcNum).Inv(2).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv3Num", Trim(Npc(NpcNum).Inv(3).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv4Num", Trim(Npc(NpcNum).Inv(4).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv5Num", Trim(Npc(NpcNum).Inv(5).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv1val", Trim(Npc(NpcNum).Inv(1).Val))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv2val", Trim(Npc(NpcNum).Inv(2).Val))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv3val", Trim(Npc(NpcNum).Inv(3).Val))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv4val", Trim(Npc(NpcNum).Inv(4).Val))
    Call PutVar(FileName, "NPC" & NpcNum, "Inv5val", Trim(Npc(NpcNum).Inv(5).Val))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell1num", Trim(Npc(NpcNum).Spell(1).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell2num", Trim(Npc(NpcNum).Spell(2).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell3num", Trim(Npc(NpcNum).Spell(3).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell4num", Trim(Npc(NpcNum).Spell(4).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell5num", Trim(Npc(NpcNum).Spell(5).Num))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell1freq", Trim(Npc(NpcNum).Spell(1).Freq))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell2freq", Trim(Npc(NpcNum).Spell(2).Freq))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell3freq", Trim(Npc(NpcNum).Spell(3).Freq))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell4freq", Trim(Npc(NpcNum).Spell(4).Freq))
    Call PutVar(FileName, "NPC" & NpcNum, "Spell5freq", Trim(Npc(NpcNum).Spell(5).Freq))
End Sub

Sub LoadNpcs()
On Error Resume Next

Dim FileName As String
Dim i As Long

    Call CheckNpcs
   
    FileName = App.Path & "\npcs.ini"
   
    For i = 1 To MAX_NPCS
        Npc(i).Name = GetVar(FileName, "NPC" & i, "Name")
        Npc(i).AttackSay = GetVar(FileName, "NPC" & i, "AttackSay")
        Npc(i).Sprite = GetVar(FileName, "NPC" & i, "Sprite")
        Npc(i).SpawnSecs = GetVar(FileName, "NPC" & i, "SpawnSecs")
        Npc(i).Behavior = GetVar(FileName, "NPC" & i, "Behavior")
        Npc(i).Range = GetVar(FileName, "NPC" & i, "Range")
        Npc(i).DropChance = GetVar(FileName, "NPC" & i, "DropChance")
        Npc(i).DropItem = GetVar(FileName, "NPC" & i, "DropItem")
        Npc(i).DropItemValue = GetVar(FileName, "NPC" & i, "DropItemValue")
        Npc(i).STR = GetVar(FileName, "NPC" & i, "STR")
        Npc(i).DEF = GetVar(FileName, "NPC" & i, "DEF")
        Npc(i).SPEED = GetVar(FileName, "NPC" & i, "SPEED")
        Npc(i).MAGI = GetVar(FileName, "NPC" & i, "MAGI")
        Npc(i).WeaponNum = GetVar(FileName, "NPC" & i, "weaponnum")
        Npc(i).ShieldNum = GetVar(FileName, "NPC" & i, "shieldnum")
        Npc(i).HelmetNum = GetVar(FileName, "NPC" & i, "helmetnum")
        Npc(i).ArmorNum = GetVar(FileName, "NPC" & i, "armornum")
        Npc(i).WalkFreq = GetVar(FileName, "NPC" & i, "walkfreq")
        Npc(i).PKPref = GetVar(FileName, "NPC" & i, "pkpref")
        Npc(i).ClassPref = GetVar(FileName, "NPC" & i, "classpref")
        Npc(i).Inv(1).Num = GetVar(FileName, "NPC" & i, "Inv1Num")
        Npc(i).Inv(2).Num = GetVar(FileName, "NPC" & i, "Inv2Num")
        Npc(i).Inv(3).Num = GetVar(FileName, "NPC" & i, "Inv3Num")
        Npc(i).Inv(4).Num = GetVar(FileName, "NPC" & i, "Inv4Num")
        Npc(i).Inv(5).Num = GetVar(FileName, "NPC" & i, "Inv5Num")
        Npc(i).Inv(1).Val = GetVar(FileName, "NPC" & i, "Inv1Val")
        Npc(i).Inv(2).Val = GetVar(FileName, "NPC" & i, "Inv2Val")
        Npc(i).Inv(3).Val = GetVar(FileName, "NPC" & i, "Inv3Val")
        Npc(i).Inv(4).Val = GetVar(FileName, "NPC" & i, "Inv4Val")
        Npc(i).Inv(5).Val = GetVar(FileName, "NPC" & i, "Inv5Val")
        Npc(i).Spell(1).Num = GetVar(FileName, "NPC" & i, "Spell1Num")
        Npc(i).Spell(2).Num = GetVar(FileName, "NPC" & i, "Spell2Num")
        Npc(i).Spell(3).Num = GetVar(FileName, "NPC" & i, "Spell3Num")
        Npc(i).Spell(4).Num = GetVar(FileName, "NPC" & i, "Spell4Num")
        Npc(i).Spell(5).Num = GetVar(FileName, "NPC" & i, "Spell5Num")
        Npc(i).Spell(1).Freq = GetVar(FileName, "NPC" & i, "Spell1Freq")
        Npc(i).Spell(2).Freq = GetVar(FileName, "NPC" & i, "Spell2Freq")
        Npc(i).Spell(3).Freq = GetVar(FileName, "NPC" & i, "Spell3Freq")
        Npc(i).Spell(4).Freq = GetVar(FileName, "NPC" & i, "Spell4Freq")
        Npc(i).Spell(5).Freq = GetVar(FileName, "NPC" & i, "Spell5Freq")
   
        DoEvents
    Next i
End Sub


Now go around, any of the old npc parts flying around, delete them and add in the new npc parts.
you should be able to do that yourself. i don't want to spoon feed you all the way!!!

Now, we move on to some of the actual parts that matter (the bits that do stuff :))

find:

' Drop the goods if they get it

and change it to:

Code:

Dim m As Long, n As Long, o As Long, p As Long, q As Long, r As Long, s As Long
Dim t As Long, w As Long, q As Long
         & ;nbs p;         & ;nbs p;         & ;nbs p; 
        ' Drop the goods if they get it
        m = Npc(NpcNum).Inv(1).Num
        n = Npc(NpcNum).Inv(2).Num
        o = Npc(NpcNum).Inv(3).Num
        p = Npc(NpcNum).Inv(4).Num
        q = Npc(NpcNum).Inv(5).Num
       
        If m > 0 Then
         & ;nbs p;  Call SpawnItem(Npc(NpcNum).Inv(1).Num, Npc(NpcNum).Inv(1).Value, MapNum, MapNpc(MapNum, MapNpcNum).x, MapNpc(MapNum, MapNpcNum).y)
        End If
       
        If n > 0 Then
         & ;nbs p;  Call SpawnItem(Npc(NpcNum).Inv(2).Num, Npc(NpcNum).Inv(2).Value, MapNum, MapNpc(MapNum, MapNpcNum).x, MapNpc(MapNum, MapNpcNum).y)
        End If
       
        If o > 0 Then
         & ;nbs p;  Call SpawnItem(Npc(NpcNum).Inv(3).Num, Npc(NpcNum).Inv(3).Value, MapNum, MapNpc(MapNum, MapNpcNum).x, MapNpc(MapNum, MapNpcNum).y)
        End If
       
        If p > 0 Then
         & ;nbs p;  Call SpawnItem(Npc(NpcNum).Inv(4).Num, Npc(NpcNum).Inv(4).Value, MapNum, MapNpc(MapNum, MapNpcNum).x, MapNpc(MapNum, MapNpcNum).y)
        End If
       
        If q > 0 Then
         & ;nbs p;  Call SpawnItem(Npc(NpcNum).Inv(5).Num, Npc(NpcNum).Inv(5).Value, MapNum, MapNpc(MapNum, MapNpcNum).x, MapNpc(MapNum, MapNpcNum).y)
        End If


Now, im sure your wondering why i took the long winded route for the inv and spells (instead of the quick way which you should know) it is simply because they confuze me. Probably 100% better coding, but if you really want to change it, do so!

There. He now drops his entire inventory!!!

(this is good if you want them to drop a few items and maybe a trphy (ie their head) ;))

Oh, and just a small side note, you know the npceditor? Maybe in npceditorinit make the max of those scrollbars we added to the appropriate value!

eg:

frmNpcEditor.scrlWeaponSlot.Max = MAX_ITEMS

:)

On part two we'll start the serverside! (yay! Thats good ;)) and we'll add in the Npc's PkPref, ClassPref and WalkFreq :)

WARNING:

I am not going to post the code i add in. I will simply post my GameAI sub! I forgot to write it down in the tut as i coded it rofl!



Replies:
Posted By: Sync
Date Posted: 26 February 2006 at 12:56pm
Cool, once this tutorial is finished, I can approve it.



Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info