ManaPlus
barterhandler.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include "net/eathena/barterrecv.h"
25 #include "net/eathena/messageout.h"
27 
28 #include "utils/foreach.h"
29 
31 
32 #include "debug.h"
33 
34 extern int packetVersionMain;
35 extern int packetVersionRe;
36 extern int packetVersionZero;
37 extern int itemIdLen;
38 
39 namespace EAthena
40 {
41 
44 {
45  barterHandler = this;
46  BarterRecv::mBuyDialog = nullptr;
47 }
48 
50 {
51  barterHandler = nullptr;
52 }
53 
55 {
56  if (packetVersionMain < 20190116 &&
57  packetVersionRe < 20190116 &&
58  packetVersionZero < 20181226)
59  {
60  return;
61  }
62 
63  createOutPacket(CMSG_NPC_BARTER_CLOSE);
64 }
65 
66 void BarterHandler::buyItems(const STD_VECTOR<ShopItem*> &items) const
67 {
68  if (packetVersionMain < 20190116 &&
69  packetVersionRe < 20190116 &&
70  packetVersionZero < 20181226)
71  {
72  return;
73  }
74  int cnt = 0;
75  const int pairSize = 10 + itemIdLen;
76 
77  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
78  {
79  const ShopItem *const item = *it;
80  const int usedQuantity = item->getUsedQuantity();
81  const ItemTypeT type = item->getType();
82  if (usedQuantity == 0)
83  continue;
84  if (type == ItemType::Weapon ||
85  type == ItemType::Armor ||
86  type == ItemType::PetEgg ||
87  type == ItemType::PetArmor)
88  {
89  cnt += item->getUsedQuantity();
90  }
91  else
92  {
93  cnt ++;
94  }
95  }
96 
97  if (cnt > 100)
98  return;
99 
100  createOutPacket(CMSG_NPC_MARKET_BUY);
101  outMsg.writeInt16(CAST_S16(4 + pairSize * cnt), "len");
102  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
103  {
104  ShopItem *const item = *it;
105  const int usedQuantity = item->getUsedQuantity();
106  if (usedQuantity == 0)
107  continue;
108  item->increaseQuantity(usedQuantity);
109  item->increaseUsedQuantity(-usedQuantity);
110  item->update();
111  const ItemTypeT type = item->getType();
112  if (type == ItemType::Weapon ||
113  type == ItemType::Armor ||
114  type == ItemType::PetEgg ||
115  type == ItemType::PetArmor)
116  {
117  for (int f = 0; f < usedQuantity; f ++)
118  {
119  outMsg.writeItemId(item->getId(),
120  "item id");
121  outMsg.writeInt32(CAST_S16(1), "amount");
122  // +++ need use player inventory index
123  outMsg.writeInt16(0, "inv index");
124  outMsg.writeInt32(item->getCurrentInvIndex(),
125  "inv index");
126  }
127  }
128  else
129  {
130  outMsg.writeItemId(item->getId(),
131  "item id");
132  outMsg.writeInt32(CAST_S16(usedQuantity), "amount");
133  // +++ need use player inventory index
134  outMsg.writeInt16(0, "inv index");
135  outMsg.writeInt32(item->getCurrentInvIndex(),
136  "inv index");
137  }
138  }
139 }
140 
141 } // namespace EAthena
Net::BarterHandler * barterHandler
Definition: net.cpp:117
#define CAST_S16
Definition: cast.h:28
void buyItems(const std::vector< ShopItem * > &items) const
int getId() const
Definition: item.h:81
ItemTypeT getType() const
Definition: item.h:225
void increaseQuantity(const int amount)
Definition: item.h:99
int getCurrentInvIndex() const
Definition: shopitem.h:108
void increaseUsedQuantity(const int amount)
Definition: shopitem.cpp:159
int getUsedQuantity() const
Definition: shopitem.h:151
void update()
Definition: shopitem.cpp:119
int itemIdLen
Definition: client.cpp:130
int packetVersionRe
Definition: client.cpp:127
int packetVersionMain
Definition: client.cpp:126
int packetVersionZero
Definition: client.cpp:128
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
ItemType ::T ItemTypeT
Definition: itemtype.h:43
#define createOutPacket(name)
Definition: messageout.h:37
BuyDialog * mBuyDialog
Definition: barterrecv.cpp:35
@ Armor
Definition: itemtype.h:34
@ Weapon
Definition: itemtype.h:33
@ PetArmor
Definition: itemtype.h:37
@ PetEgg
Definition: itemtype.h:36