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