]> git.pond.sub.org Git - empserver/blob - src/lib/subs/comsub.c
(interix, INTERIXCFLAGS, INTERIXLFLAGS, INTERIXMASTER): New, from Ron
[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     ,
55     {(caddr_t)c_price, sizeof(c_price)}
56     ,
57     {(caddr_t)c_mult, sizeof(c_mult)}
58 };
59 static int nvec = sizeof(commvec) / sizeof(struct iovec);
60 #endif
61
62 /*
63  * comm file is the list of "deity commodities"
64  * lying around.  Perhaps a better way of doing this
65  * might be at each update seeing what is on sale,
66  * then buying it, then distributing things amongst the
67  * people wanting the items.
68  *
69  * Of course that would be a gigantic pain...
70  */
71
72 int
73 commread(void)
74 {
75     int n;
76
77     (void)lseek(commf, (off_t) 0, 0);
78 #if !defined(_WIN32)
79     if ((n = readv(commf, commvec, nvec)) !=
80         sizeof(c_comm) + sizeof(c_price) + sizeof(c_mult)) {
81         logerror("commread: readv returns %d", n);
82         return -1;
83     }
84 #else
85     if ((n = read(commf, c_comm, sizeof(c_comm))) != sizeof(c_comm)) {
86         logerror("commread: read returns %d, not %d", n, sizeof(c_comm));
87         return -1;
88     }
89     if ((n = read(commf, c_price, sizeof(c_price))) != sizeof(c_price)) {
90         logerror("commread: read returns %d, not %d", n, sizeof(c_price));
91         return -1;
92     }
93     if ((n = read(commf, c_mult, sizeof(c_mult))) != sizeof(c_mult)) {
94         logerror("commread: read returns %d, not %d", n, sizeof(c_mult));
95         return -1;
96     }
97 #endif
98     return 0;
99 }
100
101 int
102 commwrite(void)
103 {
104 #if defined(_WIN32)
105     int n;
106 #endif
107     (void)lseek(commf, (off_t) 0, 0);
108 #if !defined(_WIN32)
109     if (writev(commf, commvec, nvec) !=
110         sizeof(c_comm) + sizeof(c_price) + sizeof(c_mult)) {
111         logerror("commwrite: writev failed");
112         return -1;
113     }
114 #else
115     if ((n = write(commf, c_comm, sizeof(c_comm))) != sizeof(c_comm)) {
116         logerror("commwrite: write returns %d, not %d", n, sizeof(c_comm));
117         return -1;
118     }
119     if ((n = write(commf, c_price, sizeof(c_price))) != sizeof(c_price)) {
120         logerror("commwrite: write returns %d, not %d", n,
121                  sizeof(c_price));
122         return -1;
123     }
124     if ((n = write(commf, c_mult, sizeof(c_mult))) != sizeof(c_mult)) {
125         logerror("commwrite: write returns %d, not %d", n, sizeof(c_mult));
126         return -1;
127     }
128 #endif
129     return 0;
130 }
131
132 /*
133  * returns amount of commodity, and price to the user
134  */
135 int
136 commamt(natid trader, int product, float *priceval)
137 {
138     *priceval = c_price[trader][product] * c_mult[trader][player->cnum];
139     return c_comm[trader][product];
140 }
141
142 void
143 commset(natid trader, int product, int amt)
144 {
145     c_comm[trader][product] += amt;
146 }
147
148 double
149 multread(natid trader, natid tradee)
150 {
151     return c_mult[trader][tradee];
152 }
153
154 void
155 multset(natid tradee, float newmult)
156 {
157     c_mult[player->cnum][tradee] = newmult;
158 }
159
160 void
161 commprice(int product, float *newprice)
162 {
163     c_price[player->cnum][product] = *newprice;
164 }