HiHo!
Com esse code, o NPC antes de vender a house verifica se ela tem dono. Se tiver não vende a casa... ^^
npc.cpp
ache isso:
lua_register(luaState, "selfGetPosition", NpcScript::luaSelfGetPos);
logo abaixo adicione:
lua_register(luaState, "checkHouse", NpcScript::luacheckhouse);
agora procure:
int NpcScript::luaSelfGetPos(lua_State *L){lua_pop(L,1);
Npc* mynpc = getNpc(L);
lua_pushnumber(L, mynpc->pos.x);
lua_pushnumber(L, mynpc->pos.y);
lua_pushnumber(L, mynpc->pos.z);
return 3;
}
logo abaixo adicione:
int NpcScript::luacheckhouse(lua_State *L){ Npc* mynpc = getNpc(L); if(mynpc) { if(mynpc->docheckhouse(L) == true) lua_pushnumber(L, 0); else lua_pushnumber(L, -1); } return 1;}
(mais em cima) procure por:
void Npc::doAttack(int id){attackedCreature = id;
}
logo abaixo add:
bool Npc::docheckhouse(lua_State *L){ Npc* mynpc = NpcScript::getNpc(L); Tile* tile = g_game.getTile(mynpc->pos); House* house = tile->getHouse(); if (house) { if(house->getOwner() != "") return false; else return true; }}
npc.h
procure por:
void doAttack(int id);
abaixo adicione:
bool docheckhouse(lua_State *L);
(mais em cima) procure por:
static int luaSelfGetPos(lua_State *L);
logo após adicione:
static int luacheckhouse(lua_State *L);
OBS: Para usar a função no script do npc, digite checkHouse(). Irá retornar 0 se a house não tiver dono e retornará -1 se ela tiver dono.
Exemplo de NPC
focus = 0talk_start = 0
target = 0
following = false
attacking = false
function onThingMove(creature, thing, oldpos, oldstackpos)
end
function onCreatureAppear(creature)
end
function onCreatureDisappear(cid, pos)
if focus == cid then
selfSay('Good bye then.')
focus = 0
talk_start = 0
end
end
function onCreatureTurn(creature)
end
function msgcontains(txt, str)
return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)'))
end
function onCreatureSay(cid, type, msg)
msg = string.lower(msg)
if (msgcontains(msg, 'hi') and (focus == 0)) and getDistanceToCreature(cid) < 4 then
selfSay('Hello ' .. creatureGetName(cid) .. '! I sell houses.')
focus = cid
talk_start = os.clock()
elseif (msgcontains(msg, 'hi') and (focus ~= cid)) and getDistanceToCreature(cid) < 4 then
selfSay('Sorry, ' .. creatureGetName(cid) .. '! I talk to you in a minute.') elseif focus == cid then
talk_start = os.clock()
end
if focus == cid then
talk_start = os.clock()
if (msgcontains(msg, 'great street iii')) then -- nome da house em minúsculo
selfSay('/goto 157 31 7') -- posicao de um sqm qualquer (dentro da house)
if checkHouse() == 0 then -- checa se a house tem dono(-1) ou não(0)
if pay(cid,100000) then -- preço da house -> 100 k
selfSay('/owner ' .. creatureGetName(cid))
selfSay('/c ' .. creatureGetName(cid))
selfSay('/goto 160 54 7') -- lugar onde o npc fica
else
selfSay('/goto 160 54 7') -- lugar onde o npc fica
selfSay('Sorry, you don\'t have enough money.')
end
else
selfSay('/goto 160 54 7') -- lugar onde o npc fica
selfSay('Essa casa já tem dono')
end
elseif (msgcontains(msg, 'great street iv')) then -- nome da house em minúsculo
selfSay('/goto 166 37 7') -- posicao de um sqm qualquer (dentro da house)
if checkHouse() == 0 then -- checa se a house tem dono(-1) ou não(0)
if pay(cid,100000) then -- preço da house -> 100 k
selfSay('/owner ' .. creatureGetName(cid))
selfSay('/c ' .. creatureGetName(cid))
selfSay('/goto 160 54 7') -- lugar onde o npc fica
else
selfSay('/goto 160 54 7') -- lugar onde o npc fica
selfSay('Sorry, you don\'t have enough money.')
end
else
selfSay('/goto 160 54 7') -- lugar onde o npc fica
selfSay('Essa casa já tem dono')
end
elseif string.find(msg, '(%a*)bye(%a*)') and getDistanceToCreature(cid) < 4 then
selfSay('Good bye, ' .. creatureGetName(cid) .. '!')
focus = 0
talk_start = 0
end
end
end
function onCreatureChangeOutfit(creature)
end
function onThink()
if (os.clock() - talk_start) > 30 then
if focus > 0 then
selfSay('Next Please...')
end
focus = 0
end
if focus ~= 0 then
if getDistanceToCreature(focus) > 5 then
selfSay('Good bye then.')
focus = 0
end
end
end
PS .: No exemplo acima, o NPC fica no templo. Caso você queira mudá-lo de lugar, você terá que trocar "/goto 160 54 7" para "/goto + posição que ele irá ficar".
FlW
CyA!