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