src/lib/subs/snxtitem.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  snxtitem.c: Arrange item selection using one of many criteria.
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1989
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include <ctype.h>
00037 #include "misc.h"
00038 #include "player.h"
00039 #include "xy.h"
00040 #include "nsc.h"
00041 #include "file.h"
00042 #include "prototypes.h"
00043 
00044 /*
00045  * setup the nstr structure for sector selection.
00046  * can select on NS_ALL, NS_AREA, NS_DIST, and NS_LIST.
00047  * iterate thru the "condarg" string looking
00048  * for arguments to compile into the nstr.
00049  * Using this function for anything but command arguments is usually
00050  * incorrect, because it respects conditionals.  Use the snxtitem_FOO()
00051  * instead.
00052  */
00053 int
00054 snxtitem(struct nstr_item *np, int type, char *str)
00055 {
00056     struct range range;
00057     int list[NS_LSIZE];
00058     int n;
00059     coord cx, cy;
00060     int dist;
00061     int flags;
00062     char natnumber[16];
00063     char prompt[128];
00064     char buf[1024];
00065 
00066     np->type = EF_BAD;
00067     np->sel = NS_UNDEF;
00068     if (str == 0) {
00069         sprintf(prompt, "%s(s)? ", ef_nameof(type));
00070         str = getstring(prompt, buf);
00071         if (str == 0)
00072             return 0;
00073     }
00074     if (*str == 0) {
00075         /* empty string passed by player */
00076         return 0;
00077     }
00078     if (type == EF_NATION && isalpha(*str)) {
00079         sprintf(natnumber, "%d", natarg(str, NULL));
00080         str = natnumber;
00081     }
00082     flags = ef_flags(type);
00083     switch (sarg_type(str)) {
00084     case NS_AREA:
00085         if (!(flags & EFF_XY))
00086             return 0;
00087         if (!sarg_area(str, &range))
00088             return 0;
00089         snxtitem_area(np, type, &range);
00090         break;
00091     case NS_DIST:
00092         if (!(flags & EFF_XY))
00093             return 0;
00094         if (!sarg_range(str, &cx, &cy, &dist))
00095             return 0;
00096         snxtitem_dist(np, type, cx, cy, dist);
00097         break;
00098     case NS_ALL:
00099         snxtitem_all(np, type);
00100         break;
00101     case NS_LIST:
00102         if ((n = sarg_list(str, list, NS_LSIZE)) == 0)
00103             return 0;
00104         if (!snxtitem_list(np, type, list, n))
00105             return 0;
00106         break;
00107     case NS_XY:
00108         if (!(flags & EFF_XY))
00109             return 0;
00110         if (!sarg_xy(str, &cx, &cy))
00111             return 0;
00112         snxtitem_xy(np, type, cx, cy);
00113         break;
00114     case NS_GROUP:
00115         if (!(flags & EFF_GROUP))
00116             return 0;
00117         snxtitem_group(np, type, *str);
00118         break;
00119     default:
00120         return 0;
00121     }
00122     np->flags = flags;
00123     if (player->condarg == 0)
00124         return 1;
00125     n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond), type,
00126                   player->condarg);
00127     np->ncond = n >= 0 ? n : 0;
00128     return n >= 0;
00129 }
00130 
00131 void
00132 snxtitem_area(struct nstr_item *np, int type, struct range *range)
00133 {
00134     memset(np, 0, sizeof(*np));
00135     np->cur = -1;
00136     np->type = type;
00137     np->sel = NS_AREA;
00138     np->index = -1;
00139     np->range = *range;
00140     np->read = ef_read;
00141     np->flags = ef_flags(type);
00142     xysize_range(&np->range);
00143 }
00144 
00145 void
00146 snxtitem_dist(struct nstr_item *np, int type, int cx, int cy,
00147               int dist)
00148 {
00149     struct range range;
00150 
00151     memset(np, 0, sizeof(*np));
00152     xydist_range(cx, cy, dist, &range);
00153     np->cur = -1;
00154     np->type = type;
00155     np->sel = NS_DIST;
00156     np->cx = cx;
00157     np->cy = cy;
00158     np->index = -1;
00159     np->range = range;
00160     np->dist = dist;
00161     np->read = ef_read;
00162     np->flags = ef_flags(type);
00163 #if 0
00164     /* This is no longer proper. */
00165     /* It did the wrong thing for small, hitech worlds. */
00166     xysize_range(&np->range);
00167 #endif
00168 }
00169 
00170 void
00171 snxtitem_xy(struct nstr_item *np, int type, coord x, coord y)
00172 {
00173     memset(np, 0, sizeof(*np));
00174     np->cur = -1;
00175     np->type = type;
00176     np->sel = NS_XY;
00177     np->cx = xnorm(x);
00178     np->cy = ynorm(y);
00179     np->index = -1;
00180     np->dist = 0;
00181     np->read = ef_read;
00182     np->flags = ef_flags(type);
00183 }
00184 
00185 void
00186 snxtitem_all(struct nstr_item *np, int type)
00187 {
00188     memset(np, 0, sizeof(*np));
00189     np->cur = -1;
00190     np->sel = NS_ALL;
00191     np->type = type;
00192     np->index = -1;
00193     np->read = ef_read;
00194     np->flags = ef_flags(type);
00195     xysize_range(&np->range);
00196 }
00197 
00198 void
00199 snxtitem_group(struct nstr_item *np, int type, char group)
00200 {
00201     if (group == '~')
00202         group = 0;
00203     memset(np, 0, sizeof(*np));
00204     np->cur = -1;
00205     np->sel = NS_GROUP;
00206     np->group = group;
00207     np->type = type;
00208     np->index = -1;
00209     np->read = ef_read;
00210     np->flags = ef_flags(type);
00211     xysize_range(&np->range);
00212 }
00213 
00214 void
00215 snxtitem_rewind(struct nstr_item *np)
00216 {
00217     np->cur = -1;
00218     np->index = -1;
00219 }
00220 
00221 int
00222 snxtitem_list(struct nstr_item *np, int type, int *list, int len)
00223 {
00224     int i;
00225 
00226     memset(np, 0, sizeof(*np));
00227     np->cur = -1;
00228     np->type = type;
00229     np->sel = NS_LIST;
00230     np->index = -1;
00231     np->read = ef_read;
00232     np->flags = ef_flags(type);
00233     if (len <= 0 || len > NS_LSIZE)
00234         return 0;
00235     for (i = 0; i < len; i++)
00236         np->list[i] = list[i];
00237     np->size = len;
00238     return 1;
00239 }

Generated on Fri Mar 28 11:01:16 2008 for empserver by  doxygen 1.5.2