Facebook
From Abrupt Marmoset, 7 Years ago, written in C++.
This paste is a reply to Untitled from Mature Hog - view diff
Embed
Download Paste or View Raw
Hits: 413
  1. #ifdef __MIZIAK_ADDS__
  2. int NpcScript::luaBuyItem(lua_State *L)
  3. {
  4.         int cost = (int)lua_tonumber(L, -1);
  5.         int count = (int)lua_tonumber(L, -2);
  6.         int itemid = (int)lua_tonumber(L, -3);
  7.         int cid = (int)lua_tonumber(L, -4);
  8.         lua_pop(L,4);
  9.         std::stringstream sss;
  10.  
  11.         Npc* mynpc = getNpc(L);
  12.         Creature* creature = mynpc->game->getCreatureByID(cid);
  13.         Player* player = creature? dynamic_cast<Player*>(creature) : NULL;
  14.  
  15.         if (player)
  16.         {
  17.                 if (player->getCoins(cost))
  18.                 {
  19.                         if (player->removeCoins(cost)) // double check
  20.                         {
  21.                 sss << "/privMsg " << player->getName() << ",Here you are.";
  22.                                 player->TLMaddItem(itemid, count);
  23.                                 mynpc->doSay(sss.str().c_str());
  24.                         }
  25.                         else {
  26.                 sss << "/privMsg " << player->getName() << ",Sorry, you do not have enough money.";
  27.                                 mynpc->doSay(sss.str().c_str());
  28.             }
  29.                 }
  30.                 else {
  31.             sss << "/privMsg " << player->getName() << ",Sorry, you do not have enough money.";
  32.                         mynpc->doSay(sss.str().c_str());
  33.         }
  34.         }
  35.  
  36.         return 0;
  37. }
  38.  
  39. int NpcScript::luaSellItem(lua_State *L)
  40. {
  41.    int cost = (int)lua_tonumber(L, -1);
  42.    int count = (int)lua_tonumber(L, -2);
  43.    int itemid = (int)lua_tonumber(L, -3);
  44.    int cid = (int)lua_tonumber(L, -4);
  45.    lua_pop(L,4);
  46.    std::stringstream sss;
  47.  
  48.    Npc* mynpc = getNpc(L);
  49.    Creature* creature = mynpc->game->getCreatureByID(cid);
  50.    Player* player = creature? dynamic_cast<Player*>(creature) : NULL;
  51.  
  52.         if (player)
  53.         {
  54.                 if (player->getItem(itemid, count))
  55.                 {
  56.                         if (player->removeItem(itemid, count)) // double check
  57.                         {
  58.                 sss << "/privMsg " << player->getName() << ",Thanks for this item!";
  59.                 player->payBack(cost);
  60.                             mynpc->doSay(sss.str().c_str());   
  61.                         }
  62.                         else {
  63.                 sss << "/privMsg " << player->getName() << ",Sorry, you do not have that item.";
  64.                                 mynpc->doSay(sss.str().c_str());
  65.             }
  66.                 }
  67.                 else {
  68.             sss << "/privMsg " << player->getName() << ",Sorry, you do not have that item.";
  69.                         mynpc->doSay(sss.str().c_str());
  70.         }
  71.         }
  72.  
  73.         return 0;
  74. }
  75. #endif