]> git.pond.sub.org Git - empserver/blob - src/lib/update/sect.c
Update copyright notice
[empserver] / src / lib / update / sect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, 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  *  sect.c: Do production for sectors
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1996
32  *     Markus Armbruster, 2004-2016
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "item.h"
39 #include "nat.h"
40 #include "optlist.h"
41 #include "player.h"
42 #include "prototypes.h"
43 #include "update.h"
44
45 double
46 buildeff(struct sctstr *sp)
47 {
48     int avail = sp->sct_avail / 2 * 100;
49     double cost;
50     int delta, build;
51     struct dchrstr *dcp;
52
53     cost = 0.0;
54
55     if (sp->sct_type != sp->sct_newtype) {
56         /*
57          * Tear down existing sector.
58          * Easier to destroy than to build.
59          */
60         dcp = &dchr[sp->sct_type];
61         build = 4 * avail / dcp->d_bwork;
62         if (build <= sp->sct_effic)
63             sp->sct_effic -= build;
64         else {
65             build = sp->sct_effic;
66             sp->sct_effic = 0;
67             sp->sct_type = sp->sct_newtype;
68         }
69         avail -= roundavg(build / 4.0 * dcp->d_bwork);
70         cost += build / 4.0;
71     }
72
73     if (sp->sct_type == sp->sct_newtype) {
74         dcp = &dchr[sp->sct_type];
75         delta = avail / dcp->d_bwork;
76         if (delta > 100 - sp->sct_effic)
77             delta = 100 - sp->sct_effic;
78         build = get_materials(sp, dcp->d_mat, delta);
79         sp->sct_effic += build;
80         avail -= build * dcp->d_bwork;
81         cost += build * dcp->d_cost / 100.0;
82     }
83
84     sp->sct_avail = (sp->sct_avail + 1) / 2 + avail / 100;
85     return cost;
86 }
87
88 /*
89  * enlistment sectors are special; they require military
90  * to convert civ into mil in large numbers.
91  * Conversion will happen much more slowly without
92  * some mil initially.
93  */
94 static void
95 enlist(struct natstr *np, short *vec, int etu)
96 {
97     int maxmil;
98     int enlisted;
99
100     enlisted = 0;
101     maxmil = vec[I_CIVIL] / 2 - vec[I_MILIT];
102     if (maxmil > 0) {
103         enlisted = etu * (10 + vec[I_MILIT]) * 0.05;
104         if (enlisted > maxmil)
105             enlisted = maxmil;
106         vec[I_CIVIL] -= enlisted;
107         vec[I_MILIT] += enlisted;
108     }
109
110     nat_budget[np->nat_cnum].prod[SCT_ENLIST].count += enlisted;
111     nat_budget[np->nat_cnum].prod[SCT_ENLIST].money -= enlisted * 3;
112     nat_budget[np->nat_cnum].money -= enlisted * 3;
113 }
114
115 /*
116  * Produce for a specific nation
117  */
118 void
119 produce_sect(int etu, struct bp *bp)
120 {
121     struct budget *budget;
122     struct natstr *np;
123     struct sctstr *sp, scratch_sect;
124     int n;
125     double cost;
126
127     for (n = 0; NULL != (sp = getsectid(n)); n++) {
128         if (bp_skip_sect(bp, sp))
129             continue;
130         if (sp->sct_type == SCT_WATER || sp->sct_type == SCT_SANCT)
131             continue;
132
133         /*
134          * When running the test suite, reseed PRNG for each sector
135          * with its UID, to keep results stable even when the number
136          * of PRNs consumed changes.
137          */
138         if (running_test_suite)
139             seed_prng(sp->sct_uid);
140
141         if (player->simulation) {
142             /* work on a copy, which will be discarded */
143             scratch_sect = *sp;
144             bp_to_sect(bp, &scratch_sect);
145             sp = &scratch_sect;
146         }
147
148         budget = &nat_budget[sp->sct_own];
149         np = getnatp(sp->sct_own);
150
151         if (dchr[sp->sct_type].d_maint) {
152             cost = etu * dchr[sp->sct_type].d_maint;
153             budget->bm[BUDG_SCT_MAINT].count++;
154             budget->bm[BUDG_SCT_MAINT].money -= cost;
155             budget->money -= cost;
156         }
157
158         if (sp->sct_off || budget->money < 0) {
159             sp->sct_avail = 0;
160             bp_set_from_sect(bp, sp);
161             continue;
162         }
163
164         if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
165             budget->money >= 0) {
166             cost = buildeff(sp);
167             budget->bm[BUDG_SCT_BUILD].count++;
168             budget->bm[BUDG_SCT_BUILD].money -= cost;
169             budget->money -= cost;
170         }
171
172         if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
173             sp->sct_own == sp->sct_oldown) {
174             enlist(np, sp->sct_item, etu);
175         }
176
177         /*
178          * now do the production (if sector effic >= 60%)
179          */
180
181         if (sp->sct_effic >= 60) {
182             if (budget->money >= 0 && dchr[sp->sct_type].d_prd >= 0)
183                 produce(np, sp);
184         }
185
186         bp_set_from_sect(bp, sp);
187     }
188 }