]> git.pond.sub.org Git - empserver/blob - src/lib/common/getvar.c
Import of Empire 4.2.12
[empserver] / src / lib / common / getvar.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  *  getvar.c: Routines for manipulating variable lists.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include "misc.h"
35 #include "var.h"
36 #include "file.h"
37 #include "common.h"
38
39 int
40 getvar(int vtype, s_char *sp, int ptype)
41 {
42         u_char  *vtypep;
43         u_short *vamtp;
44         u_char  *nvp;
45         int     amt;
46
47         if (ef_vars(ptype, sp, &nvp, &vtypep, &vamtp) < 0) {
48                 logerror("getvar: ptype %d has no vars", ptype);
49                 return 0;
50         }
51         amt = vl_find(vtype, vtypep, vamtp, (int) *nvp);
52         if (amt < 0) {
53                 logerror("getvar: vl_find returns %d, vtype %d",
54                         amt, vtype);
55                 return 0;
56         }
57         return amt;
58 }
59
60 int
61 getvec(int class, int *vec, s_char *sp, int ptype)
62 {
63         u_char  *vtypep;
64         u_short *vamtp;
65         u_char  *nvp;
66         int     nv;
67
68         if (ef_vars(ptype, sp, &nvp, &vtypep, &vamtp) < 0) {
69                 logerror("getvec: ptype %d has no vars", ptype);
70                 return 0;
71         }
72         nv = vl_getvec(vtypep, vamtp, (int)*nvp, class, vec);
73         if (nv < 0) {
74                 logerror("vl_getvec: returns %d, ptype %d\n",
75                         nv, ptype);
76                 return 0;
77         }
78         return nv;
79 }
80
81 int
82 putvar(int vtype, int amt, s_char *sp, int ptype)
83 {
84         u_char  *vtypep;
85         u_short *vamtp;
86         u_char  *nvp;
87         int     maxv;
88
89         if (vtype < 0 || vtype > V_MAX) {
90                 logerror("putvar: bad vtype %d\n", vtype);
91                 return 0;
92         }
93         if ((maxv = ef_vars(ptype, sp, &nvp, &vtypep, &vamtp)) < 0) {
94                 logerror("putvar: ptype %d has no vars", ptype);
95                 return 0;
96         }
97         if (amt < 0)
98                 amt = 0;
99         return vl_set(vtype, (u_int)amt, vtypep, vamtp, nvp, maxv);
100 }
101
102 int
103 putvec(int class, int *vec, s_char *sp, int ptype)
104 {
105         u_char  *vtypep;
106         u_short *vamtp;
107         u_char  *nvp;
108         int     maxv,x;
109
110         if ((maxv = ef_vars(ptype, sp, &nvp, &vtypep, &vamtp)) < 0) {
111                 logerror("putvec: ptype %d has no vars", ptype);
112                 return 0;
113         }
114         for(x=0;x<I_MAX;x++)
115                 if (vec[x] < 0)
116                         vec[x] = 0;
117         return vl_setvec(vtypep, vamtp, nvp, maxv, class, vec);
118 }