Import of Empire 4.2.12

This commit is contained in:
Markus Armbruster 2003-08-23 12:23:04 +00:00
commit d8b7fdfae1
817 changed files with 126589 additions and 0 deletions

68
src/lib/gen/Makefile Normal file
View file

@ -0,0 +1,68 @@
#
# Empire - A multi-player, client/server Internet based war game.
# Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
# Ken Stevens, Steve McClure
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ---
#
# See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
# related information and legal notices. It is expected that any future
# projects/authors will amend these files as needed.
#
# Makefile - Wolfpack, 1996
# Note that these could have been listed 1 per line, but I chose to just
# stick them all together this way to shorten the file.
include ../../../build.conf
include ../../make.src
include ../../make.defs
LIB = $(SRCDIR)/lib/libgen.a
NTLIB = $(SRCDIR)\lib\libgen.lib
OBJS = atoip.o atopi.o bit.o chance.o copy.o disassoc.o dtable.o \
emp_config.o getstarg.o getstring.o hpux.o iceil.o inet.o io.o \
io_mask.o ioqueue.o lock.o mapdist.o minmax.o numstr.o onearg.o \
parse.o plur.o queue.o round.o same.o scthash.o signal.o \
strdup.o strscan.o vsprintf.o
NTOBJS = atoip.obj atopi.obj bit.obj chance.obj copy.obj disassoc.obj \
dtable.obj emp_config.obj getstarg.obj getstring.obj hpux.obj iceil.obj \
inet.obj io.obj io_mask.obj ioqueue.obj lock.obj mapdist.obj minmax.obj \
numstr.obj onearg.obj parse.obj plur.obj queue.obj round.obj same.obj \
scthash.obj signal.obj strdup.obj strscan.obj vsprintf.obj
all: $(LIB)
nt: $(NTLIB)
$(NTLIB): $(NTOBJS)
-del /q $@
lib /OUT:$@ /DEBUGTYPE:CV $(NTOBJS)
$(LIB): $(OBJS)
rm -f $(LIB)
ar cq $(LIB) $(OBJS)
$(RANLIB) $(LIB)
clean:
-(rm -f $(OBJS))
-(del /q $(NTOBJS))
include ../../make.rules
include Makedepend

59
src/lib/gen/atoip.c Normal file
View file

@ -0,0 +1,59 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* atoip.c: Convert from decimal string to integer.
*
* Known contributors to this file:
*
*/
#include "gen.h"
int
atoip(s_char **ptrptr)
{
register int num;
register s_char *cp;
register int neg;
if (ptrptr == 0 || *ptrptr == 0) return 0;
cp = *ptrptr;
num = 0;
neg = 0;
loop:
while (*cp == ' ' || *cp == '\t')
cp++;
if (*cp == '-') {
neg++;
cp++;
goto loop;
}
while (*cp >= '0' && *cp <= '9')
num = num * 10 + *cp++ - '0';
*ptrptr = cp;
return(neg ? -num : num);
}

55
src/lib/gen/atopi.c Normal file
View file

@ -0,0 +1,55 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* atopi.c: Return positive integer from string.
*
* Known contributors to this file:
*
*/
#include "gen.h"
/*
* atopi.c
*
* from PSL Empire, 1985
*/
int
atopi(s_char *p)
{
register int r11;
if (p) {
r11 = atoi(p);
if (r11 < 0)
r11 = -r11;
return r11;
} else {
return (0);
}
}

206
src/lib/gen/bit.c Normal file
View file

@ -0,0 +1,206 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* bit.c: Allocate and search select bitfields
*
* Known contributors to this file:
* Steve McClure, 2000
*/
#include "misc.h"
#include "bit.h"
#include "gen.h" /* getfdtablesize etc. */
static int bit_nbytes;
bit_fdmask
bit_newfdmask(void)
{
bit_fdmask mask;
int nfile;
if (bit_nbytes == 0) {
nfile = getfdtablesize();
bit_nbytes = (nfile + (BIT_BITSPERMASK-1)) / BIT_NBBY;
}
mask = (bit_fdmask) malloc(bit_nbytes);
(void) bit_zero(mask);
return mask;
}
/*
* zero the bitfield
*/
void
bit_zero(bit_fdmask bitp)
{
bit_mask *mask;
register int i;
register int nwords;
mask = bitp;
nwords = bit_nbytes / sizeof(*mask);
for (i=0; i<nwords; i++)
*mask++ = 0;
}
/*
* zero the bitfield
*/
void
bit_not(bit_fdmask bitp)
{
register bit_mask *mask;
register int i;
register int nwords;
mask = bitp;
nwords = bit_nbytes / sizeof(*mask);
for (i=0; i<nwords; i++,mask++)
*mask = ~(*mask);
}
/*
* zero the bitfield
*/
void
bit_copy(bit_fdmask bitsrc, bit_fdmask bitdst)
{
register bit_mask *src;
register bit_mask *dst;
register int i;
register int nwords;
dst = bitdst;
src = bitsrc;
nwords = bit_nbytes / sizeof(*dst);
for (i=0; i<nwords; i++)
*dst++ = *src++;
}
/*
* zero the bitfield
*/
void
bit_or(bit_fdmask bitsrc, bit_fdmask bitdst)
{
register bit_mask *src;
register bit_mask *dst;
register int i;
register int nwords;
nwords = bit_nbytes / sizeof(*dst);
src = bitsrc;
dst = bitdst;
for (i=0; i<nwords; i++)
*dst++ |= *src++;
}
/*
* zero the bitfield
*/
void
bit_or3(bit_fdmask bitsrc1, bit_fdmask bitsrc2, bit_fdmask bitdst)
{
register bit_mask *src1;
register bit_mask *src2;
register bit_mask *dst;
register int i;
register int nwords;
src1 = bitsrc1;
src2 = bitsrc2;
dst = bitdst;
nwords = bit_nbytes / sizeof(*dst);
for (i=0; i<nwords; i++)
*dst++ = *src1++ | *src2++;
}
/*
* zero the bitfield
*/
void
bit_and(bit_fdmask bitsrc, bit_fdmask bitdst)
{
register bit_mask *src;
register bit_mask *dst;
register int i;
register int nwords;
nwords = bit_nbytes / sizeof(*src);
src = bitsrc;
dst = bitdst;
for (i=0; i<nwords; i++)
*dst++ &= *src++;
}
/*
* zero the bitfield
*/
void
bit_and3(bit_fdmask bitsrc1, bit_fdmask bitsrc2, bit_fdmask bitdst)
{
register bit_mask *src1;
register bit_mask *src2;
register bit_mask *dst;
register int i;
register int nwords;
src1 = bitsrc1;
src2 = bitsrc2;
dst = bitdst;
nwords = bit_nbytes / sizeof(*dst);
for (i=0; i<nwords; i++)
*dst++ = *src1++ & *src2++;
}
/*
* Return first bit set in fd mask.
* speedy version, not using BIT_ISSETB()
*/
int
bit_fd(bit_fdmask bitp)
{
register bit_mask *mask;
register unsigned int j;
register bit_mask m;
register int i;
int nwords;
mask = bitp;
nwords = bit_nbytes / sizeof(m);
for (i=0; i<nwords; i++,mask++) {
if ((m = *mask) == 0)
continue;
for (j=0; j<BIT_BITSPERMASK; j++) {
if (m & bit(j))
return i*BIT_BITSPERMASK + j;
}
/*NOTREACHED*/
}
return -1;
}

87
src/lib/gen/chance.c Normal file
View file

@ -0,0 +1,87 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* chance.c: return 1 if "roll" is under the chance.
*
* Known contributors to this file:
*
*/
#include "gen.h"
#ifdef hpux
void
srandom(unsigned int n)
{
extern void srand48();
srand48(n);
}
long
random(void)
{
extern long lrand48();
return (lrand48()); /* 5/28/91 by bailey@mcs.kent.edu */
}
#endif
int
chance(double d)
{
double roll;
roll = (random() & 0x7fff);
if (d > roll/32768.0)
return 1;
return 0;
}
int
roll(int n)
{
return (random() % n) + 1;
}
/*
* round value to nearest int (on the average). E.g. rounds up
* with a chance proportional to the size of the fractional part.
*/
int
roundavg(double val)
{
int flr;
flr = (int) val;
if (val < 0)
flr -= chance(flr - val);
else
flr += chance(val - flr);
return flr;
}

83
src/lib/gen/copy.c Normal file
View file

@ -0,0 +1,83 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* copy.c: Copy and return pointer to end of copied string.
*
* Known contributors to this file:
*
*/
#include "misc.h"
#include "xy.h"
#include "map.h"
#include "gen.h"
#include "optlist.h"
s_char *
copy(register s_char *s1, register s_char *s2)
{
while ((*s2++ = *s1++) != 0)
;
return s2 - 1;
}
#ifdef hpux
#include <memory.h>
void
bzero(s_char *ptr, int len)
{
memset(ptr, 0, len);
}
void
bcopy(s_char *src, s_char *dst, int len)
{
memcpy(dst, src, len);
}
#endif
/*
* space-fill a map or radar scan;
* null terminate
*/
void
blankfill(s_char *ptr, register struct range *range, int size)
{
register s_char *p;
register int row;
register int col;
for (row=0; row<range->height; row++) {
col = (range->width + 1) * (size + 1) / 2 - 1;
p = ptr;
while (--col >= 0)
*p++ = ' ';
*p++ = 0;
ptr += MAPWIDTH(size);
}
}

72
src/lib/gen/disassoc.c Normal file
View file

@ -0,0 +1,72 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* disassoc.c: Fork and close
*
* Known contributors to this file:
* Doug Hay, 1998
*/
/*
* boilerplate daemon code; disassociate from
* the current tty by forking, closing all file
* descriptors, opening slash, and ioctl-ing
* TIOCNOTTY
*/
#include <sys/types.h>
#if !defined(_WIN32)
#include <sys/ioctl.h>
#include <unistd.h> /* fork close dup2 */
#endif
#include <fcntl.h>
#include "gen.h"
void
disassoc(void)
{
#if !defined(_WIN32)
int i;
if (fork() != 0)
exit(0);
for (i = 0; i < 10; i++)
(void) close(i);
(void) open("/", O_RDONLY, 0);
(void) dup2(0, 1);
(void) dup2(0, 2);
#if defined hpux || defined Rel4
setsid();
#else
i = open("/dev/tty", O_RDWR, 0);
if (i > 0) {
(void) ioctl(i, TIOCNOTTY, 0);
(void) close(i);
}
#endif
#endif
}

65
src/lib/gen/dtable.c Normal file
View file

@ -0,0 +1,65 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* dtable.c: Machine-dependant descriptor table management
*
* Known contributors to this file:
* Doug Hay, 1998
*/
#if !defined(_WIN32)
#include <unistd.h> /* getdtablesize */
#endif
#include "gen.h"
/*ARGSUSED*/
void
setfdtablesize(int min, int start)
{
#ifdef sequent
extern int errno;
while (start >= min) {
if (setdtablesize(start) > 0)
break;
start -= 16;
}
errno = 0;
#endif
}
int
getfdtablesize(void)
{
#if defined(_WIN32)
return (_NFILE);
#elif defined(hpux)
return (int)sysconf(_SC_OPEN_MAX);
#else
return getdtablesize();
#endif
}

679
src/lib/gen/emp_config.c Normal file
View file

@ -0,0 +1,679 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* emp_config.c: Allows config file to control server config. from a file
*
* Known contributors to this file:
* Julian Onions, 1995
* Steve McClure, 1998-2000
*/
/*
* STILL TO DO
*
* 1. Change other constants - such as Num Countries etc.
* Just requires variables to be assigned, then dynamic allocation in
* a few places. Some checks needed in the server to check the world
* hasn't changed size etc.
* 2. Could look at loading in planes, units etc. Should be easy enough.
*
*/
#include <stdio.h>
#include <stdlib.h> /* atoi free atol */
#ifdef Rel4
#include <string.h>
#endif /* Rel4 */
#include "misc.h"
#include "com.h"
#include "match.h"
#include "file.h"
#include "optlist.h"
#include "gen.h" /* parse */
/* for systems without strdup */
#ifdef NOSTRDUP
extern char *strdup();
#endif /* NOSTRDUP */
/* All the configurable variables ... */
extern s_char *infodir, *datadir, *gamedir, *loginport, *privname, *privlog;
extern double buil_tower_bt, buil_tower_bc;
extern double buytax, flakscale, maxmult, minmult, tradetax, uwbrate;
extern double buil_bc, buil_bt, combat_mob, edu_cons, hap_cons, money_civ;
extern double money_land, money_mil, money_plane, money_res, money_ship;
extern double money_uw, people_damage, powe_cost, unit_damage, babyeat;
extern double collateral_dam, assault_penalty;
extern double bankint, eatrate, fcrate, fgrate, obrate, mission_mob_cost;
extern float btu_build_rate, easy_tech, hard_tech, land_mob_scale;
extern float level_age_rate, plane_mob_scale, sect_mob_scale;
extern float ship_mob_scale, tech_log_base, ally_factor, edu_avg, hap_avg;
extern int buil_tower_bh;
extern int startmob, at_least_one_100, buil_bh, etu_per_update;
extern int land_grow_scale, land_mob_max, m_m_p_d, max_btus, max_idle;
extern int plane_grow_scale, plane_mob_max, players_at_00, sect_mob_max;
extern int ship_grow_scale, ship_mob_max, torpedo_damage;
extern int fort_max_interdiction_range;
extern int land_max_interdiction_range;
extern int ship_max_interdiction_range;
extern int sect_mob_neg_factor;
extern int lost_items_timeout;
extern int WORLD_X;
extern int WORLD_Y;
extern int MARK_DELAY;
extern int TRADE_DELAY;
extern int morale_base;
extern float fire_range_factor;
extern long adj_update;
extern long s_p_etu;
extern int update_policy;
extern s_char *update_times;
extern int hourslop;
extern int update_window;
extern int blitz_time;
extern int update_demandpolicy;
extern s_char *update_demandtimes;
extern int update_wantmin;
extern int update_missed;
extern s_char *game_days;
extern s_char *game_hours;
/* conditional ones */
extern double decay_per_etu, fallout_spread;
extern int fuel_mult;
extern int War_Cost;
extern long last_demand_update;
extern float drnuke_const;
extern float start_education, start_happiness;
extern float start_technology, start_research;
extern int trade_1_dist, trade_2_dist, trade_3_dist;
extern float trade_1, trade_2, trade_3, trade_ally_bonus, trade_ally_cut;
/* Dummy one */
static int emp_config_dummy;
/* things that can be changed */
struct keymatch configkeys[] = {
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n### Server configuration and information" },
{ "data", optstrset, (caddr_t) &datadir, 0,
"Directory the data is stored in" },
{ "info", optstrset, (caddr_t) &infodir, 0,
"Directory the info pages are stored in" },
{ "port", optstrset, (caddr_t) &loginport, 0,
"TCP/IP port the server will start up on" },
{ "privname", optstrset, (caddr_t) &privname, 0,
"Name of the deity" },
{ "privlog", optstrset, (caddr_t) &privlog, 0,
"E-mail of the deity" },
{ "WORLD_X", worldxset, (caddr_t) &WORLD_X, 0,
"World size X dimension (enforced to be even by subtracting 1 if necessary)" },
{ "WORLD_Y", intset, (caddr_t) &WORLD_Y, 0,
"World size Y dimension" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Update policy" },
{ "update_policy", intset, (caddr_t) &update_policy, 0,
"0 - normal, 1 - update_times, 2 - blitz, 3 - demand only" },
{ "etu_per_update", intset, (caddr_t) &etu_per_update, 0,
"Number of ETUs per update" },
{ "s_p_etu", longset, (caddr_t) &s_p_etu, 0,
"Seconds per etu, updates will occur every s_p_etu * etu_per_update seconds" },
{ "adj_update", longset, (caddr_t) &adj_update, 0,
"Move the update forward or backward (in seconds)" },
{ "update_window", intset, (caddr_t) &update_window, 0,
"Window the update will occur in (in seconds) before and after the update time" },
{ "update_times", optstrset, (caddr_t) &update_times, 0,
"Times when updates occur under policy #1. Must coincide with schedule." },
{ "hourslop", intset, (caddr_t) &hourslop, 0,
"Number of minutes update check can slip to match update_times" },
{ "blitz_time", intset, (caddr_t) &blitz_time, 0,
"Number of minutes between updates under policy #2." },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Demand update policy" },
{ "update_demandpolicy",intset, (caddr_t) &update_demandpolicy, 0,
"0 - emp_tm checks, 1 - after setting, 2 - demand updates disabled" },
{ "update_wantmin", intset, (caddr_t) &update_wantmin, 0,
"number of requests needed for demand update" },
{ "update_missed", intset, (caddr_t) &update_missed, 0,
"number of demand updates country can miss before veto update" },
{ "update_demandtimes", optstrset, (caddr_t) &update_demandtimes, 0,
"Times when demand updates can occur. Ranges CANNOT cross midnight." },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Game hours restrictions" },
{ "game_days", optstrset, (caddr_t) &game_days, 0,
"Days game is up and running (Su Mo Tu We Th Fr Sa)" },
{ "game_hours", optstrset, (caddr_t) &game_hours, 0,
"Hours game is up and running (6:00-18:00)" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Options\n" },
{ "option", optionset, (caddr_t) NULL, 0, NULL },
{ "nooption", optiondel, (caddr_t) NULL, 0, NULL },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Countries" },
{ "btu_build_rate", floatset, (caddr_t) &btu_build_rate, 0,
"Rate at which BTUs accumulate (etu * civ * eff * btu_build_rate)" },
{ "m_m_p_d", intset, (caddr_t) &m_m_p_d, 0,
"Maximum minutes per day a country is allowed to be logged in" },
{ "max_btus", intset, (caddr_t) &max_btus, 0,
"Maximum number of BTUs a country can have" },
{ "max_idle", intset, (caddr_t) &max_idle, 0,
"Maximum number of minutes a player can sit idle while logged in" },
{ "players_at_00", intset, (caddr_t) &players_at_00, 0,
"Players have their coordinate system at deity 0,0 (0 - no, 1 - yes)" },
{ "at_least_one_100", intset, (caddr_t) &at_least_one_100, 0,
"Initialize new countries with at least one sector with 100 of all resource" },
{ "powe_cost", doubleset, (caddr_t) &powe_cost, 0,
"Number of BTUs needed to generate a new power report" },
{ "war_cost", intset, (caddr_t) &War_Cost, 0,
"Cost to declare war (if SLOW_WAR is on)" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Technology/Research/Education/Happiness" },
{ "easy_tech", floatset, (caddr_t) &easy_tech, 0,
"Amount of tech built with no penalty" },
{ "hard_tech", floatset, (caddr_t) &hard_tech, 0,
"Amount of in-efficiently built tech" },
{ "start_tech", floatset, (caddr_t) &start_technology, 0,
"Starting technology for new countries" },
{ "start_happy", floatset, (caddr_t) &start_happiness, 0,
"Starting happiness for new countries" },
{ "start_research", floatset, (caddr_t) &start_research, 0,
"Starting research for new countries" },
{ "start_edu", floatset, (caddr_t) &start_education, 0,
"Starting education for new countries" },
{ "level_age_rate", floatset, (caddr_t) &level_age_rate, 0,
"ETU rate at which tech decays (0 -> no decline)" },
{ "tech_log_base", floatset, (caddr_t) &tech_log_base, 0,
"Log base to apply to tech breakthroughs above the easy tech level" },
{ "ally_factor", floatset, (caddr_t) &ally_factor, 0,
"Shared tech with allies (1 / ally_factor)" },
{ "edu_avg", floatset, (caddr_t) &edu_avg, 0,
"Number of ETUs education is averaged over" },
{ "hap_avg", floatset, (caddr_t) &hap_avg, 0,
"Number of ETUs happiness is averaged over" },
{ "edu_cons", doubleset, (caddr_t) &edu_cons, 0,
"Education consumption (1 breakthrough per edu_cons)" },
{ "hap_cons", doubleset, (caddr_t) &hap_cons, 0,
"Happiness consumption (1 breakthrough per hap_cons)" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Sectors" },
{ "startmob", intset, (caddr_t) &startmob, 0,
"Starting mobility for sanctuaries" },
{ "sect_mob_scale", floatset, (caddr_t) &sect_mob_scale, 0,
"Sector mobility accumulation (sect_mob_scale * ETUs per update)" },
{ "sect_mob_max", intset, (caddr_t) &sect_mob_max, 0,
"Maximum mobility for sectors" },
{ "buil_bh", intset, (caddr_t) &buil_bh, 0,
"Number of hcms required to build a bridge span" },
{ "buil_bc", doubleset, (caddr_t) &buil_bc, 0,
"Cash required to build a bridge span" },
{ "buil_bt", doubleset, (caddr_t) &buil_bt, 0,
"Technology required to build a bridge span" },
{ "buil_tower_bh", intset, (caddr_t) &buil_tower_bh, 0,
"Number of hcms required to build a bridge tower" },
{ "buil_tower_bc", doubleset, (caddr_t) &buil_tower_bc, 0,
"Cash required to build a bridge tower" },
{ "buil_tower_bt", doubleset, (caddr_t) &buil_tower_bt, 0,
"Technology required to build a bridge tower" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Land Units" },
{ "land_mob_scale", floatset, (caddr_t) &land_mob_scale, 0,
"Land unit mobility accumulation (land_mob_scale * ETUs per update)" },
{ "land_grow_scale", intset, (caddr_t) &land_grow_scale, 0,
"How fast efficiency grows for land units each update (* ETUs)" },
{ "land_mob_max", intset, (caddr_t) &land_mob_max, 0,
"Maximum mobility for land units" },
{ "money_land", doubleset, (caddr_t) &money_land, 0,
"Cost per ETU to maintain land units (percentage of unit price)" },
{ "morale_base", intset, (caddr_t) &morale_base, 0,
"Base level for setting morale of land units" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Planes" },
{ "plane_mob_scale", floatset, (caddr_t) &plane_mob_scale, 0,
"Plane mobility accumulation (plane_mob_scale * ETUs per update)" },
{ "plane_grow_scale", intset, (caddr_t) &plane_grow_scale, 0,
"How fast efficiency grows for planes each update (* ETUs)" },
{ "plane_mob_max", intset, (caddr_t) &plane_mob_max, 0,
"Maximum mobility for planes" },
{ "money_plane", doubleset, (caddr_t) &money_plane, 0,
"Cost per ETU to maintain planes (percentage of plane price)" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Ships" },
{ "ship_mob_scale", floatset, (caddr_t) &ship_mob_scale, 0,
"Ship mobility accumulation (ship_mob_scale * ETUs per update)" },
{ "ship_grow_scale", intset, (caddr_t) &ship_grow_scale, 0,
"How fast efficiency grows for ships each update (* ETUs)" },
{ "ship_mob_max", intset, (caddr_t) &ship_mob_max, 0,
"Maximum mobility for ships" },
{ "money_ship", doubleset, (caddr_t) &money_ship, 0,
"Cost per ETU to maintain ships (percentage of ship price)" },
{ "torpedo_damage", intset, (caddr_t) &torpedo_damage, 0,
"Torpedo damage (damage is X + 1dX + 1dX)" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Combat/Damage" },
{ "fort_max_interdiction_range", intset, (caddr_t)&fort_max_interdiction_range, 0,
"Maximum range (in sectors) a fort will try to interdict another country" },
{ "land_max_interdiction_range", intset, (caddr_t)&land_max_interdiction_range, 0,
"Maximum range (in sectors) a land unit will try to interdict another country" },
{ "ship_max_interdiction_range", intset, (caddr_t)&ship_max_interdiction_range, 0,
"Maximum range (in sectors) a ship will try to interdict another country" },
{ "flakscale", doubleset, (caddr_t) &flakscale, 0,
"Scale factor for flak damage" },
{ "combat_mob", doubleset, (caddr_t) &combat_mob, 0,
"How much mobility do units spend for combat (* casualties/bodies)" },
{ "people_damage", doubleset, (caddr_t) &people_damage, 0,
"People take this amount of normal damage" },
{ "unit_damage", doubleset, (caddr_t) &unit_damage, 0,
"Land units take this amount of normal damage" },
{ "collateral_dam", doubleset, (caddr_t) &collateral_dam, 0,
"Side effect damage amount done to sector" },
{ "assault_penalty", doubleset, (caddr_t) &assault_penalty, 0,
"Amount of normal attacking efficiency for paratroopers and assaulting" },
{ "fire_range_factor", floatset, (caddr_t) &fire_range_factor, 0,
"Scale normal firing ranges by this amount" },
{ "sect_mob_neg_factor", intset, (caddr_t) &sect_mob_neg_factor, 0,
"Amount of negative mobility a sector has after takeover (ETU / x) (MOB_ACCESS)" },
{ "mission_mob_cost", doubleset, (caddr_t) &mission_mob_cost, 0,
"Cost to put something on a mission (percentage of max mob)" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Populace" },
{ "uwbrate", doubleset, (caddr_t) &uwbrate, 0,
"Birth rate for uw's" },
{ "money_civ", doubleset, (caddr_t) &money_civ, 0,
"Money gained from taxes on a civilian in one ETU" },
{ "money_mil", doubleset, (caddr_t) &money_mil, 0,
"Money gained from taxes on an active soldier in one ETU" },
{ "money_res", doubleset, (caddr_t) &money_res, 0,
"Money gained from taxes on a soldier on active reserve in one ETU" },
{ "money_uw", doubleset, (caddr_t) &money_uw, 0,
"Money gained from taxes on an uncompensated worker in one ETU" },
{ "babyeat", doubleset, (caddr_t) &babyeat, 0,
"Amount of food to mature 1 baby into a civilian" },
{ "bankint", doubleset, (caddr_t) &bankint, 0,
"Bank dollar gain (per bar per etu)" },
{ "eatrate", doubleset, (caddr_t) &eatrate, 0,
"Food eating rate for mature people" },
{ "fcrate", doubleset, (caddr_t) &fcrate, 0,
"Food cultivation rate (* workforce in sector)" },
{ "fgrate", doubleset, (caddr_t) &fgrate, 0,
"Food growth rate (* fertility of sector)" },
{ "obrate", doubleset, (caddr_t) &obrate, 0,
"Civilian birth rate" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Nukes" },
{ "decay_per_etu", doubleset, (caddr_t) &decay_per_etu, 0,
"Decay of fallout per ETU" },
{ "fallout_spread", doubleset, (caddr_t) &fallout_spread, 0,
"Amount of fallout that leaks into surrounding sectors" },
{ "drnuke_const", floatset, (caddr_t) &drnuke_const, 0,
"Amount of research to tech needed to build a nuke (if DR_NUKE is on)" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Market/Trade" },
{ "MARK_DELAY", intset, (caddr_t) &MARK_DELAY, 0,
"Number of seconds commodities stay on the market for bidding" },
{ "TRADE_DELAY", intset, (caddr_t) &TRADE_DELAY, 0,
"Number of seconds ships, planes, and units stay on the market for bidding" },
{ "maxmult", doubleset, (caddr_t) &maxmult, 0,
"Maximum trade multiple (used for mult command)" },
{ "minmult", doubleset, (caddr_t) &minmult, 0,
"Minimum trade multiple (used for mult command)" },
{ "buytax", doubleset, (caddr_t) &buytax, 0,
"Tax (in percentage points) charged to the buyer on market purchases" },
{ "tradetax", doubleset, (caddr_t) &tradetax, 0,
"Amount of a trade transaction the seller makes (the rest is tax)" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Trade ships" },
{ "trade_1_dist", intset, (caddr_t) &trade_1_dist, 0,
"Less than this distance no money for cashing in" },
{ "trade_2_dist", intset, (caddr_t) &trade_2_dist, 0,
"Less than this distance gets trade_1 money for cashing in" },
{ "trade_3_dist", intset, (caddr_t) &trade_3_dist, 0,
"Less than this distance gets trade_2 money for cashing in (>= gets trade_3" },
{ "trade_1", floatset, (caddr_t) &trade_1, 0,
"Return per sector on trade_1 distance amount" },
{ "trade_2", floatset, (caddr_t) &trade_2, 0,
"Return per sector on trade_2 distance amount" },
{ "trade_3", floatset, (caddr_t) &trade_3, 0,
"Return per sector on trade_3 distance amount" },
{ "trade_ally_bonus", floatset, (caddr_t) &trade_ally_bonus, 0,
"Bonus you get for cashing in with an ally" },
{ "trade_ally_cut", floatset, (caddr_t) &trade_ally_cut, 0,
"Bonus your ally gets for you cashing in with them" },
{ "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Misc." },
{ "fuel_mult", intset, (caddr_t) &fuel_mult, 0,
"Multiplier for fuel to mobility calculation" },
{ "lost_items_timeout", intset, (caddr_t) &lost_items_timeout, 0,
"Seconds before a lost item is timed out of the database" },
{ "last_demand_update", longset, (caddr_t) &last_demand_update, 0,
"When was the last demand update occured" },
{ NULL, NULL, (caddr_t)0, 0, NULL }
};
static void fixup_files _PROTO((void));
static struct keymatch *keylookup _PROTO((s_char *key, struct keymatch tbl[]));
/*
* read in empire configuration
*/
int
emp_config (char *file)
{
FILE *fp;
s_char scanspace[1024];
s_char *av[65];
char buf[BUFSIZ];
struct keymatch *kp;
if (file == NULL || (fp = fopen (file, "r")) == NULL) {
fixup_files ();
return RET_OK;
}
while (fgets (buf, sizeof buf, fp) != NULL) {
if (buf[0] == '#' || buf[0] == '\n')
continue;
if (parse (buf, av, 0, scanspace, 0) < 0) {
fprintf (stderr, "Can't parse line %s", buf);
continue;
}
if ((kp = keylookup (av[0], configkeys)) != NULL) {
(*kp -> km_func) (kp, av + 1);
}
else {
fprintf (stderr, "Unknown config key %s\n", av[0]);
}
}
fclose (fp);
fixup_files ();
return RET_OK;
}
struct otherfiles {
s_char **files;
char *name;
};
extern s_char *upfil, *downfil, *disablefil, *banfil, *authfil;
extern s_char *commfil, *annfil, *telfil, *teldir, *timestampfil;
/* list of other well known files... -maybe tailor these oneday
* anyway - meantime they are all relative to datadir */
static struct otherfiles ofiles[] = {
{ &upfil, "up" },
{ &downfil, "down"},
{ &disablefil, "disable"},
{ &banfil, "ban" },
{ &authfil, "auth" },
{ &commfil, "comm" },
{ &annfil, "ann" },
{ &timestampfil, "timestamp" },
{ &teldir, "tel" },
#if !defined(_WIN32)
{ &telfil, "tel/tel" },
#else
{ &telfil, "tel\\tel" },
#endif
{ NULL, NULL }
};
/* fix up the empfile struct to reference full path names */
static void fixup_files (void)
{
struct empfile *ep;
struct otherfiles *op;
s_char buf[1024];
for (ep = empfile; ep < &empfile[EF_MAX]; ep ++) {
#if !defined(_WIN32)
sprintf (buf, "%s/%s", datadir, ep->name);
#else
sprintf (buf, "%s\\%s", datadir, ep->name);
#endif
ep->file = strdup (buf);
}
for (op = ofiles; op -> files; op ++) {
#if !defined(_WIN32)
sprintf (buf, "%s/%s", datadir, op -> name);
#else
sprintf (buf, "%s\\%s", datadir, op -> name);
#endif
*op -> files = strdup (buf);
}
}
/* find the key in the table */
static struct keymatch *keylookup (register s_char *command, struct keymatch *tbl)
{
register struct keymatch *kp;
if (command == 0 || *command == 0)
return 0;
for (kp = tbl; kp->km_key != 0; kp++) {
if (strcmp (kp -> km_key, command) == 0)
return kp;
}
return NULL;
}
/* worldx int setting function */
void worldxset (struct keymatch *kp, s_char **av)
{
int *intptr = (int *)kp -> km_data;
if (*av == NULL || intptr == NULL)
return;
*intptr = atoi (*av);
if (!((*intptr % 2) == 0)) {
/* Must be div / 2, so subtract one */
*intptr = *intptr - 1;
}
}
/* generic int setting function */
void intset (struct keymatch *kp, s_char **av)
{
int *intptr = (int *)kp -> km_data;
if (*av == NULL || intptr == NULL)
return;
*intptr = atoi (*av);
}
/* generic float set function */
void floatset (struct keymatch *kp, s_char **av)
{
float *floatptr = (float *)kp -> km_data;
if (*av == NULL || floatptr == NULL)
return;
*floatptr = atof (*av);
}
/* generic string set function */
void optstrset (struct keymatch *kp, s_char **av)
{
s_char **confstrp = (s_char **)kp -> km_data;
if (*av == NULL || confstrp == NULL)
return;
if (kp -> km_flags & KM_ALLOC)
free (*confstrp);
*confstrp = strdup (*av);
kp -> km_flags |= KM_ALLOC;
}
/* generic double set function */
void doubleset (struct keymatch *kp, s_char **av)
{
double *doublep = (double *)kp -> km_data;
if (*av == NULL || doublep == NULL)
return;
*doublep = atof (*av);
}
/* generic long set function */
void longset (struct keymatch *kp, s_char **av)
{
long int *longp = (long int *)kp -> km_data;
if (*av == NULL || longp == NULL)
return;
*longp = atol (*av);
}
void
print_config (FILE *fp)
{
struct empfile *ep;
struct otherfiles *op;
struct keymatch *kp;
fprintf(fp, "# Empire Configuration File:\n");
for (kp = configkeys; kp -> km_key; kp ++) {
/* We print a few special things here */
if (kp->km_comment) {
if (kp->km_comment[0]) {
if (kp->km_comment[0] != '\n')
fprintf(fp, "\n# ");
fprintf(fp, "%s\n", kp->km_comment);
}
}
if (!kp->km_key[0])
continue;
if (kp -> km_func == optstrset) {
fprintf (fp, "%s \"%s\"\n", kp -> km_key,
*(s_char **)kp -> km_data);
}
else if (kp -> km_func == intset) {
fprintf (fp, "%s %d\n", kp -> km_key,
*(int *)kp -> km_data);
}
else if (kp -> km_func == worldxset) {
fprintf (fp, "%s %d\n", kp -> km_key,
*(int *)kp -> km_data);
}
else if (kp -> km_func == floatset) {
fprintf (fp, "%s %g\n", kp -> km_key,
*(float *)kp -> km_data);
}
else if (kp -> km_func == doubleset) {
fprintf (fp, "%s %g\n", kp -> km_key,
*(double *)kp -> km_data);
}
else if (kp -> km_func == longset) {
fprintf (fp, "%s %ld\n", kp -> km_key,
*(long *)kp -> km_data);
}
else if (kp -> km_func == optionset) {
struct option_list *op;
for (op = Options; op -> opt_key; op++) {
if (*op -> opt_valuep)
fprintf (fp, "%s %s\n", kp -> km_key,
op -> opt_key);
}
}
else if (kp -> km_func == optiondel) {
struct option_list *op;
for (op = Options; op -> opt_key; op++) {
if (*op -> opt_valuep == 0)
fprintf (fp, "%s %s\n", kp -> km_key,
op -> opt_key);
}
}
else fprintf (fp, "# Unknown format %s\n", kp -> km_key);
}
fprintf(fp, "\n");
for (ep = empfile; ep < &empfile[EF_MAX]; ep ++)
fprintf (fp, "# File %s -> %s\n", ep -> name, ep -> file);
for (op = ofiles; op -> files; op ++)
fprintf (fp, "# File %s -> %s\n", op -> name, *(op -> files));
}
/* add an option to the list */
void set_option (const char *s)
{
struct option_list *op;
for (op = Options; op -> opt_key; op++) {
if (strcmp (op -> opt_key, s) == 0)
*op -> opt_valuep = 1;
}
}
/* delete an option from the list */
void delete_option (const char *s)
{
struct option_list *op;
for (op = Options; op -> opt_key; op++) {
if (strcmp (op -> opt_key, s) == 0)
*op -> opt_valuep = 0;
}
}
/* config interface */
void optionset (struct keymatch *kp, s_char **av)
/* unused - we have a well known global */
{
char **cpp;
for (cpp = (char **)av; *cpp; cpp++)
set_option (*cpp);
}
/* config interface */
void optiondel (struct keymatch *kp, s_char **av)
/* unused - we have a well known global */
{
char **cpp;
for (cpp = (char **)av; *cpp; cpp++)
delete_option (*cpp);
}

50
src/lib/gen/getstarg.c Normal file
View file

@ -0,0 +1,50 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* getstarg.c: Get a string argument (ask if not there)
*
* Known contributors to this file:
*
*/
#include "misc.h"
#include "gen.h"
s_char *
getstarg(s_char *input, s_char *prompt, s_char *buf)
{
extern s_char *getstring(s_char *prompt, s_char *buf);
*buf = '\0';
if (input == 0 || *input == 0) {
if (getstring(prompt, buf) == 0)
return 0;
} else {
strcpy(buf, input);
}
return buf;
}

45
src/lib/gen/getstring.c Normal file
View file

@ -0,0 +1,45 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* getstring.c: get string, printing a prompt if there is one
*
* Known contributors to this file:
*
*/
#include "misc.h"
#include "gen.h"
#include "subs.h"
s_char *
getstring(s_char *prompt, s_char *buf)
{
*buf = '\0';
if (prmptrd(prompt, buf, 1024) < 0)
return 0;
return buf;
}

90
src/lib/gen/hpux.c Normal file
View file

@ -0,0 +1,90 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* hpux.c: Things specific to hpux
*
* Known contributors to this file:
*
*/
#include "misc.h"
#ifdef hpux
#include <stdio.h>
setbuffer(fp, buf, size)
FILE *fp;
s_char *buf;
int size;
{
if (size > BUFSIZ)
setbuf(fp, buf);
/* XXX else report error */
}
s_char *
rindex(sp, c)
register s_char *sp;
register int c;
{
register s_char *r;
r = NULL;
do {
if (*sp == c)
r = sp;
} while (*sp++);
return r;
}
s_char *
index(sp, c)
register s_char *sp;
register int c;
{
do {
if (*sp == c)
return (sp);
} while (*sp++);
return NULL;
}
int
ffs(marg)
register unsigned marg;
{
register unsigned bval;
register int i;
if (marg == 0)
return 0;
for (bval=1, i=1; i <= 32; i++, bval <<= 1)
if (marg & bval)
return i;
return 0;
}
#endif

52
src/lib/gen/iceil.c Normal file
View file

@ -0,0 +1,52 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* iceil.c: integer ceiling/floor functions
*
* Known contributors to this file:
*
*/
#include "gen.h"
int
iceil(double arg)
{
register int i;
i = arg;
return(i >= arg ? i : i + 1);
}
int
ifloor(double arg)
{
register int i;
i = arg;
return(i <= arg ? i : i - 1);
}

53
src/lib/gen/inet.c Normal file
View file

@ -0,0 +1,53 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* inet.c: network format conversion routines
*
* Known contributors to this file:
* Doug Hay, 1998
*/
#include <sys/types.h>
#if !defined(_WIN32)
#include <netinet/in.h>
#endif
#include <stdio.h> /* sprintf */
#include "misc.h"
#include "gen.h"
#if !defined(NeXT) && !defined(_WIN32)
s_char *
inet_ntoa(struct in_addr addr)
{
static s_char str[18];
register u_char *p;
p = (u_char *)&addr;
sprintf(str, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return str;
}
#endif /* NeXT */

535
src/lib/gen/io.c Normal file
View file

@ -0,0 +1,535 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* io.c: Arrange for input and output on a file descriptor to be queued.
*
* Known contributors to this file:
* Doug Hay, 1998
* Steve McClure, 1998
*/
/*
* Arrange for input and output on a file descriptor
* to be queued. Provide main loop -- a mechanism for
* blocking across all registered file descriptors, and
* reading or writing when appropriate.
*/
#include <errno.h>
#include <sys/types.h>
#if !defined(_WIN32)
#include <sys/uio.h>
#include <sys/file.h>
#include <unistd.h> /* close read shutdown select */
#include <sys/socket.h>
#endif
#include <time.h>
#include <fcntl.h>
#include <stdlib.h> /* malloc calloc free */
#if defined(_WIN32)
#include <winsock.h>
#endif
#include "misc.h"
#include "bit.h"
#include "queue.h"
#include "ioqueue.h"
#include "io_mask.h"
#include "empio.h"
#include "gen.h" /* getfdtablesize */
#include "empthread.h"
extern struct player *player; /* XXX */
static struct iop **io_list;
static struct io_mask *iom;
static bit_fdmask newoutput;
struct iop {
int fd;
struct ioqueue *input;
struct ioqueue *output;
int flags;
s_char *assoc;
int bufsize;
int (*notify)();
};
extern int errno;
void
io_init(void)
{
iom = iom_create(IO_READ|IO_WRITE);
io_list = (struct iop **) calloc(getfdtablesize(), sizeof(*io_list));
newoutput = bit_newfdmask();
}
struct iop *
io_open(int fd, int flags, int bufsize, int (*notify) (void), s_char *assoc)
{
struct iop *iop;
if (io_list[fd] != 0) {
/* already exists */
return 0;
}
flags = flags & (IO_READ|IO_WRITE|IO_NBLOCK|IO_NEWSOCK);
if ((flags & (IO_READ|IO_WRITE)) == 0)
return 0;
iop = (struct iop *) malloc(sizeof(struct iop));
iop->fd = fd;
iop->input = 0;
iop->output = 0;
iop->flags = 0;
iop->bufsize = bufsize;
if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
iop->input = ioq_create(bufsize);
if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
iop->output = ioq_create(bufsize);
if (flags & IO_NBLOCK)
io_noblocking(iop, 1);
iop->flags = flags;
iop->assoc = assoc;
iop->notify = notify;
io_list[fd] = iop;
iom_set(iom, flags, fd);
return iop;
}
void
io_close(struct iop *iop)
{
if (iop->input != 0)
ioq_destroy(iop->input);
if (iop->output != 0)
ioq_destroy(iop->output);
iom_clear(iom, iop->flags, iop->fd);
BIT_CLRB(iop->fd, newoutput);
io_list[iop->fd] = 0;
#if !defined(_WIN32)
(void) close(iop->fd);
#else
closesocket(iop->fd);
#endif
free((s_char *)iop);
}
int
io_input(struct iop *iop, int waitforinput)
{
s_char buf[IO_BUFSIZE];
int cc;
/* Not a read IOP */
if ((iop->flags & IO_READ) == 0)
return -1;
/* IOP is markes as in error. */
if (iop->flags & IO_ERROR)
return -1;
/* Wait for the file to have input. */
if (waitforinput) {
empth_select(iop->fd, EMPTH_FD_READ);
}
#if !defined(_WIN32)
/* Do the actual read. */
cc = read(iop->fd, buf, sizeof(buf));
if (cc < 0) {
/* would block, so nothing to read. */
if (errno == EWOULDBLOCK)
return 0;
/* Some form of file error occurred... */
iop->flags |= IO_ERROR;
iom_clear(iom, IO_READ, iop->fd);
return -1;
}
#else
cc = recv(iop->fd, buf, sizeof(buf), 0);
if (cc == SOCKET_ERROR) {
int err = WSAGetLastError();
/* Hmm, it would block. file is opened noblock, soooooo.. */
if (err == WSAEWOULDBLOCK)
return 0;
/* Some form of file error occurred... */
iop->flags |= IO_ERROR;
iom_clear(iom, IO_READ, iop->fd);
return -1;
}
#endif
/* We eof'd */
if (cc == 0) {
iop->flags |= IO_EOF;
return 0;
}
/* Append the input to the IOQ. */
ioq_append(iop->input, buf, cc);
return cc;
}
int
io_inputwaiting(struct iop *iop)
{
return ioq_qsize(iop->input);
}
int
io_outputwaiting(struct iop *iop)
{
return ioq_qsize(iop->output);
}
int
io_output(struct iop *iop, int waitforoutput)
{
#if !defined(_WIN32)
struct iovec iov[16];
#else
s_char buf[IO_BUFSIZE];
#endif
int cc;
int n;
int remain;
/* If there is no output waiting. */
if (!io_outputwaiting(iop))
return 0;
/* bit clear */
BIT_CLRB(iop->fd, newoutput);
/* If the iop is not write enabled. */
if ((iop->flags & IO_WRITE) == 0)
return -1;
/* If the io is marked as in error... */
if (iop->flags & IO_ERROR)
return -1;
/* This is the same test as io_outputwaiting.... */
if (ioq_qsize(iop->output) == 0)
return 0;
#if !defined(_WIN32)
/* make the iov point to the data in the queue. */
/* I.E., each of the elements in the queue. */
/* returns the number of elements in the iov. */
n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
#else
/* Make a buffer containing the output to write. */
n = ioq_makebuf(iop->output, buf, sizeof(buf));
#endif
if (n <= 0) {
/* If we got no elements, we have no output.... */
iom_clear(iom, IO_WRITE, iop->fd);
return 0;
}
/* wait for the file to be output ready. */
if (waitforoutput != IO_NOWAIT) {
/* This waits for the file to be ready for writing, */
/* and lets other threads run. */
empth_select(iop->fd, EMPTH_FD_WRITE);
}
/* Do the actual write. */
#if !defined(_WIN32)
cc = writev(iop->fd, iov, n);
/* if it failed.... */
if (cc < 0) {
/* Hmm, it would block. file is opened noblock, soooooo.. */
if (errno == EWOULDBLOCK) {
/* If there are remaining bytes, set the IO as remaining.. */
remain = ioq_qsize(iop->output);
if (remain > 0)
iom_set(iom, IO_WRITE, iop->fd);
return remain;
}
iop->flags |= IO_ERROR;
iom_clear(iom, IO_WRITE, iop->fd);
return -1;
}
#else
cc = send(iop->fd, buf, n, 0);
/* if it failed.... */
if (cc == SOCKET_ERROR) {
int err = WSAGetLastError();
/* Hmm, it would block. file is opened noblock, soooooo.. */
if (err == WSAEWOULDBLOCK) {
/* If there are remaining bytes, set the IO as remaining.. */
remain = ioq_qsize(iop->output);
if (remain > 0)
iom_set(iom, IO_WRITE, iop->fd);
return remain;
}
iop->flags |= IO_ERROR;
iom_clear(iom, IO_WRITE, iop->fd);
return -1;
}
#endif
/* If no bytes were written, something happened.. Like an EOF. */
#ifndef hpux
if (cc == 0) {
iop->flags |= IO_EOF;
return 0;
}
#else
if (cc == 0) {
remain = ioq_qsize(iop->output);
if (remain > 0)
iom_set(iom, IO_WRITE, iop->fd);
return remain;
}
#endif /* hpux */
/* Remove the number of written bytes from the queue. */
ioq_dequeue(iop->output, cc);
/* If the queue has stuff remaining, set it still needing output. */
remain = ioq_qsize(iop->output);
if (remain == 0) {
iom_clear(iom, IO_WRITE, iop->fd);
} else {
iom_set(iom, IO_WRITE, iop->fd);
}
return cc;
}
int
io_select(struct timeval *tv)
{
bit_fdmask readmask;
bit_fdmask writemask;
int n;
int nfds;
int fd;
struct iop *iop;
iom_getmask(iom, &nfds, &readmask, &writemask);
n = select(nfds + 1, (fd_set *)readmask, (fd_set *)writemask, 0, tv);
if (n <= 0) {
if (errno == EINTR)
return 0;
return -1;
}
while ((fd = bit_fd(readmask)) >= 0) {
iop = io_list[fd];
if ((iop->flags & IO_NEWSOCK) == 0)
(void) io_input(iop, IO_NOWAIT);
if (iop->notify != 0)
iop->notify(iop, IO_READ, iop->assoc);
BIT_CLRB(fd, readmask);
}
while ((fd = bit_fd(writemask)) >= 0) {
iop = io_list[fd];
if (io_output(iop, IO_NOWAIT) < 0 && iop->notify != 0)
iop->notify(iop, IO_WRITE, iop->assoc);
BIT_CLRB(fd, writemask);
}
return n;
}
void
io_flush(int doWait)
{
int fd;
struct iop *iop;
while ((fd = bit_fd(newoutput)) >= 0) {
iop = io_list[fd];
if (io_output(iop, doWait) < 0 && iop->notify != 0)
iop->notify(iop, IO_WRITE, iop->assoc);
}
}
int
io_peek(struct iop *iop, s_char *buf, int nbytes)
{
if ((iop->flags & IO_READ) == 0)
return -1;
return ioq_peek(iop->input, buf, nbytes);
}
int
io_read(struct iop *iop, s_char *buf, int nbytes)
{
int cc;
if ((iop->flags & IO_READ) == 0)
return -1;
cc = ioq_peek(iop->input, buf, nbytes);
if (cc > 0)
ioq_dequeue(iop->input, cc);
return cc;
}
int
io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
{
int len;
if ((iop->flags & IO_WRITE) == 0)
return -1;
ioq_append(iop->output, buf, nbytes);
BIT_SETB(iop->fd, newoutput);
len = ioq_qsize(iop->output);
if (len > iop->bufsize) {
if (doWait) {
io_output_all(iop);
} else {
/* only try a write every BUFSIZE characters */
if (((len-nbytes) % iop->bufsize) <
(len % iop->bufsize))
io_output(iop, 0);
}
}
return nbytes;
}
int
io_output_all(struct iop *iop)
{
int n;
while ((n = io_output(iop, IO_NOWAIT)) > 0) {
empth_select(iop->fd, EMPTH_FD_WRITE);
}
return n;
}
int
io_gets(struct iop *iop, s_char *buf, int nbytes)
{
if ((iop->flags & IO_READ) == 0)
return -1;
return ioq_gets(iop->input, buf, nbytes);
}
int
io_puts(struct iop *iop, s_char *buf)
{
if ((iop->flags & IO_WRITE) == 0)
return -1;
BIT_SETB(iop->fd, newoutput);
return ioq_puts(iop->output, buf);
}
int
io_shutdown(struct iop *iop, int flags)
{
flags &= (IO_READ|IO_WRITE);
if ((iop->flags & flags) != flags)
return -1;
if (flags & IO_READ) {
shutdown(iop->fd, 0);
ioq_drain(iop->input);
}
if (flags & IO_WRITE) {
shutdown(iop->fd, 1);
ioq_drain(iop->output);
}
return 0;
}
int
io_noblocking(struct iop *iop, int value)
{
#if !defined(_WIN32)
int flags;
flags = fcntl(iop->fd, F_GETFL, 0);
if (flags < 0)
return -1;
if (value == 0)
flags &= ~FNDELAY;
else
flags |= FNDELAY;
if (fcntl(iop->fd, F_SETFL, flags) < 0)
return -1;
#else
u_long arg = value;
ioctlsocket(iop->fd, FIONBIO, &arg);
#endif
if (value == 0)
iop->flags &= ~IO_NBLOCK;
else
iop->flags |= IO_NBLOCK;
return 0;
}
int
io_conn(struct iop *iop)
{
return (iop->flags & IO_CONN);
}
int
io_error(struct iop *iop)
{
return (iop->flags & IO_ERROR);
}
int
io_eof(struct iop *iop)
{
return (iop->flags & IO_EOF);
}
int
io_fileno(struct iop *iop)
{
return iop->fd;
}
struct iop *
io_iopfromfd(int fd)
{
return io_list[fd];
}

113
src/lib/gen/io_mask.c Normal file
View file

@ -0,0 +1,113 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* iom.c: implements the io mask routines
*
* Known contributors to this file:
*
*/
#include <stdlib.h> /* malloc */
#include <errno.h>
#include "misc.h"
#include "bit.h"
#include "empio.h"
#include "io_mask.h"
extern int errno;
struct io_mask *
iom_create(int what)
{
struct io_mask *imp;
imp = (struct io_mask *) malloc(sizeof(*imp));
if (what & IO_READ) {
imp->readmask = bit_newfdmask();
imp->user_readmask = bit_newfdmask();
} else {
imp->readmask = 0;
imp->user_readmask = 0;
}
if (what & IO_WRITE) {
imp->writemask = bit_newfdmask();
imp->user_writemask = bit_newfdmask();
} else {
imp->writemask = 0;
imp->user_writemask = 0;
}
imp->what = what;
imp->maxfd = 0;
return imp;
}
void
iom_getmask(struct io_mask *mask, int *nfdp, bit_fdmask *readp, bit_fdmask *writep)
{
if (mask->what & IO_READ)
bit_copy(mask->readmask, mask->user_readmask);
if (mask->what & IO_WRITE)
bit_copy(mask->writemask, mask->user_writemask);
*readp = mask->user_readmask;
*writep = mask->user_writemask;
*nfdp = mask->maxfd;
}
void
iom_set(struct io_mask *mask, int what, int fd)
{
if ((mask->what & what) == 0)
return;
if (what & IO_READ)
BIT_SETB(fd, mask->readmask);
if (what & IO_WRITE)
BIT_SETB(fd, mask->writemask);
if (fd > mask->maxfd)
mask->maxfd = fd;
}
void
iom_clear(struct io_mask *mask, int what, int fd)
{
if ((mask->what & what) == 0)
return;
if (what & IO_READ)
BIT_CLRB(fd, mask->readmask);
if (what & IO_WRITE)
BIT_CLRB(fd, mask->writemask);
}
void
iom_zero(struct io_mask *mask, int what)
{
if ((mask->what & what) == 0)
return;
if (what & IO_READ)
bit_zero(mask->readmask);
if (what & IO_WRITE)
bit_zero(mask->writemask);
}

456
src/lib/gen/ioqueue.c Normal file
View file

@ -0,0 +1,456 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* ioqueue.c: Read and write i/o queues
*
* Known contributors to this file:
*
*/
/*
* Read and write onto io queues. Note that
* the io queues don't actually do any writing;
* that is left for a higher level.
*/
#include <stdio.h>
#include <stdlib.h> /* malloc free */
#include <sys/types.h>
#if !defined(_WIN32)
#include <sys/uio.h>
#endif
#include "misc.h"
#include "queue.h"
#include "ioqueue.h"
static int ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc);
#if !defined(_WIN32)
static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max);
#endif
static int ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc);
static int appendcc(struct ioqueue *ioq, s_char *buf, int cc);
static int removecc(struct ioqueue *ioq, register int cc);
#if defined(_WIN32)
static void loc_StripDels(char *pBuf);
#endif
struct ioqueue *
ioq_create(int size)
{
struct ioqueue *ioq;
ioq = (struct ioqueue *) malloc(sizeof(struct ioqueue));
emp_initque(&ioq->list.queue);
ioq->list.nbytes = 0;
ioq->list.offset = 0;
ioq->list.size = 0;
ioq->list.data = 0;
ioq->bufsize = size;
ioq->cc = 0;
return ioq;
}
void
ioq_destroy(struct ioqueue *ioq)
{
#if !defined(aix) && !defined(NeXT)
/* ioq_drain doesn't work under aix or NeXT... dunno why --ts */
ioq_drain(ioq);
#endif /* aix */
free((s_char *)ioq);
}
void
ioq_drain(struct ioqueue *ioq)
{
struct emp_qelem *qp;
struct io *io;
while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue){
io = (struct io *) qp;
(void) emp_remque(&io->queue);
(void) free((s_char *)io->data);
(void) free((s_char *)io);
}
ioq->cc = 0;
}
/*
* copy batch of pointers into the passed
* iovec, but don't actually dequeue the data.
* return # of iovec initialized.
*/
#if !defined(_WIN32)
int
ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
{
if (ioq->cc <= 0)
return 0;
return ioqtoiov(ioq, iov, cc);
}
#endif
/*
* Copy the specified number of characters into the buffer
* provided, without actually dequeueing the data. Return
* number of bytes actually found.
*/
int
ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
{
return ioqtobuf(ioq, buf, cc);
}
int
ioq_dequeue(struct ioqueue *ioq, int cc)
{
return removecc(ioq, cc);
}
void
ioq_append(struct ioqueue *ioq, s_char *buf, int cc)
{
appendcc(ioq, buf, cc);
}
int
ioq_qsize(struct ioqueue *ioq)
{
return ioq->cc;
}
/*
* read a line of text up to (but not including)
* the newline. return -1 and read nothing if
* no input is available
*/
int
ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
{
int nbytes;
int actual;
nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
if (nbytes >= 0) {
actual = nbytes;
if (actual > cc - 1)
actual = cc - 1;
buf[actual] = '\0';
/* remove the newline too */
removecc(ioq, nbytes + 1);
#if defined(_WIN32)
loc_StripDels(buf);
#endif
}
return nbytes;
}
int
ioq_puts(struct ioqueue *ioq, s_char *buf)
{
return appendcc(ioq, buf, strlen(buf));
}
/*
* all the rest are local to this module
*/
/*
* copy cc bytes from ioq to buf.
* this routine doesn't free memory; this is
* left for a higher level.
*/
static
int
ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
{
struct io *io;
struct emp_qelem *qp;
struct emp_qelem *head;
register int nbytes;
register int nleft;
register s_char *offset;
nleft = cc;
offset = buf;
head = &ioq->list.queue;
for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
io = (struct io *) qp;
if ((nbytes = io->nbytes - io->offset) < 0) {
/* XXX log something here */
continue;
}
if (nbytes > 0) {
if (nleft < nbytes)
nbytes = nleft;
bcopy(io->data + io->offset, offset, nbytes);
offset += nbytes;
nleft -= nbytes;
}
}
return offset - buf;
}
/*
* copy at most cc bytes from ioq to buf,
* terminating on the stop character.
*/
static
int
ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc)
{
register int nbytes;
register s_char *p;
register int n;
struct io *io;
struct emp_qelem *qp;
struct emp_qelem *head;
int total;
int found;
head = &ioq->list.queue;
found = 0;
total = 0;
for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
io = (struct io *) qp;
if ((nbytes = io->nbytes - io->offset) <= 0)
continue;
p = io->data + io->offset;
for (n=0; n < nbytes && p[n] != stopc; n++)
;
total += n;
if (n < nbytes) {
found++;
break;
}
}
if (found == 0)
return -1;
ioqtobuf(ioq, buf, cc < total ? cc : total);
return total;
}
/*
* initialize an iovec to point at max bytes worth
* of data from the ioqueue.
*/
#if !defined(_WIN32)
static
int
ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
{
struct io *io;
register int cc;
register int niov;
register int len;
struct emp_qelem *qp;
cc = max;
niov = 0;
qp = ioq->list.queue.q_forw;
while (qp != &ioq->list.queue && cc > 0) {
io = (struct io *) qp;
len = io->nbytes - io->offset;
if (len > cc)
len = cc;
iov->iov_base = io->data + io->offset;
iov->iov_len = len;
cc -= len;
niov++;
iov++;
qp = qp->q_forw;
if (niov >= 16)
break;
}
return niov;
}
#endif
/*
* append a buffer to the end of the ioq.
*/
static
int
appendcc(struct ioqueue *ioq, s_char *buf, int cc)
{
struct io *io;
int len;
s_char *ptr;
int avail;
/* determine if any space is left */
io = (struct io *)ioq->list.queue.q_back;
avail = io->size - io->nbytes;
if (avail > 0) {
/* append to existing buffer */
len = cc > avail ? avail : cc;
bcopy(buf, io->data + io->nbytes, len);
io->nbytes += len;
ioq->cc += len;
if (avail < cc)
appendcc(ioq, buf + len, cc - len);
} else {
/* create a new buffer, minimum bufsize bytes */
len = cc > ioq->bufsize ? cc : ioq->bufsize;
ptr = malloc(len);
bcopy(buf, ptr, cc);
io = (struct io *) malloc(sizeof(struct io));
io->nbytes = cc;
io->size = len;
io->offset = 0;
io->data = ptr;
emp_insque(&io->queue, ioq->list.queue.q_back);
ioq->cc += cc;
}
return cc;
}
/*
* remove cc bytes from ioqueue ioq
* free memory, dequeue io elements
* which are no longer used.
*/
static
int
removecc(struct ioqueue *ioq, register int cc)
{
struct io *io;
struct emp_qelem *qp;
register int nbytes;
register int there;
register int remain;
nbytes = 0;
remain = cc;
while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
io = (struct io *) qp;
there = io->nbytes - io->offset;
if (there < 0) {
/* error */
(void) emp_remque(&io->queue);
(void) free((s_char *)io);
continue;
}
if (remain >= there) {
/* not enough or exact; free entry */
nbytes += there;
remain -= there;
(void) emp_remque(&io->queue);
(void) free(io->data);
(void) free((s_char *)io);
} else {
/* too much; increment offset */
io->offset += remain;
nbytes += remain;
remain = 0;
}
if (remain <= 0)
break;
}
ioq->cc -= nbytes;
return nbytes;
}
#if defined(_WIN32)
/*
* Make an (output) buffer up to the
* maximum size of the buffer.
*
* We don't free the bytes...
*/
int
ioq_makebuf(struct ioqueue *ioq, char *pBuf, int nBufLen)
{
struct io *io;
struct emp_qelem *qp;
struct emp_qelem *head;
int nbytes;
int nleft;
int ncopied;
s_char *offset;
ncopied = 0;
nleft = nBufLen;
offset = pBuf;
head = &ioq->list.queue;
for (qp = head->q_forw;
(qp != head) && (nleft > 0);
qp = qp->q_forw) {
io = (struct io *) qp;
nbytes = io->nbytes - io->offset;
if (nbytes < 0) {
/* Paranoid check for bad buffer. */
continue;
}
/* too many bytes, wait till next time. */
if (nbytes > nleft)
break;
bcopy(io->data + io->offset, offset, nbytes);
offset += nbytes;
nleft -= nbytes;
ncopied += nbytes;
}
return ncopied;
}
#endif /* _WIN32 */
#if defined(_WIN32)
/*
* Remove backspaces and DELs from the buffer.
*
* Why? Because I got tired of telneting into the
* server and having to type perfectly...
*/
static void
loc_StripDels(char *pBuf)
{
char *cp;
char *dp;
char *sp;
for (cp = pBuf; *cp;) {
/* Remove Backspace and DEL */
if (*cp == '\b' || *cp == '\x8F') {
if (cp == pBuf)
dp = cp;
else
dp = cp - 1;
sp = cp + 1;
while (*sp)
*dp++ = *sp++;
*dp = 0;
} else
cp++;
}
}
#endif

140
src/lib/gen/lock.c Normal file
View file

@ -0,0 +1,140 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* lock.c: Lock a file
*
* Known contributors to this file:
* Doug Hay, 1998
*/
#ifdef aix
#define L_SET 0
#endif /* aix */
#include "misc.h"
#include "gen.h"
#include "common.h"
#include <fcntl.h>
#ifdef sys5
#include <unistd.h>
#endif
#if !defined(L_SET) && !defined(_WIN32)
#include <sys/file.h>
#endif
#ifdef aix
#define L_SET 0
#endif /* aix */
#if defined(_WIN32)
#include <sys/locking.h>
int
file_lock(int fd)
{
if (_locking(fd, _LK_LOCK, 0) < 0) {
logerror("file lock (fd %d) failed", fd);
return 0;
}
return 1;
}
int
file_unlock(int fd)
{
if (_locking(fd, _LK_UNLCK, 0) < 0) {
logerror("file lock (fd %d) failed", fd);
return 0;
}
return 1;
}
#else
#ifndef NOFLOCK
int flock();
int
file_lock(int fd)
{
if (flock(fd, LOCK_EX) < 0) {
logerror("file lock (fd %d) failed", fd);
return 0;
}
return 1;
}
int
file_unlock(int fd)
{
if (flock(fd, LOCK_UN) < 0) {
logerror("file unlock (fd %d) failed", fd);
return 0;
}
return 1;
}
#else
int
file_lock(int fd)
{
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = L_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = 0;
if (fcntl(fd, F_SETLKW, &lock) < 0) {
logerror("file lock (fd %d) failed", fd);
return 0;
}
return 1;
}
int
file_unlock(int fd)
{
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = L_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = 0;
if (fcntl(fd, F_SETLKW, &lock) < 0) {
logerror("file unlock (fd %d) failed", fd);
return 0;
}
return 1;
}
#endif
#endif /* _WIN32 */

118
src/lib/gen/mapdist.c Normal file
View file

@ -0,0 +1,118 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* mapdist.c: Return the distance between two sectors
*
* Known contributors to this file:
*
*/
/*
* mapdist returns (integer) distance between two sectors.
* mapdsq returns the square of the distance -- more efficient.
*/
#include "misc.h"
#include "gen.h"
#include "optlist.h"
int
diffx(int x1, int x2)
{
int dx;
dx = x1 - x2;
dx = dx % WORLD_X;
if (dx > WORLD_X/2)
dx = dx - WORLD_X;
if (dx < -WORLD_X/2)
dx = dx + WORLD_X;
return dx;
}
int
diffy(int y1, int y2)
{
int dy;
dy = y1 - y2;
dy = dy % WORLD_Y;
if (dy > WORLD_Y/2)
dy = dy - WORLD_Y;
if (dy < -WORLD_Y/2)
dy = dy + WORLD_Y;
return dy;
}
int
deltax(int x1, int x2)
{
int dx;
dx = abs(x1 - x2);
dx = dx % WORLD_X;
if (dx > WORLD_X/2)
dx = WORLD_X - dx;
return dx;
}
int
deltay(int y1, int y2)
{
int dy;
dy = abs(y1 - y2);
dy = dy % WORLD_Y;
if (dy > WORLD_Y/2)
dy = WORLD_Y - dy;
return dy;
}
int
mapdist(int x1, int y1, int x2, int y2)
{
int dx, dy;
x1 = x1 % WORLD_X;
y1 = y1 % WORLD_Y;
x2 = x2 % WORLD_X;
y2 = y2 % WORLD_Y;
dx = deltax(x1, x2);
dy = deltay(y1, y2);
if (dx > dy)
return (dx - dy) / 2 + dy;
return dy;
}
int
mapdsq(int x1, int y1, int x2, int y2)
{
int sq;
sq = mapdist(x1, y1, x2, y2);
return sq * sq;
}

71
src/lib/gen/minmax.c Normal file
View file

@ -0,0 +1,71 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* minmax.c: Misc min and max routines
*
* Known contributors to this file:
* Doug Hay, 1998
* Steve McClure, 1998
*/
#include "gen.h"
double
dmax(double n1, double n2)
{
if (n1 > n2)
return n1;
return n2;
}
double
dmin(double n1, double n2)
{
if (n1 < n2)
return n1;
return n2;
}
#if !defined(_WIN32)
int
max(int n1, int n2)
{
if (n1 > n2)
return n1;
return n2;
}
int
min(int n1, int n2)
{
if (n1 < n2)
return n1;
return n2;
}
#endif

74
src/lib/gen/numstr.c Normal file
View file

@ -0,0 +1,74 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* numstr.c: Turn a number into a word
*
* Known contributors to this file:
* Steve McClure, 2000
*/
#ifdef Rel4
#include <string.h>
#endif /* Rel4 */
#include "misc.h"
#include "gen.h"
s_char *
numstr(s_char *buf, int n)
{
extern s_char *numnames[];
extern s_char *tennames[];
if (n > 100) {
(void) strcpy(buf, "several");
} else if (n < 0) {
(void) strcpy(buf, "a negative number of");
} else {
if (n >= 20) {
(void) strcpy(buf, tennames[n / 10]);
if (n % 10) {
(void) strcat(buf, "-");
(void) strcat(buf, numnames[n % 10]);
}
} else {
(void) strcpy(buf, numnames[n % 20]);
}
}
return buf;
}
s_char *
effadv(int n)
{
extern s_char *effadv_list[];
if (n < 0)
n = 0;
if (n >= 100)
n = 99;
return effadv_list[n/25];
}

52
src/lib/gen/onearg.c Normal file
View file

@ -0,0 +1,52 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* onearg.c: Get one argument, or ask if not there
*
* Known contributors to this file:
*
*/
#include "misc.h"
#include "gen.h"
int
onearg(s_char *arg, s_char *prompt)
{
extern s_char *getstring(s_char *prompt, s_char *buf);
int n;
s_char buf[1024];
if (arg == 0 || *arg == 0) {
if ((arg = getstring(prompt, buf)) == 0)
return -1;
}
n = atoi(arg);
if (n < 0)
return -1;
return n;
}

96
src/lib/gen/parse.c Normal file
View file

@ -0,0 +1,96 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* parse.c: Parse an Empire command line
*
* Known contributors to this file:
*
*/
/*
* parse empire command line, chop into argp
* If values argpp and spacep passed, parse will use them.
* otherwise, parse will use static space and global argp.
* parse assumes that argpp is a char *buf[16], and that spacep
* points to a buf of at least 256 bytes.
*/
#include <ctype.h>
#include "misc.h"
#include "gen.h"
int
parse(register s_char *buf, s_char **argpp, s_char **condp, s_char *space, s_char **redir)
{
register s_char *bp2;
register s_char *bp1 = space;
register s_char **arg = argpp;
int fs;
int quoted;
int argnum;
if (space == 0)
return -1;
if (redir)
*redir = 0;
if (condp != 0)
*condp = 0;
for (argnum=0; *buf && argnum < 100; ) {
arg[argnum] = bp1;
argnum++;
while (isspace(*buf))
buf++;
if (redir && (*buf == '>' || *buf == '|')) {
*redir = buf;
argnum--;
arg[argnum] = 0;
break;
}
quoted = 0;
for (bp2 = bp1; *buf; ) {
if (!quoted && isspace(*buf)) {
buf++;
break;
}
if (*buf == '"'){
quoted = !quoted;
buf++;
} else {
*bp1++ = *buf++;
}
}
*bp1++ = 0;
if (*bp2 == '?' && condp != 0) {
*condp = bp2 + 1;
argnum--;
}
}
arg[argnum] = 0;
for (fs = argnum + 1; fs < 16; fs++)
arg[fs] = 0;
return argnum;
}

59
src/lib/gen/plur.c Normal file
View file

@ -0,0 +1,59 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* plur.c: Pluralize (is that a word?) something
*
* Known contributors to this file:
*
*/
#include "misc.h"
#include "gen.h"
s_char *
plur(int n, s_char *no, s_char *yes)
{
return (n == 1 ? no : yes);
}
s_char *
splur(int n)
{
return (n == 1 ? "" : "s");
}
s_char *
esplur(int n)
{
return (n == 1 ? "" : "es");
}
s_char *
iesplur(int n)
{
return (n == 1 ? "y" : "ies");
}

65
src/lib/gen/queue.c Normal file
View file

@ -0,0 +1,65 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* queue.c: Various queue routines (for lists)
*
* Known contributors to this file:
*
*/
#include "queue.h"
void
emp_insque(struct emp_qelem *elem, struct emp_qelem *queue)
{
struct emp_qelem *next;
next = queue->q_forw;
queue->q_forw = elem;
elem->q_forw = next;
elem->q_back = queue;
next->q_back = elem;
}
void
emp_remque(struct emp_qelem *elem)
{
if (elem == (struct emp_qelem *)0)
return;
if (elem->q_forw != (struct emp_qelem *)0)
elem->q_forw->q_back = elem->q_back;
if (elem->q_back != (struct emp_qelem *)0)
elem->q_back->q_forw = elem->q_forw;
}
void
emp_initque(struct emp_qelem *elem)
{
elem->q_forw = elem;
elem->q_back = elem;
}

52
src/lib/gen/round.c Normal file
View file

@ -0,0 +1,52 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* round.c: Round off functions
*
* Known contributors to this file:
*
*/
#include "gen.h"
int
roundintby(int n, int m)
{
register int r11;
r11 = (m >> 1) + n;
return (r11 / m * m);
}
int
ldround(double a4, int ac)
{
int f4;
f4 = ac / 2.0 + a4;
return (f4 / ac * ac);
}

45
src/lib/gen/same.c Normal file
View file

@ -0,0 +1,45 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* same.c: Return true if strings are the same
*
* Known contributors to this file:
*
*/
#include "misc.h"
#include "gen.h"
int
same(register s_char *s1, register s_char *s2)
{
while (*s1 == *s2++) {
if (*s1++ == 0)
return 1;
}
return 0;
}

44
src/lib/gen/scthash.c Normal file
View file

@ -0,0 +1,44 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* scthash.c: Create a hash function based on the sector x-y mod table size
*
* Known contributors to this file:
* Dave Pare, 1986
*/
#include "gen.h"
int
scthash(register int x, register int y, int tsize)
{
if (x < 0)
x = -x;
if (y < 0)
y = -y;
return ((x*10 + y) % tsize);
}

105
src/lib/gen/signal.c Normal file
View file

@ -0,0 +1,105 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* sig.c: block and unblock signals for critical sections
*
* Known contributors to this file:
* Doug Hay, 1998
* Steve McClure, 1998
*/
#include "misc.h"
#include "gen.h"
#include <signal.h>
u_int mask;
void
blocksig(void)
{
#if !defined(_WIN32)
mask = sigsetmask(0xffffffff);
#endif
}
void
unblocksig(void)
{
#if !defined(_WIN32)
sigsetmask(mask);
#endif
}
#ifdef sys5
const s_char *sys_siglist[] = {
"no signal",
"interrupt",
"quit",
"illegal instruction",
"trace trap",
"IOT instruction",
"system crash imminent",
"floating point exception",
"kill",
"bus error",
"segmentation violation",
"bad argument to system call",
"write on a pipe with no one to read it",
"alarm clock",
"software termination",
"user defined signal 1",
"user defined signal 2",
"death of a child",
"power-fail restart",
"asychronous i/o",
"PTY read/write",
"I/O intervention required",
"monitor mode granted",
"monitor mode retracted",
"sound ack",
"data pending",
};
#else
#if (!defined __ppc__) && (!defined linux) && (!defined FBSD) && (!defined __linux__)
/* linux and osx declare sys_siglist in signal.h */
extern s_char *sys_siglist[];
#endif /* linux */
#endif /* sys5 */
const s_char *
signame(int sig)
{
#ifdef POSIX_SIGNALS
if (sig <= 0 || sig > _sys_nsig)
return "bad signal";
return _sys_siglist[sig];
#else /* POSIX_SIGNALS */
if (sig <= 0 || sig > NSIG)
return "bad signal";
return sys_siglist[sig];
#endif /* POSIX_SIGNALS */
}

48
src/lib/gen/strdup.c Normal file
View file

@ -0,0 +1,48 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* strdup.c: For systems without a strdup function
*
* Known contributors to this file:
* Yannick Tremblay
*/
/* for systems without strdup() define NOSTRDUP */
#include "gen.h"
#ifdef NOSTRDUP
char *strdup(char *x)
{
char *y;
y=(char *)malloc((sizeof(char)*strlen(x))+1);
strcpy(y,x);
return y;
}
#endif /* NOSTRDUP */

57
src/lib/gen/strscan.c Normal file
View file

@ -0,0 +1,57 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* strscan.c: scan a string for any occurence of another string
*
* Known contributors to this file:
* Steve McClure, 2000
*/
#ifdef Rel4
#include <string.h>
#endif /* Rel4 */
#include <stdio.h>
#include "misc.h"
int
strscan(s_char *target, s_char *string)
{
int i,n,delta;
if ((string == NULL) || (target == NULL))
return(1);
n = strlen(target);
delta = strlen(string);
if (delta < n)
return(1);
delta -= n;
for (i = 0; i <= delta; i++) {
if (!strncmp(target,&(string[i]),n))
return(0);
}
return(1);
}

185
src/lib/gen/vsprintf.c Normal file
View file

@ -0,0 +1,185 @@
#if !defined(_WIN32)
#ifndef __STDC__
/* Portable vsprintf by Robert A. Larson <blarson@skat.usc.edu> */
/* Copyright 1989 Robert A. Larson.
* Distribution in any form is allowed as long as the author
* retains credit, changes are noted by their author and the
* copyright message remains intact. This program comes as-is
* with no warentee of fitness for any purpouse.
*
* Thanks to Doug Gwen, Chris Torek, and others who helped clarify
* the ansi printf specs.
*
* Please send any bug fixes and improvments to blarson@skat.usc.edu .
* The use of goto is NOT a bug.
*/
/* Feb 7, 1989 blarson First usenet release */
/* This code implements the vsprintf function, without relying on
* the existance of _doprint or other system specific code.
*
* Define NOVOID if void * is not a supported type.
*
* Two compile options are available for efficency:
* INTSPRINTF should be defined if sprintf is int and returns
* the number of chacters formated.
* LONGINT should be defined if sizeof(long) == sizeof(int)
*
* They only make the code smaller and faster, they need not be
* defined.
*
* UNSIGNEDSPECIAL should be defined if unsigned is treated differently
* than int in argument passing. If this is definded, and LONGINT is not,
* the compiler must support the type unsingned long.
*
* Most quirks and bugs of the available sprintf fuction are duplicated,
* however * in the width and precision fields will work correctly
* even if sprintf does not support this, as will the n format.
*
* Bad format strings, or those with very long width and precision
* fields (including expanded * fields) will cause undesired results.
*/
#include "misc.h"
#include "gen.h"
#ifdef OSK /* os9/68k can take advantage of both */
#define LONGINT
#define INTSPRINTF
#endif
#define NOVOID
/* This must be a typedef not a #define! */
#ifdef NOVOID
typedef s_char *pointer;
#else
typedef void *pointer;
#endif
#ifdef INTSPRINTF
#define Sprintf(string,format,arg) (sprintf((string),(format),(arg)))
#else
#define Sprintf(string,format,arg) (\
sprintf((string),(format),(arg)),\
strlen(string)\
)
#endif
#include <stdarg.h>
typedef int *intp;
int vsprintf(dest, format, args)
s_char *dest;
register s_char *format;
va_list args;
{
register s_char *dp = dest;
register s_char c;
register s_char *tp;
s_char tempfmt[64];
#ifndef LONGINT
int longflag;
#endif
tempfmt[0] = '%';
while(c = *format++) {
if(c=='%') {
tp = &tempfmt[1];
#ifndef LONGINT
longflag = 0;
#endif
continue_format:
switch(c = *format++) {
case 's':
*tp++ = c;
*tp = '\0';
dp += Sprintf(dp, tempfmt, va_arg(args, s_char *));
break;
case 'u':
case 'x':
case 'o':
case 'X':
#ifdef UNSIGNEDSPECIAL
*tp++ = c;
*tp = '\0';
#ifndef LONGINT
if(longflag)
dp += Sprintf(dp, tempfmt, va_arg(args, unsigned long));
else
#endif
dp += Sprintf(dp, tempfmt, va_arg(args, unsigned));
break;
#endif
case 'd':
case 'c':
case 'i':
*tp++ = c;
*tp = '\0';
#ifndef LONGINT
if(longflag)
dp += Sprintf(dp, tempfmt, va_arg(args, long));
else
#endif
dp += Sprintf(dp, tempfmt, va_arg(args, int));
break;
case 'f':
case 'e':
case 'E':
case 'g':
case 'G':
*tp++ = c;
*tp = '\0';
dp += Sprintf(dp, tempfmt, va_arg(args, double));
break;
case 'p':
*tp++ = c;
*tp = '\0';
dp += Sprintf(dp, tempfmt, va_arg(args, pointer));
break;
case '-':
case '+':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
case ' ':
case '#':
case 'h':
*tp++ = c;
goto continue_format;
case 'l':
#ifndef LONGINT
longflag = 1;
*tp++ = c;
#endif
goto continue_format;
case '*':
tp += Sprintf(tp, "%d", va_arg(args, int));
goto continue_format;
case 'n':
*va_arg(args, intp) = dp - dest;
break;
case '%':
default:
*dp++ = c;
break;
}
} else *dp++ = c;
}
*dp = '\0';
return dp - dest;
}
#endif /* __STDC__ */
#endif