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