]> git.pond.sub.org Git - empserver/blob - src/lib/subs/comsub.c
Import of Empire 4.2.12
[empserver] / src / lib / subs / comsub.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  *  comsub.c: Commodity read/write stuff
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1998
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "var.h"
38 #include "nat.h"
39 #include "prototypes.h"
40
41 #if !defined(_WIN32)
42 #include <sys/uio.h>
43 #endif
44
45 int     commf;
46
47 int     c_comm[MAXNOC][I_MAX+1];
48 float   c_price[MAXNOC][I_MAX+1];
49 float   c_mult[MAXNOC][MAXNOC];
50
51 #if !defined(_WIN32)
52 static  struct iovec commvec[3] = {
53         { (caddr_t)c_comm,      sizeof(c_comm) },
54         { (caddr_t)c_price,     sizeof(c_price) },
55         { (caddr_t)c_mult,      sizeof(c_mult) }
56 };
57 static  int nvec = sizeof(commvec)/sizeof(struct iovec);
58 #endif
59
60 /*
61  * comm file is the list of "deity commodities"
62  * lying around.  Perhaps a better way of doing this
63  * might be at each update seeing what is on sale,
64  * then buying it, then distributing things amongst the
65  * people wanting the items.
66  *
67  * Of course that would be a gigantic pain...
68  */
69
70 int
71 commread(void)
72 {
73         int     n;
74
75         (void) lseek(commf, (off_t) 0, 0);
76 #if !defined(_WIN32)
77         if ((n = readv(commf, commvec, nvec)) !=
78             sizeof(c_comm) + sizeof(c_price) + sizeof(c_mult)) {
79                 logerror("commread: readv returns %d", n);
80                 return -1;
81         }
82 #else
83         if ((n = read(commf, c_comm, sizeof(c_comm))) != sizeof(c_comm)) {
84                 logerror("commread: read returns %d, not %d", n, sizeof(c_comm));
85                 return -1;
86     }
87         if ((n = read(commf, c_price, sizeof(c_price))) != sizeof(c_price)) {
88                 logerror("commread: read returns %d, not %d", n, sizeof(c_price));
89                 return -1;
90     }
91         if ((n = read(commf, c_mult, sizeof(c_mult))) != sizeof(c_mult)) {
92                 logerror("commread: read returns %d, not %d", n, sizeof(c_mult));
93                 return -1;
94     }
95 #endif
96         return 0;
97 }
98
99 int
100 commwrite(void)
101 {
102 #if defined(_WIN32)
103         int n;
104 #endif
105         (void) lseek(commf, (off_t) 0, 0);
106 #if !defined(_WIN32)
107         if (writev(commf, commvec, nvec) != 
108             sizeof(c_comm) + sizeof(c_price) + sizeof(c_mult)) {
109                 logerror("commwrite: writev failed");
110                 return -1;
111         }
112 #else
113         if ((n = write(commf, c_comm, sizeof(c_comm))) != sizeof(c_comm)) {
114                 logerror("commwrite: write returns %d, not %d", n, sizeof(c_comm));
115                 return -1;
116     }
117         if ((n = write(commf, c_price, sizeof(c_price))) != sizeof(c_price)) {
118                 logerror("commwrite: write returns %d, not %d", n, sizeof(c_price));
119                 return -1;
120     }
121         if ((n = write(commf, c_mult, sizeof(c_mult))) != sizeof(c_mult)) {
122                 logerror("commwrite: write returns %d, not %d", n, sizeof(c_mult));
123                 return -1;
124     }
125 #endif
126         return 0;
127 }
128
129 int
130 commlock(void)
131 {
132         return file_lock(commf);
133 }
134
135 int
136 communlock(void)
137 {
138         return file_unlock(commf);
139 }
140
141 /*
142  * returns amount of commodity, and price to the user
143  */
144 int
145 commamt(natid trader, int product, float *priceval)
146 {
147         *priceval = c_price[trader][product] * c_mult[trader][player->cnum];
148         return c_comm[trader][product];
149 }
150
151 void
152 commset(natid trader, int product, int amt)
153 {
154         c_comm[trader][product] += amt;
155 }
156
157 double
158 multread(natid trader, natid tradee)
159 {
160         return c_mult[trader][tradee];
161 }
162
163 void
164 multset(natid tradee, float newmult)
165 {
166         c_mult[player->cnum][tradee] = newmult;
167 }
168
169 void
170 commprice(int product, float *newprice)
171 {
172         c_price[player->cnum][product] = *newprice;
173 }