``` Public Sub CastSpell(ByVal index As Long, ByVal spellslot As Long, ByVal target As Long, ByVal targetType As Byte) Dim spellnum As Long Dim MPCost As Long Dim LevelReq As Long Dim mapNum As Long Dim Vital As Long Dim DidCast As Boolean Dim ClassReq As Long Dim AccessReq As Long Dim I As Long Dim AoE As Long Dim Range As Long Dim VitalType As Byte Dim increment As Boolean Dim x As Long, y As Long Dim Buffer As clsBuffer Dim SpellCastType As Long DidCast = False ' Prevent subscript out of range If spellslot MAX_PLAYER_SPELLS Then Exit Sub spellnum = GetPlayerSpell(index, spellslot) mapNum = GetPlayerMap(index) ' Make sure player has the spell If Not HasSpell(index, spellnum) Then Exit Sub MPCost = Spell(spellnum).MPCost ' Check if they have enough MP If GetPlayerVital(index, Vitals.MP) < MPCost Then Call PlayerMsg(index, "Not enough mana!", BrightRed) Exit Sub End If LevelReq = Spell(spellnum).LevelReq ' Make sure they are the right level If LevelReq > GetPlayerLevel(index) Then Call PlayerMsg(index, "You must be level " & LevelReq & " to cast this spell.", BrightRed) Exit Sub End If AccessReq = Spell(spellnum).AccessReq ' make sure they have the right access If AccessReq > GetPlayerAccess(index) Then Call PlayerMsg(index, "You must be an administrator to cast this spell.", BrightRed) Exit Sub End If ClassReq = Spell(spellnum).ClassReq ' make sure the classreq > 0 If ClassReq > 0 Then ' 0 = no req If ClassReq GetPlayerClass(index) Then Call PlayerMsg(index, "Only " & CheckGrammar(Trim$(Class(ClassReq).Name)) & " can use this spell.", BrightRed) Exit Sub End If End If ' find out what kind of spell it is! self cast, target or AOE If Spell(spellnum).Range > 0 Then ' ranged attack, single target or aoe? If Not Spell(spellnum).IsAoE Then SpellCastType = 2 ' targetted Else SpellCastType = 3 ' targetted aoe End If Else If Not Spell(spellnum).IsAoE Then SpellCastType = 0 ' self-cast Else SpellCastType = 1 ' self-cast AoE End If End If ' set the vital Vital = Spell(spellnum).Vital AoE = Spell(spellnum).AoE Range = Spell(spellnum).Range Select Case SpellCastType Case 0 ' self-cast target Select Case Spell(spellnum).Type Case SPELL_TYPE_HEALHP SpellPlayer_Effect Vitals.HP, True, index, Vital, spellnum DidCast = True Case SPELL_TYPE_HEALMP SpellPlayer_Effect Vitals.MP, True, index, Vital, spellnum DidCast = True Case SPELL_TYPE_WARP SendAnimation mapNum, Spell(spellnum).SpellAnim, 0, 0, TARGET_TYPE_PLAYER, index PlayerWarp index, Spell(spellnum).Map, Spell(spellnum).x, Spell(spellnum).y SendAnimation GetPlayerMap(index), Spell(spellnum).SpellAnim, 0, 0, TARGET_TYPE_PLAYER, index DidCast = True End Select Case 1, 3 ' self-cast AOE & targetted AOE If SpellCastType = 1 Then x = GetPlayerX(index) y = GetPlayerY(index) ElseIf SpellCastType = 3 Then If targetType = 0 Then Exit Sub If target = 0 Then Exit Sub If targetType = TARGET_TYPE_PLAYER Then x = GetPlayerX(target) y = GetPlayerY(target) Else x = MapNpc(mapNum).Npc(target).x y = MapNpc(mapNum).Npc(target).y End If If Not isInRange(Range, GetPlayerX(index), GetPlayerY(index), x, y) Then PlayerMsg index, "Target not in range.", BrightRed SendClearSpellBuffer index End If End If Select Case Spell(spellnum).Type Case SPELL_TYPE_DAMAGEHP DidCast = True For I = 1 To Player_HighIndex If IsPlaying(I) Then If I index Then If GetPlayerMap(I) = GetPlayerMap(index) Then If isInRange(AoE, x, y, GetPlayerX(I), GetPlayerY(I)) Then If CanPlayerAttackPlayer(index, I, True) Then SendAnimation mapNum, Spell(spellnum).SpellAnim, 0, 0, TARGET_TYPE_PLAYER, I PlayerAttackPlayer index, I, Vital, spellnum End If End If End If End If End If Next For I = 1 To MAX_MAP_NPCS If MapNpc(mapNum).Npc(I).Num > 0 Then If MapNpc(mapNum).Npc(I).Vital(HP) > 0 Then If isInRange(AoE, x, y, MapNpc(mapNum).Npc(I).x, MapNpc(mapNum).Npc(I).y) Then If CanPlayerAttackNpc(index, I, True) Then SendAnimation mapNum, Spell(spellnum).SpellAnim, 0, 0, TARGET_TYPE_NPC, I PlayerAttackNpc index, I, Vital, spellnum End If End If End If End If Next Case SPELL_TYPE_HEALHP, SPELL_TYPE_HEALMP, SPELL_TYPE_DAMAGEMP If Spell(spellnum).Type = SPELL_TYPE_HEALHP Then VitalType = Vitals.HP increment = True ElseIf Spell(spellnum).Type = SPELL_TYPE_HEALMP Then VitalType = Vitals.MP increment = True ElseIf Spell(spellnum).Type = SPELL_TYPE_DAMAGEMP Then VitalType = Vitals.MP increment = False End If DidCast = True For I = 1 To Player_HighIndex If IsPlaying(I) Then If GetPlayerMap(I) = GetPlayerMap(index) Then If isInRange(AoE, x, y, GetPlayerX(I), GetPlayerY(I)) Then SpellPlayer_Effect VitalType, increment, I, Vital, spellnum End If End If End If Next For I = 1 To MAX_MAP_NPCS If MapNpc(mapNum).Npc(I).Num > 0 Then If MapNpc(mapNum).Npc(I).Vital(HP) > 0 Then If isInRange(AoE, x, y, MapNpc(mapNum).Npc(I).x, MapNpc(mapNum).Npc(I).y) Then SpellNpc_Effect VitalType, increment, I, Vital, spellnum, mapNum End If End If End If Next End Select Case 2 ' targetted If targetType = 0 Then Exit Sub If target = 0 Then Exit Sub If targetType = TARGET_TYPE_PLAYER Then x = GetPlayerX(target) y = GetPlayerY(target) Else x = MapNpc(mapNum).Npc(target).x y = MapNpc(mapNum).Npc(target).y End If If Not isInRange(Range, GetPlayerX(index), GetPlayerY(index), x, y) Then PlayerMsg index, "Target not in range.", BrightRed SendClearSpellBuffer index Exit Sub End If Select Case Spell(spellnum).Type Case SPELL_TYPE_DAMAGEHP If targetType = TARGET_TYPE_PLAYER Then If CanPlayerAttackPlayer(index, target, True) Then If Vital > 0 Then SendAnimation mapNum, Spell(spellnum).SpellAnim, 0, 0, TARGET_TYPE_PLAYER, target PlayerAttackPlayer index, target, Vital, spellnum DidCast = True End If End If Else If CanPlayerAttackNpc(index, target, True) Then If Vital > 0 Then SendAnimation mapNum, Spell(spellnum).SpellAnim, 0, 0, TARGET_TYPE_NPC, target PlayerAttackNpc index, target, Vital, spellnum DidCast = True End If End If End If Case SPELL_TYPE_DAMAGEMP, SPELL_TYPE_HEALMP, SPELL_TYPE_HEALHP If Spell(spellnum).Type = SPELL_TYPE_DAMAGEMP Then VitalType = Vitals.MP increment = False ElseIf Spell(spellnum).Type = SPELL_TYPE_HEALMP Then VitalType = Vitals.MP increment = True ElseIf Spell(spellnum).Type = SPELL_TYPE_HEALHP Then VitalType = Vitals.HP increment = True End If If targetType = TARGET_TYPE_PLAYER Then If Spell(spellnum).Type = SPELL_TYPE_DAMAGEMP Then If CanPlayerAttackPlayer(index, target, True) Then SpellPlayer_Effect VitalType, increment, target, Vital, spellnum End If Else SpellPlayer_Effect VitalType, increment, target, Vital, spellnum End If Else If Spell(spellnum).Type = SPELL_TYPE_DAMAGEMP Then If CanPlayerAttackNpc(index, target, True) Then SpellNpc_Effect VitalType, increment, target, Vital, spellnum, mapNum End If Else SpellNpc_Effect VitalType, increment, target, Vital, spellnum, mapNum End If End If End Select End Select If DidCast Then Call SetPlayerVital(index, Vitals.MP, GetPlayerVital(index, Vitals.MP) - MPCost) Call SendVital(index, Vitals.MP) ' send vitals to party if in one If TempPlayer(index).inParty > 0 Then SendPartyVitals TempPlayer(index).inParty, index TempPlayer(index).SpellCD(spellslot) = GetTickCount + (Spell(spellnum).CDTime * 1000) Call SendCooldown(index, spellslot) SendActionMsg mapNum, Trim$(Spell(spellnum).Name) & "!", BrightRed, ACTIONMSG_SCROLL, GetPlayerX(index) * 32, GetPlayerY(index) * 32 End If End Sub ```