Mirage Source
http://miragesource.net/forums/

[Fix] Better NPC spawning
http://miragesource.net/forums/viewtopic.php?f=183&t=5821
Page 1 of 66

Author:  GIAKEN [ Tue Jun 23, 2009 12:51 am ]
Post subject:  [Fix] Better NPC spawning

Well, NPC spawning is a bit fucked...NPCs can spawn on other NPCs and even other players. Also if an NPC doesn't spawn, it's still created...and won't spawn until you /respawn it. This fix will only spawn an NPC if it's able to and when an open slot becomes available it will spawn there.

Replace:

Code:
Public Sub SpawnNpc(ByVal MapNpcNum As Long, ByVal MapNum As Long)


With:

Code:
Public Sub SpawnNpc(ByVal MapNpcNum As Long, ByVal MapNum As Long)
Dim Packet As String
Dim NpcNum As Long
Dim i As Long
Dim X As Long
Dim Y As Long
Dim Spawned As Boolean

    ' Check for subscript out of range
    If MapNpcNum <= 0 Or MapNpcNum > MAX_MAP_NPCS Or MapNum <= 0 Or MapNum > MAX_MAPS Then Exit Sub
   
    NpcNum = Map(MapNum).Npc(MapNpcNum)
   
    If NpcNum > 0 Then
       
        ' Well try 100 times to randomly place the sprite
        For i = 1 To 100
            X = Random(0, MAX_MAPX)
            Y = Random(0, MAX_MAPY)
           
            ' Check if the tile is walkable
            If NpcTileIsOpen(MapNum, X, Y) Then
                MapNpc(MapNum, MapNpcNum).X = X
                MapNpc(MapNum, MapNpcNum).Y = Y
                Spawned = True
                Exit For
            End If
        Next
       
        ' Didn't spawn, so now we'll just try to find a free tile
        If Not Spawned Then
            For X = 0 To MAX_MAPX
                For Y = 0 To MAX_MAPY
                    If NpcTileIsOpen(MapNum, X, Y) Then
                        MapNpc(MapNum, MapNpcNum).X = X
                        MapNpc(MapNum, MapNpcNum).Y = Y
                        Spawned = True
                    End If
                Next
            Next
        End If
       
        ' If we suceeded in spawning then send it to everyone
        If Spawned Then
       
            MapNpc(MapNum, MapNpcNum).Num = NpcNum
            MapNpc(MapNum, MapNpcNum).Target = 0
           
            MapNpc(MapNum, MapNpcNum).Vital(Vitals.HP) = GetNpcMaxVital(NpcNum, Vitals.HP)
            MapNpc(MapNum, MapNpcNum).Vital(Vitals.MP) = GetNpcMaxVital(NpcNum, Vitals.MP)
            MapNpc(MapNum, MapNpcNum).Vital(Vitals.SP) = GetNpcMaxVital(NpcNum, Vitals.SP)
           
            MapNpc(MapNum, MapNpcNum).Dir = DIR_DOWN
           
            Packet = SSpawnNpc & SEP_CHAR & MapNpcNum & SEP_CHAR & MapNpc(MapNum, MapNpcNum).Num & SEP_CHAR & MapNpc(MapNum, MapNpcNum).X & SEP_CHAR & MapNpc(MapNum, MapNpcNum).Y & SEP_CHAR & MapNpc(MapNum, MapNpcNum).Dir & END_CHAR
            Call SendDataToMap(MapNum, Packet)
        End If
    End If
   
End Sub


Then under that sub add this:

Code:
Public Function NpcTileIsOpen(ByVal MapNum As Long, ByVal X As Long, ByVal Y As Long) As Boolean
Dim LoopI As Long

    NpcTileIsOpen = True
   
    If PlayersOnMap(MapNum) Then
        For LoopI = 1 To MAX_PLAYERS
            If GetPlayerMap(LoopI) = MapNum Then
                If GetPlayerX(LoopI) = X Then
                    If GetPlayerY(LoopI) = Y Then
                        NpcTileIsOpen = False
                        Exit Function
                    End If
                End If
            End If
        Next
    End If
   
    For LoopI = 1 To MAX_MAP_NPCS
        If MapNpc(MapNum, LoopI).Num > 0 Then
            If MapNpc(MapNum, LoopI).X = X Then
                If MapNpc(MapNum, LoopI).Y = Y Then
                    NpcTileIsOpen = False
                    Exit Function
                End If
            End If
        End If
    Next
   
    If Map(MapNum).Tile(X, Y).Type <> TILE_TYPE_NONE Then NpcTileIsOpen = False
   
End Function


And that should be it.

Author:  Robin [ Tue Jun 23, 2009 8:04 am ]
Post subject:  Re: [Fix] Better NPC spawning

ty

Author:  Rian [ Tue Jun 23, 2009 5:30 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Nice one, Harold.

Author:  GIAKEN [ Sun Jun 28, 2009 9:07 pm ]
Post subject:  Re: [Fix] Better NPC spawning

I'm the best.

Author:  Joost [ Mon Jun 29, 2009 10:19 am ]
Post subject:  Re: [Fix] Better NPC spawning

Loop to MAX_PLAYERS instead of a PlayersOnMap variable is messy though ;p. + npc's cant spawn on item attributes, and other attributes that they should be able to spawn on.

Author:  GIAKEN [ Sun Jul 12, 2009 7:18 pm ]
Post subject:  Re: [Fix] Better NPC spawning

I did loop with max_players...

And yeah this isn't a rewrite, I just fixed up the current spawning...which didn't let them spawn on anything but an empty tile.

Author:  GIAKEN [ Sun Aug 23, 2009 2:44 am ]
Post subject:  Re: [Fix] Better NPC spawning

Fixed the NpcTileIsOpen function. Wasn't checking if the player was on that specific map.

Author:  wanai [ Wed Dec 01, 2021 9:05 am ]
Post subject:  Re: [Fix] Better NPC spawning

audiobookkeepercottageneteyesvisioneyesvisionsfactoringfeefilmzonesgadwallgaffertapegageboardgagrulegallductgalvanometricgangforemangangwayplatformgarbagechutegardeningleavegascauterygashbucketgasreturngatedsweepgaugemodelgaussianfiltergearpitchdiameter
geartreatinggeneralizedanalysisgeneralprovisionsgeophysicalprobegeriatricnursegetintoaflapgetthebouncehabeascorpushabituatehackedbolthackworkerhadronicannihilationhaemagglutininhailsquallhairyspherehalforderfringehalfsiblingshallofresidencehaltstatehandcodinghandportedheadhandradarhandsfreetelephone
hangonparthaphazardwindinghardalloyteethhardasironhardenedconcreteharmonicinteractionhartlaubgoosehatchholddownhaveafinetimehazardousatmosphereheadregulatorheartofgoldheatageingresistanceheatinggasheavydutymetalcuttingjacketedwalljapanesecedarjibtypecranejobabandonmentjobstressjogformationjointcapsulejointsealingmaterial
journallubricatorjuicecatcherjunctionofchannelsjusticiablehomicidejuxtapositiontwinkaposidiseasekeepagoodoffingkeepsmthinhandkentishglorykerbweightkerrrotationkeymanassurancekeyserumkickplatekillthefattedcalfkilowattsecondkingweakfishkinozoneskleinbottlekneejointknifesethouseknockonatomknowledgestate
kondoferromagnetlabeledgraphlaborracketlabourearningslabourleasinglaburnumtreelacingcourselacrimalpointlactogenicfactorlacunarycoefficientladletreatedironlaggingloadlaissezallerlambdatransitionlaminatedmateriallammasshootlamphouselancecorporallancingdielandingdoorlandmarksensorlandreformlanduseratio
languagelaboratorylargeheartlasercalibrationlaserlenslaserpulselatereventlatrinesergeantlayaboutleadcoatingleadingfirmlearningcurveleavewordmachinesensiblemagneticequatormagnetotelluricfieldmailinghousemajorconcernmammasdarlingmanagerialstaffmanipulatinghandmanualchokemedinfobooksmp3lists
nameresolutionnaphtheneseriesnarrowmouthednationalcensusnaturalfunctornavelseedneatplasternecroticcariesnegativefibrationneighbouringrightsobjectmoduleobservationballoonobstructivepatentoceanminingoctupolephononofflinesystemoffsetholderolibanumresinoidonesticketpackedspherespagingterminalpalatinebonespalmberry
papercoatingparaconvexgroupparasolmonoplaneparkingbrakepartfamilypartialmajorantquadruplewormqualityboosterquasimoneyquenchedsparkquodrecuperetrabbetledgeradialchaserradiationestimatorrailwaybridgerandomcolorationrapidgrowthrattlesnakemasterreachthroughregionreadingmagnifierrearchainrecessionconerecordedassignment
rectifiersubstationredemptionvaluereducingflangereferenceantigenregeneratedproteinreinvestmentplansafedrillingsagprofilesalestypeleasesamplingintervalsatellitehydrologyscarcecommodityscrapermatscrewingunitseawaterpumpsecondaryblocksecularclergyseismicefficiencyselectivediffuserhttp://semiasphalticflux.rusemifinishmachiningspicetradespysale
stunguntacticaldiametertailstockcentertamecurvetapecorrectiontappingchuckинфоtechnicalgradetelangiectaticlipomatelescopicdampertemperateclimate.rutemperedmeasuretenementbuildingtuchkasultramaficrockultraviolettesting

Author:  wanai [ Wed Jan 05, 2022 8:46 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Econ

Author:  wanai [ Wed Jan 05, 2022 8:47 pm ]
Post subject:  Re: [Fix] Better NPC spawning

107

Author:  wanai [ Wed Jan 05, 2022 8:48 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Bett

Author:  wanai [ Wed Jan 05, 2022 8:49 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Bett

Author:  wanai [ Wed Jan 05, 2022 8:50 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Clif

Author:  wanai [ Wed Jan 05, 2022 8:51 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Lovi

Author:  wanai [ Wed Jan 05, 2022 8:52 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Stam

Author:  wanai [ Wed Jan 05, 2022 8:54 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Adam

Author:  wanai [ Wed Jan 05, 2022 8:55 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Geor

Author:  wanai [ Wed Jan 05, 2022 8:56 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Meni

Author:  wanai [ Wed Jan 05, 2022 8:57 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Pola

Author:  wanai [ Wed Jan 05, 2022 8:58 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Inge

Author:  wanai [ Wed Jan 05, 2022 8:59 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Mich

Author:  wanai [ Wed Jan 05, 2022 9:00 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Simp

Author:  wanai [ Wed Jan 05, 2022 9:02 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Pian

Author:  wanai [ Wed Jan 05, 2022 9:03 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Else

Author:  wanai [ Wed Jan 05, 2022 9:04 pm ]
Post subject:  Re: [Fix] Better NPC spawning

Dwel

Page 1 of 66 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/