local tbNpc = Npc:GetClass("changeNgua");
local IdVatPham = {
[1] = {
[1]=string.format("%s,%s,%s,%s", 18, 1, 3024, 1),
[2]="Tiền Xu",
},
[2] = {
[1]=string.format("%s,%s,%s,%s", 18, 1, 1192, 1),
[2]="Mảnh Ghép Ngựa",
},
[3] = {
[1]=string.format("%s,%s,%s,%s", 18, 1, 3028, 3),
[2]="Đầu Ngựa",
},
[4] = {
[1]=string.format("%s,%s,%s,%s", 18, 1, 3028, 1),
[2]="Thần Thú Kinh Nghiệm Đơn",
},
[5] = {
[1]=string.format("%s,%s,%s,%s", 18, 1, 3028, 2),
[2]="Căn Cốt Đơn",
},
};
local VatPhamCanThiet = {
[1]= {
[1]=0,
[2]=0,
[3]=0,
[4]=0,
[5]=0,
[6]={
[1]=string.format("%s,%s,%s,%s", 18, 1, 3030, 1),
},
},
[2]= {
[1]=50,
[2]=500,
[3]=1,
[4]=200,
[5]=200,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 70, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 71, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 72, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 73, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 74, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 75, 4),
},
},
[3]= {
[1]=50,
[2]=600,
[3]=1,
[4]=250,
[5]=250,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 76, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 77, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 78, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 79, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 80, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 81, 4),
},
},
[4]= {
[1]=50,
[2]=700,
[3]=1,
[4]=250,
[5]=250,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 82, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 83, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 84, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 85, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 86, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 87, 4),
},
},
[5]= {
[1]=100,
[2]=800,
[3]=2,
[4]=300,
[5]=300,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 88, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 89, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 90, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 91, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 92, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 93, 4),
},
},
[6]= {
[1]=100,
[2]=1000,
[3]=2,
[4]=300,
[5]=300,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 94, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 95, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 96, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 97, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 98, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 99, 4),
},
},
[7]= {
[1]=100,
[2]=1200,
[3]=2,
[4]=350,
[5]=350,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 100, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 101, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 102, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 103, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 104, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 105, 4),
},
},
[8]= {
[1]=150,
[2]=1400,
[3]=2,
[4]=400,
[5]=400,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 106, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 107, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 108, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 109, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 110, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 111, 4),
},
},
[9]= {
[1]=150,
[2]=1600,
[3]=2,
[4]=450,
[5]=450,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 112, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 113, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 114, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 115, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 116, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 117, 4),
},
},
[10]= {
[1]=200,
[2]=2000,
[3]=4,
[4]=500,
[5]=500,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 118, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 119, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 120, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 121, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 122, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 123, 4),
[7]=string.format("%s,%s,%s,%s", 1, 12, 124, 4),
[8]=string.format("%s,%s,%s,%s", 1, 12, 125, 4),
},
},
[11]= {
[1]=200,
[2]=3000,
[3]=4,
[4]=1000,
[5]=1000,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 126, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 127, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 128, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 129, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 130, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 131, 4),
[7]=string.format("%s,%s,%s,%s", 1, 12, 132, 4),
[8]=string.format("%s,%s,%s,%s", 1, 12, 133, 4),
},
},
[12]= {
[1]=200,
[2]=3000,
[3]=5,
[4]=1500,
[5]=1500,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 134, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 135, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 136, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 137, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 138, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 139, 4),
[7]=string.format("%s,%s,%s,%s", 1, 12, 140, 4),
[8]=string.format("%s,%s,%s,%s", 1, 12, 141, 4),
},
},
[13]= {
[1]=200,
[2]=4000,
[3]=6,
[4]=2000,
[5]=2000,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 142, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 143, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 144, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 145, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 146, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 147, 4),
[7]=string.format("%s,%s,%s,%s", 1, 12, 148, 4),
[8]=string.format("%s,%s,%s,%s", 1, 12, 149, 4),
},
},
[14]= {
[1]=300,
[2]=6000,
[3]=8,
[4]=2500,
[5]=2500,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 150, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 151, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 152, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 153, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 154, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 155, 4),
[7]=string.format("%s,%s,%s,%s", 1, 12, 156, 4),
[8]=string.format("%s,%s,%s,%s", 1, 12, 157, 4),
},
},
[15]= {
[1]=500,
[2]=10000,
[3]=10,
[4]=4000,
[5]=4000,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 158, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 159, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 160, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 161, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 162, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 163, 4),
[7]=string.format("%s,%s,%s,%s", 1, 12, 164, 4),
[8]=string.format("%s,%s,%s,%s", 1, 12, 165, 4),
},
},
[16]= {
[1]=0,
[2]=0,
[3]=0,
[4]=0,
[5]=0,
[6]={
[1]=string.format("%s,%s,%s,%s", 1, 12, 166, 4),
[2]=string.format("%s,%s,%s,%s", 1, 12, 167, 4),
[3]=string.format("%s,%s,%s,%s", 1, 12, 168, 4),
[4]=string.format("%s,%s,%s,%s", 1, 12, 169, 4),
[5]=string.format("%s,%s,%s,%s", 1, 12, 170, 4),
[6]=string.format("%s,%s,%s,%s", 1, 12, 171, 4),
[7]=string.format("%s,%s,%s,%s", 1, 12, 172, 4),
[8]=string.format("%s,%s,%s,%s", 1, 12, 173, 4),
},
},
};


function tbNpc:OnDialog_1()
local szMsg = "Ta sẽ giúp nhà ngươi ghép Ngựa.";
szMsg = "Vui lòng cho tất cả Vật phẩm cường hóa vào.";
Dialog:OpenGift(szMsg, nil, {self.CheckItem, self});
end

function tbNpc:CheckItem(tbItemObj)
local DayDu = 0;
local szMsg = 'Bạn còn thiếu: \n\n';
local isVatPhamNgua_all = self:CheckItemNgua(tbItemObj, VatPhamCanThiet);
local isVatPhamNgua = tonumber(isVatPhamNgua_all[1]);
local isVatPhamNgua_y = tonumber(isVatPhamNgua_all[2]);
-- kiểm tra có vật phẩm cường hóa ngựa không
if (isVatPhamNgua == 0) then
me.Msg("Không có vật phẩm cường hóa Ngựa.");
Dialog:Say("Vui lòng cho vật phẩm cường hóa Ngựa vào để cường hóa.");
return 0;
elseif isVatPhamNgua == 16 then
me.Msg("Ngựa chưa mở cấp tiếp theo.");
Dialog:Say("Ngựa chưa mở cấp tiếp theo.");
return 0;
else
-- check is Rương
local nCount1 = me.GetItemCountInBags(self:GetItem(VatPhamCanThiet[isVatPhamNgua][6][isVatPhamNgua_y]));
if nCount1 == 0 then
Dialog:Say("Bạn phải đặt vật phẩm cường hóa trong rương");
return 0;
end

local HoanThanh = 0;
for i=1,5 do
local isItem = self:CheckItemCH(tbItemObj, IdVatPham, VatPhamCanThiet, isVatPhamNgua, i);
if isItem >= 1 then
szMsg = szMsg.." \t "..tostring(isItem).." "..IdVatPham[i][2]..".\n";
else
HoanThanh = HoanThanh +1;
end
end

if HoanThanh < 5 then
Dialog:Say(szMsg);
me.Msg("Cường hóa Ngựa thất bại do thiếu Vật Phẩm.");
else -- đầy đủ cường hóa
--- thêm vật phẩm mới vào
selfialog_showItem(VatPhamCanThiet[isVatPhamNgua +1][6], tbItemObj, isVatPhamNgua_all);
end
end
-- kiểm tra các vật phẩm khác
end;

function tbNpc:GetItem(szText)
local rows = {};

local itsat = 0;
local tmp = szText;
while itsat ~= nil do
itsat = string.find(tmp,",",1);
if itsat ~= nil then
rows[#rows+1] = string.sub(tmp,1,itsat-1);
tmp = string.sub(tmp,itsat+1);
else
rows[#rows+1] = tmp;
end
end
local returnItem = {tonumber(rows[1]), tonumber(rows[2]), tonumber(rows[3]), tonumber(rows[4])};
return unpack(returnItem);
end


function tbNpcialog_showItem(iItem, tbItemObj, isVatPhamNgua)
-- local tbOpt = {
-- {"<pic=135> <color=green>"..KItem.GetNameById(self:GetItem(iIt em[1]))..".<color>", self.AddNgua, self, iItem[1], tbItemObj, isVatPhamNgua},--
-- {"<pic=135> <color=green>"..KItem.GetNameById(self:GetItem(iIt em[2]))..".<color>", self.AddNgua, self, iItem[2], tbItemObj, isVatPhamNgua},--
-- {"<pic=135> <color=green>"..KItem.GetNameById(self:GetItem(iIt em[3]))..".<color>", self.AddNgua, self, iItem[3], tbItemObj, isVatPhamNgua},--
-- {"<pic=135> <color=green>"..KItem.GetNameById(self:GetItem(iIt em[4]))..".<color>", self.AddNgua, self, iItem[4], tbItemObj, isVatPhamNgua},--
-- {"<pic=135> <color=green>"..KItem.GetNameById(self:GetItem(iIt em[5]))..".<color>", self.AddNgua, self, iItem[5], tbItemObj, isVatPhamNgua},--
-- {"<pic=135> <color=green>"..KItem.GetNameById(self:GetItem(iIt em[6]))..".<color>", self.AddNgua, self, iItem[6], tbItemObj, isVatPhamNgua},--
-- {"Kết thúc đối thoại"},
-- }
-- local szMsg = "Chọn Ngựa mà bạn muốn lấy.";
-- Dialog:Say(szMsg, tbOpt);
local tbOpt = {};
for i=1,#iItem do
local name = KItem.GetNameById(self:GetItem(iItem[i]));
table.insert(tbOpt , {"<pic=135> <color=green>"..name..".<color>", self.AddNgua, self, iItem[i], tbItemObj, isVatPhamNgua});
end
table.insert(tbOpt , {"Kết thúc đối thoại"});
local szMsg = "Chọn Ngựa mà bạn muốn lấy.";
Dialog:Say(szMsg, tbOpt);
end

function tbNpc:AddNgua(index, tbItemObj, isVatPhamNgua)
me.AddItem(self:GetItem(index));
local szMsg = "Người chơi <color=yellow>"..me.szName.."<color> đã nâng cấp thành công <color=green>"..KItem.GetNameById(self:GetItem(ind ex))..".<color>";
KDialog.NewsMsg(0, Env.NEWSMSG_COUNT, szMsg);
KDialog.MsgToGlobal(szMsg);
selfeleteItem(isVatPhamNgua);
end

function tbNpceleteItem(isVatPhamNgua)
-- xóa vật phẩm
local x = tonumber(isVatPhamNgua[1]);
local y = tonumber(isVatPhamNgua[2]);
for i=1,5 do
me.ConsumeItemInBags(VatPhamCanThiet[x][i], self:GetItem(IdVatPham[i][1]));
end
-- xóa ngựa
me.ConsumeItemInBags(1, self:GetItem(VatPhamCanThiet[x][6][y]));
end

function tbNpc:CheckItemCH(tbItemObj, tbItemCH, tbItemList, isNgua, isCH)
local numItem = 0;
for _, pItem in pairs(tbItemObj) do
local szItem = string.format("%s,%s,%s,%s",pItem[1].nGenre, pItem[1].nDetail, pItem[1].nParticular, pItem[1].nLevel);
if (szItem == tbItemCH[isCH][1]) then
numItem = pItem[1].nCount + numItem;
end
end
if (numItem >= tbItemList[isNgua][isCH]) then
return 0;
else
return (tbItemList[isNgua][isCH] - numItem);
end
end

-- kiem tra item manh ghep ngua
function tbNpc:CheckItemNgua(tbItemObj, tbItemList)
local ThuTu = {};
if (not pItem) then
ThuTu[1] = 0;
end
for _, pItem in pairs(tbItemObj) do
local szItem = string.format("%s,%s,%s,%s",pItem[1].nGenre, pItem[1].nDetail, pItem[1].nParticular, pItem[1].nLevel);
for i = 1, #tbItemList do
local tbI = tbItemList[i][6];
for j = 1, #tbI do
local szItemTmp = tbItemList[i][6][j];
if szItem == szItemTmp then
ThuTu[1] = i;
ThuTu[2] = j;
end
end;
end;
end;
return ThuTu;
end;