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