Facebook
From Mature Hog, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 375
  1. int NpcScript::luaBuyItem(lua_State *L)
  2. {
  3.         int cost = (int)lua_tonumber(L, -1);
  4.         int count = (int)lua_tonumber(L, -2);
  5.         int itemid = (int)lua_tonumber(L, -3);
  6.         int cid = (int)lua_tonumber(L, -4);
  7.         lua_pop(L,4);
  8.  
  9.         Npc* mynpc = getNpc(L);
  10.         Creature* creature = mynpc->game->getCreatureByID(cid);
  11.         Player* player = creature? dynamic_cast<Player*>(creature) : NULL;
  12.  
  13.         if (player)
  14.         {
  15.                 if (player->getCoins(cost))
  16.                 {
  17.                         if (player->removeCoins(cost)) // double check
  18.                         {
  19.                                 player->TLMaddItem(itemid, count);
  20.                                 mynpc->doSay("Here you are.");
  21.                         }
  22.                         else
  23.                                 mynpc->doSay("Sorry, you do not have enough money.");
  24.                 }
  25.                 else
  26.                         mynpc->doSay("Sorry, you do not have enough money.");
  27.         }
  28.  
  29.         return 0;
  30. }
  31.  
  32. int NpcScript::luaSellItem(lua_State *L)
  33. {
  34.    int cost = (int)lua_tonumber(L, -1);
  35.    int count = (int)lua_tonumber(L, -2);
  36.    int itemid = (int)lua_tonumber(L, -3);
  37.    int cid = (int)lua_tonumber(L, -4);
  38.    lua_pop(L,4);
  39.  
  40.    Npc* mynpc = getNpc(L);
  41.    Creature* creature = mynpc->game->getCreatureByID(cid);
  42.    Player* player = creature? dynamic_cast<Player*>(creature) : NULL;
  43.  
  44.         if (player)
  45.         {
  46.                 if (player->getItem(itemid, count))
  47.                 {
  48.                         if (player->removeItem(itemid, count)) // double check
  49.                         {
  50.                                 player->payBack(cost);
  51.                                 mynpc->doSay("Thanks for this item!");
  52.                         }
  53.                         else
  54.                                 mynpc->doSay("Sorry, you do not have that item.");
  55.                 }
  56.                 else
  57.                         mynpc->doSay("Sorry, you do not have that item.");
  58.         }
  59.  
  60.         return 0;
  61. }

Replies to Untitled rss

Title Name Language When
Re: Untitled Abrupt Marmoset cpp 7 Years ago.