root/src/map/mail.c @ 19

Revision 1, 4.0 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                sd->status.zeny -= sd->mail.zeny;
59
60        sd->mail.zeny = 0;
61        clif_updatestatus(sd, SP_ZENY);
62
63        return 1;
64}
65
66unsigned char mail_setitem(struct map_session_data *sd, int idx, int amount)
67{
68        if (idx == 0)
69        { // Zeny Transfer
70                if( amount < 0 )
71                        return 0;
72                if( amount > sd->status.zeny )
73                        amount = sd->status.zeny;
74
75                if( !pc_can_give_items(pc_isGM(sd)) )
76                        amount = 0;
77
78                sd->mail.zeny = amount;
79                // clif_updatestatus(sd, SP_ZENY);
80                return 0;
81        }
82        else
83        { // Item Transfer
84                idx -= 2;
85                mail_removeitem(sd, 0);
86
87                if( idx < 0 || idx >= MAX_INVENTORY )
88                        return 1;
89                if( amount < 0 || amount > sd->status.inventory[idx].amount )
90                        return 1;
91                if( !pc_candrop(sd, &sd->status.inventory[idx]) )
92                        return 1;
93
94                sd->mail.index = idx;
95                sd->mail.nameid = sd->status.inventory[idx].nameid;
96                sd->mail.amount = amount;
97               
98                return 0;
99        }
100}
101
102bool mail_setattachment(struct map_session_data *sd, struct mail_message *msg)
103{
104        int n;
105       
106        nullpo_retr(false,sd);
107        nullpo_retr(false,msg);
108
109        if( sd->mail.zeny < 0 || sd->mail.zeny > sd->status.zeny )
110                return false;
111
112        n = sd->mail.index;
113        if( sd->mail.amount )
114        {
115                if( sd->status.inventory[n].nameid != sd->mail.nameid )
116                        return false;
117
118                if( sd->status.inventory[n].amount < sd->mail.amount )
119                        return false;
120
121                memcpy(&msg->item, &sd->status.inventory[n], sizeof(struct item));
122                msg->item.amount = sd->mail.amount;
123        }
124        else
125                memset(&msg->item, 0x00, sizeof(struct item));
126
127        msg->zeny = sd->mail.zeny;
128
129        // Removes the attachment from sender
130        mail_removeitem(sd,1);
131        mail_removezeny(sd,1);
132
133        return true;
134}
135
136void mail_getattachment(struct map_session_data* sd, int zeny, struct item* item)
137{
138        if( item->nameid > 0 && item->amount > 0 )
139        {
140                pc_additem(sd, item, item->amount);
141
142                if(log_config.enable_logs&0x2000)
143                        log_pick_pc(sd, "E", item->nameid, item->amount, item);
144
145                clif_Mail_getattachment(sd->fd, 0);
146        }
147
148        if( zeny > 0 )
149                pc_getzeny(sd, zeny);
150}
151
152int mail_openmail(struct map_session_data *sd)
153{
154        nullpo_retr(0,sd);
155
156        if( sd->state.storage_flag || sd->vender_id || sd->state.trading )
157                return 0;
158
159        clif_Mail_window(sd->fd, 0);
160
161        return 1;
162}
163
164void mail_deliveryfail(struct map_session_data *sd, struct mail_message *msg)
165{
166        nullpo_retv(sd);
167        nullpo_retv(msg);
168
169        pc_additem(sd, &msg->item, msg->item.amount);
170       
171        if( msg->zeny > 0 )
172        {
173                sd->status.zeny += msg->zeny;
174                clif_updatestatus(sd, SP_ZENY);
175        }
176       
177        clif_Mail_send(sd->fd, true);
178}
179
180// This function only check if the mail operations are valid
181bool mail_invalid_operation(struct map_session_data *sd)
182{
183        if( !map[sd->bl.m].flag.town && pc_isGM(sd) < get_atcommand_level(atcommand_mail) )
184        {
185                ShowWarning("clif_parse_Mail: char '%s' trying to do invalid mail operations.\n", sd->status.name);
186                return true;
187        }
188
189        return false;
190}
191
192#endif
Note: See TracBrowser for help on using the browser.