Ir para conteúdo

Featured Replies

Postado

Hoje estarei postando 3 scripts diferentes onde ao clicar na alavanca o player pode comprar itens. Antes que perguntem são 3 scripts porque o funcionamento é diferente, andei pesquisando bastante e perguntando a alguns no TibiaKing e vi que muitos disseram que era fácil, mas apenas dois conseguiram me ajudar. Um foi o @Lyu que explicou um detalhe de como "puxar" a charges para o item do items.xml ao invés de configurar no próprio script. Já o restante dos scripts foram desenvolvidos pelo @luanluciano93
 

Neste primeiro script é possível comprar uma quantidade de itens (count), que vão vir dentro de um container (containerId) e caso eles tenham "charges", a carga que o item virá é a que está configurada no items.xml

local items = {
    [1520] = {containerId = 2003, itemId = 2197, count = 20, price = 100000}, -- stone skin amulet
    [1521] = {containerId = 1992, itemId = 2164, count = 8, price = 40000}, -- might ring
    [1522] = {containerId = 1996, itemId = 2169, count = 8, price = 16000}, -- time ring
    [1523] = {containerId = 1991, itemId = 2167, count = 8, price = 16000}, -- energy ring
    [1524] = {containerId = 1993, itemId = 2214, count = 8, price = 16000} -- ring of healing
}
 
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tabela = items[item.actionid]
    if not tabela then
        return false
    end
 
    local itemTypeContainer = ItemType(tabela.containerId)
    local itemTypeItem = ItemType(tabela.itemId)
    local containerWeight = itemTypeContainer:getWeight()
    local itemWeight = itemTypeItem:getWeight()
    local playerCap = player:getFreeCapacity()
    local totalWeight = (containerWeight + (itemWeight * tabela.count))
 
    if playerCap < totalWeight then
        itemWeight = itemWeight / 100
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "You have found a " .. itemTypeContainer:getName() .. " with " .. tabela.count .. " " .. itemTypeItem:getName() .. " weighing " .. totalWeight .. " oz it's too heavy.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return false
    end
 
    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack or backpack:getEmptySlots(false) < 1 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "Your main backpack is full. You need to free up 1 available slots.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return false
    end
 
    local container = Game.createItem(tabela.containerId)
 
    if not container then
        return false
    end
 
    for i = 1, tabela.count do
        local itemContainer = Game.createItem(tabela.itemId)
        if itemTypeItem:getCharges() > 0 then
            itemContainer:setAttribute(ITEM_ATTRIBUTE_CHARGES, itemTypeItem:getCharges())
        end
        container:addItemEx(itemContainer)
    end
 
    if not container then
        return false
    end
 
    if player:getTotalMoney() > tabela.price then
        if player:removeTotalMoney(tabela.price) and player:addItemEx(container) then
            player:getPosition():sendMagicEffect(CONST_ME_DRAWBLOOD)
        end
    else        
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "You don't have ".. tabela.price .." gold coins to buy a ".. itemTypeContainer:getName() .." with ".. tabela.count .." ".. itemTypeItem:getName() ..".")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end 
 
    item:transform(item.itemid == 1945 and 1946 or 1945)
 
    return true
end

 

Nesse segundo script, é possível comprar itens soltos (sem estar dentro de containers) mas que podem ser agrupados, como por exemplo, potions

local potions = {
	[1515] = {id = 7620, charges = 100, value = 5600}, -- mana potion
	[1516] = {id = 7589, charges = 100, value = 9300}, -- strong mana potion
	[1517] = {id = 7590, charges = 100, value = 14400}, -- great mana potion
	[1518] = {id = 8472, charges = 100, value = 22800}, -- spirit potion	
	[1519] = {id = 8473, charges = 100, value = 37900}, -- ultimate health potion
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
	local potion = potions[item.actionid]
	if not potion then
		return false
	end

	local potionId = ItemType(potion.id)
	local itemWeight = potionId:getWeight() * potion.charges
	if player:getFreeCapacity() >= itemWeight then
		if not player:removeMoney(potion.value) then
			player:sendCancelMessage("You don't have ".. potion.value .." gold coins to buy ".. potion.charges .." ".. potionId:getName() ..".")
		else
			player:getPosition():sendMagicEffect(CONST_ME_DRAWBLOOD)
			player:addItem(potion.id, potion.charges)
		end
		
	else
		player:sendCancelMessage("You don't have capacity.")
		player:getPosition():sendMagicEffect(CONST_ME_POFF)
	end

	item:transform(item.itemid == 1945 and 1946 or 1945)
	return true
end

 

Nesse terceiro script, é possivel comprar apenas um item solto, não estacavel e sem charges, como uma wand, axe, etcs..

local items = {
    [2378] = {itemId = 2197, price = 100000}, -- battle axe
}
 
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tabela = items[item.actionid]
    if not tabela then
        return false
    end
 
    local itemTypeItem = ItemType(tabela.itemId)
    local itemWeight = itemTypeItem:getWeight()
    local playerCap = player:getFreeCapacity()
    if playerCap < itemWeight then
        itemWeight = itemWeight / 100
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "You have found a " .. itemTypeItem:getName() .. " weighing " .. itemWeight .. " oz it's too heavy.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return false
    end
 
    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack or backpack:getEmptySlots(false) < 1 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "Your main backpack is full. You need to free up 1 available slots.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return false
    end
 
    local itemAdd = Game.createItem(tabela.itemId)
    if not itemAdd then
        return false
    end
 
    if player:getTotalMoney() > tabela.price then
        if player:removeTotalMoney(tabela.price) and player:addItemEx(itemAdd) then
            player:getPosition():sendMagicEffect(CONST_ME_DRAWBLOOD)
        end
    else        
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "You don't have ".. tabela.price .." gold coins to buy a ".. itemTypeItem:getName() ..".")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end 
 
    item:transform(item.itemid == 1945 and 1946 or 1945)
 
    return true
end

 

  • 2 weeks later...

Participe da conversa

Você pode postar agora e se cadastrar mais tarde. Se você tem uma conta, faça o login para postar com sua conta.

Visitante
Responder

Quem Está Navegando 0

  • Nenhum usuário registrado visualizando esta página.

Estatísticas dos Fóruns

  • Tópicos 96.9k
  • Posts 519.6k

Informação Importante

Confirmação de Termo