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