]> git.pond.sub.org Git - empserver/blob - src/lib/commands/grin.c
ed7f32e2e44474c0d8c7242f831c402260a7f886
[empserver] / src / lib / commands / grin.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  *  grin.c: Grind gold bars into dust
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include <config.h>
35
36 #include "misc.h"
37 #include "player.h"
38 #include "xy.h"
39 #include "sect.h"
40 #include "nat.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "product.h"
44 #include "commands.h"
45
46 int
47 grin(void)
48 {
49     struct nstr_sect nstr;
50     struct sctstr sect;
51     s_char *p;
52     int i, n, qty;
53     int avail;
54     s_char buf[1024];
55     double grind_eff = 0.8;
56
57     if ((p = getstarg(player->argp[1], "Sectors? ", buf)) == 0)
58         return RET_SYN;
59     if (!snxtsct(&nstr, p))
60         return RET_SYN;
61     if ((p = getstarg(player->argp[2], "amount :  ", buf)) == 0 || *p == 0)
62         return RET_SYN;
63     qty = atoi(p);
64     if (qty < 0)
65         return RET_SYN;
66     while (nxtsct(&nstr, &sect)) {
67         if (!player->owner)
68             continue;
69         if (sect.sct_effic < 60 || sect.sct_own != player->cnum)
70             continue;
71         n = sect.sct_item[I_BAR] >= qty ? qty : sect.sct_item[I_BAR];
72         /* work limit */
73         avail = n * 5;
74         if (avail > sect.sct_avail) {
75             n = sect.sct_avail / 5;
76             avail = sect.sct_avail;
77         }
78         /* space limit */
79         for (i = 0; i < MAXPRCON; i++) {
80             if (!pchr[P_BAR].p_camt[i])
81                 continue;
82             if (CANT_HAPPEN(pchr[P_BAR].p_ctype[i] <= I_NONE ||
83                             pchr[P_BAR].p_ctype[i] > I_MAX))
84                 continue;
85             n = MIN(n,
86                     (double)(ITEM_MAX - sect.sct_item[pchr[P_BAR].p_ctype[i]])
87                     / (pchr[P_BAR].p_camt[i] * grind_eff));
88         }
89
90         if (n > 0) {
91             pr("%d bars ground up in %s\n", n,
92                xyas(sect.sct_x, sect.sct_y, player->cnum));
93             sect.sct_item[I_BAR] -= n;
94             for (i = 0; i < MAXPRCON; i++) {
95                 if (!pchr[P_BAR].p_camt[i])
96                     continue;
97                 if (CANT_HAPPEN(pchr[P_BAR].p_ctype[i] <= I_NONE ||
98                                 pchr[P_BAR].p_ctype[i] > I_MAX))
99                     continue;
100                 sect.sct_item[pchr[P_BAR].p_ctype[i]]
101                     += n * pchr[P_BAR].p_camt[i] * grind_eff;
102             }
103             sect.sct_avail -= avail;
104             putsect(&sect);
105         }
106     }
107     return RET_OK;
108 }