root/src/map/mail.c @ 24

Revision 24, 4.4 kB (checked in by jinshiro, 17 years ago)
Line 
1// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2// For more information, see LICENCE in the main folder
3
4#ifndef TXT_ONLY
5
6#include "../common/nullpo.h"
7#include "../common/showmsg.h"
8
9#include "mail.h"
10#include "atcommand.h"
11#include "itemdb.h"
12#include "clif.h"
13#include "pc.h"
14#include "log.h"
15
16#include <time.h>
17#include <string.h>
18
19void mail_clear(struct map_session_data *sd)
20{
21        sd->mail.nameid = 0;
22        sd->mail.index = 0;
23        sd->mail.amount = 0;
24        sd->mail.zeny = 0;
25        sd->auction.amount = 0;
26
27        return;
28}
29
30int mail_removeitem(struct map_session_data *sd, short flag)
31{
32        nullpo_retr(0,sd);
33
34        if( sd->mail.amount )
35        {
36                if (flag)
37                { // Item send
38                        if(log_config.enable_logs&0x2000)
39                                log_pick_pc(sd, "E", sd->mail.nameid, -sd->mail.amount, &sd->status.inventory[sd->mail.index]);
40
41                        pc_delitem(sd, sd->mail.index, sd->mail.amount, 1);
42                }
43                else
44                        clif_additem(sd, sd->mail.index, sd->mail.amount, 0);
45        }
46
47        sd->mail.nameid = 0;
48        sd->mail.index = 0;
49        sd->mail.amount = 0;
50        return 1;
51}
52
53int mail_removezeny(struct map_session_data *sd, short flag)
54{
55        nullpo_retr(0,sd);
56
57        if (flag && sd->mail.zeny > 0)
58        {  //Zeny send
59                if(log_config.zeny)
60                        log_zeny(sd, "E", sd, -sd->mail.zeny);
61
62                sd->status.zeny -= sd->mail.zeny;
63        }
64        sd->mail.zeny = 0;
65        clif_updatestatus(sd, SP_ZENY);
66
67        return 1;
68}
69
70unsigned char mail_setitem(struct map_session_data *sd, int idx, int amount)
71{
72        if (idx == 0)
73        { // Zeny Transfer
74                if( amount < 0 )
75                        return 0;
76                if( amount > sd->status.zeny )
77                        amount = sd->status.zeny;
78
79                if( !pc_can_give_items(pc_isGM(sd)) )
80                        amount = 0;
81
82                sd->mail.zeny = amount;
83                // clif_updatestatus(sd, SP_ZENY);
84                return 0;
85        }
86        else
87        { // Item Transfer
88                idx -= 2;
89                mail_removeitem(sd, 0);
90
91                if( idx < 0 || idx >= MAX_INVENTORY )
92                        return 1;
93                if( amount < 0 || amount > sd->status.inventory[idx].amount )
94                        return 1;
95                if( !pc_candrop(sd, &sd->status.inventory[idx]) )
96                        return 1;
97
98                sd->mail.index = idx;
99                sd->mail.nameid = sd->status.inventory[idx].nameid;
100                sd->mail.amount = amount;
101               
102                return 0;
103        }
104}
105
106bool mail_setattachment(struct map_session_data *sd, struct mail_message *msg)
107{
108        int n;
109       
110        nullpo_retr(false,sd);
111        nullpo_retr(false,msg);
112
113        if( sd->mail.zeny < 0 || sd->mail.zeny > sd->status.zeny )
114                return false;
115
116        n = sd->mail.index;
117        if( sd->mail.amount )
118        {
119                if( sd->status.inventory[n].nameid != sd->mail.nameid )
120                        return false;
121
122                if( sd->status.inventory[n].amount < sd->mail.amount )
123                        return false;
124
125                memcpy(&msg->item, &sd->status.inventory[n], sizeof(struct item));
126                msg->item.amount = sd->mail.amount;
127        }
128        else
129                memset(&msg->item, 0x00, sizeof(struct item));
130
131        msg->zeny = sd->mail.zeny;
132
133        // Removes the attachment from sender
134        mail_removeitem(sd,1);
135        mail_removezeny(sd,1);
136
137        return true;
138}
139
140void mail_getattachment(struct map_session_data* sd, int zeny, struct item* item)
141{
142        if( item->nameid > 0 && item->amount > 0 )
143        {
144                pc_additem(sd, item, item->amount);
145
146                if(log_config.enable_logs&0x2000)
147                        log_pick_pc(sd, "E", item->nameid, item->amount, item);
148
149                clif_Mail_getattachment(sd->fd, 0);
150        }
151
152        if( zeny > 0 )
153        {  //Zeny recieve
154                if(log_config.zeny)
155                        log_zeny(sd, "E", sd, zeny);
156                pc_getzeny(sd, zeny);
157        }
158}
159
160int mail_openmail(struct map_session_data *sd)
161{
162        nullpo_retr(0,sd);
163
164        if( sd->state.storage_flag || sd->vender_id || sd->state.trading )
165                return 0;
166
167        clif_Mail_window(sd->fd, 0);
168
169        return 1;
170}
171
172void mail_deliveryfail(struct map_session_data *sd, struct mail_message *msg)
173{
174        nullpo_retv(sd);
175        nullpo_retv(msg);
176
177        // Item  recieve (due to failure)
178        if(log_config.enable_logs&0x2000)
179                log_pick_pc(sd, "E", msg->item.nameid, msg->item.amount, &msg->item);
180
181        pc_additem(sd, &msg->item, msg->item.amount);
182       
183        if( msg->zeny > 0 )
184        {
185                //Zeny recieve (due to failure)
186                if(log_config.zeny)
187                        log_zeny(sd, "E", sd, msg->zeny);
188
189                sd->status.zeny += msg->zeny;
190                clif_updatestatus(sd, SP_ZENY);
191        }
192       
193        clif_Mail_send(sd->fd, true);
194}
195
196// This function only check if the mail operations are valid
197bool mail_invalid_operation(struct map_session_data *sd)
198{
199        if( !map[sd->bl.m].flag.town && pc_isGM(sd) < get_atcommand_level(atcommand_mail) )
200        {
201                ShowWarning("clif_parse_Mail: char '%s' trying to do invalid mail operations.\n", sd->status.name);
202                return true;
203        }
204
205        return false;
206}
207
208#endif
Note: See TracBrowser for help on using the browser.