]> git.pond.sub.org Git - empserver/blob - src/lib/update/deliver.c
Clean up dead stores
[empserver] / src / lib / update / deliver.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <config.h>
35
36 #include "item.h"
37 #include "path.h"
38 #include "plague.h"
39 #include "update.h"
40
41 #define DELIVER_BONUS 4.0
42
43 static int
44 deliver(struct sctstr *from, struct ichrstr *ip, int dir,
45         int thresh, int amt_src, int plague, enum i_packing packing)
46 {
47     struct sctstr *to;
48     i_type vtype;                       /* item vartype */
49     int amt_moved;
50     int amt_dst;
51     int mobility;
52     double mcost;
53     int n;
54
55     if (dir <= 0 || dir > DIR_UL)
56         return 0;
57     if (amt_src <= 0)
58         return 0;
59     if ((amt_moved = amt_src - thresh) <= 0)
60         return 0;
61     /*
62      * make sure delivery looks ok.  Check where its going,
63      * where its coming from, and see if there is more than
64      * the threshold amount
65      */
66     if (!military_control(from))
67         return 0;
68     to = getsectp(from->sct_x + diroff[dir][0],
69                   from->sct_y + diroff[dir][1]);
70     if (to->sct_own != from->sct_own) {
71         wu(0, from->sct_own, "%s delivery walkout at %s\n",
72            ip->i_name, ownxy(from));
73         return 0;
74     }
75     vtype = ip->i_uid;
76     mobility = from->sct_mobil / 2;
77     if (vtype == I_CIVIL) {
78         if (from->sct_own != from->sct_oldown) {
79             wu(0, from->sct_own,
80                "The conquered populace in %s refuses to relocate!\n",
81                ownxy(from));
82             return 0;
83         }
84         if (to->sct_own != to->sct_oldown) {
85             wu(0, from->sct_own,
86                "Citizens in %s refuse to relocate!\n", ownxy(from));
87             return 0;
88         }
89     }
90     /*
91      * disallow delivery into prohibited sectors.
92      * calculate unit movement cost; decrease amount if
93      * there isn't enough mobility.
94      */
95     mcost = sector_mcost(to, MOB_MOVE) * ip->i_lbs / ip->i_pkg[packing];
96     mcost /= DELIVER_BONUS;
97
98     if (mobility < mcost * amt_moved) {
99         /* XXX can mcost be == 0? */
100         amt_moved = (int)(mobility / mcost);
101         if (amt_moved <= 0)
102             return 0;
103     }
104     amt_dst = to->sct_item[vtype];
105     if (amt_moved > ITEM_MAX - amt_dst) {
106         /* delivery backlog */
107         amt_moved = ITEM_MAX - amt_dst;
108     }
109     to->sct_item[vtype] = amt_moved + amt_dst;
110     /* deliver the plague too! */
111     if (plague == PLG_INFECT && to->sct_pstage == PLG_HEALTHY)
112         to->sct_pstage = PLG_EXPOSED;
113     n = from->sct_mobil - (int)(mcost * amt_moved);
114     if (n < 0)
115         n = 0;
116     from->sct_mobil = n;
117     return amt_moved;
118 }
119
120 void
121 dodeliver(struct sctstr *sp)
122 {
123     i_type i;
124     int thresh;
125     int dir;
126     int plague;
127     enum i_packing packing;
128     int n;
129
130     if (sp->sct_mobil <= 0)
131         return;
132     plague = sp->sct_pstage;
133     packing = sp->sct_effic >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
134     for (i = I_NONE + 1; i <= I_MAX; i++) {
135         if (sp->sct_del[i] == 0)
136             continue;
137         thresh = sp->sct_del[i] & ~0x7;
138         dir = sp->sct_del[i] & 0x7;
139         n = deliver(sp, &ichr[i], dir, thresh, sp->sct_item[i],
140                     plague, packing);
141         if (n > 0) {
142             sp->sct_item[i] -= n;
143             if (sp->sct_mobil <= 0)
144                 break;
145         }
146     }
147 }