summaryrefslogtreecommitdiff
path: root/src/map/trade.c
blob: 41a89cd7a4f16e97141d6cea68ca02dbf6bce9cd (plain) (blame)
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
#include <stdio.h>
#include <string.h>

#include "clif.h"
#include "itemdb.h"
#include "map.h"
#include "trade.h"
#include "pc.h"
#include "npc.h"
#include "battle.h"
#include "nullpo.h"
#include "log.h"

/*==========================================
 * ����v���𑊎�ɑ���
 *------------------------------------------
 */
void trade_traderequest(struct map_session_data *sd,int target_id)
{
	struct map_session_data *target_sd;

	nullpo_retv(sd);

	if((target_sd = map_id2sd(target_id)) != NULL){
		if(!battle_config.invite_request_check) {
			if(target_sd->guild_invite>0 || target_sd->party_invite>0){
				clif_tradestart(sd,2);	// �����PT�v������Guild�v����
				return;
			}
		}
		if((target_sd->trade_partner !=0) || (sd->trade_partner !=0)) {
			trade_tradecancel(sd); //person is in another trade
		}
		else{
			if((pc_isGM(sd) < 60) && (sd->bl.m != target_sd->bl.m
			 || (sd->bl.x - target_sd->bl.x <= -5 || sd->bl.x - target_sd->bl.x >= 5)
			 || (sd->bl.y - target_sd->bl.y <= -5 || sd->bl.y - target_sd->bl.y >= 5))) {
				clif_tradestart(sd,0); //too far
			}
			else if(sd!=target_sd) {
				target_sd->trade_partner = sd->status.account_id;
				sd->trade_partner = target_sd->status.account_id;
				clif_traderequest(target_sd,sd->status.name);
			}
		}
	}
	else{
		clif_tradestart(sd,1); //character does not exist
	}
}

/*==========================================
 * ����v��
 *------------------------------------------
 */
void trade_tradeack(struct map_session_data *sd,int type)
{
	struct map_session_data *target_sd;

	nullpo_retv(sd);

	if((target_sd = map_id2sd(sd->trade_partner)) != NULL){
		clif_tradestart(target_sd,type);
		clif_tradestart(sd,type);
		if(type == 4){ // Cancel
			sd->deal_locked =0;
			sd->trade_partner=0;
			target_sd->deal_locked=0;
			target_sd->trade_partner=0;
		}
		if(sd->npc_id != 0)
			npc_event_dequeue(sd);
		if(target_sd->npc_id != 0)
			npc_event_dequeue(target_sd);
	}
}

/*==========================================
 * �A�C�e���lj�
 *------------------------------------------
 */
void trade_tradeadditem(struct map_session_data *sd,int index,int amount)
{
	struct map_session_data *target_sd;
	int trade_i;
	int trade_weight=0;
	int c;

	nullpo_retv(sd);

	if(((target_sd = map_id2sd(sd->trade_partner)) != NULL) && (sd->deal_locked < 1)){
		if(index<2 || index>=MAX_INVENTORY+2){
			if(index == 0 && amount > 0 && amount <= sd->status.zeny){
				sd->deal_zeny=amount;
				clif_tradeadditem(sd,target_sd,0,amount);
			}
		}else if(amount <= sd->status.inventory[index-2].amount && amount > 0){
			for(trade_i=0; trade_i<10;trade_i++){
				if(sd->deal_item_amount[trade_i] == 0){
					trade_weight+=sd->inventory_data[index-2]->weight*amount;
					if(target_sd->weight + trade_weight > target_sd->max_weight){
						clif_tradeitemok(sd,index,1); //fail to add item -- the player was over weighted.
                                                amount = 0; // [MouseJstr]
					}else{
						for(c=0; c==trade_i-1;c++){ // re-deal exploit protection [Valaris]
							if(sd->deal_item_index[c]==index) {
								trade_tradecancel(sd);
								return;
							}
						}
						sd->deal_item_index[trade_i] =index;
						sd->deal_item_amount[trade_i]+=amount;
						clif_tradeitemok(sd,index,0); //success to add item
						clif_tradeadditem(sd,target_sd,index,amount);
					}
					break;
				}else{
					trade_weight+=sd->inventory_data[sd->deal_item_index[trade_i]-2]->weight*sd->deal_item_amount[trade_i];
				}
			}
		}
	}
}

/*==========================================
 * �A�C�e���lj�����(ok����)
 *------------------------------------------
 */
void trade_tradeok(struct map_session_data *sd)
{
	struct map_session_data *target_sd;
	int trade_i;

	nullpo_retv(sd);
	
	for(trade_i=0;trade_i<10;trade_i++) {
		if(sd->deal_item_amount[trade_i]>sd->status.inventory[sd->deal_item_index[trade_i]-2].amount ||
			sd->deal_item_amount[trade_i]<0) {
			trade_tradecancel(sd);
			return;
		}
	
	}
	
	if((target_sd = map_id2sd(sd->trade_partner)) != NULL){
		sd->deal_locked=1;
		clif_tradeitemok(sd,0,0);
		clif_tradedeal_lock(sd,0);
		clif_tradedeal_lock(target_sd,1);
	}
}

/*==========================================
 * ����L�����Z��
 *------------------------------------------
 */
void trade_tradecancel(struct map_session_data *sd)
{
	struct map_session_data *target_sd;
	int trade_i;

	nullpo_retv(sd);

	if((target_sd = map_id2sd(sd->trade_partner)) != NULL){
		for(trade_i=0; trade_i<10;trade_i++) { //give items back (only virtual)
			if(sd->deal_item_amount[trade_i] != 0) {
				clif_additem(sd,sd->deal_item_index[trade_i]-2,sd->deal_item_amount[trade_i],0);
				sd->deal_item_index[trade_i] =0;
				sd->deal_item_amount[trade_i]=0;
			}
			if(target_sd->deal_item_amount[trade_i] != 0) {
				clif_additem(target_sd,target_sd->deal_item_index[trade_i]-2,target_sd->deal_item_amount[trade_i],0);
				target_sd->deal_item_index[trade_i] =0;
				target_sd->deal_item_amount[trade_i]=0;
			}
		}
		if(sd->deal_zeny) {
			clif_updatestatus(sd,SP_ZENY);
			sd->deal_zeny=0;
		}
		if(target_sd->deal_zeny) {
			clif_updatestatus(target_sd,SP_ZENY);
			target_sd->deal_zeny=0;
		}
		sd->deal_locked =0;
		sd->trade_partner=0;
		target_sd->deal_locked=0;
		target_sd->trade_partner=0;
		clif_tradecancelled(sd);
		clif_tradecancelled(target_sd);
	}
}

/*==========================================
 * �������(trade����)
 *------------------------------------------
 */
void trade_tradecommit(struct map_session_data *sd)
{
	struct map_session_data *target_sd;
	int trade_i;

	nullpo_retv(sd);

	if((target_sd = map_id2sd(sd->trade_partner)) != NULL){
		if( (sd->deal_locked >=1) && (target_sd->deal_locked >=1) ){ // both have pressed 'ok'
			if(sd->deal_locked < 2) {sd->deal_locked=2;} // set locked to 2
			if(target_sd->deal_locked==2) { // the other one pressed 'trade' too
				for(trade_i=0; trade_i<10;trade_i++) {
					if(sd->deal_item_amount[trade_i] != 0) {
						int n=sd->deal_item_index[trade_i]-2;
						int flag;

						//Dupe Fix by mark
						if (sd->status.inventory[n].amount < sd->deal_item_amount[trade_i])
							sd->deal_item_amount[trade_i] = sd->status.inventory[n].amount;
						//End Dupe Fix

						#ifndef TXT_ONLY
						if(log_config.trade > 0)
							log_trade(sd,target_sd,n,sd->deal_item_amount[trade_i]);
						#endif //USE_SQL

						flag = pc_additem(target_sd,&sd->status.inventory[n],sd->deal_item_amount[trade_i]);
						if(flag==0)
						pc_delitem(sd,n,sd->deal_item_amount[trade_i],1);
						else
							clif_additem(sd,n,sd->deal_item_amount[trade_i],0);
						sd->deal_item_index[trade_i] =0;
						sd->deal_item_amount[trade_i]=0;
					}
					if(target_sd->deal_item_amount[trade_i] != 0) {
						int n=target_sd->deal_item_index[trade_i]-2;
						int flag;

						//Dupe Fix by mark
						if (target_sd->status.inventory[n].amount < target_sd->deal_item_amount[trade_i])
							target_sd->deal_item_amount[trade_i] = target_sd->status.inventory[n].amount;
						//End Dupe Fix

						#ifndef TXT_ONLY
						if(log_config.trade > 0)
							log_trade(target_sd,sd,n,target_sd->deal_item_amount[trade_i]);
						#endif //USE_SQL

						flag = pc_additem(sd,&target_sd->status.inventory[n],target_sd->deal_item_amount[trade_i]);
						if(flag==0)
						pc_delitem(target_sd,n,target_sd->deal_item_amount[trade_i],1);
						else
							clif_additem(target_sd,n,target_sd->deal_item_amount[trade_i],0);
						target_sd->deal_item_index[trade_i] =0;
						target_sd->deal_item_amount[trade_i]=0;
					}
				}
				if(sd->deal_zeny) {
					#ifndef TXT_ONLY	
					if (log_config.trade > 0 && log_config.zeny > 0)
						log_zeny(sd, target_sd, sd->deal_zeny);
					#endif //USE_SQL
					sd->status.zeny -= sd->deal_zeny;
					clif_updatestatus(sd,SP_ZENY);
					target_sd->status.zeny += sd->deal_zeny;
					clif_updatestatus(target_sd,SP_ZENY);
					sd->deal_zeny=0;
				}
				if(target_sd->deal_zeny) {
					#ifndef TXT_ONLY
					if (log_config.trade > 0 && log_config.zeny > 0)
						log_zeny(target_sd, sd, target_sd->deal_zeny);
					#endif //USE_SQL
					target_sd->status.zeny -= target_sd->deal_zeny;
					clif_updatestatus(target_sd,SP_ZENY);
					sd->status.zeny += target_sd->deal_zeny;
					clif_updatestatus(sd,SP_ZENY);
					target_sd->deal_zeny=0;
				}
				sd->deal_locked =0;
				sd->trade_partner=0;
				target_sd->deal_locked=0;
				target_sd->trade_partner=0;
				clif_tradecompleted(sd,0);
				clif_tradecompleted(target_sd,0);
			}
		}
	}
}