]> git.pond.sub.org Git - empserver/blob - src/lib/commands/repo.c
(stats): Remove, simply pass around natstr * instead.
[empserver] / src / lib / commands / repo.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  repo.c: Report on various levels (tech, research) of other nations
29  * 
30  *  Known contributors to this file:
31  *     Keith Muller, 1983
32  *     Dave Pare, 1986 (rewrite)
33  *     Steve McClure, 2000
34  */
35
36 #include <config.h>
37
38 #include "misc.h"
39 #include "player.h"
40 #include "nat.h"
41 #include "file.h"
42 #include "commands.h"
43 #include "optlist.h"
44
45 static void repo_header(void);
46 static void repo_list(struct natstr *, struct natstr *);
47 static void printdiff(struct natstr *, struct natstr *, int what);
48 static int check(char *buf, double theirs, double min, double max,
49                  int shift, int tolerance);
50
51 int
52 repo(void)
53 {
54     struct natstr *natp;
55     struct natstr nat;
56     struct nstr_item ni;
57
58     if (!snxtitem(&ni, EF_NATION, player->argp[1]))
59         return RET_SYN;
60     prdate();
61     natp = getnatp(player->cnum);
62     repo_header();
63     while (nxtitem(&ni, &nat)) {
64         if (nat.nat_stat == STAT_UNUSED)
65             continue;
66         if (opt_HIDDEN) {
67             if (!player->god && !getcontact(natp, ni.cur))
68                 continue;
69         }
70         if (!player->god && nat.nat_stat != STAT_ACTIVE)
71             continue;
72         repo_list(natp, &nat);
73     }
74     return RET_OK;
75 }
76
77 static void
78 repo_header(void)
79 {
80     pr(" #    name                tech      research   education   happiness"
81        " %s\n",
82        player->god ? "capital" : " status");
83 }
84
85 static void
86 repo_list(struct natstr *plnatp, struct natstr *natp)
87 {
88     pr(" %-3d   %-14.14s ", natp->nat_cnum, natp->nat_cnam);
89     if (player->god || player->cnum == natp->nat_cnum) {
90         pr(" %7.2f     %7.2f     %7.2f     %7.2f%s",
91            natp->nat_level[NAT_TLEV], natp->nat_level[NAT_RLEV],
92            natp->nat_level[NAT_ELEV], natp->nat_level[NAT_HLEV],
93            player->god ? "" : "    ");
94     } else {
95         printdiff(plnatp, natp, NAT_TLEV);
96         printdiff(plnatp, natp, NAT_RLEV);
97         printdiff(plnatp, natp, NAT_ELEV);
98         printdiff(plnatp, natp, NAT_HLEV);
99     }
100     if (player->god) {
101         prxy("  %4d,%-4d\n", natp->nat_xcap, natp->nat_ycap, player->cnum);
102     } else {
103         if (!opt_HIDDEN && influx(natp))
104             pr("In flux\n");
105         else if (!opt_HIDDEN && natp->nat_money <= 0)
106             pr("Broke\n");
107         else
108             pr("Active\n");
109     }
110 }
111
112 static void
113 printdiff(struct natstr *plnatp, struct natstr *natp, int what)
114 {
115     double ours = plnatp->nat_level[what];
116     double theirs;
117     int shift;
118     int tolerance;
119     char buf[128];
120
121     if (ours
122         && plnatp->nat_stat >= STAT_ACTIVE && natp->nat_stat >= STAT_ACTIVE) {
123         theirs = natp->nat_level[what];
124         if ((shift = min((int)theirs, (int)ours) - 100) > 0) {
125             ours -= shift;
126             theirs -= shift;
127         } else
128             shift = 0;
129         switch (what) {
130         case NAT_TLEV:
131             tolerance = 20;
132             break;
133         case NAT_RLEV:
134             tolerance = 10;
135             break;
136         default:
137             tolerance = 5;
138         }
139         if (tolerance > 2 * ours)
140             tolerance = (int)(2 * ours);
141         if (check(buf, theirs, 2 * ours, -1.0, shift, tolerance))
142           ;
143         else if (check(buf, theirs, 1.5 * ours, 2.0 * ours, shift, tolerance))
144           ;
145         else if (check(buf, theirs, 1.2 * ours, 1.5 * ours, shift, tolerance))
146           ;
147         else if (check(buf, theirs, 1.1 * ours, 1.2 * ours, shift, tolerance))
148           ;
149         else if (check(buf, theirs, ours / 1.1, 1.1 * ours, shift, tolerance))
150           ;
151         else if (check(buf, theirs, ours / 1.2, ours / 1.1, shift, tolerance))
152           ;
153         else if (check(buf, theirs, ours / 1.5, ours / 1.2, shift, tolerance))
154           ;
155         else if (check(buf, theirs, ours / 2.0, ours / 1.5, shift, tolerance))
156           ;
157         else if (check(buf, theirs, -1.0, ours / 2.0, shift, tolerance)) ;
158         else
159             sprintf(buf, "    n/a");
160     } else
161         sprintf(buf, "    n/a");
162
163     pr("%-11s ", buf);
164 }
165
166 static int
167 check(char *buf, double theirs, double min, double max, int shift,
168       int tolerance)
169 {
170     double shove;
171
172     if (min < 0) {
173         if (theirs <= max) {
174             if (max < tolerance)
175                 max = tolerance;
176             sprintf(buf, "   0 - %d", (int)max + shift);
177             return 1;
178         }
179     } else if (max < 0) {
180         if (theirs >= min) {
181             sprintf(buf, "    >= %d", (int)min + shift);
182             return 1;
183         }
184     } else if (theirs >= min && theirs <= max) {
185         if (max - min < tolerance) {
186             shove = (tolerance - (max - min)) / 2;
187             if (min + shift - shove >= 0) {
188                 min -= shove;
189                 max += shove;
190             } else {
191                 min = 0;
192                 max = tolerance;
193             }
194         }
195         sprintf(buf, "%4d - %d", (int)min + shift, (int)max + shift);
196         return 1;
197     }
198
199     return 0;
200 }