]> git.pond.sub.org Git - empserver/blob - src/lib/subs/snxtitem.c
d11138892759043780c89ebdaae336a9ec20ceda
[empserver] / src / lib / subs / snxtitem.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  *  snxtitem.c: Arrange item selection using one of many criteria.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "var.h"
37 #include "xy.h"
38 #include "sect.h"
39 #include "nsc.h"
40 #include "file.h"
41 #include "com.h"
42 #include "prototypes.h"
43
44 /*
45  * setup the nstr structure for sector selection.
46  * can select on NS_ALL, NS_AREA, NS_DIST, and NS_LIST.
47  * iterate thru the "condarg" string looking
48  * for arguments to compile into the nstr.
49  */
50 int
51 snxtitem(register struct nstr_item *np, int type, s_char *str)
52 {
53         register s_char *cp;
54         struct  range range;
55         int     list[NS_LSIZE];
56         int     n;
57         coord   cx, cy;
58         int     dist;
59         int     flags;
60         s_char  natnumber[16];
61         s_char  prompt[128];
62         s_char  buf[1024];
63
64         np->type = NS_UNDEF;
65         np->sel = NS_UNDEF;
66         if (str == 0) {
67                 sprintf(prompt, "%s(s)? ", ef_nameof(type));
68                 str = getstring(prompt, buf);
69                 if (str == 0)
70                         return 0;
71         }
72         if (*str == 0) {
73                 /* str present, but only <cr>: nil string passed by player */
74                 return 0;
75         }
76         if (type == EF_NATION && isalpha(*str)) {
77                 sprintf(natnumber, "%d", natarg(str, ""));
78                 str = natnumber;
79         }
80         flags = ef_flags(type);
81         switch (sarg_type(str)) {
82         case NS_AREA:
83                 if (!(flags & EFF_XY))
84                         return 0;
85                 if (!sarg_area(str, &range))
86                         return 0;
87                 snxtitem_area(np, type, &range);
88                 break;
89         case NS_DIST:
90                 if (!(flags & EFF_XY))
91                         return 0;
92                 if (!sarg_range(str, &cx, &cy, &dist))
93                         return 0;
94                 snxtitem_dist(np, type, cx, cy, dist);
95                 break;
96         case NS_ALL:
97                 snxtitem_all(np, type);
98                 break;
99         case NS_LIST:
100                 if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
101                         return 0;
102                 if (!snxtitem_list(np, type, list, n))
103                         return 0;
104                 break;
105         case NS_XY:
106                 if (!(flags & EFF_XY))
107                         return 0;
108                 if (!sarg_xy(str, &cx, &cy))
109                         return 0;
110                 snxtitem_xy(np, type, cx, cy);
111                 break;
112         case NS_GROUP:
113                 if (!(flags & EFF_GROUP))
114                         return 0;
115                 snxtitem_group(np, type, *str);
116                 break;
117         default:
118                 return 0;
119         }
120         np->flags = flags;
121         if (player->condarg == 0)
122                 return 1;
123         cp = player->condarg;
124         while ((cp = nstr_comp(np->cond, &np->ncond, type, cp)) && *cp)
125                 ;
126         if (cp == 0)
127                 return 0;
128         return 1;
129 }
130
131 /*
132  * The rest of these (snxtitem_area, snxtitem_dist, etc, have been moved
133  * into the common lib, since they don't use condargs, and are useful
134  * elsewhere (update, chiefly). ---ts
135  *
136  */