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