Print Page | Close Window

NPC Spawn Tile

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=111
Printed Date: 20 December 2006 at 6:03pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: NPC Spawn Tile
Posted By: Sync
Subject: NPC Spawn Tile
Date Posted: 11 February 2006 at 2:06pm
Originally posted by FunkyNut

Spawn Npc Tile

Difficulty: 2 / 5 (Need a bit of info on spawning npc routine)

Ok, this is a Guide to adding your very own Npc Spawn Tile.   Now then What I’ll pretty just doing is simple, just making a new form so you can select the Npc to spawn, adding a new tile attribute and making the server check for spawn npc tiles and if there is a Spawn Npc Tile, spawn tjhe npc on that spot, and skip the rest of the routine that will try to spawn the npc somewhere else.

First let’s add the Tile Constant to both the client and server so find:
Public Const TILE_TYPE_KEYOPEN = 6

And below it at:
Public Const TILE_TYPE_NPCSPAWN = 7


Now we need to Blt the letter on top of the tiles so find:
                        If .Type = TILE_TYPE_KEYOPEN Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "O", QBColor(White))

And under it add:
                          If .Type = TILE_TYPE_NPCSPAWN Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "NS", QBColor(Yellow))


Now the explanation, each If Then statement is checking the Tile type and if its the correct type, will Blt a few letters to displaywhat tile it is.

This is the whole section:
                      With Map.Tile(x, y)
                          If .Type = TILE_TYPE_BLOCKED Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "B", QBColor(BrightRed))
                          If .Type = TILE_TYPE_WARP Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "W", QBColor(BrightBlue))
                          If .Type = TILE_TYPE_ITEM Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "I", QBColor(White))
                          If .Type = TILE_TYPE_NPCAVOID Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "N", QBColor(White))
                          If .Type = TILE_TYPE_KEY Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "K", QBColor(White))
                          If .Type = TILE_TYPE_KEYOPEN Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "O", QBColor(White))
                          If .Type = TILE_TYPE_NPCSPAWN Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "NS", QBColor(Yellow))
                     End With


Now the ‘.Type’ for each If Then selects the section of the With statement its checking, so for this it will check Map.Tile.Type.  The TILE_TYPE_BLOCKED etc are what were checking the .Type for, and if .Type is the same then we continue to blt the text on the tile.

The ‘TexthDC’ is kind of like the address or directions to the back buffer surface, and tells it what to write the text on, the next two sections tell the routine where to Blt the text, the next section is what is going to be bltted and the last section if the colour.


Ok, now we have to be able to add this tile type to any tile we click on so find:
                      If frmMirage.optKeyOpen.Value = True Then
                          .Type = TILE_TYPE_KEYOPEN
                          .Data1 = KeyOpenEditorX
                          .Data2 = KeyOpenEditorY
                          .Data3 = 0
                     End If


And after it add:
                      If frmMirage.optNpcSpawn.Value = True Then
                          .Type = TILE_TYPE_NPCSPAWN
                          .Data1 = SpawnNpcNum
                          .Data2 = 0
                          .Data3 = 0
                     End If


This pretty much sets the tiles properties to store what you want it to, in this case the npc you selected.


Ok, we need somewhere to input the tile properties into so add a new form call ‘frmMapSpawnNpc’ and on it at a list box called ‘lstNpc’ and two command button named cmdOk, cmdCancel.

 When we load this form, we need to load each npc that is spawned on the map into the list box so that the user can select the npc they wish to spawn, so in form load add:
Dim n As Long

    For n = 1 To MAX_MAP_NPCS
        If Map.Npc(n) > 0 Then
             lstNpc.AddItem n & ": " & Npc(Map.Npc(n)).Name
        Else
             lstNpc.AddItem n & ": No Npc"
        End If
    Next n


All this code does is scan each map npc slot and add the Map Npc num and the Npc Name. (If it doesn’t exist, it will just insert a “No Npc” statement)


Now we need a variable to remember what npc we need to spawn at the spawn point when we place it on the map so find:
Public KeyOpenEditorY As Long


And below it add:
' Used for SpawnNpc editor
Public SpawnNpcNum As Long


‘Now weve added the variable, we need to record what we’ve selected to it so add for cmdOk
    SpawnNpcNum = lstNpc.ListIndex + 1
Unload me

And of course if you haven’t already, put an unload me into cmdCancel
‘Now we just need to add the option button to frmmirage, so goto frmMirage, on the frame with the tile attribs on them, add a new option button called ‘optNpcSpawn’ and on the click event add
frmMapSpawnNpc.Show vbModal
  This basically loads the form but the ‘vbModal’ Part attaches it to frmMirage and gives it priority so you cant click on frmMirage until you’ve unloaded it

Npc Spawn Part 2

Ok, were done client side so now we want to set the server up to spawn it on the spawn location so goto the server project window and find the ‘SpawnNpc’ sub

Now just above:
             ' Well try 100 times to randomly place the sprite


Add:
        
        'Check if theres a spawn tile for the specific npc
        For x = 0 To MAX_MAPX
             For y = 0 To MAX_MAPY
                 If Map(MapNum).Tile(x, y).Type = TILE_TYPE_NPCSPAWN Then
                     If Map(MapNum).Tile(x, y).Data1 = MapNpcNum Then
                          MapNpc(MapNum, MapNpcNum).x = x
                          MapNpc(MapNum, MapNpcNum).y = y
                          Spawned = True
                          Exit For
                     End If
                 End If
             Next y
        Next x
       


All this does is scan each tile for its type and if it’s an npc spawn type it will then check if its meant to spawn the same npc that its trying to spawn right now and if it is, it will place the npc on that tile and skip the rest of the code that tries to spawn the Npc somewhere else.  Now we need to mod a bit of existing code so that we can skip it if we have already spawned an npc so just below what you’ve added, change:
             For i = 1 To 100
                 x = Int(Rnd * MAX_MAPX)
                 y = Int(Rnd * MAX_MAPY)
                
                 ' Check if the tile is walkable
                 If Map(MapNum).Tile(x, y).Type = TILE_TYPE_WALKABLE Then
                     MapNpc(MapNum, MapNpcNum).x = x
                     MapNpc(MapNum, MapNpcNum).y = y
                     Spawned = True
                     Exit For
                 End If
             Next i


To this:
        If Not Spawned Then
             ' Well try 100 times to randomly place the sprite
             For i = 1 To 100
                 x = Int(Rnd * MAX_MAPX)
                 y = Int(Rnd * MAX_MAPY)
                
                 ' Check if the tile is walkable
                 If Map(MapNum).Tile(x, y).Type = TILE_TYPE_WALKABLE Then
                     MapNpc(MapNum, MapNpcNum).x = x
                     MapNpc(MapNum, MapNpcNum).y = y
                     Spawned = True
                     Exit For
                 End If
             Next i
        End If


Ok then, that should be everything, any problems let me know :)




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