Ir para conteúdo
Entre para seguir isso  
Forger

Função onAttack(cid, weapon, target)

Recommended Posts

Forger    2
Forger

Olá.

Já tinha publicado essa função há um bom tempo na OTFans e no OpenLua.

Agora, estou publicando aqui.

Ela é usada dentro de creaturescripts, funciona como onDie, onThink, etc...

 

Sintaxe: onAttack(cid, weapon, targetId, targetName)

Descrição: quando algum player ataca qualquer monstro/player, esta função será ativada.

Requerimentos: funções getCreatureTarget(cid), setCreatureTarget(cid, value)

 

Créditos: Forger(eu) -> Desenvolvi as funções.

Ispiro -> Reorganizou algumas partes das funções.

 

Implementando:

Primeiramente, crie um novo arquivo na pasta data/creaturescripts/scripts, cujo name é onAttack.lua. Coloque isso como seu conteúdo:

function onAttackController(cid)
local targetId = getCreatureTarget(cid)
local weapon, targetName
   if(target == 0) then
       return FALSE
   end
   if (isWeapon(getPlayerSlotItemId(cid, CONST_SLOT_LEFT).itemid)) then
       weapon = getPlayerSlotItemId(cid, CONST_SLOT_LEFT)
   elseif (isWeapon(getPlayerSlotItemId(cid, CONST_SLOT_RIGHT).itemid)) then
       weapon = getPlayerSlotItemId(cid, CONST_SLOT_RIGHT)
   end
   targetName = getCreatureName(targetId)
   onAttack(cid, weapon, targetId, targetName)
end

 

Depois disso, crie uma pasta dentro da data/creaturescripts/scripts, nomeada onAttack.

Procure pelo arquivo think.lua, dentro de data/creturescripts/scripts, e adicione isso dentro da função.

dofile("./data/creaturescripts/scripts/onAttack.lua")
onAttackController(cid)

Importante: Se seu binário não possuir a função isWeapon, adicione isso no final do arquivo global.lua(dentro da pasta data):

WEAPONTYPE_CLUB = 1
WEAPONTYPE_SWORD = 2
WEAPONTYPE_AXE = 3
WEAPONTYPE_DISTANCE = 4
WEAPONTYPE_WAND = 5
WEAPONTYPE_ROD = 6

WEAPON_CLUB = {
          2321, 2382, 2391, 2394, 2398, 2401, 2416, 2417, 2421, 2422, 2423, 2424, 2433, 2434, 2436, 2437, 2439, 2444, 2445, 2448, 2449, 2452, 2453, 3961, 3966, 4846, 7379, 7381, 7387, 7392, 7410, 7414, 7415, 7424, 7425, 7426, 7427, 7429, 7430, 7431, 7432, 7437, 7451, 7452
         }
WEAPON_SWORD = {
           2376, 2377, 2379, 2383, 2384, 2385, 2390, 2392, 2393, 2395, 2396, 2397, 2400, 2402, 2403, 2404, 2406, 2407, 2408, 2409, 2411, 2412, 2413, 2419, 2420, 2438, 2442, 2446, 2450, 2451, 3963, 6101, 6528, 7382, 7383, 7384, 7385, 7386, 7390, 7402, 7404, 7405, 7406, 7407, 7408, 7416, 7449
          }
WEAPON_AXE = {
         2378, 2380, 2381, 2386, 2387, 2388, 2405, 2414, 2415, 2418, 2425, 2426, 2427, 2428, 2429, 2430, 2431, 2432, 2435, 2440, 2441, 2443, 2447, 2454, 2550, 2559, 3962, 3964, 6553, 7380, 7388, 7389, 7412, 7413, 7419, 7434
        }
WEAPON_WAND = {
          2187, 2188, 2189, 2190, 2191
         }
WEAPON_ROD = {
         2181, 2182, 2183, 2185, 2186
        }

WEAPON_DISTANCE = {
                  1294, 2111, 2389, 2399, 2410, 2455, 2456, 3965, 5803, 7366, 7367, 7368, 7378, 7438
                  }

function isWeapon(itemId)
   if(getWeaponType(itemId) ~= FALSE) then
       return TRUE
   else
       return FALSE
   end
end

function getWeaponType(itemId)
   if isInArray(WEAPON_CLUB, itemId) then
       return WEAPONTYPE_CLUB
   elseif isInArray(WEAPON_AXE, itemId) then
       return WEAPONTYPE_AXE
   elseif isInArray(WEAPON_SWORD, itemId) then
       return WEAPONTYPE_SWORD
   elseif isInArray(WEAPON_WAND, itemId) then
       return WEAPONTYPE_WAND
   elseif isInArray(WEAPON_ROD, itemId) then
       return WEAPONTYPE_ROD            
   elseif isInArray(WEAPON_DISTANCE, itemId) then
       return WEAPONTYPE_DISTANCE
   else
       return FALSE
   end
end

 

Exemplo de uso:

function onAttack(cid, weapon, targetId, targetName)
   if (isPlayer(targetId) and getPlayerAccess(targetId) >= 4) then
       doPlayerSendCancel(cid, 'You may not attack this player.')
       setCreatureTarget(cid, 0)
   end
   return TRUE
end

 

 

Ou seja, toda vez que quiser criar creaturescripts com essa função, deverá criar um novo arquivo.lua, de qualquer nome, com a função

function onAttack(cid, weapon, targetId, targetName)

end

Acho que é só isso. Editado por Dark

Compartilhar este post


Link para o post
darkk    0
darkk

Gostei, bem util, parabens!

Compartilhar este post


Link para o post
Nord    2
Nord

Muito útil, acredito que nos futuros OTs essa função estará incluida.

 

Outro exemplo de uso:

function onAttack(cid, weapon, targetId, targetName)

if isPlayer(targetId) == TRUE then

doPlayerSendTextMessage(cid, 22, "Você está atacando "..getCreatureName(targetId).." com uma "..getItemName(weapon.itemid).."")

doPlayerSendTextMessage(targetId, 22, ""..getCreatureName(cid).." está te atacando através de uma "..getItemName(weapon.itemid)..""))

return TRUE

end

 

O player que está atacando e o que está sendo atacado recebem uma mensagem mostrando que está (sendo atacado/atacando) e através de que arma.

 

Haxy

Vlw aí por ter corrigido.

Editado por Nord

Compartilhar este post


Link para o post
Haxy    0
Haxy

@Nord

O correto seria:

function onAttack(cid, weapon, targetId, targetName)
if isPlayer(targetId) == TRUE then
doPlayerSendTextMessage(cid, 22, "Você está atacando "..getCreatureName(targetId).." com uma "..getItemName(weapon.itemid).."")
doPlayerSendTextMessage([color="DarkOrange"][b]targetId[/b][/color], 22, ""..getCreatureName(cid).." está te atacando através de uma "..getItemName(weapon.itemid)..""))
return TRUE
end

 

No seu script ele estaria enviando as duas msg pra vc.

 

pS: Não testei mais creio que esta correto.

 

@Topic

 

Boa função.

Cya~

 

N0 M0r£,

Haxy~/Op^

Compartilhar este post


Link para o post
Thami    0
Thami

axo q n existe essa função na svn atual :o

setCreatureTarget(cid, target)

Compartilhar este post


Link para o post
Jonny249    0
Jonny249

mas, como add essa tag da função onAttack no creaturescripts.xml?

Compartilhar este post


Link para o post
Visitante
Este tópico está impedido de receber novos posts.
Entre para seguir isso  
  • Quem Está Navegando   0 membros estão online

    Nenhum usuário registrado visualizando esta página.

×