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