diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2013-04-14 15:10:06 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2013-04-14 15:10:06 +0200 |
commit | 080ddbe822b03416934a875b2d949e3afc84fb96 (patch) | |
tree | 8915d1c92fdd0e609952afc492cbc3ea1152d1ae | |
parent | 3a132996f798dbf552ac858b06fa656b6790a501 (diff) | |
download | manaserv-080ddbe822b03416934a875b2d949e3afc84fb96.tar.gz manaserv-080ddbe822b03416934a875b2d949e3afc84fb96.tar.bz2 manaserv-080ddbe822b03416934a875b2d949e3afc84fb96.tar.xz manaserv-080ddbe822b03416934a875b2d949e3afc84fb96.zip |
Removed NPC and character parameters from NPC functions
These functions can only be used in the context of a character talking
to an NPC, so these parameters can be deduced from that context rather
than passing them explicitly all the time.
Simplifies NPC scripting.
-rw-r--r-- | example/scripts/maps/desert.lua | 18 | ||||
-rw-r--r-- | example/scripts/npcs/banker.lua | 38 | ||||
-rw-r--r-- | example/scripts/npcs/barber.lua | 14 | ||||
-rw-r--r-- | example/scripts/npcs/debugger.lua | 40 | ||||
-rw-r--r-- | example/scripts/npcs/emotemaker.lua | 9 | ||||
-rw-r--r-- | example/scripts/npcs/healer.lua | 4 | ||||
-rw-r--r-- | example/scripts/npcs/merchant.lua | 12 | ||||
-rw-r--r-- | example/scripts/npcs/postman.lua | 12 | ||||
-rw-r--r-- | src/game-server/npc.cpp | 2 | ||||
-rw-r--r-- | src/scripting/lua.cpp | 129 | ||||
-rw-r--r-- | src/scripting/script.h | 4 |
11 files changed, 143 insertions, 139 deletions
diff --git a/example/scripts/maps/desert.lua b/example/scripts/maps/desert.lua index 6b761dcd..1fee2fc8 100644 --- a/example/scripts/maps/desert.lua +++ b/example/scripts/maps/desert.lua @@ -39,7 +39,7 @@ end) function Smith(npc, ch, list) local sword_count = chr_inv_count(ch, true, true, "Sword") if sword_count > 0 then - npc_message(npc, ch, "Ah! I can see you already have a sword.") + npc_message("Ah! I can see you already have a sword.") end Merchant(npc, ch, list) end @@ -54,7 +54,7 @@ function possessions_table(npc, ch) ..inventory_table[i].id..", "..inventory_table[i].name..", " ..inventory_table[i].amount end - npc_message(npc, ch, item_message) + npc_message(item_message) item_message = "Equipment:".. "\nSlot id, item id, item name:".. @@ -64,7 +64,7 @@ function possessions_table(npc, ch) item_message = item_message.."\n"..equipment_table[i].slot..", " ..equipment_table[i].id..", "..equipment_table[i].name end - npc_message(npc, ch, item_message) + npc_message(item_message) end @@ -74,21 +74,21 @@ function Harmony(npc, ch, list) being_apply_status(ch, 1, 99999) -- Say all the messages in the messages list. for i = 1, #list do - npc_message(npc, ch, list[i]) + npc_message(list[i]) end --- Give the player 100 units of money the first time. if harmony_have_talked_to_someone == false then - npc_message(npc, ch, "Here is some money for you to find some toys to play with.\nEh Eh!") + npc_message("Here is some money for you to find some toys to play with.\nEh Eh!") chr_money_change(ch, 100) - npc_message(npc, ch, string.format("You now have %d shiny coins!", chr_money(ch))) + npc_message(string.format("You now have %d shiny coins!", chr_money(ch))) harmony_have_talked_to_someone = true - npc_message(npc, ch, string.format("Try to come back with a better level than %i.", chr_get_level(ch))) + npc_message(string.format("Try to come back with a better level than %i.", chr_get_level(ch))) else - npc_message(npc, ch, "Let me see what you've got so far... Don't be afraid!") + npc_message("Let me see what you've got so far... Don't be afraid!") effect_create(EMOTE_WINK, npc) possessions_table(npc, ch) end - npc_message(npc, ch, "Have fun!") + npc_message("Have fun!") effect_create(EMOTE_HAPPY, npc) -- Make Harmony disappear for a while... with a small earthquake effect! local shakeX = posX(npc) diff --git a/example/scripts/npcs/banker.lua b/example/scripts/npcs/banker.lua index e6e1e284..88ba9acd 100644 --- a/example/scripts/npcs/banker.lua +++ b/example/scripts/npcs/banker.lua @@ -12,21 +12,21 @@ function Banker(npc, ch) if being_get_gender(ch) == GENDER_MALE then - npc_message(npc, ch, "Welcome to the bank, sir!") + npc_message("Welcome to the bank, sir!") elseif being_get_gender(ch) == GENDER_FEMALE then - npc_message(npc, ch, "Welcome to the bank, madam!") + npc_message("Welcome to the bank, madam!") else - npc_message(npc, ch, "Welcome to the bank... uhm... person of unspecified gender!") + npc_message("Welcome to the bank... uhm... person of unspecified gender!") end local account = tonumber(chr_get_quest(ch, "BankAccount")) local result = -1 if (account == nil) then --Initial account creation, if needed - npc_message(npc, ch, "Hello! Would you like to setup a bank account? There is a sign-on bonus right now!") - result = npc_choice(npc, ch, "Yes", "No") + npc_message("Hello! Would you like to setup a bank account? There is a sign-on bonus right now!") + result = npc_choice("Yes", "No") if (result == 1) then chr_set_quest(ch, "BankAccount", 5) - npc_message(npc, ch, "Your account has been made. Your sign-on bonus is 5GP.") + npc_message("Your account has been made. Your sign-on bonus is 5GP.") account = 5 end end @@ -37,41 +37,41 @@ function Banker(npc, ch) result = 1 while (result < 3) do --While they've choosen a valid option that isn't "Never mind" account = tonumber(chr_get_quest(ch, "BankAccount")) --Why do I need to convert this? - npc_message(npc, ch, "Your balance: " .. account .. ".\nYour money: " .. chr_money(ch) .. ".") - result = npc_choice(npc, ch, "Deposit", "Withdraw", "Never mind") + npc_message("Your balance: " .. account .. ".\nYour money: " .. chr_money(ch) .. ".") + result = npc_choice("Deposit", "Withdraw", "Never mind") if (result == 1) then --Deposit money = chr_money(ch); if (money > 0) then --Make sure they have money to deposit - npc_message(npc, ch, "How much would you like to deposit? (0 will cancel)") - input = npc_ask_integer(npc, ch, 0, money, 1) + npc_message("How much would you like to deposit? (0 will cancel)") + input = npc_ask_integer(0, money, 1) money = chr_money(ch) if (input > 0 and input <= money) then --Make sure something weird doesn't happen and they try to deposit more than they have chr_money_change(ch, -input) chr_set_quest(ch, "BankAccount", account + input) - npc_message(npc, ch, input .. " GP deposited.") + npc_message(input .. " GP deposited.") elseif (input > money) then --Chosen more than they have - npc_message(npc, ch, "You don't have that much money. But you just did....") + npc_message("You don't have that much money. But you just did....") end else - npc_message(npc, ch, "You don't have any money to deposit!") + npc_message("You don't have any money to deposit!") end elseif (result == 2) then --Withdraw if (account > 0) then --Make sure they have money to withdraw - npc_message(npc, ch, "How much would you like to withdraw? (0 will cancel)") - input = npc_ask_integer(npc, ch, 0, account, 1) + npc_message("How much would you like to withdraw? (0 will cancel)") + input = npc_ask_integer(0, account, 1) if (input > 0 and input <= account) then --Make sure something weird doesn't happen and they try to withdraw more than they have chr_money_change(ch, input) chr_set_quest(ch, "BankAccount", account - input) - npc_message(npc, ch, input .. " GP withdrawn.") + npc_message(input .. " GP withdrawn.") elseif (input > account) then --Chosen more than they have - npc_message(npc, ch, "You don't have that much in your account. But you just did....") + npc_message("You don't have that much in your account. But you just did....") end else - npc_message(npc, ch, "Your account is empty!") + npc_message("Your account is empty!") end end end --This ends the while loop end - npc_message(npc, ch, "Thank you. Come again!") + npc_message("Thank you. Come again!") end diff --git a/example/scripts/npcs/barber.lua b/example/scripts/npcs/barber.lua index 70efb435..58aa1a90 100644 --- a/example/scripts/npcs/barber.lua +++ b/example/scripts/npcs/barber.lua @@ -79,11 +79,11 @@ function Barber(npc, ch, data) -- Choose an appropriate message if result == 1 then - npc_message(npc, ch, "Hello! What style would you like today?") + npc_message("Hello! What style would you like today?") elseif result == 2 then - npc_message(npc, ch, "Hello! What color would you like today?") + npc_message("Hello! What color would you like today?") else - npc_message(npc, ch, "Hello! What can I do for you today?") + npc_message("Hello! What can I do for you today?") end print("#styles ==", #styles) @@ -91,7 +91,7 @@ function Barber(npc, ch, data) -- Repeat until the user selects nothing repeat if (result == 1) then -- Do styles - result = npc_choice(npc, ch, "Bald", styles, "Surprise me", "Never mind") + result = npc_choice("Bald", styles, "Surprise me", "Never mind") result = result -1 @@ -113,7 +113,7 @@ function Barber(npc, ch, data) result = 3 end elseif (result == 2) then -- Do colors - result = npc_choice(npc, ch, colors, "Surprise me", "Never mind") + result = npc_choice(colors, "Surprise me", "Never mind") --Random if (result == #colors + 1) then @@ -130,10 +130,10 @@ function Barber(npc, ch, data) -- If we have both styles and colors, show the main menu if #styles > 0 and #colors > 0 then - result = npc_choice(npc, ch, "Change my style", "Change my color", "Never mind") + result = npc_choice("Change my style", "Change my color", "Never mind") end until result >= 3 --While they've choosen a valid option that isn't "Never mind" -- Let's close up - npc_message(npc, ch, "Thank you. Come again!") + npc_message("Thank you. Come again!") end diff --git a/example/scripts/npcs/debugger.lua b/example/scripts/npcs/debugger.lua index 621ba0f6..e21982a8 100644 --- a/example/scripts/npcs/debugger.lua +++ b/example/scripts/npcs/debugger.lua @@ -12,38 +12,38 @@ function npc1_talk(npc, ch) on_remove(ch, function() print "Player has left the map." end); - npc_message(npc, ch, "Hello! I am the testing NPC.") + npc_message("Hello! I am the testing NPC.") local rights = chr_get_rights(ch); if (rights >= 128) then - npc_message(npc, ch, "Oh mighty server administrator, how can I avoid your wrath?") + npc_message("Oh mighty server administrator, how can I avoid your wrath?") elseif (rights >= 8) then - npc_message(npc, ch, "How can I be of assistance, sir gamemaster?") + npc_message("How can I be of assistance, sir gamemaster?") elseif (rights >= 4) then - npc_message(npc, ch, "What feature would you like to debug, developer?") + npc_message("What feature would you like to debug, developer?") elseif (rights >= 2) then - npc_message(npc, ch, "How can I assist you in your testing duties?") + npc_message("How can I assist you in your testing duties?") elseif (rights >= 1) then - npc_message(npc, ch, "What do you want, lowly player?") + npc_message("What do you want, lowly player?") else - npc_message(npc, ch, "...aren't you supposed to be banned??") + npc_message("...aren't you supposed to be banned??") end - local v = npc_choice(npc, ch, "Guns! Lots of guns!", - "A Christmas party!", - "To make a donation.", - "Slowly count from one to ten.", - "Tablepush Test") + local v = npc_choice("Guns! Lots of guns!", + "A Christmas party!", + "To make a donation.", + "Slowly count from one to ten.", + "Tablepush Test") if v == 1 then - npc_message(npc, ch, "Sorry, this is a heroic-fantasy game, I do not have any gun.") + npc_message("Sorry, this is a heroic-fantasy game, I do not have any gun.") elseif v == 2 then local n1, n2 = chr_inv_count(ch, 524, 511) if n1 == 0 or n2 ~= 0 then - npc_message(npc, ch, "Yeah right...") + npc_message("Yeah right...") else - npc_message(npc, ch, "I can't help you with the party. But I see you have a fancy hat. I could change it into Santa's hat. Not much of a party, but it would get you going.") - v = npc_choice(npc, ch, "Please do.", "No way! Fancy hats are classier.") + npc_message("I can't help you with the party. But I see you have a fancy hat. I could change it into Santa's hat. Not much of a party, but it would get you going.") + v = npc_choice("Please do.", "No way! Fancy hats are classier.") if v == 1 then chr_inv_change(ch, 524, -1, 511, 1) end @@ -51,14 +51,14 @@ function npc1_talk(npc, ch) elseif v == 3 then if chr_money_change(ch, -100) then - npc_message(npc, ch, string.format("Thank you for you patronage! You are left with %d GP.", chr_money(ch))) + npc_message(string.format("Thank you for you patronage! You are left with %d GP.", chr_money(ch))) local g = tonumber(chr_get_quest(ch, "001_donation")) if not g then g = 0 end g = g + 100 chr_set_quest(ch, "001_donation", g) - npc_message(npc, ch, string.format("As of today, you have donated %d GP.", g)) + npc_message(string.format("As of today, you have donated %d GP.", g)) else - npc_message(npc, ch, "I would feel bad taking money from someone that poor.") + npc_message("I would feel bad taking money from someone that poor.") end elseif v == 4 then @@ -95,6 +95,6 @@ function npc1_talk(npc, ch) print("---------------"); end - npc_message(npc, ch, "See you later!") + npc_message("See you later!") end diff --git a/example/scripts/npcs/emotemaker.lua b/example/scripts/npcs/emotemaker.lua index 0f546634..ee8928b6 100644 --- a/example/scripts/npcs/emotemaker.lua +++ b/example/scripts/npcs/emotemaker.lua @@ -21,11 +21,10 @@ function emote_talk(npc, ch) elseif emo_state == EMOTE_HAPPY then state = "happy" end - npc_message(npc, ch, string.format("The emotional palm seems %s.", state)) - v = npc_choice(npc, ch, - "Stupid palm, you are ugly and everyone hates you!", - "You are such a nice palm, let me give you a hug.", - "Are you a cocos nucifera or a syagrus romanzoffiana?") + npc_message(string.format("The emotional palm seems %s.", state)) + v = npc_choice("Stupid palm, you are ugly and everyone hates you!", + "You are such a nice palm, let me give you a hug.", + "Are you a cocos nucifera or a syagrus romanzoffiana?") if (v == 1) then emo_state = EMOTE_SAD diff --git a/example/scripts/npcs/healer.lua b/example/scripts/npcs/healer.lua index 35696736..910e2211 100644 --- a/example/scripts/npcs/healer.lua +++ b/example/scripts/npcs/healer.lua @@ -5,8 +5,8 @@ --]] function Healer(npc, ch) - npc_message(npc, ch, "Do you need healing?") - local c = npc_choice(npc, ch, "Heal me fully", "Heal 100 HP", "Don't heal me") + npc_message("Do you need healing?") + local c = npc_choice("Heal me fully", "Heal 100 HP", "Don't heal me") if c == 1 then being_heal(ch) elseif c == 2 then diff --git a/example/scripts/npcs/merchant.lua b/example/scripts/npcs/merchant.lua index 2ef0a6b7..bcec5fa0 100644 --- a/example/scripts/npcs/merchant.lua +++ b/example/scripts/npcs/merchant.lua @@ -12,9 +12,7 @@ function Merchant(npc, ch, buy_sell_table) - local function say(message) - npc_message(npc, ch, message) - end + local say = npc_message -- Important note: You can add two tables made of trinoms here when calling the -- merchant function. E.g.: Merchant(npc, ch, buy_table, sell_table) @@ -56,7 +54,7 @@ function Merchant(npc, ch, buy_sell_table) table.insert (choice_table, "Tell me about the objects on this map") table.insert (choice_table, "Nevermind...") - local v = npc_choice(npc, ch, choice_table) + local v = npc_choice(choice_table) --Debug and learning purpose --for i,k in ipairs(choice_table) do print(i,k) end @@ -67,7 +65,7 @@ function Merchant(npc, ch, buy_sell_table) if v == 1 then -- "To buy." - local buycase = npc_trade(npc, ch, false, buy_sell_table[1]) + local buycase = npc_trade(false, buy_sell_table[1]) if buycase == 0 then say "What do you want to buy?" elseif buycase == 1 then @@ -80,7 +78,7 @@ function Merchant(npc, ch, buy_sell_table) if buy_sell_table[2] == nil then -- "To sell stuff..." - local sellcase = npc_trade(npc, ch, true) + local sellcase = npc_trade(true) if sellcase == 0 then say "Ok, what do you want to sell?" elseif sellcase == 1 then @@ -90,7 +88,7 @@ function Merchant(npc, ch, buy_sell_table) end else -- "Can you make me a price for what I have?" - local sellcase = npc_trade(npc, ch, true, buy_sell_table[2]) + local sellcase = npc_trade(true, buy_sell_table[2]) if sellcase == 0 then say "Here we go:" elseif sellcase == 1 then diff --git a/example/scripts/npcs/postman.lua b/example/scripts/npcs/postman.lua index 95e6844e..c0901f8d 100644 --- a/example/scripts/npcs/postman.lua +++ b/example/scripts/npcs/postman.lua @@ -11,17 +11,17 @@ ---------------------------------------------------------------------------------- function post_talk(npc, ch) - npc_message(npc, ch, "Hello " .. being_get_name(ch)) + npc_message("Hello " .. being_get_name(ch)) local strength = being_get_attribute(ch, ATTR_STRENGTH) - npc_message(npc, ch, "You have " .. tostring(strength) .. " strength") - npc_message(npc, ch, "What would you like to do?") - local answer = npc_choice(npc, ch, "View Mail", "Send Mail", "Nothing") + npc_message("You have " .. tostring(strength) .. " strength") + npc_message("What would you like to do?") + local answer = npc_choice("View Mail", "Send Mail", "Nothing") if answer == 1 then local sender, post = chr_get_post(ch) if sender == "" then - npc_message(npc, ch, "No Post right now, sorry") + npc_message("No Post right now, sorry") else - npc_message(npc, ch, tostring(sender) .. " sent you " .. tostring(post)) + npc_message(tostring(sender) .. " sent you " .. tostring(post)) end end if answer == 2 then diff --git a/src/game-server/npc.cpp b/src/game-server/npc.cpp index b5e17eac..980358da 100644 --- a/src/game-server/npc.cpp +++ b/src/game-server/npc.cpp @@ -94,6 +94,8 @@ void Npc::start(Entity *npc, Entity *ch) { Script::Thread *thread = script->newThread(); thread->getContext().map = npc->getMap(); + thread->getContext().npc = npc; + thread->getContext().character = ch; script->prepare(talkCallback); script->push(npc); script->push(ch); diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 2d9692b8..37d1832a 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -493,32 +493,34 @@ static int item_drop(lua_State *s) */ /** LUA npc_message (input) - * npc_message(handle npc, handle character, string message) + * npc_message(string message) ** * **Warning:** May only be called from an NPC talk function. * - * Shows an NPC dialog box on the screen of character ''ch'' displaying the - * string ''msg''. Idles the current thread until the user click "OK". + * Shows an NPC dialog box on the screen of displaying the string ''msg''. + * Idles the current thread until the user click "OK". */ static int npc_message(lua_State *s) { - Entity *npc = checkNpc(s, 1); - Entity *q = checkCharacter(s, 2); - const char *m = luaL_checkstring(s, 3); + const char *m = luaL_checkstring(s, 1); Script::Thread *thread = checkCurrentThread(s); + Entity *npc = thread->getContext().npc; + Entity *character = thread->getContext().character; + if (!(npc && character)) + luaL_error(s, "not in npc conversation"); MessageOut msg(GPMSG_NPC_MESSAGE); msg.writeInt16(npc->getComponent<ActorComponent>()->getPublicID()); msg.writeString(m); - gameHandler->sendTo(q, msg); + gameHandler->sendTo(character, msg); thread->mState = Script::ThreadPaused; return lua_yield(s, 0); } /** LUA npc_choice (input) - * npc_choice(handle npc, handle character, item1, item2, ... itemN) + * npc_choice(item1, item2, ... itemN) ** * **Return value:** Number of the option the player selected (starting with 1). * @@ -526,23 +528,24 @@ static int npc_message(lua_State *s) * * Shows an NPC dialog box on the users screen with a number of dialog options * to choose from. Idles the current thread until the user selects one or - * aborts the current thread when the user clicks "cancel". + * aborts the current thread when the user clicks "cancel". * * Items are either strings or tables of strings (indices are ignored, * but presumed to be taken in order). So, - * ''npc_choice(npc, ch, "A", {"B", "C", "D"}, "E")'' is the same as - * ''npc_choice(npc, ch, "A", "B", "C", "D", "E")''. + * ''npc_choice("A", {"B", "C", "D"}, "E")'' is the same as + * ''npc_choice("A", "B", "C", "D", "E")''. */ static int npc_choice(lua_State *s) { - Entity *npc = checkNpc(s, 1); - Entity *q = checkCharacter(s, 2); - Script::Thread *thread = checkCurrentThread(s); + Entity *npc = thread->getContext().npc; + Entity *character = thread->getContext().character; + if (!(npc && character)) + luaL_error(s, "not in npc conversation"); MessageOut msg(GPMSG_NPC_CHOICE); msg.writeInt16(npc->getComponent<ActorComponent>()->getPublicID()); - for (int i = 3, i_end = lua_gettop(s); i <= i_end; ++i) + for (int i = 1, i_end = lua_gettop(s); i <= i_end; ++i) { if (lua_isstring(s, i)) { @@ -572,14 +575,14 @@ static int npc_choice(lua_State *s) return 0; } } - gameHandler->sendTo(q, msg); + gameHandler->sendTo(character, msg); thread->mState = Script::ThreadExpectingNumber; return lua_yield(s, 0); } /** LUA npc_ask_integer (input) - * npc_ask_integer(handle npc, handle character, min_num, max_num, [default_num]) + * npc_ask_integer(min_num, max_num, [default_num]) ** * **Return value:** The number the player entered into the field. * @@ -591,27 +594,29 @@ static int npc_choice(lua_State *s) */ static int npc_ask_integer(lua_State *s) { - Entity *npc = checkNpc(s, 1); - Entity *q = checkCharacter(s, 2); - int min = luaL_checkint(s, 3); - int max = luaL_checkint(s, 4); - int defaultValue = luaL_optint(s, 5, min); + int min = luaL_checkint(s, 1); + int max = luaL_checkint(s, 2); + int defaultValue = luaL_optint(s, 3, min); Script::Thread *thread = checkCurrentThread(s); + Entity *npc = thread->getContext().npc; + Entity *character = thread->getContext().character; + if (!(npc && character)) + luaL_error(s, "not in npc conversation"); MessageOut msg(GPMSG_NPC_NUMBER); msg.writeInt16(npc->getComponent<ActorComponent>()->getPublicID()); msg.writeInt32(min); msg.writeInt32(max); msg.writeInt32(defaultValue); - gameHandler->sendTo(q, msg); + gameHandler->sendTo(character, msg); thread->mState = Script::ThreadExpectingNumber; return lua_yield(s, 0); } /** LUA npc_ask_string (input) - * npc_ask_string(handle npc, handle character) + * npc_ask_string() ** * **Return value:** The string the player entered. * @@ -621,32 +626,34 @@ static int npc_ask_integer(lua_State *s) */ static int npc_ask_string(lua_State *s) { - Entity *npc = checkNpc(s, 1); - Entity *q = checkCharacter(s, 2); - Script::Thread *thread = checkCurrentThread(s); + Entity *npc = thread->getContext().npc; + Entity *character = thread->getContext().character; + if (!(npc && character)) + luaL_error(s, "not in npc conversation"); MessageOut msg(GPMSG_NPC_STRING); msg.writeInt16(npc->getComponent<ActorComponent>()->getPublicID()); - gameHandler->sendTo(q, msg); + gameHandler->sendTo(character, msg); thread->mState = Script::ThreadExpectingString; return lua_yield(s, 0); } /** LUA npc_post (input) - * npc_post(handle npc, handle character) + * npc_post() ** * Starts retrieving post. Better not try to use it so far. */ static int npc_post(lua_State *s) { - Entity *npc = checkNpc(s, 1); - Entity *q = checkCharacter(s, 2); + const Script::Context *context = getScript(s)->getContext(); + Entity *npc = context->npc; + Entity *character = context->character; MessageOut msg(GPMSG_NPC_POST); msg.writeInt16(npc->getComponent<ActorComponent>()->getPublicID()); - gameHandler->sendTo(q, msg); + gameHandler->sendTo(character, msg); return 0; } @@ -704,19 +711,15 @@ static int announce(lua_State *s) */ /** LUA npc_trade (inventory) - * npc_trade(handle npc, - * handle character, - * bool mode, + * npc_trade(bool mode, * {int item1id, int item1amount, int item1cost}, ..., * {int itemNid, int itemNamount, int itemNcost}) - * npc_trade(handle npc, - * handle character, - * bool mode, + * npc_trade(bool mode, * {string item1name, int item1amount, int item1cost}, ..., * {string itemNname, int itemNamount, int itemNcost}) ** * FIXME: Move into a seperate file - * Opens a trade window for ''character'' while talking with ''npc''. ''mode'' + * Opens a trade window from an NPC conversation. ''mode'' * is true for selling and false for buying. You have to set each items the NPC * is buying/selling, the cost and the maximum amount in {}. * @@ -735,24 +738,24 @@ static int announce(lua_State *s) * **Examples:** * <code lua npc_trade.lua> * -- "A buy sample." - * local buycase = npc_trade(npc, ch, false, { + * local buycase = npc_trade(false, { * {"Sword", 10, 20}, * {"Bow", 10, 30}, * {"Dagger", 10, 50} * }) * if buycase == 0 then - * npc_message(npc, ch, "What do you want to buy?") + * npc_message("What do you want to buy?") * elseif buycase == 1 then - * npc_message(npc, ch, "I've got no items to sell.") + * npc_message("I've got no items to sell.") * else - * npc_message(npc, ch, "Hmm, something went wrong... Ask a scripter to + * npc_message("Hmm, something went wrong... Ask a scripter to * fix the buying mode!") * end * * -- ... * * -- "Example: Let the player sell only pre-determined items." - * local sellcase = npc_trade(npc, ch, true, { + * local sellcase = npc_trade(true, { * {"Sword", 10, 20}, * {"Bow", 10, 30}, * {"Dagger", 10, 200}, @@ -761,42 +764,40 @@ static int announce(lua_State *s) * {"Cactus Drink", 10, 25} * }) * if sellcase == 0 then - * npc_message(npc, ch, "Here we go:") + * npc_message("Here we go:") * elseif sellcase == 1 then - * npc_message(npc, ch, "I'm not interested by your items.") + * npc_message("I'm not interested by your items.") * else - * npc_message(npc, ch, "Hmm, something went wrong...") - * npc_message(npc, ch, "Ask a scripter to fix me!") + * npc_message("Hmm, something went wrong...") + * npc_message("Ask a scripter to fix me!") * end * * -- ... * * -- "Example: Let the player sell every item with a 'value' parameter in * the server's items.xml file - * local sellcase = npc_trade(npc, ch, true) + * local sellcase = npc_trade(true) * if sellcase == 0 then - * npc_message(npc, ch, "Ok, what do you want to sell:") + * npc_message("Ok, what do you want to sell:") * elseif sellcase == 1 then - * npc_message(npc, ch, "I'm not interested by any of your items.") + * npc_message("I'm not interested by any of your items.") * else - * npc_message(npc, ch, "Hmm, something went wrong...") - * npc_message(npc, ch, "Ask a scripter to fix me!") + * npc_message("Hmm, something went wrong...") + * npc_message("Ask a scripter to fix me!") * end * </code> */ static int npc_trade(lua_State *s) { - Entity *npc = checkNpc(s, 1); - Entity *q = checkCharacter(s, 2); - if (!lua_isboolean(s, 3)) - { - luaL_error(s, "npc_trade called with incorrect parameters."); - return 0; - } + const Script::Context *context = getScript(s)->getContext(); + Entity *npc = context->npc; + Entity *character = context->character; + + luaL_argcheck(s, lua_isboolean(s, 1), 1, "boolean expected"); + bool sellMode = lua_toboolean(s, 1); - bool sellMode = lua_toboolean(s, 3); - BuySell *t = new BuySell(q, sellMode); - if (!lua_istable(s, 4)) + BuySell *t = new BuySell(character, sellMode); + if (!lua_istable(s, 2)) { if (sellMode) { @@ -833,7 +834,7 @@ static int npc_trade(lua_State *s) int nbItems = 0; lua_pushnil(s); - while (lua_next(s, 4)) + while (lua_next(s, 2)) { if (!lua_istable(s, -1)) { diff --git a/src/scripting/script.h b/src/scripting/script.h index 694deed6..7cf457cc 100644 --- a/src/scripting/script.h +++ b/src/scripting/script.h @@ -43,9 +43,13 @@ class Script : public sigc::trackable struct Context { MapComposite *map; + Entity *npc; + Entity *character; Context() : map(0) + , npc(0) + , character(0) {} }; |