]> git.pond.sub.org Git - empserver/blob - src/lib/commands/stre.c
f5f6b022c131f39179391829e66179d78fcdf183
[empserver] / src / lib / commands / stre.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  *  stre.c: Calculate military strengths of sectors
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  */
33
34 #include <config.h>
35
36 #include "misc.h"
37 #include "player.h"
38 #include "file.h"
39 #include "sect.h"
40 #include "path.h"
41 #include "nat.h"
42 #include "xy.h"
43 #include "land.h"
44 #include "nsc.h"
45 #include "ship.h"
46 #include "combat.h"
47 #include "commands.h"
48
49 static double units_in_sector(struct combat *def);
50 static void stre_hdr(void);
51
52 int
53 stre(void)
54 {
55     struct sctstr sect;
56     int nsect = 0;
57     struct nstr_sect nstr;
58     double dtotal, r_total, eff;
59     struct combat def[1];
60     int dummy;
61
62     if (!snxtsct(&nstr, player->argp[1]))
63         return RET_SYN;
64     prdate();
65     nsect = 0;
66     att_combat_init(def, EF_SECTOR);
67     while (nxtsct(&nstr, &sect)) {
68         if (!player->owner)
69             continue;
70         if (nsect++ == 0)
71             stre_hdr();
72         if (player->god)
73             pr("%3d ", sect.sct_own);
74         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
75         pr(" %c", dchr[sect.sct_type].d_mnem);
76         pr("%4d%%", sect.sct_effic);
77         def->x = nstr.x;
78         def->y = nstr.y;
79         def->set = 0;
80         att_get_combat(def, 1);
81         if (def->mil)
82             pr("%5d", def->mil);
83         else
84             pr("%5s", "");
85         dtotal = units_in_sector(def);
86         if (dtotal > 0)
87             pr("%7d", (int)dtotal);
88         else
89             pr("%7s", "");
90
91         if (def->sct_type != SCT_MOUNT)
92             r_total = att_reacting_units(def, 0, 0, &dummy, 9999999);
93         else
94             r_total = 0.0;
95         def->own = 0;
96         eff = att_combat_eff(def);
97         if (sect.sct_own == sect.sct_oldown || player->god) {
98             if (sect.sct_mines > 0)
99                 pr("%7d", sect.sct_mines);
100             else
101                 pr("%7s", "");
102             eff *= (1.0 + MIN(sect.sct_mines, 20) * 0.02);
103         } else {
104             pr("%7s", "?");
105         }
106         pr("%6.2f", eff);
107         pr("%9d", (int)((dtotal + def->mil) * eff));
108         if (r_total > 0)
109             pr(" %9d", (int)(r_total * eff));
110         else
111             pr(" %9s", "");
112         pr("%9d\n", (int)((dtotal + def->mil + r_total) * eff));
113         /*
114          * This command is quite compute intensive.  Yield the
115          * processor after every sector, to keep the game responsive
116          * for other players.
117          */
118         empth_yield();
119     }
120     if (!nsect) {
121         if (player->argp[1])
122             pr("%s: No sector(s)\n", player->argp[1]);
123         else
124             pr("%s: No sector(s)\n", "");
125         return RET_FAIL;
126     } else
127         pr("%d sector%s\n", nsect, splur(nsect));
128     return 0;
129 }
130
131 static double
132 units_in_sector(struct combat *def)
133 {
134     double d_unit;
135     double dtotal = 0.0;
136     struct nstr_item ni;
137     struct lndstr land;
138
139     snxtitem_xy(&ni, EF_LAND, def->x, def->y);
140     while (nxtitem(&ni, &land)) {
141         if (land.lnd_own == 0)
142             continue;
143         if (land.lnd_own != def->own)
144             continue;
145         if (land.lnd_ship >= 0)
146             continue;
147         d_unit = defense_val(&land);
148         if (!has_supply(&land))
149             d_unit /= 2.0;
150         dtotal += d_unit;
151     }
152     return dtotal;
153 }
154
155 static void
156 stre_hdr(void)
157 {
158     if (player->god)
159         pr("    ");
160     pr("DEFENSE STRENGTH               land  sect   sector  reacting    total\n");
161     if (player->god)
162         pr("own ");
163     pr("  sect       eff  mil  units  mines  mult  defense     units  defense\n");
164 }