• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Action [0.3.6] Simple Item Crafting System

dami1310

◄ Unidentified ►
Joined
Jan 27, 2013
Messages
651
Solutions
9
Reaction score
552
Location
Poland
This is something I've done tonight. Might be useful to somebody.
I believe I described everything, but if you have any questions, feel free to ask.
Also, if you know how to write it better, please tell me.

Btw.
This script will probably be only useful to custom ots since you need to create a lot of different items (unless you edit it to use actionid instead of itemid)
It was easier for me to create different items than work with actionids.

Principle of operation:
You use item a on item b.
Item a - id specified in actions.xml (chance to break, can be added to "different tools" if you want different extra success chance(default 0%) or different break chance(default 1%))
Item b - id specified in recipes table, recipeId (always disappears, needs to be different for every craft recipe)

actions/actions.xml
Just do it by yourself

actions/scripts/crafting.lua
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
--variables
local chance = 0
local extraChance = 0
local breakChance = 0
local properItem = 0
local ingredientCheck = 0

--different tools
if item.itemid == 13145 then --add all of these ids to actions.xml   this is item a
    extraChance = 0 -- additional % chance for craft to success
    breakChance = 3 --% chance for tool to break
elseif item.itemid == 13146 then   -- this is also item a
    extraChance = 10
    breakChance = 2
elseif item.itemid == 13147 then     --this too is an item a
    extraChance = 20
    breakChance = 1
else
    extraChance = 0
    breakChance = 1
end

--recipes table
recipes = {
    [1] = {
        recipeId = 1947, --item that you are using your tool on (will get destroyed)   this is so called item b
        creation = 12999, --item you want to craft
        quality = 1, --changes the chance of success: 1 quality = 10% less
        ingredientCount = 4, --number of ingredients
        ingredients = {13156, 10, 13157, 10, 13158, 10, 13159, 10}, --id1, count1, id2, count2 etc
    },
    [2] = {
        recipeId = 1948, -- needs to be unique(different every time) itemid for script to work     this is also item b (but different)
        creation = 13000,
        quality = 4,
        ingredientCount = 2,
        ingredients = {13156, 10, 13157, 10},
    }
}

    for i = 1, #recipes do
        if recipes[i].recipeId == itemEx.itemid then --check if proper recipe
            for j = 1, (recipes[i].ingredientCount)*2, 2 do --check ingredients
                if(getPlayerItemCount(cid, recipes[i].ingredients[j]) >= recipes[i].ingredients[j+1]) then
                    ingredientCheck = ingredientCheck + 1
                else
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you have "..getPlayerItemCount(cid, recipes[i].ingredients[j]).." out of "..recipes[i].ingredients[j+1].." "..getItemNameById(recipes[i].ingredients[j])..".")
                    return false
                end
            end       
            if recipes[i].ingredientCount == ingredientCheck then
                for k = 1, (recipes[i].ingredientCount)*2, 2 do --remove ingredients
                    doPlayerRemoveItem(cid, recipes[i].ingredients[k], recipes[i].ingredients[k+1])
                end
                chance = 90 - (recipes[i].quality * 10) + extraChance --chance formula including recipe quality and tool extra chance
                if(math.random(1, 100) <= chance) then
                    --craft success
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Congratulations, you have crafted "..getItemNameById(recipes[i].creation).." successfully.")
                    doPlayerAddItem(cid, recipes[i].creation, 1)
                    doSendMagicEffect(getPlayerPosition(cid), 9)
                else
                    --craft fail
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Something went wrong and you failed. You get some of the materials back.")
                    doSendMagicEffect(getPlayerPosition(cid), 3)
                    for l = 1, (recipes[i].ingredientCount)*2, 2 do --recover random amount of materials
                        doPlayerAddItem(cid, recipes[i].ingredients[l], math.random(1, recipes[i].ingredients[l+1]))
                    end
                end
                doRemoveItem(itemEx.uid, 1) --remove recipe
                if(math.random(1, 100) <= breakChance) then --tool break
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Your tool has broken during the process.")
                    doRemoveItem(item.uid, 1)
                end
            end
        properItem = 1
        end
    end
    if properItem == 0 then
        doPlayerSendCancel(cid, "This is not a proper craft recipe.")
    end
   
    return true
end
 
Top