int NpcScript::luaBuyItem(lua_State *L) { int cost = (int)lua_tonumber(L, -1); int count = (int)lua_tonumber(L, -2); int itemid = (int)lua_tonumber(L, -3); int cid = (int)lua_tonumber(L, -4); lua_pop(L,4); Npc* mynpc = getNpc(L); Creature* creature = mynpc->game->getCreatureByID(cid); Player* player = creature? dynamic_cast(creature) : NULL; if (player) { if (player->getCoins(cost)) { if (player->removeCoins(cost)) // double check { player->TLMaddItem(itemid, count); mynpc->doSay("Here you are."); } else mynpc->doSay("Sorry, you do not have enough money."); } else mynpc->doSay("Sorry, you do not have enough money."); } return 0; } int NpcScript::luaSellItem(lua_State *L) { int cost = (int)lua_tonumber(L, -1); int count = (int)lua_tonumber(L, -2); int itemid = (int)lua_tonumber(L, -3); int cid = (int)lua_tonumber(L, -4); lua_pop(L,4); Npc* mynpc = getNpc(L); Creature* creature = mynpc->game->getCreatureByID(cid); Player* player = creature? dynamic_cast(creature) : NULL; if (player) { if (player->getItem(itemid, count)) { if (player->removeItem(itemid, count)) // double check { player->payBack(cost); mynpc->doSay("Thanks for this item!"); } else mynpc->doSay("Sorry, you do not have that item."); } else mynpc->doSay("Sorry, you do not have that item."); } return 0; }