]> git.pond.sub.org Git - empserver/blob - src/lib/update/populace.c
Update copyright notice
[empserver] / src / lib / update / populace.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  *  populace.c: Return workforce available
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Markus Armbruster, 2004-2016
32  */
33
34 #include <config.h>
35
36 #include "chance.h"
37 #include "lost.h"
38 #include "optlist.h"
39 #include "prototypes.h"
40 #include "nat.h"
41 #include "sect.h"
42 #include "update.h"
43
44 void
45 check_pop_loss(struct sctstr *sp)
46 {
47     int civ = sp->sct_item[I_CIVIL];
48     int mil = sp->sct_item[I_MILIT];
49
50     if (!civ) {
51         sp->sct_work = 100;
52         sp->sct_loyal = 0;
53         sp->sct_oldown = sp->sct_own;
54     }
55     if (sp->sct_own && !civ && !mil
56         && !has_units(sp->sct_x, sp->sct_y, sp->sct_own)) {
57         makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
58         sp->sct_own = 0;
59         sp->sct_oldown = 0;
60         sp->sct_mobil = 0;
61     }
62 }
63
64 void
65 populace(struct sctstr *sp, int etu)
66 {
67     struct natstr *np = getnatp(sp->sct_own);
68     double hap, pct;
69     int n;
70     int civ = sp->sct_item[I_CIVIL];
71     int mil = sp->sct_item[I_MILIT];
72
73     if (!civ && !mil && !sp->sct_item[I_UW])
74         return;
75
76     if (sp->sct_own != sp->sct_oldown && sp->sct_loyal == 0) {
77         sp->sct_oldown = sp->sct_own;
78     }
79
80     hap = np->nat_level[NAT_HLEV];
81     pct = hap_req(np);
82     if (sp->sct_own == sp->sct_oldown && hap < pct &&
83         chance((pct - hap) / 5.0)) {
84         /*
85          * zap the loyalty of unhappy civilians.
86          * there is a 20% chance per hap point below the
87          * "recommended" amount of this happening.
88          */
89         n = roundavg(etu * 0.125);
90         if (n == 0)
91             n = 1;
92         n = sp->sct_loyal + roll(n);
93         if (n > 127)
94             n = 127;
95         sp->sct_loyal = n;
96     }
97     if (sp->sct_loyal > 65 && mil < civ / 20) {
98         int work_red;
99
100         work_red = sp->sct_loyal - (49 + roll(15));
101         n = sp->sct_work - work_red;
102         if (n < 0)
103             n = 0;
104         sp->sct_work = n;
105         if (chance(work_red / 1000.0)) {
106             /*
107              * small chance of rebellion...
108              * if work_red is (max) 67,
109              * then revolt chance is 6.7%
110              */
111             revolt(sp);
112         } else if (chance(.30) && sp->sct_own)
113             wu(0, sp->sct_own, "Civil unrest in %s!\n", ownxy(sp));
114     }
115     if (sp->sct_loyal) {
116         n = sp->sct_loyal;
117         if (chance(0.75))
118             n -= roundavg(etu * 0.25);
119         else
120             n += roundavg(etu * 0.125);
121         sp->sct_loyal = LIMIT_TO(n, 0, 127);
122         if (sp->sct_loyal == 0) {
123             if (sp->sct_oldown != sp->sct_own) {
124                 wu(0, sp->sct_own,
125                    "Sector %s is now fully yours\n", ownxy(sp));
126                 sp->sct_oldown = sp->sct_own;
127             }
128         }
129     }
130     return;
131 }
132
133 int
134 total_work(int sctwork, int etu, int civil, int milit, int uw,
135            int maxworkers)
136 {
137     if (civil > maxworkers)
138         civil = maxworkers;
139     if (milit > maxworkers)
140         milit = maxworkers;
141     if (uw > maxworkers)
142         uw = maxworkers;
143
144     return roundavg((civil * sctwork / 100.0 + milit / 2.5 + uw)
145                     * etu / 100.0);
146 }