]> git.pond.sub.org Git - empserver/blob - src/lib/commands/repo.c
Update copyright notice.
[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 "sect.h"
41 #include "nat.h"
42 #include "file.h"
43 #include "xy.h"
44 #include "nsc.h"
45 #include <fcntl.h>
46 #include <ctype.h>
47 #include "commands.h"
48 #include "optlist.h"
49
50 struct stats {
51     double res;
52     double tech;
53     double edu;
54     double hap;
55     int stat;
56 };
57
58 static void printdiff(int mystat, double ours, struct natstr *natp,
59                       int what);
60 static void repo_header(void);
61 static void repo_list(struct stats *stat, natid cn, struct natstr *natp);
62
63 static int check(s_char *buf, double theirs, double min, double max,
64                  int shift, int tolerance);
65
66 int
67 repo(void)
68 {
69     struct natstr *natp;
70     struct stats mystat;
71     struct natstr nat;
72     struct nstr_item ni;
73     int first;
74
75     if (!snxtitem(&ni, EF_NATION, player->argp[1]))
76         return RET_SYN;
77     prdate();
78     natp = getnatp(player->cnum);
79     memset(&mystat, 0, sizeof(struct stats));
80     mystat.stat = natp->nat_stat;
81     if (mystat.stat >= STAT_ACTIVE) {
82         mystat.res = natp->nat_level[NAT_RLEV];
83         mystat.tech = natp->nat_level[NAT_TLEV];
84         mystat.edu = natp->nat_level[NAT_ELEV];
85         mystat.hap = natp->nat_level[NAT_HLEV];
86     }
87     if (opt_HIDDEN) {
88         repo_header();
89         first = 0;
90     } else {
91         first = 1;
92     }
93     while (nxtitem(&ni, &nat)) {
94         if (nat.nat_stat == STAT_UNUSED)
95             continue;
96         if (opt_HIDDEN) {
97             if (!player->god && !getcontact(getnatp(player->cnum), ni.cur))
98                 continue;
99         }
100         if (first) {
101             repo_header();
102             first = 0;
103         }
104         repo_list(&mystat, (natid)ni.cur, &nat);
105     }
106     return RET_OK;
107 }
108
109 static void
110 repo_header(void)
111 {
112     pr(" #    name                tech      research   education   happiness ");
113     if (player->god)
114         pr("capital\n");
115     else {
116         if (opt_HIDDEN)
117             pr("\n");
118         else
119             pr(" status\n");
120     }
121 }
122
123 static void
124 repo_list(struct stats *stat, natid cn, struct natstr *natp)
125 {
126     if (player->god) {
127         pr(" %-3d   %-14.14s ", cn, natp->nat_cnam);
128         pr(" %7.2f    %7.2f      %7.2f     %7.2f",
129            natp->nat_level[NAT_TLEV],
130            natp->nat_level[NAT_RLEV],
131            natp->nat_level[NAT_ELEV], natp->nat_level[NAT_HLEV]);
132         prxy("  %4d,%-4d\n", natp->nat_xcap, natp->nat_ycap, player->cnum);
133         return;
134     }
135     if (natp->nat_stat == STAT_ACTIVE) {
136         pr(" %-3d   %-14.14s ", cn, natp->nat_cnam);
137         printdiff(stat->stat, stat->tech, natp, NAT_TLEV);
138         printdiff(stat->stat, stat->res, natp, NAT_RLEV);
139         printdiff(stat->stat, stat->edu, natp, NAT_ELEV);
140         printdiff(stat->stat, stat->hap, natp, NAT_HLEV);
141         if (opt_HIDDEN) {
142             pr("\n");
143         } else {
144             if (influx(natp))
145                 pr("In flux\n");
146             else if (natp->nat_money <= 0)
147                 pr("Broke\n");
148             else
149                 pr("Active\n");
150         }
151     }
152 }
153
154 static void
155 printdiff(int mystat, double ours, struct natstr *natp, int what)
156 {
157     double theirs;
158     int shift;
159     int tolerance;
160     s_char buf[128];
161
162     if (natp->nat_cnum == player->cnum) {
163         pr(" %7.2f    ", ours);
164         return;
165     }
166     if (ours && mystat >= STAT_ACTIVE && natp->nat_stat >= STAT_ACTIVE) {
167         theirs = natp->nat_level[what];
168         if ((shift = min((int)theirs, (int)ours) - 100) > 0) {
169             ours -= shift;
170             theirs -= shift;
171         } else
172             shift = 0;
173         switch (what) {
174         case NAT_TLEV:
175             tolerance = 20;
176             break;
177         case NAT_RLEV:
178             tolerance = 10;
179             break;
180         default:
181             tolerance = 5;
182         }
183         if (tolerance > 2 * ours)
184             tolerance = (int)(2 * ours);
185         if (check(buf, theirs, 2 * ours, -1.0, shift, tolerance))
186           ;
187         else if (check(buf, theirs, 1.5 * ours, 2.0 * ours, shift, tolerance))
188           ;
189         else if (check(buf, theirs, 1.2 * ours, 1.5 * ours, shift, tolerance))
190           ;
191         else if (check(buf, theirs, 1.1 * ours, 1.2 * ours, shift, tolerance))
192           ;
193         else if (check(buf, theirs, ours / 1.1, 1.1 * ours, shift, tolerance))
194           ;
195         else if (check(buf, theirs, ours / 1.2, ours / 1.1, shift, tolerance))
196           ;
197         else if (check(buf, theirs, ours / 1.5, ours / 1.2, shift, tolerance))
198           ;
199         else if (check(buf, theirs, ours / 2.0, ours / 1.5, shift, tolerance))
200           ;
201         else if (check(buf, theirs, -1.0, ours / 2.0, shift, tolerance)) ;
202         else
203             sprintf(buf, "    n/a");
204     } else
205         sprintf(buf, "    n/a");
206
207     pr("%-11s ", buf);
208 }
209
210 static int
211 check(s_char *buf, double theirs, double min, double max, int shift,
212       int tolerance)
213 {
214     double shove;
215
216     if (min < 0) {
217         if (theirs <= max) {
218             if (max < tolerance)
219                 max = tolerance;
220             sprintf(buf, "   0 - %d", (int)max + shift);
221             return 1;
222         }
223     } else if (max < 0) {
224         if (theirs >= min) {
225             sprintf(buf, "    >= %d", (int)min + shift);
226             return 1;
227         }
228     } else if (theirs >= min && theirs <= max) {
229         if (max - min < tolerance) {
230             shove = (tolerance - (max - min)) / 2;
231             if (min + shift - shove >= 0) {
232                 min -= shove;
233                 max += shove;
234             } else {
235                 min = 0;
236                 max = tolerance;
237             }
238         }
239         sprintf(buf, "%4d - %d", (int)min + shift, (int)max + shift);
240         return 1;
241     }
242
243     return 0;
244 }