aalqaq2
Trying to help, but I'm not too good at it.
- Joined
- Apr 10, 2017
- Messages
- 112
- Solutions
- 3
- Reaction score
- 8
Hey guys,
What is the best way to save player choices into an array and add the items later? I want to give the players the ability to choose a secondary weapon and choose between a spellbook or shield when they enter the mainland. I thought about using a secondary table and
but I'm not sure if that's the best way. I'm also pretty sure I'm handling the implementation in the worst possible manner.
The code is below. Any feedback is appreciated.
What is the best way to save player choices into an array and add the items later? I want to give the players the ability to choose a secondary weapon and choose between a spellbook or shield when they enter the mainland. I thought about using a secondary table and
Lua:
local a = table.insert(a, item)
The code is below. Any feedback is appreciated.
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local vocation = {}
local town = {}
local destination = {}
function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
--Equipment Upon Arrival to Mainland
--{8601, 1}, {2439, 1}, {8602, 1}, {2175, 1} | steel axe, daramian mace, jagged sword, spellbook
local config = {
[1] = { -- Sorcerer
--Equipment: wand of vortex, spirit cloak, plate legs, soldier helmet, scarf, leather boots
items = {{2190, 1}, {8870, 1}, {2647, 1}, {2481, 1}, {2661, 1}, {2643, 1}},
--Backpack: rope, shovel, 5 mana potion, 5 health potions, 1 crystal coin, 10 orichalcum pearls, 100 brown mushrooms
container = {{2120, 1}, {2554, 1}, {7620, 5}, {7618, 5}, {2160, 1}, {5022, 10}, {2789,100}, {2525, 1}}
},
[2] = { -- Druid
--Equipment: snakebite rod, spirit cloak, plate legs, soldier helmet, scarf, leather boots
items = {{2182, 1}, {8870, 1}, {2647, 1}, {2481, 1}, {2661, 1}, {2643, 1}},
--Backpack: rope, shovel, 5 mana potion, 5 health potion, 1 crystal coin, 10 orichalcum pearls, 100 brown mushrooms
container = {{2120, 1}, {2554, 1}, {7620, 5}, {7618, 5}, {2160, 1}, {5022, 10}, {2789,100}}
},
[3] = { -- Paladin
--Equipment: dwarven shield, 1 spear, scale armor, plate legs, soldier helmet, scarf, leather boots
items = {{2525, 1}, {2389, 1}, {2483, 1}, {2647, 1}, {2481, 1}, {2661, 1}, {2643, 1}},
--Backpack: rope, shovel, 5 mana potion, 5 health potion, 1 crystal coin, 10 orichalcum pearls, 100 brown mushrooms, bow, 50 arrows
container = {{2120, 1}, {2554, 1}, {7620, 5}, {7618, 5}, {2160, 1}, {5022, 10}, {2789,100}, {2456, 1}, {2544, 50}}
},
[4] = { -- Knight
--Equipment: dwarven shield, scale armor, plate legs, soldier helmet, scarf, leather boots
items = {{2525, 1}, {2483, 1}, {2647, 1}, {2481, 1}, {2661, 1}, {2643, 1}},
--Backpack: rope, shovel, 5 mana potion, 5 health potion, 1 crystal coin, 10 orichalcum pearls, 100 brown mushrooms
container = {{2120, 1}, {2554, 1}, {7620, 5}, {7618, 5} ,{2160, 1}, {5022, 10}, {2789,100}}
}
}
--End--
local function greetCallback(cid)
local player = Player(cid)
local level = player:getLevel()
if level < 8 then
npcHandler:say("CHILD! COME BACK WHEN YOU HAVE GROWN UP!", cid)
return false
elseif level > 9 then
npcHandler:say(player:getName() .. ", I CAN'T LET YOU LEAVE - YOU ARE TOO STRONG ALREADY! YOU CAN ONLY LEAVE WITH LEVEL 9 OR LOWER.", cid)
return false
elseif player:getVocation():getId() > 0 then
npcHandler:say("YOU ALREADY HAVE A VOCATION!", cid)
return false
end
return true
end
local function creatureSayCallback(cid, type, msg)
if not npcHandler:isFocused(cid) then
return false
end
if msgcontains(msg, "yes") and npcHandler.topic[cid] == 0 then
npcHandler:say("IN WHICH TOWN DO YOU WANT TO LIVE: {THAIS}, {CARLIN}, {VENORE}, {AB'DENRIEL}, {EDRON}, {YALAHAR}, {SVARGROND}, {FARMINE}, {ROSHAMUUL}, {LIBERTY BAY}, {PORT HOPE}, {ANKRAHMUN}, {DARASHIA}, {RATHLETON}?", cid)
npcHandler.topic[cid] = 1
elseif npcHandler.topic[cid] == 1 then
if msgcontains(msg, "thais") then
town[cid] = 2
destination[cid] = Position(32369, 32241, 7)
npcHandler:say("IN THAIS! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "carlin") then
town[cid] = 4
destination[cid] = Position(32360, 31782, 7)
npcHandler:say("IN CARLIN! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "venore") then
town[cid] = 1
destination[cid] = Position(32957, 32076, 7)
npcHandler:say("IN VENORE! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "ab'dendriel") then
town[cid] = 5
destination[cid] = Position(32732, 31634, 7)
npcHandler:say("IN AB'DENDRIEL! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "edron") then
town[cid] = 11
destination[cid] = Position(33217, 31814, 8)
npcHandler:say("IN EDRON! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "yalahar") then
town[cid] = 13
destination[cid] = Position(32787, 31276, 7)
npcHandler:say("IN YALAHAR! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "svargrond") then
town[cid] = 12
destination[cid] = Position(32212, 31132, 7)
npcHandler:say("IN SVARGROND! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "farmine") then
town[cid] = 14
destination[cid] = Position(33023, 31525, 11)
npcHandler:say("IN FARMINE! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "roshamuul") then
town[cid] = 29
destination[cid] = Position(33513, 32363, 6)
npcHandler:say("IN ROSHAMUUL! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "liberty bay") then
town[cid] = 7
destination[cid] = Position(32317, 32826, 7)
npcHandler:say("IN LIBERTY BAY! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "port hope") then
town[cid] = 8
destination[cid] = Position(32594, 32745, 7)
npcHandler:say("IN PORT HOPE! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "ankrahmun") then
town[cid] = 9
destination[cid] = Position(33194, 32853, 8)
npcHandler:say("IN ANKRAHMUN! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "darashia") then
town[cid] = 10
destination[cid] = Position(33213, 32454, 1)
npcHandler:say("IN DARASHIA! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "rathleton") then
town[cid] = 33
destination[cid] = Position(33594, 31899, 6)
npcHandler:say("IN RATHLETON! AND WHAT PROFESSION HAVE YOU CHOSEN: {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
else
npcHandler:say("IN WHICH TOWN DO YOU WANT TO LIVE: {THAIS}, {CARLIN}, {VENORE}, {AB'DENRIEL}?", cid)
end
elseif npcHandler.topic[cid] == 2 then
if msgcontains(msg, "sorcerer") then
npcHandler:say("YOU WILL BE GIVEN A WAND OF VORTEX. WOULD YOU LIKE TO CHOOSE A SECONDARY {WEAPON}?", cid)
if isInArray({"weapon", "yes"}, msg:lower()) then
npcHandler:say("WOULD YOU LIKE A {SWORD} WEAPON, {AXE} WEAPON, OR {CLUB} WEAPON?", cid)
if msgcontains(msg, "sword") then
--Add Sword Here {8602, 1}
npcHandler:say("YOU WILL RECEIVE A SWORD WEAPON!", cid)
npcHandler:say("WOULD YOU LIKE A {SPELLBOOK} OR A {SHIELD}?", cid)
if msgcontains(msg, "spellbook") then
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
elseif msgcontains(msg, "shield") then
--Add shield here {2525, 1}
npcHandler:say("YOU WILL RECEIVE A SHIELD!", cid)
else
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
end
elseif msgcontains(msg, "axe") then
--Add Axe Here {8601, 1}
npcHandler:say("YOU WILL RECEIVE AN AXE WEAPON!", cid)
npcHandler:say("WOULD YOU LIKE A {SPELLBOOK} OR A {SHIELD}?", cid)
if msgcontains(msg, "spellbook") then
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
elseif msgcontains(msg, "shield") then
--Add shield here {2525, 1}
npcHandler:say("YOU WILL RECEIVE A SHIELD!", cid)
else
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
end
elseif msgcontains(msg, "club") then
--Add Club Here {2439, 1}
npcHandler:say("YOU WILL RECEIVE A CLUB WEAPON!", cid)
npcHandler:say("WOULD YOU LIKE A {SPELLBOOK} OR A {SHIELD}?", cid)
if msgcontains(msg, "spellbook") then
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
elseif msgcontains(msg, "shield") then
--Add shield here {2525, 1}
npcHandler:say("YOU WILL RECEIVE A SHIELD!", cid)
else
--Add Error handler here
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
end
else
npcHandler:say("YOU WILL NOT RECEIVE A SECONDARY WEAPON!", cid)
end
else
npcHandler:say("YOU WILL NOT RECEIVE A SECONDARY WEAPON!", cid)
end
npcHandler:say("ARE YOU READY TO ENTER THE MAINLAND?!", cid)
npcHandler.topic[cid] = 3
vocation[cid] = 1
elseif msgcontains(msg, "druid") then
npcHandler:say("YOU WILL BE GIVEN A SNAKEBITE ROD. WOULD YOU LIKE TO CHOOSE A SECONDARY {WEAPON}?", cid)
if isInArray({"weapon", "yes"}, msg:lower()) then
npcHandler:say("WOULD YOU LIKE A {SWORD} WEAPON, {AXE} WEAPON, OR {CLUB} WEAPON?", cid)
if msgcontains(msg, "sword") then
--Add Sword Here {8602, 1}
npcHandler:say("YOU WILL RECEIVE A SWORD WEAPON!", cid)
npcHandler:say("WOULD YOU LIKE A {SPELLBOOK} OR A {SHIELD}?", cid)
if msgcontains(msg, "spellbook") then
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
elseif msgcontains(msg, "shield") then
--Add shield here {2525, 1}
npcHandler:say("YOU WILL RECEIVE A SHIELD!", cid)
else
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
end
elseif msgcontains(msg, "axe") then
--Add Axe Here {8601, 1}
npcHandler:say("YOU WILL RECEIVE AN AXE WEAPON!", cid)
npcHandler:say("WOULD YOU LIKE A {SPELLBOOK} OR A {SHIELD}?", cid)
if msgcontains(msg, "spellbook") then
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
elseif msgcontains(msg, "shield") then
--Add shield here {2525, 1}
npcHandler:say("YOU WILL RECEIVE A SHIELD!", cid)
else
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
end
elseif msgcontains(msg, "club") then
--Add Club Here {2439, 1}
npcHandler:say("YOU WILL RECEIVE A CLUB WEAPON!", cid)
npcHandler:say("WOULD YOU LIKE A {SPELLBOOK} OR A {SHIELD}?", cid)
if msgcontains(msg, "spellbook") then
--Add spellbook here {2175, 1}
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
elseif msgcontains(msg, "shield") then
--Add shield here {2525, 1}
npcHandler:say("YOU WILL RECEIVE A SHIELD!", cid)
else
--Add Error handler here
npcHandler:say("YOU WILL RECEIVE A SPELLBOOK!", cid)
end
else
npcHandler:say("YOU WILL NOT RECEIVE A SECONDARY WEAPON!", cid)
end
else
npcHandler:say("YOU WILL NOT RECEIVE A SECONDARY WEAPON!", cid)
end
npcHandler:say("ARE YOU READY TO ENTER THE MAINLAND?!", cid)
npcHandler.topic[cid] = 3
vocation[cid] = 2
elseif msgcontains(msg, "paladin") then
npcHandler:say("YOU WILL BE GIVEN A BOW, ARROWS, AND A SPEAR. WOULD YOU LIKE TO CHOOSE A MELEE {WEAPON}?", cid)
if isInArray({"weapon", "yes"}, msg:lower()) then
npcHandler:say("WOULD YOU LIKE A {SWORD} WEAPON, {AXE} WEAPON, OR {CLUB} WEAPON?", cid)
if msgcontains(msg, "sword") then
--Add Sword Here {8602, 1}
npcHandler:say("YOU WILL RECEIVE A SWORD WEAPON!", cid)
elseif msgcontains(msg, "axe") then
--Add Axe Here {8601, 1}
npcHandler:say("YOU WILL RECEIVE AN AXE WEAPON!", cid)
elseif msgcontains(msg, "club") then
--Add Club Here {2439, 1}
npcHandler:say("YOU WILL RECEIVE A CLUB WEAPON!", cid)
else
npcHandler:say("YOU WILL NOT RECEIVE A SECONDARY WEAPON!", cid)
end
else
npcHandler:say("YOU WILL NOT RECEIVE A SECONDARY WEAPON!", cid)
end
npcHandler:say("ARE YOU READY TO ENTER THE MAINLAND?!", cid)
npcHandler.topic[cid] = 3
vocation[cid] = 3
elseif msgcontains(msg, "knight") then
npcHandler:say("THE DEFAULT WEAPON IS A SWORD. WOULD YOU LIKE TO CHOOSE A DIFFERENT {WEAPON}?", cid)
if isInArray({"weapon", "yes"}, msg:lower()) then
npcHandler:say("WOULD YOU LIKE A {SWORD} WEAPON, {AXE} WEAPON, OR {CLUB} WEAPON?", cid)
if msgcontains(msg, "sword") then
--Add Sword Here {8602, 1}
npcHandler:say("YOU WILL RECEIVE A SWORD WEAPON!", cid)
elseif msgcontains(msg, "axe") then
--Add Axe Here {8601, 1}
npcHandler:say("YOU WILL RECEIVE AN AXE WEAPON!", cid)
elseif msgcontains(msg, "club") then
--Add Club Here {2439, 1}
npcHandler:say("YOU WILL RECEIVE A CLUB WEAPON!", cid)
else
npcHandler:say("YOU WILL RECEIVE A SWORD!", cid)
end
else
npcHandler:say("YOU WILL RECEIVE A SWORD!", cid)
end
npcHandler:say("ARE YOU READY TO ENTER THE MAINLAND?!", cid)
npcHandler.topic[cid] = 3
vocation[cid] = 4
else
npcHandler:say("{KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
end
elseif npcHandler.topic[cid] == 3 then
if msgcontains(msg, "yes") then
local player = Player(cid)
npcHandler:say("SO BE IT!", cid)
player:setVocation(Vocation(vocation[cid]))
player:setTown(Town(town[cid]))
local destination = destination[cid]
npcHandler:releaseFocus(cid)
player:teleportTo(destination)
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
destination:sendMagicEffect(CONST_ME_TELEPORT)
--------------------------------------
--Equipment Upon Arrival to Mainland--
--------------------------------------
local firstItems = {2480, 2464, 2468, 2643, 2530, 2383}
local player = Player(cid)
-- Remove rook items from player inventory
for i = 1, #firstItems do
player:removeItem(firstItems[i], 1)
end
local player = Player(cid)
local targetVocation = config[player:getVocation():getId()]
if not targetVocation then
return true
end
-- Add Mainland items to player inventory
for i = 1, #targetVocation.items do
player:addItem(targetVocation.items[i][1], targetVocation.items[i][2])
end
local backpack = player:addItem(1988)
if not backpack then
return true
end
for i = 1, #targetVocation.container do
backpack:addItem(targetVocation.container[i][1], targetVocation.container[i][2])
end
--------------------------------------
----------End Custom Section----------
--------------------------------------
else
npcHandler:say("THEN WHAT? {KNIGHT}, {PALADIN}, {SORCERER}, OR {DRUID}?", cid)
npcHandler.topic[cid] = 2
end
end
return true
end
local function onAddFocus(cid)
town[cid] = 0
vocation[cid] = 0
destination[cid] = 0
end
local function onReleaseFocus(cid)
town[cid] = nil
vocation[cid] = nil
destination[cid] = nil
end
npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())