]> git.pond.sub.org Git - empserver/blob - src/lib/commands/grin.c
(grin): Grind up whatever banks produce instead of P_BAR. This is in
[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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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     char *p;
52     int prd, i, n, qty;
53     char buf[1024];
54     double grind_eff = 0.8;
55     struct pchrstr *pp;
56     i_type ctype;
57     unsigned camt;
58
59     prd = dchr[SCT_BANK].d_prd;
60     if (prd < 0 || pchr[prd].p_type < 0) {
61         pr("Grinding is disabled.\n");
62         return RET_FAIL;
63     }
64     pp = &pchr[prd];
65
66     if ((p = getstarg(player->argp[1], "Sectors? ", buf)) == 0)
67         return RET_SYN;
68     if (!snxtsct(&nstr, p))
69         return RET_SYN;
70     if ((p = getstarg(player->argp[2], "amount :  ", buf)) == 0 || *p == 0)
71         return RET_SYN;
72     qty = atoi(p);
73     if (qty < 0)
74         return RET_SYN;
75
76     while (nxtsct(&nstr, &sect)) {
77         if (!player->owner)
78             continue;
79         if (sect.sct_effic < 60 || sect.sct_own != player->cnum)
80             continue;
81
82         /* materials limit */
83         n = MIN(qty, sect.sct_item[pp->p_type]);
84         /* work limit */
85         n = MIN(n, sect.sct_avail / 5);
86         /* space limit */
87         for (i = 0; i < MAXPRCON; i++) {
88             ctype = pp->p_ctype[i];
89             camt = pp->p_camt[i];
90             if (!camt)
91                 continue;
92             if (CANT_HAPPEN(ctype <= I_NONE || ctype > I_MAX))
93                 continue;
94             n = MIN(n,
95                     (double)(ITEM_MAX - sect.sct_item[ctype])
96                     / (camt * grind_eff));
97         }
98
99         if (n > 0) {
100             pr("%d bars ground up in %s\n", n,
101                xyas(sect.sct_x, sect.sct_y, player->cnum));
102             sect.sct_item[I_BAR] -= n;
103             for (i = 0; i < MAXPRCON; i++) {
104                 ctype = pp->p_ctype[i];
105                 camt = pp->p_camt[i];
106                 if (!camt)
107                     continue;
108                 if (CANT_HAPPEN(ctype <= I_NONE || ctype > I_MAX))
109                     continue;
110                 sect.sct_item[ctype] += n * camt * grind_eff;
111             }
112             sect.sct_avail -= n * 5;
113             putsect(&sect);
114         }
115     }
116     return RET_OK;
117 }