]> git.pond.sub.org Git - empserver/blob - src/lib/update/deliver.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / update / deliver.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  deliver.c: Deliver commodities to neighboring sector
29  * 
30  *  Known contributors to this file:
31  *  
32  */
33
34 #include "misc.h"
35 #include "var.h"
36 #include "sect.h"
37 #include "item.h"
38 #include "path.h"
39 #include "file.h"
40 #include "xy.h"
41 #include "update.h"
42 #include "subs.h"
43 #include "common.h"
44
45 int
46 deliver(register struct sctstr *from, struct ichrstr *ip, int dir,
47         int thresh, int amt_src, int plague)
48 {
49     register struct sctstr *to;
50     int vtype;                  /* item vartype */
51     int pack_src;
52     int amt_moved;
53     int amt_dst;
54     int mobility;
55     float mcost;
56     struct dchrstr *dp;
57     int n;
58
59     if (dir <= 0 || dir > DIR_UL)
60         return 0;
61     if (amt_src <= 0)
62         return 0;
63     if ((amt_moved = amt_src - thresh) <= 0)
64         return 0;
65     /*
66      * make sure delivery looks ok.  Check where its going,
67      * where its coming from, and see if there is more than
68      * the threshold amount
69      */
70     if (!military_control(from))
71         return 0;
72     to = getsectp(from->sct_x + diroff[dir][0],
73                   from->sct_y + diroff[dir][1]);
74     if (to->sct_own != from->sct_own) {
75         wu(0, from->sct_own, "%s delivery walkout at %s\n",
76            ip->i_name, ownxy(from));
77         return 0;
78     }
79     dp = &dchr[from->sct_type];
80     vtype = ip->i_vtype;
81     pack_src = ip->i_pkg[dp->d_pkg];
82     mobility = from->sct_mobil / 2;
83     if (vtype == V_CIVIL && from->sct_own != from->sct_oldown) {
84         wu(0, from->sct_own,
85            "The conquered populace in %s refuses to relocate!\n",
86            ownxy(from));
87         return 0;
88     }
89     /*
90      * disallow delivery into prohibited sectors.
91      * calculate unit movement cost; decrease amount if
92      * there isn't enough mobility.
93      */
94     mcost = sector_mcost(to, MOB_ROAD) * ip->i_lbs / pack_src;
95     mcost /= 4.0;
96
97     if (mobility < mcost * amt_moved) {
98         /* XXX can mcost be == 0? */
99         amt_moved = (int)(mobility / mcost);
100         if (amt_moved <= 0)
101             return 0;
102     }
103     amt_dst = getvar(vtype, (caddr_t)to, EF_SECTOR);
104     if (amt_moved + amt_dst > 9990) {
105         /* delivery backlog */
106         if ((amt_moved = 9990 - amt_dst) <= 0)
107             return 0;
108     }
109     if (putvar(vtype, amt_moved + amt_dst, (s_char *)to, EF_SECTOR) < 0) {
110         /* "No room to deliver commodities */
111         wu(0, from->sct_own, "no room for %s in %s\n",
112            ip->i_name, ownxy(to));
113         return 0;
114     }
115     /* deliver the plague too! */
116     if (plague == PLG_INFECT
117         && getvar(V_PSTAGE, (s_char *)to, EF_SECTOR) == 0)
118         putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)to, EF_SECTOR);
119     n = from->sct_mobil - (int)(mcost * amt_moved);
120     if (n < 0)
121         n = 0;
122     from->sct_mobil = n;
123     return amt_moved;
124 }