]> git.pond.sub.org Git - empserver/blob - src/lib/common/btu.c
06486124ec6e31db825db8b57e435ec95b7e3622
[empserver] / src / lib / common / btu.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  *  btu.c: Dealing with BTUs
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2007
31  */
32
33 #include <config.h>
34
35 #include "file.h"
36 #include "nat.h"
37 #include "optlist.h"
38 #include "prototypes.h"
39 #include "sect.h"
40
41 /*
42  * Return BTUs produced by CAP in ETU ETUs.
43  */
44 static int
45 accrued_btus(struct sctstr *cap, int etu)
46 {
47     double eff, civ;
48
49     switch (cap->sct_type) {
50     case SCT_CAPIT:
51     case SCT_SANCT:
52         eff = cap->sct_effic;
53         break;
54     case SCT_MOUNT:
55         eff = 0;
56         break;
57     default:
58         return 0;
59     }
60
61     eff *= cap->sct_work / 100.0;
62     if (eff < 0.5)
63         eff = 0.5;
64
65     civ = cap->sct_item[I_CIVIL];
66     if (civ > 1000)
67         civ = 1000;
68
69     return roundavg(etu * civ * eff * btu_build_rate);
70 }
71
72 /*
73  * Grant nation NP the BTUs produced by its capital in ETU ETUs.
74  * Return whether it has a capital.
75  */
76 int
77 grant_btus(struct natstr *np, int etu)
78 {
79     int has_cap, delta;
80     struct sctstr sect;
81
82     getsect(np->nat_xcap, np->nat_ycap, &sect);
83     has_cap = np->nat_stat >= STAT_ACTIVE && !influx(np);
84
85     if (has_cap) {
86         delta = accrued_btus(&sect, etu);
87         if (delta + np->nat_btu > max_btus)
88             np->nat_btu = max_btus;
89         else
90             np->nat_btu += delta;
91     }
92     if (np->nat_stat == STAT_VIS || np->nat_stat == STAT_GOD)
93         np->nat_btu = max_btus;
94
95     return has_cap;
96 }