trade.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "trade.hpp"
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "../common/nullpo.h"
  7. #include "../common/socket.h"
  8. #include "clif.hpp"
  9. #include "itemdb.hpp"
  10. #include "path.hpp"
  11. #include "pc.hpp"
  12. #include "chrif.hpp"
  13. #include "storage.hpp"
  14. #include "intif.hpp"
  15. #include "atcommand.hpp"
  16. #include "log.hpp"
  17. #include "pc_groups.hpp"
  18. #include "battle.hpp"
  19. #define TRADE_DISTANCE 2 ///Max distance from traders to enable a trade to take place.
  20. /**
  21. * Player initiates a trade request.
  22. * @param sd : player requesting the trade
  23. * @param target_sd : player requested
  24. */
  25. void trade_traderequest(struct map_session_data *sd, struct map_session_data *target_sd)
  26. {
  27. nullpo_retv(sd);
  28. if (map[sd->bl.m].flag.notrade) {
  29. clif_displaymessage (sd->fd, msg_txt(sd,272));
  30. return; //Can't trade in notrade mapflag maps.
  31. }
  32. if (target_sd == NULL || sd == target_sd) {
  33. clif_tradestart(sd, 1); // character does not exist
  34. return;
  35. }
  36. if (target_sd->npc_id) { // Trade fails if you are using an NPC.
  37. clif_tradestart(sd, 2);
  38. return;
  39. }
  40. if (!battle_config.invite_request_check) {
  41. if (target_sd->guild_invite > 0 || target_sd->party_invite > 0 || target_sd->adopt_invite) {
  42. clif_tradestart(sd, 2);
  43. return;
  44. }
  45. }
  46. if ( sd->trade_partner != 0 ) { // If a character tries to trade to another one then cancel the previous one
  47. struct map_session_data *previous_sd = map_id2sd(sd->trade_partner);
  48. if( previous_sd ){
  49. previous_sd->trade_partner = 0;
  50. clif_tradecancelled(previous_sd);
  51. } // Once cancelled then continue to the new one.
  52. sd->trade_partner = 0;
  53. clif_tradecancelled(sd);
  54. }
  55. if (target_sd->trade_partner != 0) {
  56. clif_tradestart(sd, 2); // person is in another trade
  57. return;
  58. }
  59. if (!pc_can_give_items(sd) || !pc_can_give_items(target_sd)) { // check if both GMs are allowed to trade
  60. clif_displaymessage(sd->fd, msg_txt(sd,246));
  61. clif_tradestart(sd, 2); // GM is not allowed to trade
  62. return;
  63. }
  64. // Players can not request trade from far away, unless they are allowed to use @trade.
  65. if (!pc_can_use_command(sd, "trade", COMMAND_ATCOMMAND) &&
  66. (sd->bl.m != target_sd->bl.m || !check_distance_bl(&sd->bl, &target_sd->bl, TRADE_DISTANCE))) {
  67. clif_tradestart(sd, 0); // too far
  68. return ;
  69. }
  70. target_sd->trade_partner = sd->status.account_id;
  71. sd->trade_partner = target_sd->status.account_id;
  72. clif_traderequest(target_sd, sd->status.name);
  73. }
  74. /**
  75. * Reply to a trade-request.
  76. * @param sd : player receiving the trade request answer
  77. * @param type : answer code
  78. * 0: Char is too far
  79. * 1: Character does not exist
  80. * 2: Trade failed
  81. * 3: Accept
  82. * 4: Cancel
  83. * Weird enough, the client should only send 3/4
  84. * and the server is the one that can reply 0~2
  85. */
  86. void trade_tradeack(struct map_session_data *sd, int type)
  87. {
  88. struct map_session_data *tsd;
  89. nullpo_retv(sd);
  90. if (sd->state.trading || !sd->trade_partner)
  91. return; // Already trading or no partner set.
  92. if ((tsd = map_id2sd(sd->trade_partner)) == NULL) {
  93. clif_tradestart(sd, 1); // Character does not exist
  94. sd->trade_partner=0;
  95. return;
  96. }
  97. if (tsd->state.trading || tsd->trade_partner != sd->bl.id) {
  98. clif_tradestart(sd, 2);
  99. sd->trade_partner=0;
  100. return; // Already trading or wrong partner.
  101. }
  102. if (type == 4) { // Cancel
  103. clif_tradestart(tsd, type);
  104. clif_tradestart(sd, type);
  105. sd->state.deal_locked = 0;
  106. sd->trade_partner = 0;
  107. tsd->state.deal_locked = 0;
  108. tsd->trade_partner = 0;
  109. return;
  110. }
  111. if (type != 3)
  112. return; //If client didn't send accept, it's a broken packet?
  113. // Players can not request trade from far away, unless they are allowed to use @trade.
  114. // Check here as well since the original character could had warped.
  115. if (!pc_can_use_command(sd, "trade", COMMAND_ATCOMMAND) &&
  116. (sd->bl.m != tsd->bl.m || !check_distance_bl(&sd->bl, &tsd->bl, TRADE_DISTANCE))) {
  117. clif_tradestart(sd, 0); // too far
  118. sd->trade_partner=0;
  119. tsd->trade_partner = 0;
  120. return;
  121. }
  122. // Check if you can start trade.
  123. if (sd->npc_id || sd->state.vending || sd->state.buyingstore || sd->state.storage_flag ||
  124. tsd->npc_id || tsd->state.vending || tsd->state.buyingstore || tsd->state.storage_flag) { // Fail
  125. clif_tradestart(sd, 2);
  126. clif_tradestart(tsd, 2);
  127. sd->state.deal_locked = 0;
  128. sd->trade_partner = 0;
  129. tsd->state.deal_locked = 0;
  130. tsd->trade_partner = 0;
  131. return;
  132. }
  133. // Initiate trade
  134. sd->state.trading = 1;
  135. tsd->state.trading = 1;
  136. memset(&sd->deal, 0, sizeof(sd->deal));
  137. memset(&tsd->deal, 0, sizeof(tsd->deal));
  138. clif_tradestart(tsd, type);
  139. clif_tradestart(sd, type);
  140. }
  141. /**
  142. * Check here hacker for duplicate item in trade
  143. * normal client refuse to have 2 same types of item (except equipment) in same trade window
  144. * normal client authorise only no equipped item and only from inventory
  145. * This function could end player connection if too much hack is detected
  146. * @param sd : player to check
  147. * @return -1:zeny hack, 0:all fine, 1:item hack
  148. */
  149. int impossible_trade_check(struct map_session_data *sd)
  150. {
  151. struct item inventory[MAX_INVENTORY];
  152. char message_to_gm[200];
  153. int i, index;
  154. nullpo_retr(1, sd);
  155. if(sd->deal.zeny > sd->status.zeny) {
  156. pc_setglobalreg(sd, add_str("ZENY_HACKER"), 1);
  157. return -1;
  158. }
  159. // get inventory of player
  160. memcpy(&inventory, &sd->inventory.u.items_inventory, sizeof(struct item) * MAX_INVENTORY);
  161. // remove this part: arrows can be trade and equipped
  162. // re-added! [celest]
  163. // remove equipped items (they can not be trade)
  164. for (i = 0; i < MAX_INVENTORY; i++)
  165. if (inventory[i].nameid > 0 && inventory[i].equip && !(inventory[i].equip & EQP_AMMO))
  166. memset(&inventory[i], 0, sizeof(struct item));
  167. // check items in player inventory
  168. for(i = 0; i < 10; i++) {
  169. if (!sd->deal.item[i].amount)
  170. continue;
  171. index = sd->deal.item[i].index;
  172. if (inventory[index].amount < sd->deal.item[i].amount) { // if more than the player have -> hack
  173. sprintf(message_to_gm, msg_txt(sd,538), sd->status.name, sd->status.account_id); // Hack on trade: character '%s' (account: %d) try to trade more items that he has.
  174. intif_wis_message_to_gm(wisp_server_name, PC_PERM_RECEIVE_HACK_INFO, message_to_gm);
  175. sprintf(message_to_gm, msg_txt(sd,539), inventory[index].amount, inventory[index].nameid, sd->deal.item[i].amount); // This player has %d of a kind of item (id: %d), and try to trade %d of them.
  176. intif_wis_message_to_gm(wisp_server_name, PC_PERM_RECEIVE_HACK_INFO, message_to_gm);
  177. // if we block people
  178. if (battle_config.ban_hack_trade < 0) {
  179. chrif_req_login_operation(-1, sd->status.name, CHRIF_OP_LOGIN_BLOCK, 0, 0, 0); // type: 1 - block
  180. set_eof(sd->fd); // forced to disconnect because of the hack
  181. // message about the ban
  182. strcpy(message_to_gm, msg_txt(sd,540)); // This player has been definitively blocked.
  183. // if we ban people
  184. } else if (battle_config.ban_hack_trade > 0) {
  185. chrif_req_login_operation(-1, sd->status.name, CHRIF_OP_LOGIN_BAN, battle_config.ban_hack_trade*60, 0, 0); // type: 2 - ban (year, month, day, hour, minute, second)
  186. set_eof(sd->fd); // forced to disconnect because of the hack
  187. // message about the ban
  188. sprintf(message_to_gm, msg_txt(sd,507), battle_config.ban_hack_trade); // This player has been banned for %d minute(s).
  189. } else
  190. // message about the ban
  191. strcpy(message_to_gm, msg_txt(sd,508)); // This player hasn't been banned (Ban option is disabled).
  192. intif_wis_message_to_gm(wisp_server_name, PC_PERM_RECEIVE_HACK_INFO, message_to_gm);
  193. return 1;
  194. }
  195. inventory[index].amount -= sd->deal.item[i].amount; // remove item from inventory
  196. }
  197. return 0;
  198. }
  199. /**
  200. * Checks if trade is possible (against zeny limits, inventory limits, etc)
  201. * @param sd : player 1 trading
  202. * @param tsd : player 2 trading
  203. * @return 0:error, 1:success
  204. */
  205. int trade_check(struct map_session_data *sd, struct map_session_data *tsd)
  206. {
  207. struct item inventory[MAX_INVENTORY];
  208. struct item inventory2[MAX_INVENTORY];
  209. struct item_data *data;
  210. int trade_i, i, n;
  211. // check zeny value against hackers (Zeny was already checked on time of adding, but you never know when you lost some zeny since then.
  212. if(sd->deal.zeny > sd->status.zeny || (tsd->status.zeny > MAX_ZENY - sd->deal.zeny))
  213. return 0;
  214. if(tsd->deal.zeny > tsd->status.zeny || (sd->status.zeny > MAX_ZENY - tsd->deal.zeny))
  215. return 0;
  216. // get inventory of player
  217. memcpy(&inventory, &sd->inventory.u.items_inventory, sizeof(struct item) * MAX_INVENTORY);
  218. memcpy(&inventory2, &tsd->inventory.u.items_inventory, sizeof(struct item) * MAX_INVENTORY);
  219. // check free slot in both inventory
  220. for(trade_i = 0; trade_i < 10; trade_i++) {
  221. short amount;
  222. amount = sd->deal.item[trade_i].amount;
  223. if (amount) {
  224. n = sd->deal.item[trade_i].index;
  225. if (amount > inventory[n].amount)
  226. return 0; // Quantity Exploit?
  227. data = itemdb_search(inventory[n].nameid);
  228. i = MAX_INVENTORY;
  229. if (itemdb_isstackable2(data)) { // Stackable item.
  230. for(i = 0; i < MAX_INVENTORY; i++)
  231. if (inventory2[i].nameid == inventory[n].nameid &&
  232. inventory2[i].card[0] == inventory[n].card[0] && inventory2[i].card[1] == inventory[n].card[1] &&
  233. inventory2[i].card[2] == inventory[n].card[2] && inventory2[i].card[3] == inventory[n].card[3]) {
  234. if (inventory2[i].amount + amount > MAX_AMOUNT)
  235. return 0;
  236. inventory2[i].amount += amount;
  237. inventory[n].amount -= amount;
  238. break;
  239. }
  240. }
  241. if (i == MAX_INVENTORY) { // look for an empty slot.
  242. for(i = 0; i < MAX_INVENTORY && inventory2[i].nameid; i++);
  243. if (i == MAX_INVENTORY)
  244. return 0;
  245. memcpy(&inventory2[i], &inventory[n], sizeof(struct item));
  246. inventory2[i].amount = amount;
  247. inventory[n].amount -= amount;
  248. }
  249. }
  250. amount = tsd->deal.item[trade_i].amount;
  251. if (!amount)
  252. continue;
  253. n = tsd->deal.item[trade_i].index;
  254. if (amount > inventory2[n].amount)
  255. return 0;
  256. // search if it's possible to add item (for full inventory)
  257. data = itemdb_search(inventory2[n].nameid);
  258. i = MAX_INVENTORY;
  259. if (itemdb_isstackable2(data)) {
  260. for(i = 0; i < MAX_INVENTORY; i++)
  261. if (inventory[i].nameid == inventory2[n].nameid &&
  262. inventory[i].card[0] == inventory2[n].card[0] && inventory[i].card[1] == inventory2[n].card[1] &&
  263. inventory[i].card[2] == inventory2[n].card[2] && inventory[i].card[3] == inventory2[n].card[3]) {
  264. if (inventory[i].amount + amount > MAX_AMOUNT)
  265. return 0;
  266. inventory[i].amount += amount;
  267. inventory2[n].amount -= amount;
  268. break;
  269. }
  270. }
  271. if (i == MAX_INVENTORY) {
  272. for(i = 0; i < MAX_INVENTORY && inventory[i].nameid; i++);
  273. if (i == MAX_INVENTORY)
  274. return 0;
  275. memcpy(&inventory[i], &inventory2[n], sizeof(struct item));
  276. inventory[i].amount = amount;
  277. inventory2[n].amount -= amount;
  278. }
  279. }
  280. return 1;
  281. }
  282. /**
  283. * Adds an item/qty to the trade window
  284. * @param sd : Player requesting to add stuff to the trade
  285. * @param index : index of item in inventory
  286. * @param amount : amount of item to add from index
  287. */
  288. void trade_tradeadditem(struct map_session_data *sd, short index, short amount)
  289. {
  290. struct map_session_data *target_sd;
  291. struct item *item;
  292. int trade_i, trade_weight;
  293. int src_lv, dst_lv;
  294. nullpo_retv(sd);
  295. if( !sd->state.trading || sd->state.deal_locked > 0 )
  296. return; // Can't add stuff.
  297. if( (target_sd = map_id2sd(sd->trade_partner)) == NULL ) {
  298. trade_tradecancel(sd);
  299. return;
  300. }
  301. if( !amount ) { // Why do this.. ~.~ just send an ack, the item won't display on the trade window.
  302. clif_tradeitemok(sd, index, 0);
  303. return;
  304. }
  305. index -= 2; // 0 is for zeny, 1 is unknown. Gravity, go figure...
  306. // Item checks...
  307. if( index < 0 || index >= MAX_INVENTORY )
  308. return;
  309. if( amount < 0 || amount > sd->inventory.u.items_inventory[index].amount )
  310. return;
  311. item = &sd->inventory.u.items_inventory[index];
  312. src_lv = pc_get_group_level(sd);
  313. dst_lv = pc_get_group_level(target_sd);
  314. if( !itemdb_cantrade(item, src_lv, dst_lv) && // Can't trade
  315. (pc_get_partner(sd) != target_sd || !itemdb_canpartnertrade(item, src_lv, dst_lv)) ) { // Can't partner-trade
  316. clif_displaymessage (sd->fd, msg_txt(sd,260));
  317. clif_tradeitemok(sd, index+2, 1);
  318. return;
  319. }
  320. if( item->expire_time ) { // Rental System
  321. clif_displaymessage (sd->fd, msg_txt(sd,260));
  322. clif_tradeitemok(sd, index+2, 1);
  323. return;
  324. }
  325. if( ((item->bound == BOUND_ACCOUNT || item->bound > BOUND_GUILD) || (item->bound == BOUND_GUILD && sd->status.guild_id != target_sd->status.guild_id)) && !pc_can_give_bounded_items(sd) ) { // Item Bound
  326. clif_displaymessage(sd->fd, msg_txt(sd,293));
  327. clif_tradeitemok(sd, index+2, 1);
  328. return;
  329. }
  330. if (item->bound)
  331. sd->state.isBoundTrading |= (1<<item->bound);
  332. // Locate a trade position
  333. ARR_FIND( 0, 10, trade_i, sd->deal.item[trade_i].index == index || sd->deal.item[trade_i].amount == 0 );
  334. if( trade_i == 10 ) { // No space left
  335. clif_tradeitemok(sd, index+2, 1);
  336. return;
  337. }
  338. trade_weight = sd->inventory_data[index]->weight * amount;
  339. if( target_sd->weight + sd->deal.weight + trade_weight > target_sd->max_weight ) { // fail to add item -- the player was over weighted.
  340. clif_tradeitemok(sd, index+2, 1);
  341. return;
  342. }
  343. if( sd->deal.item[trade_i].index == index ) { // The same item as before is being readjusted.
  344. if( sd->deal.item[trade_i].amount + amount > sd->inventory.u.items_inventory[index].amount ) { // packet deal exploit check
  345. amount = sd->inventory.u.items_inventory[index].amount - sd->deal.item[trade_i].amount;
  346. trade_weight = sd->inventory_data[index]->weight * amount;
  347. }
  348. sd->deal.item[trade_i].amount += amount;
  349. } else { // New deal item
  350. sd->deal.item[trade_i].index = index;
  351. sd->deal.item[trade_i].amount = amount;
  352. }
  353. sd->deal.weight += trade_weight;
  354. clif_tradeitemok(sd, index+2, 0); // Return the index as it was received
  355. clif_tradeadditem(sd, target_sd, index+2, amount);
  356. }
  357. /**
  358. * Adds the specified amount of zeny to the trade window
  359. * This function will check if the player have enough money to do so
  360. * And if the target player have enough space for that money
  361. * @param sd : Player who's adding zeny
  362. * @param amount : zeny amount
  363. */
  364. void trade_tradeaddzeny(struct map_session_data* sd, int amount)
  365. {
  366. struct map_session_data* target_sd;
  367. nullpo_retv(sd);
  368. if( !sd->state.trading || sd->state.deal_locked > 0 )
  369. return; //Can't add stuff.
  370. if( (target_sd = map_id2sd(sd->trade_partner)) == NULL ) {
  371. trade_tradecancel(sd);
  372. return;
  373. }
  374. if( amount < 0 || amount > sd->status.zeny || amount > MAX_ZENY - target_sd->status.zeny ) { // invalid values, no appropriate packet for it => abort
  375. trade_tradecancel(sd);
  376. return;
  377. }
  378. sd->deal.zeny = amount;
  379. clif_tradeadditem(sd, target_sd, 0, amount);
  380. }
  381. /**
  382. * 'Ok' button on the trade window is pressed.
  383. * @param sd : Player that pressed the button
  384. */
  385. void trade_tradeok(struct map_session_data *sd)
  386. {
  387. struct map_session_data *target_sd;
  388. if(sd->state.deal_locked || !sd->state.trading)
  389. return;
  390. if ((target_sd = map_id2sd(sd->trade_partner)) == NULL) {
  391. trade_tradecancel(sd);
  392. return;
  393. }
  394. sd->state.deal_locked = 1;
  395. clif_tradeitemok(sd, 0, 0);
  396. clif_tradedeal_lock(sd, 0);
  397. clif_tradedeal_lock(target_sd, 1);
  398. }
  399. /**
  400. * 'Cancel' is pressed. (or trade was force-cancelled by the code)
  401. * @param sd : Player that pressed the button
  402. */
  403. void trade_tradecancel(struct map_session_data *sd)
  404. {
  405. struct map_session_data *target_sd;
  406. int trade_i;
  407. nullpo_retv(sd);
  408. target_sd = map_id2sd(sd->trade_partner);
  409. sd->state.isBoundTrading = 0;
  410. if(!sd->state.trading) { // Not trade accepted
  411. if( target_sd ) {
  412. target_sd->trade_partner = 0;
  413. clif_tradecancelled(target_sd);
  414. }
  415. sd->trade_partner = 0;
  416. clif_tradecancelled(sd);
  417. return;
  418. }
  419. for(trade_i = 0; trade_i < 10; trade_i++) { // give items back (only virtual)
  420. if (!sd->deal.item[trade_i].amount)
  421. continue;
  422. clif_additem(sd, sd->deal.item[trade_i].index, sd->deal.item[trade_i].amount, 0);
  423. sd->deal.item[trade_i].index = 0;
  424. sd->deal.item[trade_i].amount = 0;
  425. }
  426. if (sd->deal.zeny) {
  427. clif_updatestatus(sd, SP_ZENY);
  428. sd->deal.zeny = 0;
  429. }
  430. sd->state.deal_locked = 0;
  431. sd->state.trading = 0;
  432. sd->trade_partner = 0;
  433. clif_tradecancelled(sd);
  434. if (!target_sd)
  435. return;
  436. for(trade_i = 0; trade_i < 10; trade_i++) { // give items back (only virtual)
  437. if (!target_sd->deal.item[trade_i].amount)
  438. continue;
  439. clif_additem(target_sd, target_sd->deal.item[trade_i].index, target_sd->deal.item[trade_i].amount, 0);
  440. target_sd->deal.item[trade_i].index = 0;
  441. target_sd->deal.item[trade_i].amount = 0;
  442. }
  443. if (target_sd->deal.zeny) {
  444. clif_updatestatus(target_sd, SP_ZENY);
  445. target_sd->deal.zeny = 0;
  446. }
  447. target_sd->state.deal_locked = 0;
  448. target_sd->trade_partner = 0;
  449. target_sd->state.trading = 0;
  450. clif_tradecancelled(target_sd);
  451. }
  452. /**
  453. * Execute the trade
  454. * lock sd and tsd trade data, execute the trade, clear, then save players
  455. * @param sd : Player that has click on trade button
  456. */
  457. void trade_tradecommit(struct map_session_data *sd)
  458. {
  459. struct map_session_data *tsd;
  460. int trade_i;
  461. nullpo_retv(sd);
  462. if (!sd->state.trading || !sd->state.deal_locked) //Locked should be 1 (pressed ok) before you can press trade.
  463. return;
  464. if ((tsd = map_id2sd(sd->trade_partner)) == NULL) {
  465. trade_tradecancel(sd);
  466. return;
  467. }
  468. sd->state.deal_locked = 2;
  469. if (tsd->state.deal_locked < 2)
  470. return; //Not yet time for trading.
  471. // Now is a good time (to save on resources) to check that the trade can indeed be made and it's not exploitable.
  472. // check exploit (trade more items that you have)
  473. if (impossible_trade_check(sd)) {
  474. trade_tradecancel(sd);
  475. return;
  476. }
  477. // check exploit (trade more items that you have)
  478. if (impossible_trade_check(tsd)) {
  479. trade_tradecancel(tsd);
  480. return;
  481. }
  482. // check for full inventory (can not add traded items)
  483. if (!trade_check(sd,tsd)) { // check the both players
  484. trade_tradecancel(sd);
  485. return;
  486. }
  487. // trade is accepted and correct.
  488. for( trade_i = 0; trade_i < 10; trade_i++ ) {
  489. int n;
  490. unsigned char flag = 0;
  491. if (sd->deal.item[trade_i].amount) {
  492. n = sd->deal.item[trade_i].index;
  493. flag = pc_additem(tsd, &sd->inventory.u.items_inventory[n], sd->deal.item[trade_i].amount,LOG_TYPE_TRADE);
  494. if (flag == 0)
  495. pc_delitem(sd, n, sd->deal.item[trade_i].amount, 1, 6, LOG_TYPE_TRADE);
  496. else
  497. clif_additem(sd, n, sd->deal.item[trade_i].amount, 0);
  498. sd->deal.item[trade_i].index = 0;
  499. sd->deal.item[trade_i].amount = 0;
  500. }
  501. if (tsd->deal.item[trade_i].amount) {
  502. n = tsd->deal.item[trade_i].index;
  503. flag = pc_additem(sd, &tsd->inventory.u.items_inventory[n], tsd->deal.item[trade_i].amount,LOG_TYPE_TRADE);
  504. if (flag == 0)
  505. pc_delitem(tsd, n, tsd->deal.item[trade_i].amount, 1, 6, LOG_TYPE_TRADE);
  506. else
  507. clif_additem(tsd, n, tsd->deal.item[trade_i].amount, 0);
  508. tsd->deal.item[trade_i].index = 0;
  509. tsd->deal.item[trade_i].amount = 0;
  510. }
  511. }
  512. if( sd->deal.zeny ) {
  513. pc_payzeny(sd ,sd->deal.zeny, LOG_TYPE_TRADE, tsd);
  514. pc_getzeny(tsd,sd->deal.zeny,LOG_TYPE_TRADE, sd);
  515. sd->deal.zeny = 0;
  516. }
  517. if ( tsd->deal.zeny) {
  518. pc_payzeny(tsd,tsd->deal.zeny,LOG_TYPE_TRADE, sd);
  519. pc_getzeny(sd ,tsd->deal.zeny,LOG_TYPE_TRADE, tsd);
  520. tsd->deal.zeny = 0;
  521. }
  522. sd->state.deal_locked = 0;
  523. sd->trade_partner = 0;
  524. sd->state.trading = 0;
  525. sd->state.isBoundTrading = 0;
  526. tsd->state.deal_locked = 0;
  527. tsd->trade_partner = 0;
  528. tsd->state.trading = 0;
  529. tsd->state.isBoundTrading = 0;
  530. clif_tradecompleted(sd, 0);
  531. clif_tradecompleted(tsd, 0);
  532. // save both player to avoid crash: they always have no advantage/disadvantage between the 2 players
  533. if (save_settings&CHARSAVE_TRADE) {
  534. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  535. chrif_save(tsd, CSAVE_INVENTORY|CSAVE_CART);
  536. }
  537. }