add clang-format config and format everything

This commit is contained in:
Stefan Bühler 2021-09-15 19:20:06 +02:00
parent 187ec1a68e
commit 56458af5fc
20 changed files with 2193 additions and 2183 deletions

29
.clang-format Normal file
View File

@ -0,0 +1,29 @@
---
BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
---
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AlignOperands: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 160
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 0
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
PointerAlignment: Left
SpaceAfterCStyleCast: true
Standard: Cpp11
UseTab: Always
...

View File

@ -2,12 +2,12 @@
#include "announce.h"
#include "network.h"
#include <time.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
@ -20,10 +20,10 @@
#define SERVICE_TYPE "_sdlbomber._udp"
static AvahiClient *client = NULL;
static AvahiThreadedPoll *threaded_poll = NULL;
static AvahiEntryGroup *group = NULL;
static AvahiServiceBrowser *browser = NULL;
static AvahiClient* client = NULL;
static AvahiThreadedPoll* threaded_poll = NULL;
static AvahiEntryGroup* group = NULL;
static AvahiServiceBrowser* browser = NULL;
static char* name = NULL;
@ -37,22 +37,21 @@ static char buffer_glchanged[10];
gamelistentry gamelistentries[10];
int gamelistsize = 0;
static void myerror(AvahiClient *c, const char *s) {
static void myerror(AvahiClient* c, const char* s) {
fprintf(stderr, "Error in: %s\n (%s)", s, avahi_strerror(avahi_client_errno(c)));
}
static void create_services(AvahiClient *c);
static void create_services(AvahiClient* c);
static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
static void entry_group_callback(AvahiEntryGroup* g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void* userdata) {
group = g;
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED :
case AVAHI_ENTRY_GROUP_ESTABLISHED:
break;
case AVAHI_ENTRY_GROUP_COLLISION :
{
char *n = avahi_alternative_service_name(name);
case AVAHI_ENTRY_GROUP_COLLISION: {
char* n = avahi_alternative_service_name(name);
avahi_free(name);
name = n;
}
@ -62,7 +61,7 @@ static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
create_services(avahi_entry_group_get_client(g));
break;
case AVAHI_ENTRY_GROUP_FAILURE :
case AVAHI_ENTRY_GROUP_FAILURE:
fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
avahi_threaded_poll_quit(threaded_poll);
@ -74,7 +73,7 @@ static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
}
}
static void create_services(AvahiClient *c) {
static void create_services(AvahiClient* c) {
int ret;
if (!group) {
@ -90,10 +89,9 @@ again:
snprintf(buf_version, sizeof(buf_version), "version=%X", (unsigned int) version);
if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, SERVICE_TYPE, NULL, NULL, port, buf_version, NULL)) < 0) {
if (ret == AVAHI_ERR_COLLISION)
goto collision;
if (ret == AVAHI_ERR_COLLISION) goto collision;
fprintf(stderr, "Failed to add "SERVICE_TYPE": %s\n", avahi_strerror(ret));
fprintf(stderr, "Failed to add " SERVICE_TYPE ": %s\n", avahi_strerror(ret));
goto fail;
}
@ -105,12 +103,11 @@ again:
return;
collision:
{
char *n = avahi_alternative_service_name(name);
collision : {
char* n = avahi_alternative_service_name(name);
avahi_free(name);
name = n;
}
}
avahi_entry_group_reset(group);
goto again;
@ -118,8 +115,8 @@ fail:
avahi_threaded_poll_quit(threaded_poll);
}
static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
(void)userdata;
static void client_callback(AvahiClient* c, AvahiClientState state, void* userdata) {
(void) userdata;
client = c;
switch (state) {
@ -132,16 +129,14 @@ static void client_callback(AvahiClient *c, AvahiClientState state, void * userd
break;
case AVAHI_CLIENT_S_COLLISION:
case AVAHI_CLIENT_S_REGISTERING:
if (group) {
avahi_entry_group_reset(group);
}
if (group) { avahi_entry_group_reset(group); }
break;
case AVAHI_CLIENT_CONNECTING:
break;
}
}
int registergame(const char *playername, uint16_t p, const unsigned char v[4]) {
int registergame(const char* playername, uint16_t p, const unsigned char v[4]) {
if (name) avahi_free(name);
name = avahi_strdup(playername);
@ -165,7 +160,7 @@ void unregistergame(void) {
avahi_threaded_poll_unlock(threaded_poll);
}
static void remove_game_with_name(const char *name) {
static void remove_game_with_name(const char* name) {
int i;
for (i = 0; i < buffer_glsize; i++) {
@ -180,15 +175,26 @@ static void remove_game_with_name(const char *name) {
}
}
static void resolve_callback(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event,
const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address,
uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void* userdata) {
static void resolve_callback(
AvahiServiceResolver* r,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiResolverEvent event,
const char* name,
const char* type,
const char* domain,
const char* host_name,
const AvahiAddress* address,
uint16_t port,
AvahiStringList* txt,
AvahiLookupResultFlags flags,
void* userdata) {
int i;
uint32_t want_version;
(void)interface;
(void)host_name;
(void)flags;
(void)userdata;
(void) interface;
(void) host_name;
(void) flags;
(void) userdata;
assert(r);
if (protocol != AVAHI_PROTO_INET) goto done; /* ignore non IPv4 for now */
@ -201,15 +207,21 @@ static void resolve_callback(AvahiServiceResolver *r, AvahiIfIndex interface, Av
switch (event) {
case AVAHI_RESOLVER_FAILURE:
fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
fprintf(
stderr,
"(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n",
name,
type,
domain,
avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
break;
case AVAHI_RESOLVER_FOUND: {
gamelistentry *ge;
gamelistentry* ge;
unsigned int version;
int have_version = 0;
AvahiStringList *psl;
for (psl = txt ; psl ; psl = psl->next) {
AvahiStringList* psl;
for (psl = txt; psl; psl = psl->next) {
if (0 == strncmp("version=", (const char*) psl->text, 8)) {
sscanf((const char*) psl->text, "version=%X", &version);
if (version != want_version) goto done; /* version mismatch */
@ -233,9 +245,17 @@ done:
avahi_service_resolver_free(r);
}
static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event,
const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* userdata) {
(void)flags;
static void browse_callback(
AvahiServiceBrowser* b,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* name,
const char* type,
const char* domain,
AvahiLookupResultFlags flags,
void* userdata) {
(void) flags;
assert(b);

View File

@ -9,7 +9,7 @@ struct gamelistentry {
char name[16];
};
int registergame(const char *playername, uint16_t port, const unsigned char version[4]);
int registergame(const char* playername, uint16_t port, const unsigned char version[4]);
void unregistergame(void);
int searchgames(void);

View File

@ -1,31 +1,31 @@
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include "bomber.h"
#include "gfx.h"
#include "announce.h"
#include "sound.h"
#include "menu.h"
#include "utils.h"
#include "network.h"
#include "game.h"
#include "bomber.h"
#include "draw.h"
#include "game.h"
#include "gfx.h"
#include "menu.h"
#include "network.h"
#include "sound.h"
#include "utils.h"
int main(void) {
char *p;
char* p;
strcpy(playername,"ANONYMOUS");
p=getenv("USER");
if(p) strncpy(playername,p,sizeof(playername)-1);
strcpy(playername, "ANONYMOUS");
p = getenv("USER");
if (p) strncpy(playername, p, sizeof(playername) - 1);
create_seed_unique();

View File

@ -1,7 +1,8 @@
#ifndef BOMBER_H
#define BOMBER_H
#include "SDL.h"
#include "list.h"
#include <SDL.h>
typedef unsigned char uchar;
extern int xcolors[256];
@ -38,30 +39,25 @@ extern int xcolors[256];
typedef struct gfxset {
uchar gs_colormap[768];
uchar gs_inout[256];
uchar *gs_pic;
uchar* gs_pic;
int gs_xsize;
int gs_ysize;
} gfxset;
typedef struct figure {
int xsize,ysize;
int xdelta,ydelta;
uchar *graphics;
int xsize, ysize;
int xdelta, ydelta;
uchar* graphics;
} figure;
typedef struct solid {
int xsize,ysize;
uchar *graphics;
int xsize, ysize;
uchar* graphics;
} solid;
typedef struct listhead listhead;
struct listhead {
listhead *prev, *next;
};
typedef struct player {
listhead list, list_all_players;
int xpos,ypos;
int xpos, ypos;
int flags;
int abilities;
int speed;
@ -69,15 +65,15 @@ typedef struct player {
int bombsused;
int bombsavailable;
int flamelength;
int *at;
int* at;
int figcount;
int doing;
int action;
int color;
int controller;
int fixx,fixy;
int fixx, fixy;
int kills, deaths;
figure *figure;
figure* figure;
} player;
#define FLG_CONTROL 1
@ -86,24 +82,24 @@ typedef struct player {
typedef struct sprite {
int flags;
int xpos,ypos;
figure *fig;
int xpos, ypos;
figure* fig;
} sprite;
typedef struct damage {
int xpos,ypos;
int xsize,ysize;
int xpos, ypos;
int xsize, ysize;
} damage;
typedef struct bomb {
listhead list;
int type;
int xpos,ypos;
int px,py;
int xpos, ypos;
int px, py;
int power;
int timer;
int figcount;
player *owner;
player* owner;
} bomb;
@ -117,11 +113,11 @@ typedef struct bomb {
typedef struct flame {
listhead list;
int xpos,ypos;
int px,py;
int xpos, ypos;
int px, py;
int timer;
int lurd;
player *owner;
player* owner;
} flame;
#define FL_UP 2
@ -131,27 +127,27 @@ typedef struct flame {
typedef struct brickdecay {
listhead list;
int xpos,ypos;
int px,py;
int xpos, ypos;
int px, py;
int timer;
} brickdecay;
typedef struct bonustile {
listhead list;
int xpos,ypos;
int px,py;
int xpos, ypos;
int px, py;
int type;
} bonustile;
typedef struct generic {
listhead list;
int xpos,ypos;
int px,py;
int xpos, ypos;
int px, py;
int timer;
void (*process)();
void (*draw)();
void *ptr1,*ptr2;
int data1,data2;
void *ptr1, *ptr2;
int data1, data2;
} generic;
enum tile_types {
@ -200,7 +196,6 @@ enum tile_types {
#define CODE_ALLDEAD 2
#define MAXTHINGS 500
#define MAXSETS 8
#define MAXSPRITES 256

View File

@ -1,15 +1,15 @@
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include "bomber.h"
#include "draw.h"
@ -48,183 +48,198 @@ figure flamefigs[MAXSETS][NUMFLAMEFRAMES];
figure tiles[15];
figure death[NUMDEATHFRAMES];
int fontxsize,fontysize;
int bigfontxsize,bigfontysize,bigfontyspace;
int fontxsize, fontysize;
int bigfontxsize, bigfontysize, bigfontyspace;
static int textx,texty;
static int textx, texty;
static const unsigned char *remapstring = (const unsigned char*) "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.:!?\177/\\*-,>< =";
static const unsigned char* remapstring = (const unsigned char*) "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.:!?\177/\\*-,>< =";
static char asciiremap[256];
/* On screen array variables */
int arraynumx=15;
int arraynumy=11;
int arraystartx=20;
int arraystarty=70;
int arrayspacex=40;
int arrayspacey=36;
int arraynumx = 15;
int arraynumy = 11;
int arraystartx = 20;
int arraystarty = 70;
int arrayspacex = 40;
int arrayspacey = 36;
static sprite sprites[MAXSPRITES];
static int spritesused=0;
static int spritesused = 0;
#define IBUFFLEN 1024
int ileft=0,ihand=0,byteswide;
unsigned char ibuff[IBUFFLEN],*itake;
int ileft = 0, ihand = 0, byteswide;
unsigned char ibuff[IBUFFLEN], *itake;
static void freegfxset(gfxset *gs) {
if(gs->gs_pic) free(gs->gs_pic);
gs->gs_pic=0;
static void freegfxset(gfxset* gs) {
if (gs->gs_pic) free(gs->gs_pic);
gs->gs_pic = 0;
}
static int myci() {
if (!ileft) {
ileft=read(ihand,ibuff,IBUFFLEN);
ileft = read(ihand, ibuff, IBUFFLEN);
if(!ileft) return -1;
itake=ibuff;
if (!ileft) return -1;
itake = ibuff;
}
ileft--;
return *itake++;
}
static int dopcxreal(char *name,gfxset *gs) {
int xs,ys;
int i,j,k;
static int dopcxreal(char* name, gfxset* gs) {
int xs, ys;
int i, j, k;
int totalsize;
int width,height;
unsigned char *bm,*lp;
int width, height;
unsigned char *bm, *lp;
char tname[256];
memset(gs,0,sizeof(gfxset));
ileft=0;
snprintf(tname,sizeof(tname),DATADIR "/%s",name);
ihand=open(tname,O_RDONLY);
if(ihand<0) {
memset(gs, 0, sizeof(gfxset));
ileft = 0;
snprintf(tname, sizeof(tname), DATADIR "/%s", name);
ihand = open(tname, O_RDONLY);
if (ihand < 0) {
char tname2[260];
snprintf(tname2,sizeof(tname2),"%s.pcx",tname);
ihand=open(tname2,O_RDONLY);
if(ihand<0)
return 1;
snprintf(tname2, sizeof(tname2), "%s.pcx", tname);
ihand = open(tname2, O_RDONLY);
if (ihand < 0) return 1;
}
if(myci()!=10) {close(ihand);return 2;} // 10=zsoft .pcx
if(myci()!=5) {close(ihand);return 3;} // version 3.0
if(myci()!=1) {close(ihand);return 4;} //encoding method
if(myci()!=8) {close(ihand);return 5;} //bpp
xs=myci();
xs|=myci()<<8;
ys=myci();
ys|=myci()<<8;
width=myci();
width|=myci()<<8;
height=myci();
height|=myci()<<8;
width=width+1-xs;
height=height+1-ys;
for(i=0;i<48+4;++i) myci();
if (myci() != 10) {
close(ihand);
return 2;
} // 10=zsoft .pcx
if (myci() != 5) {
close(ihand);
return 3;
} // version 3.0
if (myci() != 1) {
close(ihand);
return 4;
} // encoding method
if (myci() != 8) {
close(ihand);
return 5;
} // bpp
xs = myci();
xs |= myci() << 8;
ys = myci();
ys |= myci() << 8;
width = myci();
width |= myci() << 8;
height = myci();
height |= myci() << 8;
width = width + 1 - xs;
height = height + 1 - ys;
for (i = 0; i < 48 + 4; ++i) myci();
myci();
if(myci()!=1) {close(ihand);return 6;} // # of planes
byteswide=myci();
byteswide|=myci()<<8;
i=myci();
i|=myci()<<8;
// if(i!=1) {close(ihand);return 7;} // 1=color/bw,2=grey
for(i=0;i<58;++i) myci();
totalsize=height*byteswide;
bm=malloc(totalsize+1);
if(!bm) {close(ihand);return 8;} // no memory
gs->gs_pic=bm;
gs->gs_xsize=width;
gs->gs_ysize=height;
while(height--) {
lp=bm;
i=byteswide;
while(i>0) {
j=myci();
if(j<0xc0) {
*lp++=j;
if (myci() != 1) {
close(ihand);
return 6;
} // # of planes
byteswide = myci();
byteswide |= myci() << 8;
i = myci();
i |= myci() << 8;
// if(i!=1) {close(ihand);return 7;} // 1=color/bw,2=grey
for (i = 0; i < 58; ++i) myci();
totalsize = height * byteswide;
bm = malloc(totalsize + 1);
if (!bm) {
close(ihand);
return 8;
} // no memory
gs->gs_pic = bm;
gs->gs_xsize = width;
gs->gs_ysize = height;
while (height--) {
lp = bm;
i = byteswide;
while (i > 0) {
j = myci();
if (j < 0xc0) {
*lp++ = j;
--i;
} else {
j&=0x3f;
k=myci();
while(j-- && i) {
*lp++=k;
j &= 0x3f;
k = myci();
while (j-- && i) {
*lp++ = k;
--i;
}
}
}
bm+=width;
bm += width;
}
lseek(ihand,-0x300,SEEK_END);
read(ihand,gs->gs_colormap,0x300);
lseek(ihand, -0x300, SEEK_END);
read(ihand, gs->gs_colormap, 0x300);
close(ihand);
return 0;
}
static int dopcx(char *name,gfxset *gs) {
static int dopcx(char* name, gfxset* gs) {
int err;
err=dopcxreal(name,gs);
if(err)
printf("Error loading \"%s\":code %d\n",name,err);
err = dopcxreal(name, gs);
if (err) printf("Error loading \"%s\":code %d\n", name, err);
return err;
}
static void getgroup(char *name,gfxset *colorgs,figure *fig,int count) {
static void getgroup(char* name, gfxset* colorgs, figure* fig, int count) {
int err;
int i;
gfxset gs;
err=dopcx(name,&gs);
if(err) exit(1000+err);
err = dopcx(name, &gs);
if (err) exit(1000 + err);
createinout(&gs);
for(i=0;i<MAXSETS;++i,fig+=count,++colorgs) {
if(!colorgs->gs_pic) continue;
memmove(gs.gs_colormap,colorgs->gs_colormap,
sizeof(gs.gs_colormap));
for (i = 0; i < MAXSETS; ++i, fig += count, ++colorgs) {
if (!colorgs->gs_pic) continue;
memmove(gs.gs_colormap, colorgs->gs_colormap, sizeof(gs.gs_colormap));
createinout(&gs);
gfxfetch(&gs,fig,count);
gfxfetch(&gs, fig, count);
}
freegfxset(&gs);
}
static void getsingle(char *name,figure *fig,int count) {
static void getsingle(char* name, figure* fig, int count) {
gfxset gs;
int err;
err=dopcx(name,&gs);
if(err) exit(1000+err);
err = dopcx(name, &gs);
if (err) exit(1000 + err);
createinout(&gs);
gfxfetch(&gs,fig,count);
gfxfetch(&gs, fig, count);
freegfxset(&gs);
}
static void texthome(void) {
textx=texty=10;
textx = texty = 10;
}
void drawstring(int xpos, int ypos, const char *str) {
void drawstring(int xpos, int ypos, const char* str) {
char ch;
while((ch=*str++)) {
drawfigure(xpos,ypos,font+asciiremap[toupper(ch)]);
xpos+=fontxsize;
while ((ch = *str++)) {
drawfigure(xpos, ypos, font + asciiremap[toupper(ch)]);
xpos += fontxsize;
}
}
void drawbigstring(int xpos, int ypos, const char *str) {
void drawbigstring(int xpos, int ypos, const char* str) {
char ch;
while('\0' != (ch=*str++)) {
drawfigure(xpos,ypos,bigfont+asciiremap[toupper(ch)]);
xpos+=bigfontxsize;
while ('\0' != (ch = *str++)) {
drawfigure(xpos, ypos, bigfont + asciiremap[toupper(ch)]);
xpos += bigfontxsize;
}
}
void centerbig(int y, const char *str) {
void centerbig(int y, const char* str) {
int w;
w=strlen(str)*bigfontxsize;
drawbigstring((IXSIZE-w)>>1,y,str);
w = strlen(str) * bigfontxsize;
drawbigstring((IXSIZE - w) >> 1, y, str);
}
// static void scrprintf(char *str,...) {
@ -253,201 +268,196 @@ void centerbig(int y, const char *str) {
// copyup();
// }
static void bigscrprintf(char *str,...) {
char output[256],*p,*p2;
static void bigscrprintf(char* str, ...) {
char output[256], *p, *p2;
va_list ap;
va_start(ap, str);
vsnprintf(output,sizeof(output),str,ap);
p=output;
for(;;) {
p2=p;
while(*p2 && *p2!='\n') ++p2;
if(*p2) {
*p2=0;
drawbigstring(textx,texty,p);
texty+=bigfontysize;
textx=10;
p=p2+1;
vsnprintf(output, sizeof(output), str, ap);
p = output;
for (;;) {
p2 = p;
while (*p2 && *p2 != '\n') ++p2;
if (*p2) {
*p2 = 0;
drawbigstring(textx, texty, p);
texty += bigfontysize;
textx = 10;
p = p2 + 1;
} else {
drawbigstring(textx,texty,p);
textx+=bigfontxsize*(p2-p);
drawbigstring(textx, texty, p);
textx += bigfontxsize * (p2 - p);
break;
}
}
copyup();
}
void addsprite(int x,int y,figure *fig) {
sprite *sp;
void addsprite(int x, int y, figure* fig) {
sprite* sp;
if(spritesused==MAXSPRITES) return;
sp=sprites+spritesused++;
sp->flags=0;
sp->xpos=x;
sp->ypos=y;
sp->fig=fig;
if (spritesused == MAXSPRITES) return;
sp = sprites + spritesused++;
sp->flags = 0;
sp->xpos = x;
sp->ypos = y;
sp->fig = fig;
}
void plotsprites(void) {
int i;
sprite *sp;
sprite* sp;
sp=sprites;
for(i=0;i<spritesused;++i) {
drawfigure(sp->xpos,sp->ypos,sp->fig);
sp = sprites;
for (i = 0; i < spritesused; ++i) {
drawfigure(sp->xpos, sp->ypos, sp->fig);
++sp;
}
}
void erasesprites(void) {
int i;
sprite *sp;
figure *fig;
sprite* sp;
figure* fig;
sp=sprites;
for(i=0;i<spritesused;++i) {
fig=sp->fig;
sp = sprites;
for (i = 0; i < spritesused; ++i) {
fig = sp->fig;
solidcopy(&background,
sp->xpos+fig->xdelta,sp->ypos+fig->ydelta,
fig->xsize,fig->ysize);
solidcopy(&background, sp->xpos + fig->xdelta, sp->ypos + fig->ydelta, fig->xsize, fig->ysize);
++sp;
}
}
void clearsprites(void) {
int i;
sprite *sp;
figure *fig;
sprite* sp;
figure* fig;
sp=sprites;
for(i=0;i<spritesused;++i) {
fig=sp->fig;
sp = sprites;
for (i = 0; i < spritesused; ++i) {
fig = sp->fig;
clearrect(sp->xpos+fig->xdelta,sp->ypos+fig->ydelta,
fig->xsize,fig->ysize);
clearrect(sp->xpos + fig->xdelta, sp->ypos + fig->ydelta, fig->xsize, fig->ysize);
++sp;
}
}
void clearspritelist(void) {
spritesused=0;
spritesused = 0;
}
int tovideox(int x) {
return (x>>FRACTION)+arraystartx;
return (x >> FRACTION) + arraystartx;
}
int tovideoy(int y) {
return (y>>FRACTION)+arraystarty;
return (y >> FRACTION) + arraystarty;
}
int screentoarrayx(int x) {
x+=arrayspacex << (FRACTION+2);
return ((x>>FRACTION)+(arrayspacex>>1))/arrayspacex-4;
x += arrayspacex << (FRACTION + 2);
return ((x >> FRACTION) + (arrayspacex >> 1)) / arrayspacex - 4;
}
int screentoarrayy(int y) {
y+=arrayspacey << (FRACTION+2);
return ((y>>FRACTION)+(arrayspacey>>1))/arrayspacey-4;
y += arrayspacey << (FRACTION + 2);
return ((y >> FRACTION) + (arrayspacey >> 1)) / arrayspacey - 4;
}
int arraytoscreenx(int x) {
return arrayspacex*x<<FRACTION;
return arrayspacex * x << FRACTION;
}
int arraytoscreeny(int y) {
return arrayspacey*y<<FRACTION;
return arrayspacey * y << FRACTION;
}
static void loadfonts(void) {
int i,j;
const unsigned char *p;
int i, j;
const unsigned char* p;
getsingle(fontname,font,NUMCHARACTERS);
getsingle(bigfontname,bigfont,NUMCHARACTERS);
fontxsize=8;
fontysize=12;
bigfontxsize=16;
bigfontysize=24;
bigfontyspace=32;
p=remapstring;
j=0;while(*p && *p!=' ') ++p,++j;
memset(asciiremap,j,sizeof(asciiremap));
p=remapstring;
i=0;
while(*p && i<NUMCHARACTERS)
asciiremap[(int)*p++]=i++;
getsingle(fontname, font, NUMCHARACTERS);
getsingle(bigfontname, bigfont, NUMCHARACTERS);
fontxsize = 8;
fontysize = 12;
bigfontxsize = 16;
bigfontysize = 24;
bigfontyspace = 32;
p = remapstring;
j = 0;
while (*p && *p != ' ') ++p, ++j;
memset(asciiremap, j, sizeof(asciiremap));
p = remapstring;
i = 0;
while (*p && i < NUMCHARACTERS) asciiremap[(int) *p++] = i++;
}
void loadgfx() {
gfxset *gs;
gfxset *colorgs;
gfxset* gs;
gfxset* colorgs;
int err;
int i;
char name[267];
strcpy(walkingname,"walk");
strcpy(colorsetname,"pal");
strcpy(backgroundname,"field0");
strcpy(blocksname,"blocks3");
strcpy(blocksxname,"blocks3x");
strcpy(bombs1name,"bomb1");
strcpy(bombs2name,"bomb2");
strcpy(flamesname,"flames");
strcpy(tilesname,"tiles");
strcpy(deathname,"death1");
strcpy(fontname,"font");
strcpy(bigfontname,"bigfont");
strcpy(walkingname, "walk");
strcpy(colorsetname, "pal");
strcpy(backgroundname, "field0");
strcpy(blocksname, "blocks3");
strcpy(blocksxname, "blocks3x");
strcpy(bombs1name, "bomb1");
strcpy(bombs2name, "bomb2");
strcpy(flamesname, "flames");
strcpy(tilesname, "tiles");
strcpy(deathname, "death1");
strcpy(fontname, "font");
strcpy(bigfontname, "bigfont");
gs=malloc((MAXSETS+1)*sizeof(gfxset));
if(!gs)
nomem("loadgfx");
colorgs=gs+1;
gs = malloc((MAXSETS + 1) * sizeof(gfxset));
if (!gs) nomem("loadgfx");
colorgs = gs + 1;
for(i=0;i<MAXSETS;++i) {
snprintf(name,sizeof(name),"%s%d",colorsetname,i);
err=dopcx(name,colorgs+i);
if(err) continue;
for (i = 0; i < MAXSETS; ++i) {
snprintf(name, sizeof(name), "%s%d", colorsetname, i);
err = dopcx(name, colorgs + i);
if (err) continue;
}
loadfonts();
texthome();
bigscrprintf("Loading graphics...\n");
err=dopcx(backgroundname,gs);
if(err) exit(1000+err);
err = dopcx(backgroundname, gs);
if (err) exit(1000 + err);
createinout(gs);
solidfetch(gs,&background);
solidfetch(gs,&backgroundoriginal);
solidfetch(gs, &background);
solidfetch(gs, &backgroundoriginal);
freegfxset(gs);
bigscrprintf("Loading blocks\n");
getsingle(blocksname,blocks,3);
getsingle(blocksname, blocks, 3);
bigscrprintf("Loading block explosions\n");
getsingle(blocksxname,blocksx,9);
getsingle(blocksxname, blocksx, 9);
bigscrprintf("Loading walking figures\n");
getgroup(walkingname,colorgs,walking[0],NUMWALKFRAMES);
getgroup(walkingname, colorgs, walking[0], NUMWALKFRAMES);
bigscrprintf("Loading normal bombs\n");
getgroup(bombs1name,colorgs,bombs1[0],NUMBOMBFRAMES);
getgroup(bombs1name, colorgs, bombs1[0], NUMBOMBFRAMES);
bigscrprintf("Loading controlled bombs\n");
getgroup(bombs2name,colorgs,bombs2[0],NUMBOMBFRAMES);
getgroup(bombs2name, colorgs, bombs2[0], NUMBOMBFRAMES);
bigscrprintf("Loading flames\n");
// getgroup(flamesname,colorgs,flamefigs[0],NUMFLAMEFRAMES);
getsingle(flamesname,flamefigs[0],NUMFLAMEFRAMES);
// getgroup(flamesname,colorgs,flamefigs[0],NUMFLAMEFRAMES);
getsingle(flamesname, flamefigs[0], NUMFLAMEFRAMES);
bigscrprintf("Loading bonus tiles\n");
getsingle(tilesname,tiles,15);
getsingle(tilesname, tiles, 15);
bigscrprintf("Loading death sequence\n");
getsingle(deathname,death,NUMDEATHFRAMES);
getsingle(deathname, death, NUMDEATHFRAMES);
for(i=0;i<MAXSETS;++i)
freegfxset(colorgs+i);
for (i = 0; i < MAXSETS; ++i) freegfxset(colorgs + i);
free(gs);
bigscrprintf("Done loading graphics\n");
}
void failure(char *str,...) {
void failure(char* str, ...) {
char output[256];
va_list ap;
int len;
@ -458,11 +468,11 @@ void failure(char *str,...) {
len = vsnprintf(output, sizeof(output), str, ap);
if (len >= 256) len = 255; /* truncated string */
clear();
drawbigstring((IXSIZE - len*bigfontxsize) / 2, (IYSIZE-bigfontysize) / 2, output);
drawbigstring((IXSIZE - len * bigfontxsize) / 2, (IYSIZE - bigfontysize) / 2, output);
copyup();
now = longtime();
while (!exitflag && longtime()-now < 3) {
while (!exitflag && longtime() - now < 3) {
scaninput();
if (anydown()) {
takedown();

View File

@ -3,12 +3,12 @@
void loadgfx(void);
void drawstring(int xpos, int ypos, const char *str);
void drawbigstring(int xpos, int ypos, const char *str);
void drawstring(int xpos, int ypos, const char* str);
void drawbigstring(int xpos, int ypos, const char* str);
void centerbig(int y, const char *str);
void centerbig(int y, const char* str);
void addsprite(int x,int y,figure *fig);
void addsprite(int x, int y, figure* fig);
void plotsprites(void);
void erasesprites(void);
void clearsprites(void);
@ -22,10 +22,10 @@ int screentoarrayy(int y);
int arraytoscreenx(int x);
int arraytoscreeny(int y);
void failure(char *str,...);
void failure(char* str, ...);
extern int fontxsize,fontysize;
extern int bigfontxsize,bigfontysize,bigfontyspace;
extern int fontxsize, fontysize;
extern int bigfontxsize, bigfontysize, bigfontyspace;
/* On screen array variables */
extern int arraynumx, arraynumy, arraystartx, arraystarty, arrayspacex, arrayspacey;
@ -37,6 +37,7 @@ extern int arraynumx, arraynumy, arraystartx, arraystarty, arrayspacex, arrayspa
#define NUMDEATHFRAMES 41
extern figure blocks[3], blocksx[9], bombs1[MAXSETS][NUMBOMBFRAMES], bombs2[MAXSETS][NUMBOMBFRAMES], flamefigs[MAXSETS][NUMFLAMEFRAMES], tiles[15], death[NUMDEATHFRAMES];
extern figure blocks[3], blocksx[9], bombs1[MAXSETS][NUMBOMBFRAMES], bombs2[MAXSETS][NUMBOMBFRAMES], flamefigs[MAXSETS][NUMFLAMEFRAMES], tiles[15],
death[NUMDEATHFRAMES];
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,13 @@
#ifndef GAME_H
#define GAME_H
#include "bomber.h"
#define FRACTION 9
#define SPEEDDELTA (1<<(FRACTION-1))
#define SPEEDMAX (10<<FRACTION)
#define SPEEDSTART (6<<FRACTION)
#define SPEEDTURTLE (3<<FRACTION)
#define SPEEDDELTA (1 << (FRACTION - 1))
#define SPEEDMAX (10 << FRACTION)
#define SPEEDSTART (6 << FRACTION)
#define SPEEDTURTLE (3 << FRACTION)
#define SPEEDTURTLE_TIMEOUT 250
#define TEMPNODES 2
@ -18,10 +20,10 @@ struct GameOptions {
void run_single_player(void);
void run_network_game(void);
void set_game_options(GameOptions *options);
void set_game_options(GameOptions* options);
extern char playername[16];
extern solid background,backgroundoriginal;
extern solid background, backgroundoriginal;
#endif

1261
src/gfx.c

File diff suppressed because it is too large Load Diff

View File

@ -8,15 +8,15 @@
extern int usedcolors;
extern uchar mymap[];
extern int screen;
extern int fontbase,fontysize;
extern char *imageloc;
extern int fontbase, fontysize;
extern char* imageloc;
extern long map2[];
extern uchar fmap[128];
extern int buttonstate,buttondown;
extern int mxpos,mypos;
extern int buttonstate, buttondown;
extern int mxpos, mypos;
extern int pressedcodes[KEYMAX],downcodes[KEYMAX],numpressed,numdown;
extern int pressedcodes[KEYMAX], downcodes[KEYMAX], numpressed, numdown;
void opengfx(void);
@ -25,21 +25,21 @@ void gfxunlock(void);
void pollinput(void);
int takedown(void);
void closegfx(void);
void greyrect(int x,int y,int xsize,int ysize);
void clearrect(int x,int y,int xsize,int ysize);
void solidfetch(gfxset *gs,solid *dest);
void greyrect(int x, int y, int xsize, int ysize);
void clearrect(int x, int y, int xsize, int ysize);
void solidfetch(gfxset* gs, solid* dest);
extern void dumpgfx(void);
extern void createinout(struct gfxset *);
extern void createinout(struct gfxset*);
extern void getcolors(void);
extern void gfxfetch(struct gfxset *,struct figure *,int);
extern void puttile(int destx,int desty,int source);
extern void store(int x,int y,int which);
extern void restore(int x,int y,int which);
extern void gfxfetch(struct gfxset*, struct figure*, int);
extern void puttile(int destx, int desty, int source);
extern void store(int x, int y, int which);
extern void restore(int x, int y, int which);
extern void copyup(void);
extern void copyupxy(int x,int y);
extern void copyupxysize(int x,int y,int xsize,int ysize);
extern void copyupxy(int x, int y);
extern void copyupxysize(int x, int y, int xsize, int ysize);
extern void getfigures(void);
extern unsigned long getcolor(char *name); /* unsigned long */
extern unsigned long getcolor(char* name); /* unsigned long */
extern int checkpressed(int code);
extern int checkdown(int code);
extern int checkbutton(int button);
@ -48,22 +48,22 @@ extern int anydown(void);
extern int firstdown(void);
extern void scaninput(void);
extern void fontinit(void);
extern void writechar(int x,int y,uchar ch);
extern void writechar(int x, int y, uchar ch);
extern void clear(void);
extern void drawbox(int x,int y,int size,int color);
extern void drawbox2(int x,int y,int sizex,int sizey,int color);
extern void drawfillrect(int x,int y,int size,int color);
extern void bigpixel(int x,int y,int color);
extern void invert(int x,int y);
extern void drawbox(int x, int y, int size, int color);
extern void drawbox2(int x, int y, int sizex, int sizey, int color);
extern void drawfillrect(int x, int y, int size, int color);
extern void bigpixel(int x, int y, int color);
extern void invert(int x, int y);
extern int getmousex(void);
extern int getmousey(void);
extern void drawsquare(int x,int y,uchar *source);
extern void drawsquare(int x, int y, uchar* source);
extern void colormapon(void);
extern void colormapoff(void);
extern void palette(uchar *pal);
extern void drawfigure(int x,int y,figure *fig);
extern void drawfigureany(int x,int y,figure *fig,solid *dest);
void solidcopy(solid *src,int destx,int desty,int sizex,int sizey);
void solidcopyany(solid *src,solid *dest,int destx,int desty,int sizex,int sizey);
extern void palette(uchar* pal);
extern void drawfigure(int x, int y, figure* fig);
extern void drawfigureany(int x, int y, figure* fig, solid* dest);
void solidcopy(solid* src, int destx, int desty, int sizex, int sizey);
void solidcopyany(solid* src, solid* dest, int destx, int desty, int sizex, int sizey);
#endif // GFXX_H

View File

@ -1,11 +1,11 @@
#include "bomber.h"
#include "list.h"
#include "bomber.h"
#include "utils.h"
typedef union genericlistitem genericlistitem;
union genericlistitem {
genericlistitem *next;
genericlistitem* next;
listhead list;
bomb b;
flame f;
@ -16,8 +16,8 @@ union genericlistitem {
};
/* doesn't use prev link */
static genericlistitem *things=0;
static genericlistitem *free_things;
static genericlistitem* things = 0;
static genericlistitem* free_things;
static int count_used = 0;
@ -27,20 +27,18 @@ void alloc_things(void) {
if (!things) {
things = calloc(sizeof(genericlistitem), MAXTHINGS);
} else {
memset(things, 0, sizeof(genericlistitem)*MAXTHINGS);
memset(things, 0, sizeof(genericlistitem) * MAXTHINGS);
}
if (!things) nomem("Trying to allocate thing memory");
for (i=0; i < MAXTHINGS - 1; ++i) {
things[i].next = &things[i + 1];
}
for (i = 0; i < MAXTHINGS - 1; ++i) { things[i].next = &things[i + 1]; }
things[i].next = 0;
free_things = things;
count_used = 0;
}
void *_allocentry(size_t size) {
genericlistitem *entry = free_things;
void* _allocentry(size_t size) {
genericlistitem* entry = free_things;
if (size > sizeof(genericlistitem)) return 0;
@ -52,33 +50,33 @@ void *_allocentry(size_t size) {
return entry;
}
void freeentry(void *ptr) {
genericlistitem *entry = ptr;
void freeentry(void* ptr) {
genericlistitem* entry = ptr;
entry->next = free_things;
free_things = entry;
count_used--;
}
void list_add_tail(listhead *head, listhead *entry) {
void list_add_tail(listhead* head, listhead* entry) {
entry->next = head;
entry->prev = head->prev;
head->prev->next = entry;
head->prev = entry;
}
void list_del(listhead *entry) {
void list_del(listhead* entry) {
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
entry->next = entry->prev = entry;
}
void list_init_head(listhead *head) {
void list_init_head(listhead* head) {
head->next = head;
head->prev = head;
}
void things_list_clear(listhead *head) {
genericlistitem *entry;
void things_list_clear(listhead* head) {
genericlistitem* entry;
while (head->next != head) {
entry = container_of(head->next, __typeof__(*entry), list);

View File

@ -1,10 +1,16 @@
#ifndef LIST_H
#define LIST_H
#include <stddef.h>
/* from linux kernel (with some changes for "iso" c) */
#define container_of(ptr, type, member) \
((type *)( (char *)(__typeof__( ((type *)0)->member ) *)(ptr) - offsetof(type,member)))
typedef struct listhead listhead;
struct listhead {
listhead *prev, *next;
};
#define container_of(ptr, type, member) ((type*) ((char*) (__typeof__(((type*) 0)->member)*) (ptr) -offsetof(type, member)))
/**
* list_entry - get the struct for this entry
@ -15,20 +21,20 @@
#define list_entry(ptr, type, member) container_of(ptr, type, member)
void alloc_things(void);
void *_allocentry(size_t size);
void* _allocentry(size_t size);
#define allocentry(type) (type*) _allocentry(sizeof(type))
void freeentry(void *ptr);
void freeentry(void* ptr);
void list_add_tail(listhead *header, listhead *entry);
void list_add_tail(listhead* header, listhead* entry);
#define addtail(head, entry) list_add_tail(head, &(entry->list));
/* remove entry from list */
void list_del(listhead *entry);
void list_del(listhead* entry);
#define removeitem(entry) list_del(&(entry->list));
void list_init_head(listhead *head);
void list_init_head(listhead* head);
void things_list_clear(listhead *head); /* listhead member must be the first member */
void things_list_clear(listhead* head); /* listhead member must be the first member */
/**
* list_for_each_entry - iterate over list of given type
@ -37,8 +43,7 @@ void things_list_clear(listhead *head); /* listhead member must be the first mem
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, __typeof__(*pos), member); \
/* prefetch(pos->member.next), */ \
for (pos = list_entry((head)->next, __typeof__(*pos), member); /* prefetch(pos->member.next), */ \
&pos->member != (head); \
pos = list_entry(pos->member.next, __typeof__(*pos), member))
@ -50,9 +55,7 @@ void things_list_clear(listhead *head); /* listhead member must be the first mem
* @member:>the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, __typeof__(*pos), member), \
n = list_entry(pos->member.next, __typeof__(*pos), member); \
&pos->member != (head); \
for (pos = list_entry((head)->next, __typeof__(*pos), member), n = list_entry(pos->member.next, __typeof__(*pos), member); &pos->member != (head); \
pos = n, n = list_entry(n->member.next, __typeof__(*n), member))
#define list_empty(head) ((head) == (head)->next)

View File

@ -1,17 +1,17 @@
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/timeb.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#define MAXMSG 256
@ -20,7 +20,7 @@
#define TIMETOLIVE 600 // seconds
typedef unsigned char uchar;
char logging=0;
char logging = 0;
unsigned char mesg[MAXMSG];
int lastsize;
@ -35,8 +35,7 @@ char masterhostname[256];
#define MAXMATCHES 16
struct registration
{
struct registration {
uchar id;
uchar unique[4];
uchar password[4];
@ -44,16 +43,14 @@ struct registration
uchar name[16];
uchar status;
};
struct query
{
struct query {
uchar id;
uchar password[4];
uchar version[4];
};
struct gamehost
{
struct gamehost *next,*prev;
struct gamehost {
struct gamehost *next, *prev;
uchar machine[4];
uchar port[2];
struct registration reg;
@ -61,107 +58,93 @@ struct gamehost
};
struct gamehost *freehosts=0,*activehosts=0;
struct gamehost *freehosts = 0, *activehosts = 0;
int udpsocket,myport;
struct sockaddr_in myname={0},mastername={0};
int udpsocket, myport;
struct sockaddr_in myname = {0}, mastername = {0};
socklen_t senderlength;
struct sockaddr_in sender={0};
struct sockaddr_in sender = {0};
long longtime(void)
{
struct timeb tb;
long longtime(void) {
struct timeb tb;
ftime(&tb);
return tb.time;
}
char *timestr()
{
static char timestring[80];
char* timestr() {
static char timestring[80];
time_t t;
int l;
time_t t;
int l;
time(&t);
strcpy(timestring,ctime(&t));
l=strlen(timestring);
if(l && timestring[l-1]=='\n') timestring[l-1]=0;
strcpy(timestring, ctime(&t));
l = strlen(timestring);
if (l && timestring[l - 1] == '\n') timestring[l - 1] = 0;
return timestring;
}
int putmsg(struct sockaddr_in *toname,unsigned char *msg,int len)
{
int status;
int putmsg(struct sockaddr_in* toname, unsigned char* msg, int len) {
int status;
status=sendto(udpsocket,msg,len,0,
(struct sockaddr *)toname,sizeof(struct sockaddr_in));
status = sendto(udpsocket, msg, len, 0, (struct sockaddr*) toname, sizeof(struct sockaddr_in));
return status;
}
void ack()
{
uchar copy[256];
void ack() {
uchar copy[256];
*copy=PKT_ACK;
memmove(copy+1,mesg,lastsize);
putmsg(&sender,copy,lastsize+1);
*copy = PKT_ACK;
memmove(copy + 1, mesg, lastsize);
putmsg(&sender, copy, lastsize + 1);
}
int getmsg(int seconds)
{
int size;
int getmsg(int seconds) {
int size;
lastsize=-1;
memset(&sender,0,sizeof(sender));
senderlength=sizeof(sender);
if(seconds)
{
lastsize = -1;
memset(&sender, 0, sizeof(sender));
senderlength = sizeof(sender);
if (seconds) {
struct timeval timeout;
fd_set readfds;
int res;
timeout.tv_sec=seconds;
timeout.tv_usec=0;
timeout.tv_sec = seconds;
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(udpsocket,&readfds);
res=select(udpsocket+1,&readfds,0,0,&timeout);
if(res<=0) return -1;
FD_SET(udpsocket, &readfds);
res = select(udpsocket + 1, &readfds, 0, 0, &timeout);
if (res <= 0) return -1;
}
lastsize=size=recvfrom(udpsocket,mesg,MAXMSG,0,
(struct sockaddr *)&sender,&senderlength);
lastsize = size = recvfrom(udpsocket, mesg, MAXMSG, 0, (struct sockaddr*) &sender, &senderlength);
return size;
}
long longind(unsigned char *p)
{
return (p[0]<<24L) | (p[1]<<16L) | (p[2]<<8) | p[3];
long longind(unsigned char* p) {
return (p[0] << 24L) | (p[1] << 16L) | (p[2] << 8) | p[3];
}
short shortind(unsigned char *p)
{
return (p[0]<<8L) | p[1];
short shortind(unsigned char* p) {
return (p[0] << 8L) | p[1];
}
void openport(int portwant)
{
int status;
void openport(int portwant) {
int status;
myport=portwant;
udpsocket=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(udpsocket==-1)
{
myport = portwant;
udpsocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udpsocket == -1) {
perror("socket()");
exit(1);
}
memset(&myname,0,sizeof(myname));
myname.sin_family=AF_INET;
myname.sin_addr.s_addr=htonl(INADDR_ANY);
myname.sin_port=htons(myport);
memset(&myname, 0, sizeof(myname));
myname.sin_family = AF_INET;
myname.sin_addr.s_addr = htonl(INADDR_ANY);
myname.sin_port = htons(myport);
status=bind(udpsocket,(struct sockaddr *) &myname,sizeof(myname));
if(status==-1)
{
status = bind(udpsocket, (struct sockaddr*) &myname, sizeof(myname));
if (status == -1) {
perror("bind()");
exit(1);
}
@ -169,224 +152,206 @@ int status;
#define PERBLOCK 512
struct gamehost *newhost()
{
struct gamehost *h,*block;
int i;
struct gamehost* newhost() {
struct gamehost *h, *block;
int i;
if(!freehosts)
{
block=malloc(sizeof(struct gamehost)*PERBLOCK);
if(!block) return 0;
freehosts=block;
i=PERBLOCK-1;
while(i--)
{
block->next=block+1;
if (!freehosts) {
block = malloc(sizeof(struct gamehost) * PERBLOCK);
if (!block) return 0;
freehosts = block;
i = PERBLOCK - 1;
while (i--) {
block->next = block + 1;
++block;
}
block->next=0;
block->next = 0;
}
h=freehosts;
freehosts=freehosts->next;
memset(h,0,sizeof(struct gamehost));
h = freehosts;
freehosts = freehosts->next;
memset(h, 0, sizeof(struct gamehost));
return h;
}
void freehost(struct gamehost *h)
{
h->next=freehosts;
freehosts=h;
void freehost(struct gamehost* h) {
h->next = freehosts;
freehosts = h;
}
struct gamehost *findmatch(struct registration *key)
{
struct gamehost *h;
h=activehosts;
while(h)
{
if(!memcmp(&h->reg,key,sizeof(struct registration)-1))
return h;
h=h->next;
struct gamehost* findmatch(struct registration* key) {
struct gamehost* h;
h = activehosts;
while (h) {
if (!memcmp(&h->reg, key, sizeof(struct registration) - 1)) return h;
h = h->next;
}
return 0;
}
void insert(struct gamehost *h)
{
if(activehosts)
{
h->next=activehosts;
h->prev=activehosts->prev;
activehosts->prev=h;
activehosts=h;
} else
{
h->next=h->prev=0;
activehosts=h;
void insert(struct gamehost* h) {
if (activehosts) {
h->next = activehosts;
h->prev = activehosts->prev;
activehosts->prev = h;
activehosts = h;
} else {
h->next = h->prev = 0;
activehosts = h;
}
}
void delete(struct gamehost *h)
{
if(h->prev)
h->prev->next=h->next;
void delete (struct gamehost* h) {
if (h->prev)
h->prev->next = h->next;
else
activehosts=h->next;
if(h->next)
h->next->prev=h->prev;
activehosts = h->next;
if (h->next) h->next->prev = h->prev;
freehost(h);
}
void doreg()
{
struct registration *new;
struct gamehost *match;
long now;
void doreg() {
struct registration* new;
struct gamehost* match;
long now;
new=(struct registration *)mesg;
match=findmatch(new);
if(logging)
{
unsigned addr=ntohl(sender.sin_addr.s_addr);
unsigned short port=ntohs(sender.sin_port);
printf("reg :%s:%d.%d.%d.%d:%d %c%lx '%s'\n",timestr(),
(addr>>24)&255,(addr>>16)&255,(addr>>8)&255,addr&255,port,
new->status ? '+' : '-',(long)match,new->name);
new = (struct registration*) mesg;
match = findmatch(new);
if (logging) {
unsigned addr = ntohl(sender.sin_addr.s_addr);
unsigned short port = ntohs(sender.sin_port);
printf(
"reg :%s:%d.%d.%d.%d:%d %c%lx '%s'\n",
timestr(),
(addr >> 24) & 255,
(addr >> 16) & 255,
(addr >> 8) & 255,
addr & 255,
port,
new->status ? '+' : '-',
(long) match,
new->name);
fflush(stdout);
}
if(!match && !new->status) {ack();return;}
if(match && new->status) {ack();return;}
if(!match && new->status)
{
match=newhost();
if(!match) return; // No memory, what can we do?
memmove(match->machine,&sender.sin_addr.s_addr,4);
memmove(match->port,&sender.sin_port,2);
match->reg=*new;
now=longtime();
match->timeout=now+TIMETOLIVE;
if (!match && !new->status) {
ack();
return;
}
if (match && new->status) {
ack();
return;
}
if (!match && new->status) {
match = newhost();
if (!match) return; // No memory, what can we do?
memmove(match->machine, &sender.sin_addr.s_addr, 4);
memmove(match->port, &sender.sin_port, 2);
match->reg = *new;
now = longtime();
match->timeout = now + TIMETOLIVE;
ack();
insert(match);
return;
} else // match && !new->status
{
delete(match);
delete (match);
ack();
return;
}
}
void doquery()
{
uchar *password;
uchar *version;
struct gamehost *h;
uchar response[2048],*rput,*countersave;
int counter;
void doquery() {
uchar* password;
uchar* version;
struct gamehost* h;
uchar response[2048], *rput, *countersave;
int counter;
if(logging)
{
unsigned addr=ntohl(sender.sin_addr.s_addr);
unsigned short port=ntohs(sender.sin_port);
printf("query:%s:%d.%d.%d.%d:%d\n",timestr(),
(addr>>24)&255,(addr>>16)&255,(addr>>8)&255,addr&255,port);
if (logging) {
unsigned addr = ntohl(sender.sin_addr.s_addr);
unsigned short port = ntohs(sender.sin_port);
printf("query:%s:%d.%d.%d.%d:%d\n", timestr(), (addr >> 24) & 255, (addr >> 16) & 255, (addr >> 8) & 255, addr & 255, port);
fflush(stdout);
}
password=mesg+1;
version=mesg+5;
h=activehosts;
rput=response;
*rput++=PKT_INFO;
memmove(rput,password,4);
rput+=4;
memmove(rput,version,4);
rput+=4;
countersave=rput;
*rput++=0;
*rput++=0;
counter=0;
password = mesg + 1;
version = mesg + 5;
h = activehosts;
rput = response;
*rput++ = PKT_INFO;
memmove(rput, password, 4);
rput += 4;
memmove(rput, version, 4);
rput += 4;
countersave = rput;
*rput++ = 0;
*rput++ = 0;
counter = 0;
while(h)
{
if(!memcmp(password,h->reg.password,4) &&
!memcmp(version,h->reg.version,4) && counter<MAXMATCHES)
{
while (h) {
if (!memcmp(password, h->reg.password, 4) && !memcmp(version, h->reg.version, 4) && counter < MAXMATCHES) {
++counter;
memmove(rput,h->reg.unique,4);
rput+=4;
memmove(rput,h->machine,4);
rput+=4;
memmove(rput,h->port,2);
rput+=2;
memmove(rput,h->reg.name,sizeof(h->reg.name));
rput+=sizeof(h->reg.name);
memmove(rput, h->reg.unique, 4);
rput += 4;
memmove(rput, h->machine, 4);
rput += 4;
memmove(rput, h->port, 2);
rput += 2;
memmove(rput, h->reg.name, sizeof(h->reg.name));
rput += sizeof(h->reg.name);
}
h=h->next;
h = h->next;
}
*countersave++=counter>>8;
*countersave++=counter;
putmsg(&sender,response,rput-response);
*countersave++ = counter >> 8;
*countersave++ = counter;
putmsg(&sender, response, rput - response);
}
void purge(long cutoff)
{
struct gamehost *h,*h2;
h=activehosts;
while(h)
{
h2=h;
h=h->next;
if(cutoff-h2->timeout>0)
{
delete(h2);
}
void purge(long cutoff) {
struct gamehost *h, *h2;
h = activehosts;
while (h) {
h2 = h;
h = h->next;
if (cutoff - h2->timeout > 0) { delete (h2); }
}
}
int main(int argc,char **argv)
{
int i;
int want;
int size;
long purgetime;
long now;
int main(int argc, char** argv) {
int i;
int want;
int size;
long purgetime;
long now;
want=PORT;
if(argc>1)
{
for(i=1;i<argc;++i)
if(!strncmp(argv[i],"-p",2))
{
if(strlen(argv[i])>2) want=atoi(argv[i]+2);
else if(i+1<argc) want=atoi(argv[i+1]);
want = PORT;
if (argc > 1) {
for (i = 1; i < argc; ++i)
if (!strncmp(argv[i], "-p", 2)) {
if (strlen(argv[i]) > 2)
want = atoi(argv[i] + 2);
else if (i + 1 < argc)
want = atoi(argv[i + 1]);
}
}
freehosts=0;
freehosts = 0;
openport(want);
purgetime=longtime()+TIMETOLIVE;
for(;;)
{
size=getmsg(10);
if(size>=1)
switch(*mesg)
{
purgetime = longtime() + TIMETOLIVE;
for (;;) {
size = getmsg(10);
if (size >= 1) switch (*mesg) {
case PKT_REGISTER:
if(size<sizeof(struct registration)) continue;
if (size < sizeof(struct registration)) continue;
doreg();
break;
case PKT_QUERY:
if(size<sizeof(struct query)) continue;
if (size < sizeof(struct query)) continue;
doquery();
break;
}
now=longtime();
if(now-purgetime>0) // avoid year 203x bug...
now = longtime();
if (now - purgetime > 0) // avoid year 203x bug...
{
purge(purgetime);
purgetime+=TIMETOLIVE;
purgetime += TIMETOLIVE;
}
}
}

View File

@ -1,73 +1,66 @@
#include "bomber.h"
#include "menu.h"
#include "draw.h"
#include "gfx.h"
#include "utils.h"
#include "sound.h"
#include "network.h"
#include "announce.h"
#include "bomber.h"
#include "draw.h"
#include "game.h"
#include "gfx.h"
#include "network.h"
#include "sound.h"
#include "utils.h"
GameOptions configopts = { 2, 1, 0, 2 };
GameOptions configopts = {2, 1, 0, 2};
/* Generic menu */
typedef enum {
MENU_ANY = -1,
MENU_MAIN = 0,
MENU_CONFIG = 2,
MENU_JOIN = 3
} menuname;
typedef enum { MENU_ANY = -1, MENU_MAIN = 0, MENU_CONFIG = 2, MENU_JOIN = 3 } menuname;
#define MENU_SELECT(x) ((menuname) (-x-1))
#define MENU_SELECT(x) ((menuname)(-x - 1))
static int menuhistory[32]={0};
static int menuhistory[32] = {0};
static char menustring[1024];
static char *menuput,*menuitems[40],*menutitle;
static char *menuput, *menuitems[40], *menutitle;
static int menunum, menuexit;
static int menudelta;
static void drawmenu(int selected) {
int i,j;
int tx,ty;
int i, j;
int tx, ty;
clear();
j=strlen(menutitle)*bigfontxsize;
drawbigstring((IXSIZE-j) >> 1,20,menutitle);
ty=((IYSIZE-(bigfontysize*menunum))>>1)-(IYSIZE>>3);
for(i=0;i<menunum;++i)
{
j=strlen(menuitems[i])*bigfontxsize;
tx=(IXSIZE-j) >> 1;
if(i==selected)
{
greyrect(0,ty-1,tx-5,bigfontysize);
greyrect(tx+j+3,ty-1,IXSIZE-(tx+j+3),bigfontysize);
j = strlen(menutitle) * bigfontxsize;
drawbigstring((IXSIZE - j) >> 1, 20, menutitle);
ty = ((IYSIZE - (bigfontysize * menunum)) >> 1) - (IYSIZE >> 3);
for (i = 0; i < menunum; ++i) {
j = strlen(menuitems[i]) * bigfontxsize;
tx = (IXSIZE - j) >> 1;
if (i == selected) {
greyrect(0, ty - 1, tx - 5, bigfontysize);
greyrect(tx + j + 3, ty - 1, IXSIZE - (tx + j + 3), bigfontysize);
}
drawbigstring(tx,ty,menuitems[i]);
ty+=bigfontyspace;
drawbigstring(tx, ty, menuitems[i]);
ty += bigfontyspace;
}
}
static int domenu(menuname whichmenu, int (*pause)(void)) {
char redraw;
int selected;
int mcount=0;
int mcount = 0;
if(whichmenu>=0)
selected=menuhistory[whichmenu];
if (whichmenu >= 0)
selected = menuhistory[whichmenu];
else
selected=-whichmenu-1;
selected = -whichmenu - 1;
if (!pause) pause=mypause;
if (!pause) pause = mypause;
redraw=1;
redraw = 1;
clearspritelist();
while(!exitflag) {
if(redraw) {
while (!exitflag) {
if (redraw) {
drawmenu(selected);
redraw=0;
redraw = 0;
}
if (!pause()) return -1;
scaninput();
@ -75,37 +68,37 @@ static int domenu(menuname whichmenu, int (*pause)(void)) {
clearsprites();
clearspritelist();
addsprite(IXSIZE/2-50-20,IYSIZE-80,walking[2]+(30+mcount%15));
addsprite(IXSIZE/2+50-20,IYSIZE-80,walking[3]+(30+(mcount+7)%15));
addsprite(IXSIZE / 2 - 50 - 20, IYSIZE - 80, walking[2] + (30 + mcount % 15));
addsprite(IXSIZE / 2 + 50 - 20, IYSIZE - 80, walking[3] + (30 + (mcount + 7) % 15));
plotsprites();
copyup();
if(anydown()) playsound(3);
while(anydown()) {
switch(takedown()) {
if (anydown()) playsound(3);
while (anydown()) {
switch (takedown()) {
case MYLEFT:
menudelta=-1;
menudelta = -1;
return selected;
case MYRIGHT:
case ' ':
case 13:
menudelta=1;
menudelta = 1;
return selected;
case 'k':
case MYUP:
if (selected) --selected;
else selected=menunum-1;
if (whichmenu>=0)
menuhistory[whichmenu]=selected;
redraw=1;
if (selected)
--selected;
else
selected = menunum - 1;
if (whichmenu >= 0) menuhistory[whichmenu] = selected;
redraw = 1;
break;
case 'j':
case MYDOWN:
++selected;
if (selected==menunum) selected=0;
if (whichmenu>=0)
menuhistory[whichmenu]=selected;
redraw=1;
if (selected == menunum) selected = 0;
if (whichmenu >= 0) menuhistory[whichmenu] = selected;
redraw = 1;
break;
case 0x1b:
if (MENU_MAIN == whichmenu && menuexit != selected) {
@ -123,45 +116,45 @@ static int domenu(menuname whichmenu, int (*pause)(void)) {
}
static void menustart() {
menunum=-1;
menuput=menustring;
*menuput=0;
menunum = -1;
menuput = menustring;
*menuput = 0;
menuexit = 0;
}
static void additem_s(char *item, int len) {
if (len < 0 || (menustring+sizeof(menustring)-menuput <= len)) return;
static void additem_s(char* item, int len) {
if (len < 0 || (menustring + sizeof(menustring) - menuput <= len)) return;
if(menunum<0)
menutitle=menuput;
if (menunum < 0)
menutitle = menuput;
else
menuitems[menunum]=menuput;
menuitems[menunum] = menuput;
++menunum;
memcpy(menuput,item,len+1);
menuput += len+1;
memcpy(menuput, item, len + 1);
menuput += len + 1;
}
static void additem(char *item,...) {
static void additem(char* item, ...) {
char output[256];
va_list ap;
int len;
va_start(ap, item);
len = vsnprintf(output,sizeof(output),item,ap);
len = vsnprintf(output, sizeof(output), item, ap);
if (len >= 256) len = 255; /* truncated string */
additem_s(output, len);
}
static void addexit(char *item,...) {
static void addexit(char* item, ...) {
char output[256];
va_list ap;
int len;
va_start(ap, item);
len = vsnprintf(output,sizeof(output),item,ap);
len = vsnprintf(output, sizeof(output), item, ap);
if (len >= 256) len = 255; /* truncated string */
menuexit = menunum;
@ -173,9 +166,9 @@ static void addexit(char *item,...) {
/* game menues */
static const char *densities[]={"PACKED","HIGH","MEDIUM","LOW"};
static const char *generosities[]={"LOW","MEDIUM","HIGH","RIDICULOUS"};
static const char *dis_en_abled[]={"DISABLED","ENABLED"};
static const char* densities[] = {"PACKED", "HIGH", "MEDIUM", "LOW"};
static const char* generosities[] = {"LOW", "MEDIUM", "HIGH", "RIDICULOUS"};
static const char* dis_en_abled[] = {"DISABLED", "ENABLED"};
static void config_menu(void) {
int sel;
@ -184,30 +177,30 @@ static void config_menu(void) {
menustart();
additem("GAME OPTIONS");
additem("RETURN TO MAIN MENU");
additem("DENSITY: %s",densities[configopts.density]);
additem("GENEROSITY: %s",generosities[configopts.generosity]);
additem("INITIAL FLAME LENGTH: %d",configopts.flames+1);
additem("INITIAL NUMBER OF BOMBS: %d",configopts.bombs+1);
additem("DENSITY: %s", densities[configopts.density]);
additem("GENEROSITY: %s", generosities[configopts.generosity]);
additem("INITIAL FLAME LENGTH: %d", configopts.flames + 1);
additem("INITIAL NUMBER OF BOMBS: %d", configopts.bombs + 1);
additem("SOUND: %s", dis_en_abled[sound_enabled]);
sel=domenu(MENU_CONFIG, NULL);
sel = domenu(MENU_CONFIG, NULL);
switch (sel) {
case 0:
return;
case 1:
configopts.density+=menudelta;
configopts.density&=3;
configopts.density += menudelta;
configopts.density &= 3;
break;
case 2:
configopts.generosity+=menudelta;
configopts.generosity&=3;
configopts.generosity += menudelta;
configopts.generosity &= 3;
break;
case 3:
configopts.flames+=menudelta;
configopts.flames&=7;
configopts.flames += menudelta;
configopts.flames &= 7;
break;
case 4:
configopts.bombs+=menudelta;
configopts.bombs&=7;
configopts.bombs += menudelta;
configopts.bombs &= 7;
break;
case 5:
sound_enabled = 1 - sound_enabled;
@ -218,20 +211,20 @@ static void config_menu(void) {
static void draw_host_game(void) {
int i;
char *name;
char* name;
char temp[64];
#define M3X (IXSIZE/3)
#define M3Y (IYSIZE/4)
#define M3X (IXSIZE / 3)
#define M3Y (IYSIZE / 4)
clear();
centerbig(20,"HOST NETWORK GAME");
drawbigstring(M3X,M3Y,"SLOT NAME");
for(i=0;i<MAXNETNODES;++i) {
if(!netnodes[i].used) continue;
name=netnodes[i].name;
snprintf(temp,sizeof(temp)," %d %s",i+1,name);
drawbigstring(M3X,M3Y+(i+2)*bigfontyspace,temp);
centerbig(20, "HOST NETWORK GAME");
drawbigstring(M3X, M3Y, "SLOT NAME");
for (i = 0; i < MAXNETNODES; ++i) {
if (!netnodes[i].used) continue;
name = netnodes[i].name;
snprintf(temp, sizeof(temp), " %d %s", i + 1, name);
drawbigstring(M3X, M3Y + (i + 2) * bigfontyspace, temp);
}
copyup();
@ -246,10 +239,10 @@ static void host_game(void) {
}
draw_host_game();
for(;;) {
for (;;) {
scaninput();
while(anydown()) {
switch(takedown()) {
while (anydown()) {
switch (takedown()) {
case 0x1b:
unregistergame();
cancel_network_game();
@ -278,27 +271,25 @@ static void drawjoinscreen(void) {
char name[17];
char temp[64];
#define JX (IXSIZE/3)
#define JY (IYSIZE/4)
#define JX (IXSIZE / 3)
#define JY (IYSIZE / 4)
clear();
centerbig(20,"JOIN NETWORK GAME");
drawbigstring(JX,JY,"SLOT NAME");
centerbig(20, "JOIN NETWORK GAME");
drawbigstring(JX, JY, "SLOT NAME");
for (i = 0; i < MAXNETNODES; ++i) {
if(!netnodes[i].used) continue;
memmove(name,netnodes[i].name,16);
name[16]=0;
snprintf(temp,sizeof(temp)," %d %s",i+1,name);
drawbigstring(JX,JY+(i+1)*bigfontyspace,temp);
if (!netnodes[i].used) continue;
memmove(name, netnodes[i].name, 16);
name[16] = 0;
snprintf(temp, sizeof(temp), " %d %s", i + 1, name);
drawbigstring(JX, JY + (i + 1) * bigfontyspace, temp);
}
}
static int tryjoin(int which) {
int res;
if (0 == (res = send_join(&gamelistentries[which].netname, playername))) {
return 0;
}
if (0 == (res = send_join(&gamelistentries[which].netname, playername))) { return 0; }
while (!exitflag) {
switch (res) {
@ -309,18 +300,20 @@ static int tryjoin(int which) {
drawjoinscreen();
copyup();
break;
case 3: return 1;
default: break;
case 3:
return 1;
default:
break;
}
scaninput();
while(anydown()) {
switch(takedown()) {
while (anydown()) {
switch (takedown()) {
case 0x1b:
send_quit();
return 0;
}
}
res=scaninvite(200);
res = scaninvite(200);
}
return 0;
}
@ -335,9 +328,7 @@ static void join_game(void) {
int i;
int sel = -1;
if (!searchgames()) {
return;
}
if (!searchgames()) { return; }
menuhistory[MENU_JOIN] = 0;
@ -348,9 +339,7 @@ static void join_game(void) {
addexit("EXIT");
} else {
additem("JOIN NETWORK GAME");
for (i = 0; i < gamelistsize; i++) {
additem(gamelistentries[i].name);
}
for (i = 0; i < gamelistsize; i++) { additem(gamelistentries[i].name); }
addexit("EXIT");
}
sel = domenu(MENU_JOIN, join_game_pause);
@ -358,12 +347,8 @@ static void join_game(void) {
}
stop_search();
if(menuexit == sel || !gamelistsize) {
return;
}
if(!tryjoin(sel)) {
return;
}
if (menuexit == sel || !gamelistsize) { return; }
if (!tryjoin(sel)) { return; }
run_network_game();
}
@ -371,16 +356,16 @@ void mainloop(void) {
int sel;
exitflag = 0;
while(!exitflag) {
while (!exitflag) {
menustart();
additem("BOMBER MAIN MENU");
additem("EXIT GAME");
additem("START SINGLE PLAYER GAME");
additem("OPTIONS");
// additem("REMAP MOVEMENT KEYS");
// additem("REMAP MOVEMENT KEYS");
additem("START NETWORK GAME");
additem("JOIN NETWORK GAME");
sel=domenu(MENU_MAIN, NULL);
sel = domenu(MENU_MAIN, NULL);
if (menudelta < 0) sel = -1;
switch (sel) {
case 0:

View File

@ -1,34 +1,34 @@
#include "bomber.h"
#include "network.h"
#include "announce.h"
#include "bomber.h"
#include "draw.h"
#include "game.h"
#include "menu.h"
#include "network.h"
#include "utils.h"
#include "draw.h"
#define MAXMSG 4096
int udpsocket;
const unsigned char gameversion[4]={0xda,0x01,0x00,0x09};
const unsigned char gameversion[4] = {0xda, 0x01, 0x00, 0x09};
struct netnode netnodes[MAXNETNODES];
static int informsize;
static unsigned char regpacket[64];
static struct sockaddr_in myname={0},mastername={0};
static struct sockaddr_in myname = {0}, mastername = {0};
static socklen_t senderlength;
static struct sockaddr_in sender={0};
static struct sockaddr_in sender = {0};
static unsigned char mesg[MAXMSG]="";
uchar needwhole=0;
static unsigned char mesg[MAXMSG] = "";
uchar needwhole = 0;
int mydatacount;
int myslot;
network_type network = NETWORK_NONE;
int actioncount;
unsigned char actionblock[ACTIONHIST*MAXNETNODES];
unsigned char actionblock[ACTIONHIST * MAXNETNODES];
int myaction;
unsigned char actions[MAXNETNODES];
@ -38,7 +38,7 @@ long latestcounts[MAXNETNODES];
enum network_packet_types {
PKT_ACK, /* perfect copy of packet received */
/* join / host game */
/* join / host game */
/* slave -> master packets */
PKT_JOIN, /* 4 bytes version #, 4 bytes joinunique #, 16 bytes name */
PKT_QUIT, /* 4 bytes unique # */
@ -48,7 +48,7 @@ enum network_packet_types {
PKT_CONFIG, /* 4 bytes unique #, config */
PKT_ACCEPT, /* 4 bytes join unique #, 132 bytes seed + unique #, config, slot info (see invite) */
PKT_REJECT, /* 4 bytes join unique #, 4 bytes version #, 1: reason */
/* ingame actions */
/* ingame actions */
/* slave -> master packets */
PKT_MYDATA, /* 4 bytes unique #,4 bytes frame #, 1 byte data */
/* master -> slave packets */
@ -65,10 +65,7 @@ enum reject_reason {
#define _REJECT_LAST REJECT_VERSION
const char *reject_reason_str[] = {
"Server full",
"Version mismatch"
};
const char* reject_reason_str[] = {"Server full", "Version mismatch"};
/* all bytes stored MSB first */
@ -125,135 +122,126 @@ received will be answered with PKT_QUIT.
/* Network I/O, building/checking packets */
#if defined (TEST_LATENCY)
#if defined(TEST_LATENCY)
#define NUMQ 512
struct message {
int time;
struct sockaddr_in *to;
struct sockaddr_in* to;
int tosize;
unsigned char msg[512];
int len;
} message[NUMQ]={0};
} message[NUMQ] = {0};
outmsgs() {
int i;
for(i=0;i<NUMQ;++i) {
if(message[i].time) {
for (i = 0; i < NUMQ; ++i) {
if (message[i].time) {
--message[i].time;
if(message[i].time) continue;
sendto(udpsocket,message[i].msg,message[i].len,0,
message[i].to,sizeof(struct sockaddr_in));
if (message[i].time) continue;
sendto(udpsocket, message[i].msg, message[i].len, 0, message[i].to, sizeof(struct sockaddr_in));
}
}
}
#endif
static int putmsg(struct sockaddr_in *toname,unsigned char *msg,int len) {
static int putmsg(struct sockaddr_in* toname, unsigned char* msg, int len) {
int status;
#if defined (TEST_LATENCY)
#if defined(TEST_LATENCY)
int i;
for(i=0;i<NUMQ;++i) {
if(!message[i].time) {
message[i].time=10;
message[i].to=toname;
memcpy(message[i].msg,msg,len);
message[i].len=len;
for (i = 0; i < NUMQ; ++i) {
if (!message[i].time) {
message[i].time = 10;
message[i].to = toname;
memcpy(message[i].msg, msg, len);
message[i].len = len;
break;
}
}
return 0;
#else
status=sendto(udpsocket,msg,len,0,
(struct sockaddr *)toname,sizeof(struct sockaddr_in));
status = sendto(udpsocket, msg, len, 0, (struct sockaddr*) toname, sizeof(struct sockaddr_in));
return status;
#endif
}
static int getmsg(int msec) {
int size;
memset(&sender,0,sizeof(sender));
senderlength=sizeof(sender);
if(msec) {
memset(&sender, 0, sizeof(sender));
senderlength = sizeof(sender);
if (msec) {
struct timeval timeout;
fd_set readfds;
int res;
memset(&timeout,0,sizeof(timeout));
timeout.tv_sec=msec/1000;
timeout.tv_usec=(msec%1000)*1000;
memset(&timeout, 0, sizeof(timeout));
timeout.tv_sec = msec / 1000;
timeout.tv_usec = (msec % 1000) * 1000;
FD_ZERO(&readfds);
FD_SET(udpsocket,&readfds);
res=select(udpsocket+1,&readfds,0,0,&timeout);
if(res<=0) return -1;
FD_SET(udpsocket, &readfds);
res = select(udpsocket + 1, &readfds, 0, 0, &timeout);
if (res <= 0) return -1;
}
size=recvfrom(udpsocket,mesg,MAXMSG,0,
(struct sockaddr *)&sender,&senderlength);
size = recvfrom(udpsocket, mesg, MAXMSG, 0, (struct sockaddr*) &sender, &senderlength);
return size;
}
static int isvalidunique(unsigned char *p) {
static int isvalidunique(unsigned char* p) {
return 0 == memcmp(p, &network_unique, 4);
}
static int isvalidversion(unsigned char *p) {
static int isvalidversion(unsigned char* p) {
return 0 == memcmp(p, gameversion, 4);
}
static unsigned char* writeuint32(unsigned char *p, Uint32 i) {
p[0]=i>>24L;
p[1]=i>>16L;
p[2]=i>>8L;
p[3]=i;
return p+4;
static unsigned char* writeuint32(unsigned char* p, Uint32 i) {
p[0] = i >> 24L;
p[1] = i >> 16L;
p[2] = i >> 8L;
p[3] = i;
return p + 4;
}
static Uint32 readuint32(unsigned char *p) {
return (p[0]<<24L) | (p[1]<<16L) | (p[2]<<8) | p[3];
static Uint32 readuint32(unsigned char* p) {
return (p[0] << 24L) | (p[1] << 16L) | (p[2] << 8) | p[3];
}
static unsigned char* write_unique(unsigned char *p) {
static unsigned char* write_unique(unsigned char* p) {
memcpy(p, &network_unique, 4);
return p + 4;
}
static unsigned char* write_version(unsigned char *p) {
static unsigned char* write_version(unsigned char* p) {
memcpy(p, &gameversion, 4);
return p + 4;
}
static int isvalidmsg_from_slave() {
int i;
void *host;
void *port;
void* host;
void* port;
if (!isvalidunique(mesg+1)) return -1;
host=&sender.sin_addr.s_addr;
port=&sender.sin_port;
for(i=1;i<MAXNETNODES;++i)
if(netnodes[i].used &&
!memcmp(&netnodes[i].netname.sin_addr.s_addr,host,4) &&
!memcmp(&netnodes[i].netname.sin_port,port,2))
return i;
if (!isvalidunique(mesg + 1)) return -1;
host = &sender.sin_addr.s_addr;
port = &sender.sin_port;
for (i = 1; i < MAXNETNODES; ++i)
if (netnodes[i].used && !memcmp(&netnodes[i].netname.sin_addr.s_addr, host, 4) && !memcmp(&netnodes[i].netname.sin_port, port, 2)) return i;
return -1;
}
static int isvalidmsg_from_master() {
if (sender.sin_family != mastername.sin_family
|| sender.sin_addr.s_addr != mastername.sin_addr.s_addr
|| sender.sin_port != mastername.sin_port) return 0;
if (sender.sin_family != mastername.sin_family || sender.sin_addr.s_addr != mastername.sin_addr.s_addr || sender.sin_port != mastername.sin_port) return 0;
return 1;
}
/* Handling game actions */
static void addactions(void) {
memmove(actionblock+MAXNETNODES, actionblock, (ACTIONHIST-1)*MAXNETNODES);
memmove(actionblock + MAXNETNODES, actionblock, (ACTIONHIST - 1) * MAXNETNODES);
memcpy(actionblock, actions, MAXNETNODES);
++actioncount;
}
@ -264,8 +252,8 @@ static void sendactions(int which) {
msg[0] = PKT_STEP;
write_unique(msg + 1);
writeuint32(msg + 5, actioncount);
memcpy(msg + 9, actionblock, MAXNETNODES*ACTIONHIST);
putmsg(&netnodes[which].netname, msg, MAXNETNODES*ACTIONHIST + 9);
memcpy(msg + 9, actionblock, MAXNETNODES * ACTIONHIST);
putmsg(&netnodes[which].netname, msg, MAXNETNODES * ACTIONHIST + 9);
}
static void sendmine(int frame) {
@ -289,17 +277,15 @@ int networktraffic(void) {
case NETWORK_NONE:
return -1;
case NETWORK_MASTER:
memcpy(actions,latestactions,MAXNETNODES);
actions[0]=myaction;
memcpy(actions, latestactions, MAXNETNODES);
actions[0] = myaction;
if (myaction == ACT_QUIT) {
for (i = 1; i < MAXNETNODES; ++i) {
if (netnodes[i].used)
actions[i] = ACT_QUIT;
if (netnodes[i].used) actions[i] = ACT_QUIT;
}
} else {
for (i = 1; i < MAXNETNODES; ++i) {
if (netnodes[i].used)
actions[i] &= ACT_MASK; /* only keep direction */
if (netnodes[i].used) actions[i] &= ACT_MASK; /* only keep direction */
}
now = gtime();
for (;;) {
@ -313,7 +299,7 @@ int networktraffic(void) {
if (length < 10) continue;
whosent = isvalidmsg_from_slave();
if (whosent <= 0) continue;
count = readuint32(mesg+5);
count = readuint32(mesg + 5);
if (count > latestcounts[whosent]) {
latestcounts[whosent] = count;
actions[whosent] = (actions[whosent] & ~ACT_MASK) | mesg[9]; /* don't drop "action" keys */
@ -324,10 +310,9 @@ int networktraffic(void) {
addactions(); /* update action history block */
for (i = 1; i < MAXNETNODES; ++i) {
if(netnodes[i].used) {
if (netnodes[i].used) {
sendactions(i); /* send actions to every active node */
if (actions[i] == ACT_QUIT)
netnodes[i].used = 0; /* remove disconnected clients */
if (actions[i] == ACT_QUIT) netnodes[i].used = 0; /* remove disconnected clients */
}
}
return actioncount;
@ -361,37 +346,37 @@ int networktraffic(void) {
/* Handling socket init */
int winsock=0;
int winsock = 0;
void getsocket(void) {
int status;
socklen_t slen = sizeof(myname);
#if defined(__WIN32__) || defined(WIN32)
char dummydata[128];
char dummydata[128];
if(WSAStartup(0x0101,(void *)dummydata)) {
if (WSAStartup(0x0101, (void*) dummydata)) {
printf("Windows dumped\n");
exit(1);
}
winsock=1;
winsock = 1;
#endif
udpsocket=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(udpsocket==-1) {
udpsocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udpsocket == -1) {
perror("socket()");
exit(1);
}
memset(&myname,0,sizeof(myname));
myname.sin_family=AF_INET;
myname.sin_addr.s_addr=htonl(INADDR_ANY);
myname.sin_port=htons(0);
status=bind(udpsocket,(struct sockaddr *) &myname,sizeof(myname));
if(-1 == status) {
memset(&myname, 0, sizeof(myname));
myname.sin_family = AF_INET;
myname.sin_addr.s_addr = htonl(INADDR_ANY);
myname.sin_port = htons(0);
status = bind(udpsocket, (struct sockaddr*) &myname, sizeof(myname));
if (-1 == status) {
perror("bind()");
exit(1);
}
status = getsockname(udpsocket, (struct sockaddr *) &myname, &slen);
if(-1 == status) {
status = getsockname(udpsocket, (struct sockaddr*) &myname, &slen);
if (-1 == status) {
perror("getsockname()");
exit(1);
}
@ -399,7 +384,7 @@ char dummydata[128];
void freesocket(void) {
#if defined(__WIN32__) || defined(WIN32)
if(!winsock) return;
if (!winsock) return;
WSACleanup();
#endif
}
@ -413,7 +398,7 @@ static unsigned char* write_inform(unsigned char* put) {
*put++ = 0xff; /* slot specific for each slave */
for (i = 0; i < MAXNETNODES; ++i) {
if(!netnodes[i].used) continue;
if (!netnodes[i].used) continue;
*put++ = i;
memmove(put, netnodes[i].name, 16);
put += 16;
@ -424,15 +409,15 @@ static unsigned char* write_inform(unsigned char* put) {
static void send_inform_all(unsigned char type) {
int i;
unsigned char *put = mesg;
unsigned char* put = mesg;
*put++ = type;
put = write_unique(put);
put = write_inform(put);
informsize = put-mesg;
informsize = put - mesg;
for(i=1;i<MAXNETNODES;++i) {
if(netnodes[i].used) {
for (i = 1; i < MAXNETNODES; ++i) {
if (netnodes[i].used) {
mesg[5] = i;
putmsg(&netnodes[i].netname, mesg, informsize);
}
@ -448,36 +433,35 @@ static unsigned char* write_config(unsigned char* put) {
}
static void build_config() {
unsigned char *put;
unsigned char* put;
put=mesg;
*put++=PKT_CONFIG;
put = mesg;
*put++ = PKT_CONFIG;
put = write_config(put);
informsize=put-mesg;
informsize = put - mesg;
}
static void send_config1(int which) {
putmsg(&netnodes[which].netname,mesg,informsize);
putmsg(&netnodes[which].netname, mesg, informsize);
}
void send_config() {
int i;
build_config();
for (i = 1; i < MAXNETNODES; ++i)
if (netnodes[i].used)
send_config1(i);
if (netnodes[i].used) send_config1(i);
}
static void send_reject(struct sockaddr_in *toname, Uint32 network_join_unique, unsigned char reason) {
static void send_reject(struct sockaddr_in* toname, Uint32 network_join_unique, unsigned char reason) {
mesg[0] = PKT_REJECT;
memcpy(mesg+1, &network_join_unique, sizeof(network_join_unique));
write_version(mesg+5);
memcpy(mesg + 1, &network_join_unique, sizeof(network_join_unique));
write_version(mesg + 5);
mesg[9] = reason;
putmsg(toname,mesg,10);
putmsg(toname, mesg, 10);
}
static void send_accept(Uint32 network_join_unique) {
unsigned char *put = mesg;
unsigned char* put = mesg;
*put++ = PKT_ACCEPT;
memcpy(put, &network_join_unique, sizeof(network_join_unique));
@ -485,15 +469,15 @@ static void send_accept(Uint32 network_join_unique) {
put = write_seed_unique(put);
put = write_config(put);
put = write_inform(put);
putmsg(&sender,mesg,put-mesg);
putmsg(&sender, mesg, put - mesg);
}
int start_network_game() {
if(!registergame(playername, myname.sin_port, gameversion)) return 0;
memset(netnodes,0,sizeof(netnodes));
netnodes[0].used=1;
memmove(netnodes[0].name,playername,16);
myslot=0;
if (!registergame(playername, myname.sin_port, gameversion)) return 0;
memset(netnodes, 0, sizeof(netnodes));
netnodes[0].used = 1;
memmove(netnodes[0].name, playername, 16);
myslot = 0;
return 1;
}
@ -512,13 +496,11 @@ void cancel_network_game() {
int i;
mesg[0] = PKT_INVITE;
write_unique(mesg+1);
write_unique(mesg + 1);
mesg[5] = 0xff;
for(i=1;i<MAXNETNODES;++i) {
if(netnodes[i].used) {
putmsg(&netnodes[i].netname,mesg,6);
}
for (i = 1; i < MAXNETNODES; ++i) {
if (netnodes[i].used) { putmsg(&netnodes[i].netname, mesg, 6); }
}
}
@ -528,18 +510,18 @@ int handle_joins() {
unsigned char temp[64];
Uint32 network_join_unique = 0;
size=getmsg(40);
size = getmsg(40);
switch (*mesg) {
case PKT_JOIN:
if (size < 25) return 0;
memcpy(&network_join_unique, mesg+5, sizeof(network_join_unique));
if (!isvalidversion(mesg+1)) {
memcpy(&network_join_unique, mesg + 5, sizeof(network_join_unique));
if (!isvalidversion(mesg + 1)) {
send_reject(&sender, network_join_unique, REJECT_VERSION);
return 0;
}
break;
case PKT_QUIT:
if (size < 5 || !isvalidunique(mesg+1)) return 0;
if (size < 5 || !isvalidunique(mesg + 1)) return 0;
break;
default:
return 0;
@ -556,37 +538,33 @@ int handle_joins() {
if (-1 == j) j = i;
continue; /* don't compare with unused host */
}
if(memcmp(&netnodes[i].netname.sin_addr.s_addr,
&sender.sin_addr.s_addr,4)) continue;
if(memcmp(&netnodes[i].netname.sin_port,
&sender.sin_port,2)) continue;
if (memcmp(&netnodes[i].netname.sin_addr.s_addr, &sender.sin_addr.s_addr, 4)) continue;
if (memcmp(&netnodes[i].netname.sin_port, &sender.sin_port, 2)) continue;
/* found host */
break;
}
switch (*mesg) {
case PKT_QUIT:
if(i < MAXNETNODES) /* if host found, reset entry */
memset(netnodes+i,0,sizeof(struct netnode));
if (i < MAXNETNODES) /* if host found, reset entry */
memset(netnodes + i, 0, sizeof(struct netnode));
/* send always ACK for QUITs */
*temp=PKT_ACK;
memmove(temp+1,mesg,5);
putmsg(&sender,temp,6);
*temp = PKT_ACK;
memmove(temp + 1, mesg, 5);
putmsg(&sender, temp, 6);
break;
case PKT_JOIN:
if (i==MAXNETNODES && j==-1) { /* reject */
if (i == MAXNETNODES && j == -1) { /* reject */
send_reject(&sender, network_join_unique, REJECT_FULL);
return 0;
}
if(i==MAXNETNODES) i=j;
memmove(&netnodes[i].netname.sin_addr.s_addr,
&sender.sin_addr.s_addr,4);
memmove(&netnodes[i].netname.sin_port,
&sender.sin_port,2);
netnodes[i].netname.sin_family=AF_INET;
netnodes[i].used=1;
memcpy(netnodes[i].name,mesg+9,16);
if (i == MAXNETNODES) i = j;
memmove(&netnodes[i].netname.sin_addr.s_addr, &sender.sin_addr.s_addr, 4);
memmove(&netnodes[i].netname.sin_port, &sender.sin_port, 2);
netnodes[i].netname.sin_family = AF_INET;
netnodes[i].used = 1;
memcpy(netnodes[i].name, mesg + 9, 16);
netnodes[i].name[15] = '\0';
send_accept(network_join_unique);
@ -597,18 +575,20 @@ int handle_joins() {
/* Client side */
static int read_inform(unsigned char** pbuf, int *psize) {
unsigned char *buf = *pbuf;
static int read_inform(unsigned char** pbuf, int* psize) {
unsigned char* buf = *pbuf;
int size = *psize;
int i;
if (size < 1) return 0;
myslot = *buf++; size--;
myslot = *buf++;
size--;
if (0xff == myslot) return 1;
if (size < 1) return 0;
while (0xff != *buf) {
i = *buf++; size--;
i = *buf++;
size--;
if (size < 17 || i >= MAXNETNODES) return 0;
netnodes[i].used = 1;
memcpy(netnodes[i].name, buf, 16);
@ -634,16 +614,16 @@ static void read_config(unsigned char* buf) {
/* returns 0=ignore packet,1=we're rejected,2=INVITE/CONFIG,3=BEGIN */
int scaninvite(int msec) {
int size;
unsigned char *take;
unsigned char* take;
size = getmsg(msec);
if (size < 6) return 0;
if (*mesg!=PKT_INVITE && *mesg!=PKT_BEGIN && *mesg!=PKT_CONFIG) return 0;
if (*mesg != PKT_INVITE && *mesg != PKT_BEGIN && *mesg != PKT_CONFIG) return 0;
if (!isvalidmsg_from_master()) return 0;
if (!isvalidunique(mesg+1)) return 0;
if (!isvalidunique(mesg + 1)) return 0;
take = mesg+5;
take = mesg + 5;
size -= 5;
switch (*mesg) {
@ -666,35 +646,35 @@ int scaninvite(int msec) {
}
}
int send_join(struct sockaddr_in *netname, char playername[16]) {
int send_join(struct sockaddr_in* netname, char playername[16]) {
int size;
long now;
Uint32 join_unique = gtime();
unsigned char *buf;
unsigned char* buf;
mastername = *netname;
*regpacket=PKT_JOIN;
*regpacket = PKT_JOIN;
write_version(regpacket + 1);
writeuint32(regpacket+5, join_unique);
memcpy(regpacket+9, playername, 16);
now=longtime();
putmsg(&mastername,regpacket,1+4+4+16);
while(longtime()-now < 3) {
writeuint32(regpacket + 5, join_unique);
memcpy(regpacket + 9, playername, 16);
now = longtime();
putmsg(&mastername, regpacket, 1 + 4 + 4 + 16);
while (longtime() - now < 3) {
if (0 == (size = getmsg(1000))) {
/* got no message, send join again */
putmsg(&mastername,regpacket,1+4+4+16);
putmsg(&mastername, regpacket, 1 + 4 + 4 + 16);
continue;
}
if (size < 5) continue;
if (readuint32(mesg+1) != join_unique) continue;
if (readuint32(mesg + 1) != join_unique) continue;
switch (*mesg) {
case PKT_ACCEPT:
if (size < 1+4+132+4+2) continue;
if (size < 1 + 4 + 132 + 4 + 2) continue;
read_seed_unique(mesg + 5);
read_config(mesg+137);
buf = mesg+141;
read_config(mesg + 137);
buf = mesg + 141;
size -= 141;
if (!read_inform(&buf,&size)) return 0;
if (!read_inform(&buf, &size)) return 0;
return 2;
case PKT_REJECT:
if (size < 10 || mesg[9] > _REJECT_LAST) {
@ -716,14 +696,13 @@ void send_quit() {
int size;
*regpacket = PKT_QUIT;
write_unique(regpacket+1);
now=longtime();
while(longtime()-now<10) {
putmsg(&mastername,regpacket,5);
size=getmsg(1000);
if(size<6) continue;
if(mesg[0] != PKT_ACK || mesg[1] != PKT_QUIT) continue;
if (isvalidunique(mesg+2))
break;
write_unique(regpacket + 1);
now = longtime();
while (longtime() - now < 10) {
putmsg(&mastername, regpacket, 5);
size = getmsg(1000);
if (size < 6) continue;
if (mesg[0] != PKT_ACK || mesg[1] != PKT_QUIT) continue;
if (isvalidunique(mesg + 2)) break;
}
}

View File

@ -1,13 +1,11 @@
#ifndef NETWORK_H
#define NETWORK_H
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
enum {
MAXNETNODES = 8
};
enum { MAXNETNODES = 8 };
struct netnode {
struct sockaddr_in netname;
@ -22,7 +20,7 @@ extern struct netnode netnodes[MAXNETNODES];
void getsocket(void);
void freesocket(void);
int send_join(struct sockaddr_in *netname, char playername[16]);
int send_join(struct sockaddr_in* netname, char playername[16]);
void send_config();
void send_quit();
int scaninvite(int msec);
@ -45,8 +43,8 @@ int networktraffic(void);
extern int mydatacount;
extern int myslot;
extern int actionput,actioncount;
extern unsigned char actionblock[ACTIONHIST*MAXNETNODES];
extern int actionput, actioncount;
extern unsigned char actionblock[ACTIONHIST * MAXNETNODES];
extern int myaction;
extern unsigned char actions[MAXNETNODES];

View File

@ -1,9 +1,9 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <SDL_audio.h>
#include <SDL_error.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sound.h"
@ -13,16 +13,16 @@
int sound_enabled = 1;
static char dirlist[]=DATADIR;
static char dirlist[] = DATADIR;
static int readsound(int num);
#define NUMSOUNDS ((int)(sizeof(soundnames)/sizeof(char*)))
#define NUMSOUNDS ((int) (sizeof(soundnames) / sizeof(char*)))
#define MIXMAX 16
#define SOUND_QUIET -1
static const char *soundnames[] = {
static const char* soundnames[] = {
"bomb1.raw",
"power1.raw",
"death.raw",
@ -31,9 +31,8 @@ static const char *soundnames[] = {
"power2.raw",
};
typedef struct sample
{
char *data;
typedef struct sample {
char* data;
int len;
} sample;
@ -41,151 +40,140 @@ typedef struct sample
static sample samples[NUMSOUNDS];
static int soundworking=0;
static int soundworking = 0;
static int fragment;
// static int soundwrite,soundread;
static int *soundbuffer;
static int* soundbuffer;
static int soundbufferlen;
static unsigned char sndclip[8192];
#define MAXSOUNDCOMMANDS 32
static char soundcommands[MAXSOUNDCOMMANDS];
static int soundtake,soundput;
static int soundtake, soundput;
static int sndplaying[MIXMAX],sndposition[MIXMAX];
static void fillaudio(void *udata,Uint8 *buffer,int len)
{
char com,*p;
int i,j,*ip;
int which;
(void)udata;
static int sndplaying[MIXMAX], sndposition[MIXMAX];
static void fillaudio(void* udata, Uint8* buffer, int len) {
char com, *p;
int i, j, *ip;
int which;
(void) udata;
while(soundtake!=soundput)
{
com=soundcommands[soundtake];
soundtake=(soundtake+1)&(MAXSOUNDCOMMANDS-1);
if(com==SOUND_QUIET) {memset(sndposition,0,sizeof(sndposition));continue;}
if(com<NUMSOUNDS)
{
for(i=0;i<MIXMAX;++i)
if(!sndposition[i])
{
sndposition[i]=1;
sndplaying[i]=com;
while (soundtake != soundput) {
com = soundcommands[soundtake];
soundtake = (soundtake + 1) & (MAXSOUNDCOMMANDS - 1);
if (com == SOUND_QUIET) {
memset(sndposition, 0, sizeof(sndposition));
continue;
}
if (com < NUMSOUNDS) {
for (i = 0; i < MIXMAX; ++i)
if (!sndposition[i]) {
sndposition[i] = 1;
sndplaying[i] = com;
break;
}
}
}
memset(soundbuffer,0,soundbufferlen);
for(i=0;i<MIXMAX;++i)
{
if(!sndposition[i]) continue;
which=sndplaying[i];
if(sndposition[i]==samples[which].len)
{
sndposition[i]=0;
memset(soundbuffer, 0, soundbufferlen);
for (i = 0; i < MIXMAX; ++i) {
if (!sndposition[i]) continue;
which = sndplaying[i];
if (sndposition[i] == samples[which].len) {
sndposition[i] = 0;
continue;
}
p=samples[which].data;
if(!p) continue;
p+=len*(sndposition[i]++ -1);
ip=soundbuffer;
j=len;
while(j--) *ip++ += *p++;
p = samples[which].data;
if (!p) continue;
p += len * (sndposition[i]++ - 1);
ip = soundbuffer;
j = len;
while (j--) *ip++ += *p++;
}
j=len;
ip=soundbuffer;;
while(j--) *buffer++ = sndclip[4096+*ip++];
j = len;
ip = soundbuffer;
;
while (j--) *buffer++ = sndclip[4096 + *ip++];
}
int soundopen(void) {
SDL_AudioSpec wanted;
int i,j;
SDL_AudioSpec wanted;
int i, j;
soundtake=soundput=0;
memset(sndposition,0,sizeof(sndposition));
memset(sndplaying,0,sizeof(sndplaying));
fragment=SNDFRAGMENT<<1;
soundbufferlen=fragment*sizeof(int);
soundbuffer=malloc(soundbufferlen);
if(!soundbuffer) return -2;
soundtake = soundput = 0;
memset(sndposition, 0, sizeof(sndposition));
memset(sndplaying, 0, sizeof(sndplaying));
fragment = SNDFRAGMENT << 1;
soundbufferlen = fragment * sizeof(int);
soundbuffer = malloc(soundbufferlen);
if (!soundbuffer) return -2;
memset(&wanted,0,sizeof(wanted));
wanted.freq=22050;
wanted.channels=2;
wanted.format=AUDIO_U8;
wanted.samples=fragment>>1;
wanted.callback=fillaudio;
wanted.userdata=0;
memset(&wanted, 0, sizeof(wanted));
wanted.freq = 22050;
wanted.channels = 2;
wanted.format = AUDIO_U8;
wanted.samples = fragment >> 1;
wanted.callback = fillaudio;
wanted.userdata = 0;
if(SDL_OpenAudio(&wanted,0)<0)
{
fprintf(stderr,"Couldn't open audio: %s\n",SDL_GetError());
if (SDL_OpenAudio(&wanted, 0) < 0) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return -1;
}
soundworking=1;
soundworking = 1;
for(i=0;i<8192;i++)
{
j=i-4096;
sndclip[i]=j > 127 ? 255 : (j<-128 ? 0 : j+128);
for (i = 0; i < 8192; i++) {
j = i - 4096;
sndclip[i] = j > 127 ? 255 : (j < -128 ? 0 : j + 128);
}
for(i=0;i<NUMSOUNDS;++i)
readsound(i);
for (i = 0; i < NUMSOUNDS; ++i) readsound(i);
SDL_PauseAudio(0);
return 0;
}
void soundclose(void) {
if(soundworking)
{
if (soundworking) {
SDL_CloseAudio();
soundworking=0;
soundworking = 0;
}
}
int readsound(int num) {
char name[256],*p1,*p2,ch;
int file,size,len;
p1=dirlist;
for(;;)
{
p2=name;
while(*p1 && (ch=*p1++)!=',')
*p2++=ch;
if(p2>name && p2[-1]!='/') *p2++='/';
strcpy(p2,soundnames[num]);
file=open(name,O_RDONLY);
if(file>=0) break;
if(!*p1)
{
samples[num].len=-1;
char name[256], *p1, *p2, ch;
int file, size, len;
p1 = dirlist;
for (;;) {
p2 = name;
while (*p1 && (ch = *p1++) != ',') *p2++ = ch;
if (p2 > name && p2[-1] != '/') *p2++ = '/';
strcpy(p2, soundnames[num]);
file = open(name, O_RDONLY);
if (file >= 0) break;
if (!*p1) {
samples[num].len = -1;
return 0;
}
}
size=lseek(file,0,SEEK_END);
lseek(file,0,SEEK_SET);
len=samples[num].len=(size+fragment-1)/fragment;
len*=fragment;
p1=samples[num].data=malloc(len);
if(p1)
{
read(file,p1,size);
if(len-size) memset(p1+size,0,len-size);
while(size--) *p1++ ^= 0x80;
size = lseek(file, 0, SEEK_END);
lseek(file, 0, SEEK_SET);
len = samples[num].len = (size + fragment - 1) / fragment;
len *= fragment;
p1 = samples[num].data = malloc(len);
if (p1) {
read(file, p1, size);
if (len - size) memset(p1 + size, 0, len - size);
while (size--) *p1++ ^= 0x80;
} else
samples[num].data=0;
samples[num].data = 0;
close(file);
return 0;
}
void playsound(int n) {
if (sound_enabled) {
soundcommands[soundput]=n;
soundput=(soundput+1)&(MAXSOUNDCOMMANDS-1);
soundcommands[soundput] = n;
soundput = (soundput + 1) & (MAXSOUNDCOMMANDS - 1);
}
}

View File

@ -1,17 +1,17 @@
#include "bomber.h"
#include "utils.h"
#include "bomber.h"
#include "gfx.h"
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int volatile hc=0;
char volatile interrupted=0;
int volatile hc = 0;
char volatile interrupted = 0;
static Uint32 cur_unique;
Uint32 network_unique;
@ -20,12 +20,12 @@ Uint32 gtime(void) {
}
Uint32 longtime(void) {
return gtime()/1000;
return gtime() / 1000;
}
/* surf random generator: x (Daniel J. Bernstein) */
#define ROT(x, b) (((x) << (b)) | ((x) >> (32-(b))))
#define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROT(x,b))
#define ROT(x, b) (((x) << (b)) | ((x) >> (32 - (b))))
#define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROT(x, b))
static void surf(Uint32 out[8], const Uint32 in[12], const Uint32 seed[32]) {
Uint32 t[12], x, sum = 0;
int r, i, loop;
@ -35,11 +35,20 @@ static void surf(Uint32 out[8], const Uint32 in[12], const Uint32 seed[32]) {
for (loop = 0; loop < 2; ++loop) {
for (r = 0; r < 16; ++r) {
sum += 0x9e3779b9;
MUSH(0, 5); MUSH(1, 7); MUSH(2, 9); MUSH(3, 13);
MUSH(4, 5); MUSH(5, 7); MUSH(6, 9); MUSH(7, 13);
MUSH(8, 5); MUSH(9, 7); MUSH(10, 9); MUSH(11, 13);
MUSH(0, 5);
MUSH(1, 7);
MUSH(2, 9);
MUSH(3, 13);
MUSH(4, 5);
MUSH(5, 7);
MUSH(6, 9);
MUSH(7, 13);
MUSH(8, 5);
MUSH(9, 7);
MUSH(10, 9);
MUSH(11, 13);
}
for (i = 0; i < 8; ++i) out[i] ^= t[i+4];
for (i = 0; i < 8; ++i) out[i] ^= t[i + 4];
}
}
#undef ROT
@ -62,10 +71,14 @@ static Uint32 surf_init(void) {
if (-1 == fd) {
Uint32 genseed[4][8];
surf_seed[0] = surf_seed[1] = gtime();
surf_in[0]++; surf(genseed[0], surf_in, surf_seed);
surf_in[0]++; surf(genseed[1], surf_in, surf_seed);
surf_in[0]++; surf(genseed[2], surf_in, surf_seed);
surf_in[0]++; surf(genseed[3], surf_in, surf_seed);
surf_in[0]++;
surf(genseed[0], surf_in, surf_seed);
surf_in[0]++;
surf(genseed[1], surf_in, surf_seed);
surf_in[0]++;
surf(genseed[2], surf_in, surf_seed);
surf_in[0]++;
surf(genseed[3], surf_in, surf_seed);
memcpy(surf_seed, genseed[0], 32);
memcpy(surf_seed, genseed[1], 32);
memcpy(surf_seed, genseed[2], 32);
@ -86,14 +99,15 @@ static Uint32 surf_init(void) {
static Uint32 surf_random(void) {
if (surf_left == 0) {
int i;
for (i = 0; (i < 12) && !(++surf_in[i]); i++) ;
for (i = 0; (i < 12) && !(++surf_in[i]); i++)
;
surf_left = 8;
surf(surf_out, surf_in, surf_seed);
}
return surf_out[--surf_left];
}
void read_seed_unique(unsigned char *buf) {
void read_seed_unique(unsigned char* buf) {
int i;
memset(surf_in, 0, sizeof(surf_in));
@ -101,12 +115,12 @@ void read_seed_unique(unsigned char *buf) {
surf_left = 0;
memcpy(&surf_seed, buf, sizeof(surf_seed));
memcpy(&network_unique, buf+sizeof(surf_seed), sizeof(network_unique));
memcpy(&network_unique, buf + sizeof(surf_seed), sizeof(network_unique));
cur_unique = ntohl(network_unique);
for (i = 0; i < 32; i++) surf_seed[i] = ntohl(surf_seed[i]);
}
unsigned char* write_seed_unique(unsigned char *buf) {
unsigned char* write_seed_unique(unsigned char* buf) {
int i;
for (i = 0; i < 32; i++) {
@ -200,34 +214,34 @@ Uint32 get_unique(void) {
}
#endif
void nomem(char *str) {
printf("No memory!!![%s]\n",str);
void nomem(char* str) {
printf("No memory!!![%s]\n", str);
exit(1);
}
int mypause(void) {
while(!interrupted) {
while (!interrupted) {
pollinput();
SDL_Delay(1);
}
interrupted=0;
interrupted = 0;
return 1;
}
static Uint32 sdlhandler(Uint32 time) {
#if defined (SDL_LATENCY)
#if defined(SDL_LATENCY)
outmsgs();
#endif
interrupted=1;
interrupted = 1;
hc++;
return time;
}
void pulseon(void) {
SDL_SetTimer(40,sdlhandler);
SDL_SetTimer(40, sdlhandler);
}
void hexdump(unsigned char *p, int len) {
void hexdump(unsigned char* p, int len) {
int i;
for (i = 0; i < len; i++) {
if (15 == len % 16)
@ -235,6 +249,5 @@ void hexdump(unsigned char *p, int len) {
else
fprintf(stderr, "0x%X ", p[i]);
}
if (i % 16)
fprintf(stderr, "\n");
if (i % 16) fprintf(stderr, "\n");
}

View File

@ -1,6 +1,8 @@
#ifndef UTILS_H
#define UTILS_H
#include <SDL.h>
Uint32 gtime(void);
Uint32 longtime(void);
@ -10,21 +12,21 @@ void set_unique(Uint32 unique);
Uint32 get_unique(void);
#endif
#define SEED_UNIQUE_SIZE (32*4+4)
void read_seed_unique(unsigned char *buf);
unsigned char* write_seed_unique(unsigned char *buf);
#define SEED_UNIQUE_SIZE (32 * 4 + 4)
void read_seed_unique(unsigned char* buf);
unsigned char* write_seed_unique(unsigned char* buf);
void create_seed_unique(void);
extern Uint32 network_unique;
int myrand(void);
void nomem(char *str);
void nomem(char* str);
int mypause(void);
void pulseon(void);
void hexdump(unsigned char *p, int len);
void hexdump(unsigned char* p, int len);
extern volatile int hc;
extern volatile char interrupted;