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
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.
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.
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
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.
28 * nstr.c: compile and execute the item selections on sectors
30 * Known contributors to this file:
46 #include "prototypes.h"
48 static int legal_val(s_char *str, int val);
51 * Compiles and adds "str" to the list of conditionals.
52 * type is the EF typename of the item type we're selecting.
53 * returns amount of "str" used by nstr_comp (i.e. how far
54 * the pointer was advanced). The last is only meaningful
55 * if several conditionals are expected in one string.
58 nstr_comp(struct nscstr *np, int *size, int type, s_char *str)
67 strncpy(arg, str, sizeof(arg) - 1);
68 arg[sizeof(arg) - 1] = 0;
71 while ((c = *cp++) && bp < &ident[sizeof(ident) - 1]) {
72 if (c == '<' || c == '=' || c == '>' || c == '#')
78 pr("'%s'? -- meaningless condition?\n", arg);
81 np[*size].oper = c & NSC_OPMASK;
82 if ((val = encode(ident, &np[*size].fld1, type)) < 0)
85 np[*size].oper |= NSC_ISNUM1;
87 while ((c = *cp++) && bp < &ident[sizeof(ident) - 1]) {
93 if ((val = encode(ident, &np[*size].fld2, type)) < 0)
96 np[*size].oper |= NSC_ISNUM2;
100 return str + (cp - arg);
104 * return true if the conditions on this item
108 nstr_exec(struct nscstr *conds, register int ncond, void *ptr, int type)
110 register struct nscstr *nsc;
116 for (nsc = conds; --ncond >= 0; nsc++) {
118 if (oper & NSC_ISNUM2) {
121 rhs = decode(player->cnum, nsc->fld2, ptr, type);
123 if (oper & NSC_ISNUM1) {
126 lhs = decode(player->cnum, nsc->fld1, ptr, type);
128 op = oper & NSC_OPMASK;
129 if ((op == '<' && lhs >= rhs)
130 || (op == '=' && lhs != rhs)
131 || (op == '>' && lhs <= rhs)
132 || (op == '#' && lhs == rhs))
139 encode(register s_char *str, long int *val, int type)
148 if (isdigit(*str) || ((*str == '-') && isdigit(str[1]))) {
153 * FIXME This accepts the first match found, even if there are
154 * more matches in other tables, i.e. it quietly accepts ambiguous
155 * matches (matches in multiple tables), and fails to prefer an
156 * exact match to partial match in an earlier table.
158 if ((i = typematch(str, type)) >= 0) {
162 if ((cap = ef_cadef(type)) != 0) {
163 i = stmtch(str, (caddr_t)cap, fldoff(castr, ca_name),
164 sizeof(struct castr));
166 *val = cap[i].ca_code;
167 return legal_val(str, *val);
169 if (i == M_NOTUNIQUE) {
170 pr("%s -- ambiguous type selector\n", str);
175 * Only check for commodity selectors on objects which
176 * are allowed to have commodities.
178 if (ef_flags(type) & EFF_COM) {
179 i = stmtch(str, (caddr_t)var_ca, fldoff(castr, ca_name),
180 sizeof(struct castr));
182 *val = var_ca[i].ca_code;
183 return legal_val(str, *val);
185 if (i == M_NOTUNIQUE) {
186 pr("%s -- ambiguous commodity selector\n", str);
190 pr("%s -- not a valid selector\n", str);
195 legal_val(s_char *str, int val)
197 if (val & NSC_DEITY && !player->god) {
198 pr("%s -- permission denied\n", str);
205 decode(natid cnum, long int code, void *addr, int type)
208 register int nsc_code;
210 long code_type = (code & NSC_TMASK);
212 val = (code & ~NSC_MASK) & 0xffff;
214 /* handle negative numbers properly */
215 /* this assumes a binary two's complement number representation */
219 nsc_code = code & NSC_CMASK;
220 if (nsc_code == NSC_VAR) {
221 u_short *item = ef_items(type, addr);
222 val = item ? item[val] : 0;
223 } else if (nsc_code == NSC_OFF) {
225 * add offset to value
227 addr = (s_char *)addr + val;
230 val = *((time_t *) addr);
233 val = *((s_char *)addr);
236 val = (int)*((unsigned char *)addr);
239 val = *((short *)addr);
242 val = *((u_short *)addr);
245 val = *((int *)addr);
248 val = *((long *)addr);
251 val = *((short *)addr);
256 val = *((short *)addr);
261 logerror("bad type in decode: %lx!\n", code & NSC_TMASK);
270 decodep(long int code, void *addr)
272 addr = (char *)addr + ((code & ~NSC_MASK) & 0xffff);
274 if ((code & NSC_TMASK) == NSC_CHARP)
275 return *(s_char **)addr ? *((s_char **)addr) : (s_char *)"";