Trying to modify for trade on a NPC

Post Reply
KOSMOS
Posts: 67
Joined: Thu Jan 29, 2015 10:36 pm

Trying to modify for trade on a NPC

Post by KOSMOS » Sun Apr 10, 2016 7:14 pm

Anyone able to point out what I am doing wrong?

I have tried prob 30+ different ways to get this npc lua to take a trade and give back an item. I know I have to be close but I am missing something. It does not throw any errors on the game server. Though it also does not do anything either. I have looked at the guide on wiki, maat.lua and the question marks in sky to get ideas. If anyone could help me figure out what I am doing wrong I would greatly appreciate it.

Thanks,

Code: Select all

-----------------------------------
--    Area: Ru'Lud Gardens
--    NPC:  Baran
--    Standard Info NPC
-----------------------------------
package.loaded["scripts/zones/RuLude_Gardens/TextIDs"] = nil;
require("scripts/globals/settings");
require("scripts/globals/titles");
require("scripts/globals/keyitems");
require("scripts/globals/quests");
require("scripts/zones/RuLude_Gardens/TextIDs");
-----------------------------------
-- onTrade Action
-----------------------------------

function onTrade(player,npc,trade)
local tradeCount = trade:getItemCount();
----------------------------------
--Empyrean Weapon Upgrades
----------------------------------
--Redemption 90->95
if (player:getFreeSlotsCount() > 0 and trade:hasItemQty(3509,99) and trade:hasItemQty(19540,1) and tradeCount == 2) then
	player:tradeComplete();
	player:addItem(19638);
	player:messageSpecial(ITEM_OBTAINED,19638);
end 
end;
-------------------------
-- onTrigger Action
-----------------------------------

function onTrigger(player,npc)
    player:startEvent(0x0096);
end;

-----------------------------------
-- onEventUpdate
-----------------------------------

function onEventUpdate(player,csid,option)
--printf("CSID: %u",csid);
--printf("RESULT: %u",option);
end;

-----------------------------------
-- onEventFinish
-----------------------------------

function onEventFinish(player,csid,option)
--printf("CSID: %u",csid);
--printf("RESULT: %u",option);
end;




KOSMOS
Posts: 67
Joined: Thu Jan 29, 2015 10:36 pm

Re: Trying to modify for trade on a NPC

Post by KOSMOS » Tue Apr 12, 2016 9:00 pm

OK so I can get this to work just fine for one item.

Code: Select all

local tradeCount = trade:getItemCount();

if (trade:hasItemQty(3509,99) and trade:hasItemQty(19540,1) and tradeCount == 2) then

end
if (player:getFreeSlotsCount() >= 1) then
                player:tradeComplete();
                player:addItem(19638);
                player:messageSpecial(ITEM_OBTAINED,19638);
end
end;
But I am having issues setting it up for multiple items, I know it is my lack of knowledge of how to use variables correctly in lua. I feel I have to be close but I do not get how to call variables out side of an if statement.

Code: Select all

function onTrade(player,npc,trade)
local tradeCount = trade:getItemCount();
local a = 0;
if (trade:hasItemQty(3509,99) and trade:hasItemQty(19540,1) and tradeCount == 2) then
a = 19638
elseif (trade:hasItemQty(3509,99) and trade:hasItemQty(19539,1) and tradeCount == 2)     then
a = 19637
end
if (player:getFreeSlotsCount() >= 1) then
		player:tradeComplete();
		player:PrintToPlayer(a);
		player:addItem(a);
		player:messageSpecial(ITEM_OBTAINED,a);
end
end;
Is what I have though it only spits out a as 0. Any help would be greatly appreciated and amazing.

Thanks

Avatarati
Posts: 53
Joined: Mon Jun 30, 2014 2:51 pm

Re: Trying to modify for trade on a NPC

Post by Avatarati » Wed Apr 13, 2016 1:25 am

You have three Boolean conditions in your if statement: 1) Does the player have 99 Heavy Metals? 2) Does the player have one Redemption? 3) Has the player given me 2 items total?

The third Boolean is false because you are actually trading 100 items; therefore, the player has not met the conditions of the if statement.

In your working example, the first if statement, whether conditions are met or not, has no functionality; so it is simply passed over in any case and the next argument is evaluated. Does the player have a free slot in their inventory? If yes, then give this item. This kind of if statement will accept any item as a trade as long as there is space in the player's inventory.

Code: Select all

local tradeCount = trade:getItemCount();

if (trade:hasItemQty(3509,99) and trade:hasItemQty(19540,1) and tradeCount == 2) then

end
if (player:getFreeSlotsCount() >= 1) then
                player:tradeComplete();
                player:addItem(19638);
                player:messageSpecial(ITEM_OBTAINED,19638);
end
As a note, there are several different ways of evaluating traded items. Check out the lua_trade_container.h header file and you'll see all of the options:
  • getGil
    getItem
    getItemSubId
    getItemQty
    hasItemQty
    getSlotQty
    getItemCount
    getSlotCount
    confirmItem
And opening the corresponding lua_trade_container.cpp will let you see how each one functions.

KOSMOS
Posts: 67
Joined: Thu Jan 29, 2015 10:36 pm

Re: Trying to modify for trade on a NPC

Post by KOSMOS » Wed Apr 13, 2016 3:56 am

Avatarati wrote:You have three Boolean conditions in your if statement: 1) Does the player have 99 Heavy Metals? 2) Does the player have one Redemption? 3) Has the player given me 2 items total?

The third Boolean is false because you are actually trading 100 items; therefore, the player has not met the conditions of the if statement.

In your working example, the first if statement, whether conditions are met or not, has no functionality; so it is simply passed over in any case and the next argument is evaluated. Does the player have a free slot in their inventory? If yes, then give this item. This kind of if statement will accept any item as a trade as long as there is space in the player's inventory.

Code: Select all

local tradeCount = trade:getItemCount();

if (trade:hasItemQty(3509,99) and trade:hasItemQty(19540,1) and tradeCount == 2) then

end
if (player:getFreeSlotsCount() >= 1) then
                player:tradeComplete();
                player:addItem(19638);
                player:messageSpecial(ITEM_OBTAINED,19638);
end
As a note, there are several different ways of evaluating traded items. Check out the lua_trade_container.h header file and you'll see all of the options:
  • getGil
    getItem
    getItemSubId
    getItemQty
    hasItemQty
    getSlotQty
    getItemCount
    getSlotCount
    confirmItem
And opening the corresponding lua_trade_container.cpp will let you see how each one functions.
Awesome thank you,

I guess I was not really thinking about that fact of the actual item count being 100 and it was just passing that over. Makes sense why when I started to add variables it would not add them.

Also thanks for the tips I will look at that file and see all of the different options on how to use these.

KOSMOS
Posts: 67
Joined: Thu Jan 29, 2015 10:36 pm

Re: Trying to modify for trade on a NPC

Post by KOSMOS » Wed Apr 13, 2016 4:26 am

Thanks again Avatarati,

Ok so I was able to get this working with multiple weapons. I plan to add much more too it but wanted to get this up if anyone else was wondering how this works or to see a finished functional working copy of the file.

Code: Select all

-----------------------------------
--    Area: Ru'Lud Gardens
--    NPC:  Baran
--    Standard Info NPC
-----------------------------------
package.loaded["scripts/zones/RuLude_Gardens/TextIDs"] = nil;
require("scripts/globals/settings");
require("scripts/globals/titles");
require("scripts/globals/keyitems");
require("scripts/globals/quests");
require("scripts/zones/RuLude_Gardens/TextIDs");
------------------------
--Globals
-----------------------

-----------------------------------
-- onTrade Action
-----------------------------------

--------------------------------------------------
--------------------------------------------------
-- Weapon Trials
--------------------------------------------------
-------------------------------------------------

function onTrade(player,npc,trade)
local a = 0;
-------------------------------------------------
-- Empyrean Weapon Trials
-------------------------------------------------
if (trade:hasItemQty(19540,1) and trade:hasItemQty(3509,99) and trade:getItemCount(100))then --Redemption 90->95
a = 19638
elseif (trade:hasItemQty(19539,1) and trade:hasItemQty(3509,99) and trade:getItemCount(100))then --Ukonvasara 90->95
a = 19637
end
if (player:getFreeSlotsCount() >= 1) then
		player:tradeComplete();
		player:addItem(a);
		player:messageSpecial(ITEM_OBTAINED,a);

end
end;
-------------------------
-- onTrigger Action
-----------------------------------

function onTrigger(player,npc)
--    player:startEvent(0x0096);
 player:PrintToPlayer("Baran : Trade me your Weapons if you dare........ Now accepting Empyrean Weapons!", 0xD); 
 end;

-----------------------------------
-- onEventUpdate
-----------------------------------

function onEventUpdate(player,csid,option)
--printf("CSID: %u",csid);
--printf("RESULT: %u",option);
end;

-----------------------------------
-- onEventFinish
-----------------------------------

function onEventFinish(player,csid,option)
--printf("CSID: %u",csid);
--printf("RESULT: %u",option);
end;




Avatarati
Posts: 53
Joined: Mon Jun 30, 2014 2:51 pm

Re: Trying to modify for trade on a NPC

Post by Avatarati » Wed Apr 13, 2016 5:38 am

You're on the right track now :) Still be careful with this though:

Code: Select all

function onTrade(player,npc,trade)
local a = 0;

if (player:getFreeSlotsCount() >= 1) then
      player:tradeComplete();
      player:addItem(a);
      player:messageSpecial(ITEM_OBTAINED,a);

end
end;
If the player trades something other than a Redemption or Ukonvasara it will delete the item from the player's inventory with the way it is.

Checking to see if 'a' changed is a simple way to prevent that from happening:

Code: Select all

function onTrade(player,npc,trade)

	local freeSlot = player:getFreeSlotsCount()
	local count = trade:getItemCount()
	local a = 0
	
	if (trade:hasItemQty(19540,1) and trade:hasItemQty(3509,99) and count == 100) then --Redemption 90->95
		a = 19638
	elseif (trade:hasItemQty(19539,1) and trade:hasItemQty(3509,99) and count == 100) then --Ukonvasara 90->95
		a = 19637
	end
	
	if (a > 0 and freeSlot > 0) then
		  player:tradeComplete()
		  player:addItem(a)
		  player:messageSpecial(ITEM_OBTAINED,a)
	else
		player:PrintToPlayer("Baran : I\'m sorry, but I cannot accept that item. Please try again.", 0xD)
	end
	
end;

KOSMOS
Posts: 67
Joined: Thu Jan 29, 2015 10:36 pm

Re: Trying to modify for trade on a NPC

Post by KOSMOS » Thu Apr 14, 2016 7:46 am

Oh nice thank you. That would of been bad, now I know how this happened in a retail a few times when new updates would happen.

Here is the code with the else catch for wrong items. It is tested and works for both the weapons and also if you trade wrong items.

Code: Select all

-----------------------------------
--    Area: Ru'Lud Gardens
--    NPC:  Baran
--    Standard Info NPC
-----------------------------------
package.loaded["scripts/zones/RuLude_Gardens/TextIDs"] = nil;
require("scripts/globals/settings");
require("scripts/globals/titles");
require("scripts/globals/keyitems");
require("scripts/globals/quests");
require("scripts/zones/RuLude_Gardens/TextIDs");
------------------------
--Globals
-----------------------

-----------------------------------
-- onTrade Action
-----------------------------------

--------------------------------------------------
--------------------------------------------------
-- Weapon Trials
--------------------------------------------------
-------------------------------------------------

function onTrade(player,npc,trade)
local a = 0;

-------------------------------------------------
-- Empyrean Weapon Trials
-------------------------------------------------
if (trade:hasItemQty(19540,1) and trade:hasItemQty(3509,99) and trade:getItemCount(100)) then --Redemption 90->95
a = 19638
elseif (trade:hasItemQty(19539,1) and trade:hasItemQty(3509,99) and trade:getItemCount(100)) then --Ukonvasara 90->95
a = 19637
end
if (a > 0 and player:getFreeSlotsCount() >= 1) then
		player:tradeComplete();
		player:addItem(a);
		player:messageSpecial(ITEM_OBTAINED,a);
else 
player:PrintToPlayer("Baran : I\'m sorry, but I cannot accept that item. Please try again.", 0xD)
end
end;
-------------------------
-- onTrigger Action
-----------------------------------

function onTrigger(player,npc)
--    player:startEvent(0x0096);
 player:PrintToPlayer("Baran : Trade me your Weapons if you dare........ Now accepting Empyrean Weapons!", 0xD); 
 end;

-----------------------------------
-- onEventUpdate
-----------------------------------

function onEventUpdate(player,csid,option)
--printf("CSID: %u",csid);
--printf("RESULT: %u",option);
end;

-----------------------------------
-- onEventFinish
-----------------------------------

function onEventFinish(player,csid,option)
--printf("CSID: %u",csid);
--printf("RESULT: %u",option);
end;

User avatar
TeoTwawki
Developer
Posts: 527
Joined: Mon Jul 15, 2013 9:50 pm

Re: Trying to modify for trade on a NPC

Post by TeoTwawki » Fri Apr 15, 2016 8:06 pm

No offense intended but you look like you need this viewtopic.php?f=19&t=2721&p=14914#p14914
Hi, I run The Demiurge server.


Image
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. - Martin Golding
PLS USE [ code ] CODE TAGS [ /code ] WHEN POSTING CODE
DO NOT PRIVATE MESSAGE ME ABOUT BUGS

KOSMOS
Posts: 67
Joined: Thu Jan 29, 2015 10:36 pm

Re: Trying to modify for trade on a NPC

Post by KOSMOS » Fri Apr 15, 2016 9:07 pm

TeoTwawki wrote:No offense intended but you look like you need this viewtopic.php?f=19&t=2721&p=14914#p14914
Kool thank you, and no offense taken.
I will read up on this, I 100% realize I am new to this and can take criticism and learn from it.
I will fix those other files to the proper way and get the pull request file change on github.

Post Reply