1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* The Mana World
* Copyright (C) 2004 The Mana World Development Team
*
* This file is part of The Mana World.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sstream>
#include <guichan/font.hpp>
#include "button.h"
#include "chat.h"
#include "inventorywindow.h"
#include "item_amount.h"
#include "itemcontainer.h"
#include "label.h"
#include "scrollarea.h"
#include "textfield.h"
#include "trade.h"
#include "widgets/layout.h"
#include "../inventory.h"
#include "../item.h"
#include "../localplayer.h"
#include "../units.h"
#ifdef TMWSERV_SUPPORT
#include "../net/tmwserv/gameserver/player.h"
#else
#include "../net/messageout.h"
#include "../net/ea/protocol.h"
#endif
#include "../utils/gettext.h"
#include "../utils/stringutils.h"
#include "../utils/strprintf.h"
#ifdef TMWSERV_SUPPORT
TradeWindow::TradeWindow():
#else
TradeWindow::TradeWindow(Network *network):
#endif
Window(_("Trade: You")),
#ifdef EATHENA_SUPPORT
mNetwork(network),
mMyInventory(new Inventory(INVENTORY_SIZE, 2)),
mPartnerInventory(new Inventory(INVENTORY_SIZE, 2))
#else
mMyInventory(new Inventory(INVENTORY_SIZE)),
mPartnerInventory(new Inventory(INVENTORY_SIZE)),
mStatus(PREPARING)
#endif
{
setWindowName("Trade");
setResizable(true);
setCloseButton(true);
setDefaultSize(342, 209, ImageRect::CENTER);
setMinWidth(342);
setMinHeight(209);
std::string longestName = getFont()->getWidth(_("OK")) >
getFont()->getWidth(_("Trade")) ?
_("OK") : _("Trade");
Button *mAddButton = new Button(_("Add"), "add", this);
#ifdef EATHENA_SUPPORT
mOkButton = new Button(longestName, "ok", this);
#endif
Button *mCancelButton = new Button(_("Cancel"), "cancel", this);
mTradeButton = new Button(_("Propose trade"), "trade", this);
mTradeButton->setWidth(8 + std::max(
mTradeButton->getFont()->getWidth(_("Propose trade")),
mTradeButton->getFont()->getWidth(_("Confirm trade"))));
#ifdef TMWSERV_SUPPORT
mMyItemContainer = new ItemContainer(mMyInventory.get(), 4, 3, 0);
#else
mMyItemContainer = new ItemContainer(mMyInventory.get(), 4, 3, 2);
#endif
mMyItemContainer->addSelectionListener(this);
ScrollArea *mMyScroll = new ScrollArea(mMyItemContainer);
#ifdef TMWSERV_SUPPORT
mPartnerItemContainer = new ItemContainer(mPartnerInventory.get(), 4, 3, 0);
#else
mPartnerItemContainer = new ItemContainer(mPartnerInventory.get(), 4, 3, 2);
#endif
mPartnerItemContainer->addSelectionListener(this);
ScrollArea *mPartnerScroll = new ScrollArea(mPartnerItemContainer);
mMoneyLabel = new Label(strprintf(_("You get %s."), ""));
gcn::Label *mMoneyLabel2 = new Label(_("You give:"));
mMoneyField = new TextField;
mMoneyField->setWidth(40);
Button *mMoneyChange = new Button(_("Change"), "money", this);
place(1, 0, mMoneyLabel);
place(0, 1, mMyScroll).setPadding(3);
place(1, 1, mPartnerScroll).setPadding(3);
ContainerPlacer place;
place = getPlacer(0, 0);
place(0, 0, mMoneyLabel2);
place(1, 0, mMoneyField);
place(2, 0, mMoneyChange).setHAlign(LayoutCell::LEFT);
place = getPlacer(0, 2);
place(0, 0, mAddButton);
#ifdef EATHENA_SUPPORT
place(1, 0, mOkButton);
#else
place(2, 0, mTradeButton);
place(3, 0, mCancelButton);
#endif
Layout &layout = getLayout();
layout.extend(0, 2, 2, 1);
layout.setRowHeight(1, Layout::AUTO_SET);
layout.setRowHeight(2, 0);
layout.setColWidth(0, Layout::AUTO_SET);
layout.setColWidth(1, Layout::AUTO_SET);
#ifdef EATHENA_SUPPORT
mOkButton->setCaption(_("OK"));
#endif
loadWindowState();
}
TradeWindow::~TradeWindow()
{
}
void TradeWindow::setMoney(int amount)
{
mMoneyLabel->setCaption(strprintf(_("You get %s."),
Units::formatCurrency(amount).c_str()));
mMoneyLabel->adjustSize();
#ifdef TMWSERV_SUPPORT
setStatus(PREPARING);
#endif
}
#ifdef TMWSERV_SUPPORT
void TradeWindow::addItem(int id, bool own, int quantity)
{
(own ? mMyInventory : mPartnerInventory)->addItem(id, quantity);
setStatus(PREPARING);
}
#endif
#ifdef EATHENA_SUPPORT
void TradeWindow::addItem(int id, bool own, int quantity, bool equipment)
{
if (own)
{
mMyInventory->addItem(id, quantity, equipment);
}
else
{
mPartnerInventory->addItem(id, quantity, equipment);
}
}
void TradeWindow::changeQuantity(int index, bool own, int quantity)
{
if (own)
mMyInventory->getItem(index)->setQuantity(quantity);
else
mPartnerInventory->getItem(index)->setQuantity(quantity);
}
void TradeWindow::increaseQuantity(int index, bool own, int quantity)
{
if (own)
mMyInventory->getItem(index)->increaseQuantity(quantity);
else
mPartnerInventory->getItem(index)->increaseQuantity(quantity);
}
#endif
void TradeWindow::reset()
{
mMyInventory->clear();
mPartnerInventory->clear();
#ifdef EATHENA_SUPPORT
mOkButton->setCaption(_("OK"));
mOkButton->setActionEventId("ok");
mOkButton->setEnabled(true);
mOkOther = false;
mOkMe = false;
#endif
mMoneyLabel->setCaption(strprintf(_("You get %s."), ""));
mMoneyField->setEnabled(true);
mMoneyField->setText("");
#ifdef TMWSERV_SUPPORT
setStatus(PREPARING);
#endif
}
#ifdef TMWSERV_SUPPORT
void TradeWindow::receivedOk()
{
setStatus(ACCEPTING);
}
#else
void TradeWindow::receivedOk(bool own)
{
if (own)
{
mOkMe = true;
if (mOkOther)
{
mOkButton->setCaption(_("Trade"));
mOkButton->setActionEventId("trade");
}
}
else
{
mOkOther = true;
if (mOkMe)
{
mOkButton->setCaption(_("Trade"));
mOkButton->setActionEventId("trade");
}
}
}
#endif
void TradeWindow::tradeItem(Item *item, int quantity)
{
#ifdef TMWSERV_SUPPORT
Net::GameServer::Player::tradeItem(item->getInvIndex(), quantity);
addItem(item->getId(), true, quantity);
item->increaseQuantity(-quantity);
#else
// TODO: Our newer version of eAthena doesn't register this following
// function. Detect the actual server version, and re-enable this
// for that version only.
//addItem(item->getId(), true, quantity, item->isEquipment());
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_ITEM_ADD_REQUEST);
outMsg.writeInt16(item->getInvIndex());
outMsg.writeInt32(quantity);
#endif
}
void TradeWindow::valueChanged(const gcn::SelectionEvent &event)
{
const Item *item;
/* If an item is selected in one container, make sure no item is selected
* in the other container.
*/
if (event.getSource() == mMyItemContainer &&
(item = mMyItemContainer->getSelectedItem()))
mPartnerItemContainer->selectNone();
else if ((item = mPartnerItemContainer->getSelectedItem()))
mMyItemContainer->selectNone();
}
#ifdef TMWSERV_SUPPORT
void TradeWindow::setStatus(Status s)
{
if (s == mStatus) return;
mStatus = s;
mTradeButton->setCaption
(s == PREPARING ? _("Propose trade") : _("Confirm trade"));
mTradeButton->setEnabled(s != PROPOSING);
}
#endif
void TradeWindow::action(const gcn::ActionEvent &event)
{
Item *item = inventoryWindow->getSelectedItem();
if (event.getId() == "add")
{
if (!inventoryWindow->isVisible()) return;
if (!item)
return;
if (mMyInventory->getFreeSlot() < 1)
return;
if (mMyInventory->contains(item))
{
chatWindow->chatLog(_("Failed adding item. You can not "
"overlap one kind of item on the window."),
BY_SERVER);
return;
}
if (item->getQuantity() == 1)
{
tradeItem(item, 1);
}
else
{
// Choose amount of items to trade
new ItemAmountWindow(AMOUNT_TRADE_ADD, this, item);
}
#ifdef TMWSERV_SUPPORT
setStatus(PREPARING);
#endif
}
else if (event.getId() == "cancel")
{
setVisible(false);
reset();
player_node->setTrading(false);
#ifdef TMWSERV_SUPPORT
Net::GameServer::Player::acceptTrade(false);
#else
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_CANCEL_REQUEST);
#endif
}
#ifdef EATHENA_SUPPORT
else if (event.getId() == "ok")
{
std::stringstream tempMoney(mMoneyField->getText());
int tempInt;
if (tempMoney >> tempInt)
{
mMoneyField->setText(toString(tempInt));
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_ITEM_ADD_REQUEST);
outMsg.writeInt16(0);
outMsg.writeInt32(tempInt);
}
else
{
mMoneyField->setText("");
}
mMoneyField->setEnabled(false);
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_ADD_COMPLETE);
}
#endif
else if (event.getId() == "trade")
{
#ifdef TMWSERV_SUPPORT
Net::GameServer::Player::acceptTrade(true);
setStatus(PROPOSING);
#else
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_OK);
#endif
}
#ifdef TMWSERV_SUPPORT
else if (event.getId() == "money")
{
int v = atoi(mMoneyField->getText().c_str());
Net::GameServer::Player::tradeMoney(v);
mMoneyField->setText(strprintf("%d", v));
setStatus(PREPARING);
}
#endif
}
void TradeWindow::close()
{
#ifdef TMWSERV_SUPPORT
Net::GameServer::Player::acceptTrade(false);
#else
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_CANCEL_REQUEST);
#endif
}
|