]> 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-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  populace.c: Return workforce available
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  */
33
34 #include <config.h>
35
36 #include "land.h"
37 #include "lost.h"
38 #include "path.h"
39 #include "update.h"
40
41 void
42 populace(struct natstr *np, struct sctstr *sp, int etu)
43 {
44     double hap, pct;
45     int n;
46     int civ = sp->sct_item[I_CIVIL];
47     int mil = sp->sct_item[I_MILIT];
48
49     if (civ == 0 && mil > 0) {
50         sp->sct_work = 100;
51         sp->sct_loyal = 0;
52         sp->sct_oldown = sp->sct_own;
53     }
54     if (!civ && !mil && !sp->sct_item[I_UW]
55         && !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
56         makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
57         sp->sct_own = 0;
58         sp->sct_oldown = 0;
59         return;
60     }
61     if (sp->sct_own != sp->sct_oldown && sp->sct_loyal == 0) {
62         sp->sct_oldown = sp->sct_own;
63     }
64
65     hap = np->nat_level[NAT_HLEV];
66     pct = hap_req(np);
67     if (sp->sct_own == sp->sct_oldown && hap < pct &&
68         chance((pct - hap) / 5.0)) {
69         /*
70          * zap the loyalty of unhappy civilians.
71          * there is a 20% chance per hap point below the
72          * "recommended" amount of this happening.
73          */
74         n = roundavg(etu * 0.125);
75         if (n == 0)
76             n = 1;
77         n = sp->sct_loyal + (random() % n) + 1;
78         if (n > 127)
79             n = 127;
80         sp->sct_loyal = n;
81     }
82     if (sp->sct_loyal > 65 && mil < civ / 20) {
83         int work_red;
84
85         work_red = sp->sct_loyal - (50 + (random() % 15));
86         n = sp->sct_work - work_red;
87         if (n < 0)
88             n = 0;
89         sp->sct_work = n;
90         if (chance(work_red / 1000.0)) {
91             /*
92              * small chance of rebellion...
93              * if work_red is (max) 67,
94              * then revolt chance is 6.7%
95              */
96             revolt(sp);
97         } else if (chance(.30) && sp->sct_own)
98             wu(0, sp->sct_own, "Civil unrest in %s!\n", ownxy(sp));
99     }
100     if (sp->sct_loyal) {
101         n = sp->sct_loyal;
102         if (chance(0.75))
103             n -= roundavg(etu * 0.25);
104         else
105             n += roundavg(etu * 0.125);
106         if (n < 0)
107             n = 0;
108         else if (n > 127)
109             n = 127;
110         sp->sct_loyal = n;
111         if (sp->sct_loyal == 0) {
112             if (sp->sct_oldown != sp->sct_own) {
113                 wu(0, sp->sct_own,
114                    "Sector %s is now fully yours\n", ownxy(sp));
115                 sp->sct_oldown = sp->sct_own;
116             }
117         }
118     }
119     return;
120 }
121
122 int
123 total_work(int sctwork, int etu, int civil, int milit, int uw, int maxpop)
124 {
125     if (civil > maxpop)
126         civil = maxpop;
127     if (milit > maxpop)
128         milit = maxpop;
129     if (uw > maxpop)
130         uw = maxpop;
131
132     return (civil * sctwork / 100.0 + milit * 0.4 + uw) * etu / 100.0;
133 }