From 56458af5fc07585c47d3969120624ad05760101a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Wed, 15 Sep 2021 19:20:06 +0200 Subject: [PATCH] add clang-format config and format everything --- .clang-format | 29 ++ src/announce.c | 198 ++++---- src/announce.h | 2 +- src/bomber.c | 34 +- src/bomber.h | 75 ++- src/draw.c | 446 ++++++++--------- src/draw.h | 17 +- src/game.c | 813 +++++++++++++++---------------- src/game.h | 14 +- src/gfx.c | 1261 +++++++++++++++++++++++++----------------------- src/gfx.h | 56 +-- src/list.c | 32 +- src/list.h | 39 +- src/matcher.c | 453 ++++++++--------- src/menu.c | 247 +++++----- src/network.c | 359 +++++++------- src/network.h | 14 +- src/sound.c | 202 ++++---- src/utils.c | 73 +-- src/utils.h | 12 +- 20 files changed, 2193 insertions(+), 2183 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..f053cb9 --- /dev/null +++ b/.clang-format @@ -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 +... diff --git a/src/announce.c b/src/announce.c index 226c6cc..6f11796 100644 --- a/src/announce.c +++ b/src/announce.c @@ -2,12 +2,12 @@ #include "announce.h" #include "network.h" -#include +#include #include #include -#include -#include #include +#include +#include #include #include @@ -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,32 +37,31 @@ 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); - avahi_free(name); - name = n; - } + case AVAHI_ENTRY_GROUP_COLLISION: { + char* n = avahi_alternative_service_name(name); + avahi_free(name); + name = n; + } /* And recreate the services */ avahi_entry_group_reset(group); 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); - avahi_free(name); - name = n; - } +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 */ @@ -200,75 +206,89 @@ static void resolve_callback(AvahiServiceResolver *r, AvahiIfIndex interface, Av /* Called whenever a service has been resolved successfully or timed out */ 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)))); - break; + 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)))); + break; - case AVAHI_RESOLVER_FOUND: { - gamelistentry *ge; - unsigned int version; - int have_version = 0; - 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 */ - have_version = 1; - } + case AVAHI_RESOLVER_FOUND: { + gamelistentry* ge; + unsigned int version; + int have_version = 0; + 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 */ + have_version = 1; } - if (!have_version) goto done; - remove_game_with_name(name); - i = buffer_glsize++; - ge = &buffer_glentries[i]; - buffer_glchanged[i] = 1; - memset(ge, 0, sizeof(*ge)); - ge->netname.sin_addr.s_addr = address->data.ipv4.address; - ge->netname.sin_family = AF_INET; - ge->netname.sin_port = port; - strncpy(ge->name, name, 15); } + if (!have_version) goto done; + remove_game_with_name(name); + i = buffer_glsize++; + ge = &buffer_glentries[i]; + buffer_glchanged[i] = 1; + memset(ge, 0, sizeof(*ge)); + ge->netname.sin_addr.s_addr = address->data.ipv4.address; + ge->netname.sin_family = AF_INET; + ge->netname.sin_port = port; + strncpy(ge->name, name, 15); + } } 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); /* Called whenever a new services becomes available on the LAN or is removed from the LAN */ switch (event) { - case AVAHI_BROWSER_FAILURE: + case AVAHI_BROWSER_FAILURE: - fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(client))); - avahi_threaded_poll_quit(threaded_poll); - return; + fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(client))); + avahi_threaded_poll_quit(threaded_poll); + return; - case AVAHI_BROWSER_NEW: - /* fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain); */ + case AVAHI_BROWSER_NEW: + /* fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain); */ - /* We ignore the returned resolver object. In the callback - function we free it. If the server is terminated before - the callback function is called the server will free - the resolver for us. */ + /* We ignore the returned resolver object. In the callback + function we free it. If the server is terminated before + the callback function is called the server will free + the resolver for us. */ - if (!(avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, userdata))) - fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(client))); + if (!(avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, userdata))) + fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(client))); - break; + break; - case AVAHI_BROWSER_REMOVE: - remove_game_with_name(name); - break; + case AVAHI_BROWSER_REMOVE: + remove_game_with_name(name); + break; - case AVAHI_BROWSER_ALL_FOR_NOW: - break; - case AVAHI_BROWSER_CACHE_EXHAUSTED: - break; + case AVAHI_BROWSER_ALL_FOR_NOW: + break; + case AVAHI_BROWSER_CACHE_EXHAUSTED: + break; } } diff --git a/src/announce.h b/src/announce.h index e264242..f06a5e7 100644 --- a/src/announce.h +++ b/src/announce.h @@ -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); diff --git a/src/bomber.c b/src/bomber.c index 134060a..27a3685 100644 --- a/src/bomber.c +++ b/src/bomber.c @@ -1,31 +1,31 @@ +#include +#include +#include +#include #include #include -#include #include -#include -#include #include #include -#include -#include -#include +#include +#include -#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(); diff --git a/src/bomber.h b/src/bomber.h index 86d9b39..cebddfd 100644 --- a/src/bomber.h +++ b/src/bomber.h @@ -1,7 +1,8 @@ #ifndef BOMBER_H #define BOMBER_H -#include "SDL.h" +#include "list.h" +#include 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 @@ -85,25 +81,25 @@ typedef struct player { typedef struct sprite { - int flags; - int xpos,ypos; - figure *fig; + int flags; + 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 { @@ -175,10 +171,10 @@ enum tile_types { // #define ACT_INVALID 0x88 -#define ACT_NONE 0 -#define ACT_UP 1 -#define ACT_DOWN 2 -#define ACT_LEFT 3 +#define ACT_NONE 0 +#define ACT_UP 1 +#define ACT_DOWN 2 +#define ACT_LEFT 3 #define ACT_RIGHT 4 #define ACT_ENTER 5 #define ACT_QUIT 6 @@ -200,7 +196,6 @@ enum tile_types { #define CODE_ALLDEAD 2 - #define MAXTHINGS 500 #define MAXSETS 8 #define MAXSPRITES 256 diff --git a/src/draw.c b/src/draw.c index 055b89d..2176a88 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,15 +1,15 @@ +#include +#include +#include +#include #include #include -#include #include -#include -#include #include #include -#include -#include -#include +#include +#include #include "bomber.h" #include "draw.h" @@ -48,191 +48,206 @@ 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;igs_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,...) { // char output[256],*p,*p2; // va_list ap; -// +// // va_start(ap, str); -// +// // vsprintf(output,str,ap); // p=output; // for(;;) { @@ -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;ixpos,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;ifig; + 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;ifig; + 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<= 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(); diff --git a/src/draw.h b/src/draw.h index ee602e9..50d88b3 100644 --- a/src/draw.h +++ b/src/draw.h @@ -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 diff --git a/src/game.c b/src/game.c index c2b22a8..366fca3 100644 --- a/src/game.c +++ b/src/game.c @@ -1,13 +1,13 @@ -#include "bomber.h" #include "game.h" -#include "gfx.h" -#include "network.h" +#include "bomber.h" #include "draw.h" -#include "utils.h" -#include "sound.h" -#include "menu.h" +#include "gfx.h" #include "list.h" +#include "menu.h" +#include "network.h" +#include "sound.h" +#include "utils.h" static int gameframe; @@ -25,7 +25,7 @@ solid background, backgroundoriginal; /* The playfield array, contains FIELD_* equates */ unsigned char field[32][32]; -void *info[32][32]; +void* info[32][32]; volatile char exitflag = 0; static int framecount = 0; @@ -35,61 +35,46 @@ char playername[16]; static int mycount; static int bonustotal; -static const int bonuschances[]= { -TILE_BOMB,20, -TILE_FLAME,20, -TILE_TRIGGER,2, -TILE_GOLDFLAME,2, -TILE_SKATES,20, -TILE_TURTLE,5, -TILE_NONE,160 -}; +static const int bonuschances[] = {TILE_BOMB, 20, TILE_FLAME, 20, TILE_TRIGGER, 2, TILE_GOLDFLAME, 2, TILE_SKATES, 20, TILE_TURTLE, 5, TILE_NONE, 160}; static GameOptions gameoptions; -static const unsigned char playerpositions[MAXNETNODES*3] = { /* color, x, y */ -2,0,0, -3,14,10, -4,14,0, -5,0,10, -1,6,0, -6,8,10, -7,0,6, -8,14,4, +static const unsigned char playerpositions[MAXNETNODES * 3] = { + /* color, x, y */ + 2, 0, 0, 3, 14, 10, 4, 14, 0, 5, 0, 10, 1, 6, 0, 6, 8, 10, 7, 0, 6, 8, 14, 4, }; -static void resetplayer(player *pl, int color, int x, int y) { - pl->speed=SPEEDSTART; - pl->flags=0; - pl->flamelength=gameoptions.flames+1; - pl->bombsavailable=gameoptions.bombs+1; +static void resetplayer(player* pl, int color, int x, int y) { + pl->speed = SPEEDSTART; + pl->flags = 0; + pl->flamelength = gameoptions.flames + 1; + pl->bombsavailable = gameoptions.bombs + 1; - pl->color=color; + pl->color = color; - pl->xpos=arraytoscreenx(x); - pl->ypos=arraytoscreeny(y); + pl->xpos = arraytoscreenx(x); + pl->ypos = arraytoscreeny(y); - field[y][x]=FIELD_EMPTY; - if(x) field[y][x-1]=FIELD_EMPTY; - if(y) field[y-1][x]=FIELD_EMPTY; - if(xlist_all_players); - pl->controller=controller; - pl->fixx=-4; - pl->fixy=-40; + pl->controller = controller; + pl->fixx = -4; + pl->fixy = -40; pl->kills = pl->deaths = 0; resetplayer(pl, color, x, y); @@ -97,33 +82,33 @@ static void initplayer(int color,int x,int y,int controller) { static void initplayers(void) { int i; - const unsigned char *p; - int c,x,y; + const unsigned char* p; + int c, x, y; - if(NETWORK_NONE == network) { - initplayer(2,0,0,-1); + if (NETWORK_NONE == network) { + initplayer(2, 0, 0, -1); return; } - p=playerpositions; - for(i=0;i=comp ? FIELD_BRICK : FIELD_EMPTY; + bonustotal += 64 * (3 - gameoptions.generosity); + memset(field, 0, sizeof(field)); + comp = gameoptions.density; + for (j = 0; j < arraynumy; ++j) + for (i = 0; i < arraynumx; ++i) { + /* if((i&j)&1) { + field[j][i]=FIELD_BORDER; + } else*/ + field[j][i] = (myrand() & 3) >= comp ? FIELD_BRICK : FIELD_EMPTY; } - solidcopyany(&backgroundoriginal,&background,0,0,IXSIZE,IYSIZE); + solidcopyany(&backgroundoriginal, &background, 0, 0, IXSIZE, IYSIZE); resetplayers(); - for(j=0;jpx=px; - fl->py=py; - fl->xpos=arraytoscreenx(px); - fl->ypos=arraytoscreeny(py); - fl->owner=owner; - if(px && field[py][px-1]==FIELD_FLAME) { - fl2=info[py][px-1]; - fl->lurd|=FL_LEFT; - fl2->lurd|=FL_RIGHT; + if (!fl) return; + addtail(&activeflames, fl); + field[py][px] = FIELD_FLAME; + info[py][px] = fl; + fl->px = px; + fl->py = py; + fl->xpos = arraytoscreenx(px); + fl->ypos = arraytoscreeny(py); + fl->owner = owner; + if (px && field[py][px - 1] == FIELD_FLAME) { + fl2 = info[py][px - 1]; + fl->lurd |= FL_LEFT; + fl2->lurd |= FL_RIGHT; } - if(py && field[py-1][px]==FIELD_FLAME) { - fl2=info[py-1][px]; - fl->lurd|=FL_UP; - fl2->lurd|=FL_DOWN; + if (py && field[py - 1][px] == FIELD_FLAME) { + fl2 = info[py - 1][px]; + fl->lurd |= FL_UP; + fl2->lurd |= FL_DOWN; } - if(pxlurd|=FL_RIGHT; - fl2->lurd|=FL_LEFT; + if (px < arraynumx - 1 && field[py][px + 1] == FIELD_FLAME) { + fl2 = info[py][px + 1]; + fl->lurd |= FL_RIGHT; + fl2->lurd |= FL_LEFT; } - if(pylurd|=FL_DOWN; - fl2->lurd|=FL_UP; + if (py < arraynumy - 1 && field[py + 1][px] == FIELD_FLAME) { + fl2 = info[py + 1][px]; + fl->lurd |= FL_DOWN; + fl2->lurd |= FL_UP; } } -static void dropbomb(player *pl,int px,int py,int type){ - bomb *bmb; +static void dropbomb(player* pl, int px, int py, int type) { + bomb* bmb; - if(field[py][px]!=FIELD_EMPTY) return; + if (field[py][px] != FIELD_EMPTY) return; bmb = allocentry(bomb); - if(!bmb) return; + if (!bmb) return; playsound(3); - addtail(&activebombs,bmb); + addtail(&activebombs, bmb); --(pl->bombsavailable); - field[py][px]=FIELD_BOMB; - info[py][px]=bmb; - bmb->type=type; - bmb->px=px; - bmb->py=py; - bmb->xpos=arraytoscreenx(px); - bmb->ypos=arraytoscreeny(py); - bmb->power=pl->flamelength; - bmb->owner=pl; + field[py][px] = FIELD_BOMB; + info[py][px] = bmb; + bmb->type = type; + bmb->px = px; + bmb->py = py; + bmb->xpos = arraytoscreenx(px); + bmb->ypos = arraytoscreeny(py); + bmb->power = pl->flamelength; + bmb->owner = pl; } -static void adddecay(int px,int py) { - brickdecay *bd; - int xpos,ypos; +static void adddecay(int px, int py) { + brickdecay* bd; + int xpos, ypos; bd = allocentry(brickdecay); - if(!bd) return; - field[py][px]=FIELD_EXPLODING; - bd->xpos=arraytoscreenx(px); - bd->ypos=arraytoscreeny(py); - bd->px=px; - bd->py=py; - addtail(&activedecays,bd); - xpos=tovideox(bd->xpos); - ypos=tovideoy(bd->ypos); - solidcopyany(&backgroundoriginal,&background,xpos,ypos, - arrayspacex,arrayspacey); - solidcopy(&background,xpos,ypos,arrayspacex,arrayspacey); + if (!bd) return; + field[py][px] = FIELD_EXPLODING; + bd->xpos = arraytoscreenx(px); + bd->ypos = arraytoscreeny(py); + bd->px = px; + bd->py = py; + addtail(&activedecays, bd); + xpos = tovideox(bd->xpos); + ypos = tovideoy(bd->ypos); + solidcopyany(&backgroundoriginal, &background, xpos, ypos, arrayspacex, arrayspacey); + solidcopy(&background, xpos, ypos, arrayspacex, arrayspacey); } -static void addbonus(int px,int py,int type) { - bonustile *bonus; +static void addbonus(int px, int py, int type) { + bonustile* bonus; bonus = allocentry(bonustile); - if(!bonus) return; - addtail(&activebonus,bonus); - bonus->px=px; - bonus->py=py; - bonus->xpos=arraytoscreenx(px); - bonus->ypos=arraytoscreeny(py); - bonus->type=type; - field[py][px]=FIELD_BONUS; - info[py][px]=bonus; + if (!bonus) return; + addtail(&activebonus, bonus); + bonus->px = px; + bonus->py = py; + bonus->xpos = arraytoscreenx(px); + bonus->ypos = arraytoscreeny(py); + bonus->type = type; + field[py][px] = FIELD_BONUS; + info[py][px] = bonus; } -static void trybonus(int px,int py) { - int i=0, r; - const int *p; +static void trybonus(int px, int py) { + int i = 0, r; + const int* p; - if(field[py][px]!=FIELD_EMPTY) return; - p=bonuschances; - r=myrand()%bonustotal; - while(r>=0) { - i=*p++; - r-=*p++; + if (field[py][px] != FIELD_EMPTY) return; + p = bonuschances; + r = myrand() % bonustotal; + while (r >= 0) { + i = *p++; + r -= *p++; } - if(i==TILE_NONE) return; - addbonus(px,py,i); + if (i == TILE_NONE) return; + addbonus(px, py, i); } -static void deletebonus(bonustile *bonus) { - int px,py; +static void deletebonus(bonustile* bonus) { + int px, py; - px=bonus->px; - py=bonus->py; - field[py][px]=0; - info[py][px]=0; + px = bonus->px; + py = bonus->py; + field[py][px] = 0; + info[py][px] = 0; list_del(&bonus->list); freeentry(bonus); } -static void adddetonate(bomb *bmb) { - if (bmb->type==BOMB_OFF) return; +static void adddetonate(bomb* bmb) { + if (bmb->type == BOMB_OFF) return; bmb->type = BOMB_OFF; field[bmb->py][bmb->px] = FIELD_EXPLODING; @@ -336,10 +321,9 @@ static void processbombs() { list_for_each_entry_safe(bmb, next, &activebombs, list) { ++(bmb->figcount); ++bmb->timer; - switch(bmb->type) { + switch (bmb->type) { case BOMB_NORMAL: - if (bmb->timer == BOMBLIFE) - adddetonate(bmb); + if (bmb->timer == BOMBLIFE) adddetonate(bmb); break; case BOMB_CONTROLLED: if (bmb->timer == BOMB_CONTROLLED_LIFE) { @@ -351,70 +335,70 @@ static void processbombs() { } } -static void flameshaft(player *owner,int px,int py,int dx,int dy,int power) { +static void flameshaft(player* owner, int px, int py, int dx, int dy, int power) { int there; - bomb *bmb; + bomb* bmb; - while(power--) { - px+=dx; - py+=dy; - if(px<0 || py<0 || px>=arraynumx || py>=arraynumy) break; - there=field[py][px]; - switch(there) { + while (power--) { + px += dx; + py += dy; + if (px < 0 || py < 0 || px >= arraynumx || py >= arraynumy) break; + there = field[py][px]; + switch (there) { case FIELD_BOMB: - bmb=info[py][px]; + bmb = info[py][px]; adddetonate(bmb); break; case FIELD_EMPTY: - addflame(owner,px,py); + addflame(owner, px, py); break; case FIELD_BRICK: - adddecay(px,py); - power=0; + adddecay(px, py); + power = 0; break; case FIELD_BONUS: deletebonus(info[py][px]); - power=0; + power = 0; break; case FIELD_BORDER: case FIELD_EXPLODING: default: - power=0; + power = 0; case FIELD_FLAME: break; } } } -static void detonatebomb(bomb *bmb) { - int px,py; +static void detonatebomb(bomb* bmb) { + int px, py; int power; - player *owner; + player* owner; ++(bmb->owner->bombsavailable); - px=bmb->px; - py=bmb->py; - power=bmb->power; - owner=bmb->owner; + px = bmb->px; + py = bmb->py; + power = bmb->power; + owner = bmb->owner; list_del(&bmb->list); freeentry(bmb); - addflame(owner,px,py); - flameshaft(owner,px,py,-1,0,power); - flameshaft(owner,px,py,0,-1,power); - flameshaft(owner,px,py,1,0,power); - flameshaft(owner,px,py,0,1,power); + addflame(owner, px, py); + flameshaft(owner, px, py, -1, 0, power); + flameshaft(owner, px, py, 0, -1, power); + flameshaft(owner, px, py, 1, 0, power); + flameshaft(owner, px, py, 0, 1, power); } static void dodetonations(void) { int i = 0; - bomb *bmb; + bomb* bmb; while (!list_empty(&detonatebombs)) { bmb = (bomb*) detonatebombs.next; ++i; detonatebomb(bmb); } - if(i) playsound((myrand()&1) ? 0 : 4); + if (i) playsound((myrand() & 1) ? 0 : 4); } static void processflames(void) { @@ -423,9 +407,9 @@ static void processflames(void) { list_for_each_entry_safe(fl, next, &activeflames, list) { ++(fl->timer); - if(fl->timer==FLAMELIFE) { - field[fl->py][fl->px]=FIELD_EMPTY; - info[fl->py][fl->px]=0; + if (fl->timer == FLAMELIFE) { + field[fl->py][fl->px] = FIELD_EMPTY; + info[fl->py][fl->px] = 0; list_del(&fl->list); freeentry(fl); } @@ -437,7 +421,7 @@ static void processdecays() { list_for_each_entry_safe(bd, next, &activedecays, list) { ++(bd->timer); - if(bd->timer == DECAYLIFE) { + if (bd->timer == DECAYLIFE) { field[bd->py][bd->px] = FIELD_EMPTY; trybonus(bd->px, bd->py); list_del(&bd->list); @@ -448,119 +432,115 @@ static void processdecays() { static void drawbombs(void) { int j; - bomb *bmb; - struct figure *figtab; + bomb* bmb; + struct figure* figtab; int color; - int xpos,ypos; + int xpos, ypos; list_for_each_entry(bmb, &activebombs, list) { - color=bmb->owner->color; - figtab=(bmb->type==BOMB_NORMAL) ? bombs1[color] : bombs2[color]; - j=bmb->figcount % (NUMBOMBFRAMES<<1); - if(j>=NUMBOMBFRAMES) j=(NUMBOMBFRAMES<<1)-j-1; - xpos=tovideox(bmb->xpos); - ypos=tovideoy(bmb->ypos)-3; + color = bmb->owner->color; + figtab = (bmb->type == BOMB_NORMAL) ? bombs1[color] : bombs2[color]; + j = bmb->figcount % (NUMBOMBFRAMES << 1); + if (j >= NUMBOMBFRAMES) j = (NUMBOMBFRAMES << 1) - j - 1; + xpos = tovideox(bmb->xpos); + ypos = tovideoy(bmb->ypos) - 3; - addsprite(xpos,ypos,figtab+j); + addsprite(xpos, ypos, figtab + j); } } static void drawflames(void) { - flame *fl; - int xpos,ypos; + flame* fl; + int xpos, ypos; /* no player specific flame sprites yet */ /* int color; */ int fig; list_for_each_entry(fl, &activeflames, list) { /* color=fl->owner->color; */ - xpos=tovideox(fl->xpos); - ypos=tovideoy(fl->ypos); - fig=(fl->timer*10)/FLAMELIFE; - if(fig>=5) fig=9-fig; - fig+=5*fl->lurd; - addsprite(xpos,ypos,flamefigs[0/* color */]+fig); + xpos = tovideox(fl->xpos); + ypos = tovideoy(fl->ypos); + fig = (fl->timer * 10) / FLAMELIFE; + if (fig >= 5) fig = 9 - fig; + fig += 5 * fl->lurd; + addsprite(xpos, ypos, flamefigs[0 /* color */] + fig); } } static void drawdecays() { - brickdecay *bd; + brickdecay* bd; list_for_each_entry(bd, &activedecays, list) { - addsprite(tovideox(bd->xpos),tovideoy(bd->ypos), - blocksx+(bd->timer*9)/DECAYLIFE); + addsprite(tovideox(bd->xpos), tovideoy(bd->ypos), blocksx + (bd->timer * 9) / DECAYLIFE); } } static void drawbonus() { - bonustile *bonus; + bonustile* bonus; list_for_each_entry(bonus, &activebonus, list) { - addsprite(tovideox(bonus->xpos),tovideoy(bonus->ypos), - tiles+bonus->type); + addsprite(tovideox(bonus->xpos), tovideoy(bonus->ypos), tiles + bonus->type); } } static void drawplayers() { - player *pl; - int xpos,ypos; + player* pl; + int xpos, ypos; list_for_each_entry(pl, &activeplayers, list) { - if(!(pl->flags & FLG_DEAD)) { - if(!pl->figure) - pl->figure=walking[pl->color]+30; - xpos=tovideox(pl->xpos)+pl->fixx; - ypos=tovideoy(pl->ypos)+pl->fixy; - addsprite(xpos,ypos,pl->figure); + if (!(pl->flags & FLG_DEAD)) { + if (!pl->figure) pl->figure = walking[pl->color] + 30; + xpos = tovideox(pl->xpos) + pl->fixx; + ypos = tovideoy(pl->ypos) + pl->fixy; + addsprite(xpos, ypos, pl->figure); } } } -static void detonatecontrolled(player *pl) { +static void detonatecontrolled(player* pl) { bomb *bmb, *next; list_for_each_entry_safe(bmb, next, &activebombs, list) { - if(bmb->owner==pl && bmb->type==BOMB_CONTROLLED) - adddetonate(bmb); + if (bmb->owner == pl && bmb->type == BOMB_CONTROLLED) adddetonate(bmb); } } -static void playonce(generic *gen) { +static void playonce(generic* gen) { if (gen->timer == gen->data1) { list_del(&gen->list); freeentry(gen); } } -static void drawgeneric(generic *gen) { - addsprite(gen->xpos,gen->ypos,((figure *)(gen->ptr1))+gen->timer); +static void drawgeneric(generic* gen) { + addsprite(gen->xpos, gen->ypos, ((figure*) (gen->ptr1)) + gen->timer); } -static void queuesequence(int xpos,int ypos,figure *fig,int count) { - generic *gen; +static void queuesequence(int xpos, int ypos, figure* fig, int count) { + generic* gen; gen = allocentry(generic); - if(!gen) return; - gen->xpos=xpos; - gen->ypos=ypos; - gen->data1=count; - gen->process=playonce; - gen->draw=drawgeneric; - gen->ptr1=fig; - addtail(&activegeneric,gen); + if (!gen) return; + gen->xpos = xpos; + gen->ypos = ypos; + gen->data1 = count; + gen->process = playonce; + gen->draw = drawgeneric; + gen->ptr1 = fig; + addtail(&activegeneric, gen); } -static void adddeath(player *pl) { - int xpos,ypos; +static void adddeath(player* pl) { + int xpos, ypos; - xpos=tovideox(pl->xpos)+pl->fixx-10; - ypos=tovideoy(pl->ypos)+pl->fixy; - queuesequence(xpos,ypos,death,NUMDEATHFRAMES); + xpos = tovideox(pl->xpos) + pl->fixx - 10; + ypos = tovideoy(pl->ypos) + pl->fixy; + queuesequence(xpos, ypos, death, NUMDEATHFRAMES); } -static void killplayer(player *pl) { +static void killplayer(player* pl) { pl->deaths++; - pl->flags|=FLG_DEAD; + pl->flags |= FLG_DEAD; playsound(2); adddeath(pl); detonatecontrolled(pl); @@ -576,7 +556,7 @@ static void processgenerics(void) { } static void drawgenerics(void) { - generic *gen; + generic* gen; list_for_each_entry(gen, &activegeneric, list) { gen->draw(gen); @@ -584,9 +564,9 @@ static void drawgenerics(void) { } static void drawstats(void) { - player *pl; + player* pl; int p = 0; - const char *n; + const char* n; char buf[17]; solidcopy(&background, 0, 0, IXSIZE, arraystarty); @@ -597,191 +577,183 @@ static void drawstats(void) { n = playername; } snprintf(buf, sizeof(buf), "%-8.8s %2i/%2i", n, pl->kills % 100, pl->deaths % 100); - drawstring(11 + (p/2) * (15 * fontxsize + 7), 11 + (p%2) * (fontysize+2), buf); + drawstring(11 + (p / 2) * (15 * fontxsize + 7), 11 + (p % 2) * (fontysize + 2), buf); p++; } } -static int centerxchange(player *pl) { +static int centerxchange(player* pl) { int speed; int val; int line; int max; - max=arrayspacex<speed; - val=pl->xpos+(max<<2); - val%=max; - line=max>>1; - if(valspeed; + val = pl->xpos + (max << 2); + val %= max; + line = max >> 1; + if (val < line) { + if (val - speed < 0) return -val; else return -speed; - } else if(val>=line) { - if(val+speed>max) - return max-val; + } else if (val >= line) { + if (val + speed > max) + return max - val; else return speed; } return 0; } -static void centerx(player *pl) { - pl->xpos+=centerxchange(pl); +static void centerx(player* pl) { + pl->xpos += centerxchange(pl); } -static int centerychange(player *pl) { +static int centerychange(player* pl) { int speed; int val; int line; int max; - max=arrayspacey<speed; - val=pl->ypos+(max<<2); - val%=max; - line=max>>1; - if(valspeed; + val = pl->ypos + (max << 2); + val %= max; + line = max >> 1; + if (val < line) { + if (val - speed < 0) return -val; else return -speed; - } else if(val>=line) - { - if(val+speed>max) - return max-val; + } else if (val >= line) { + if (val + speed > max) + return max - val; else return speed; } return 0; } -static void centery(player *pl) { - pl->ypos+=centerychange(pl); +static void centery(player* pl) { + pl->ypos += centerychange(pl); } -#define SGN(x) ((x)==0 ? 0 : ((x)<0 ? -1 : 1)) +#define SGN(x) ((x) == 0 ? 0 : ((x) < 0 ? -1 : 1)) -static void trymove(player *pl,int dx,int dy) { - int wx,wy; - int sx,sy; +static void trymove(player* pl, int dx, int dy) { + int wx, wy; + int sx, sy; int there; - int px,py; - static int depth=0; - int tx,ty; + int px, py; + static int depth = 0; + int tx, ty; ++depth; - sx=(dx*(arrayspacex+1)) << (FRACTION-1); - sy=(dy*(arrayspacey+1)) << (FRACTION-1); + sx = (dx * (arrayspacex + 1)) << (FRACTION - 1); + sy = (dy * (arrayspacey + 1)) << (FRACTION - 1); - wx=screentoarrayx(pl->xpos+sx); - wy=screentoarrayy(pl->ypos+sy); - px=screentoarrayx(pl->xpos); - py=screentoarrayy(pl->ypos); + wx = screentoarrayx(pl->xpos + sx); + wy = screentoarrayy(pl->ypos + sy); + px = screentoarrayx(pl->xpos); + py = screentoarrayy(pl->ypos); - if(wx<0 || wx>=arraynumx || wy<0 || wy>=arraynumy) { + if (wx < 0 || wx >= arraynumx || wy < 0 || wy >= arraynumy) { --depth; return; } - there=field[wy][wx]; - if((px!=wx || py!=wy) && - (there==FIELD_BRICK||there==FIELD_BOMB||there==FIELD_BORDER)) - { - if(dx && !dy) { - ty=centerychange(pl); - if(ty && depth==1) - trymove(pl,0,-SGN(ty)); + there = field[wy][wx]; + if ((px != wx || py != wy) && (there == FIELD_BRICK || there == FIELD_BOMB || there == FIELD_BORDER)) { + if (dx && !dy) { + ty = centerychange(pl); + if (ty && depth == 1) trymove(pl, 0, -SGN(ty)); - } else if(dy && !dx) { - tx=centerxchange(pl); - if(tx && depth==1) - trymove(pl,-SGN(tx),0); + } else if (dy && !dx) { + tx = centerxchange(pl); + if (tx && depth == 1) trymove(pl, -SGN(tx), 0); } } else { - pl->xpos+=dx*pl->speed; - pl->ypos+=dy*pl->speed; - if(dx && !dy) centery(pl); - if(dy && !dx) centerx(pl); + pl->xpos += dx * pl->speed; + pl->ypos += dy * pl->speed; + if (dx && !dy) centery(pl); + if (dy && !dx) centerx(pl); } --depth; } -static void applybonus(player *pl,bonustile *bonus) { +static void applybonus(player* pl, bonustile* bonus) { int type; int maxflame; - maxflame=arraynumx>arraynumy ? arraynumx : arraynumy; - type=bonus->type; + maxflame = arraynumx > arraynumy ? arraynumx : arraynumy; + type = bonus->type; deletebonus(bonus); - switch(type) { + switch (type) { case TILE_BOMB: - if (pl->bombsavailable < 9) - ++(pl->bombsavailable); + if (pl->bombsavailable < 9) ++(pl->bombsavailable); break; case TILE_FLAME: - if(pl->flamelengthflamelength); + if (pl->flamelength < maxflame) ++(pl->flamelength); break; case TILE_GOLDFLAME: - pl->flamelength=maxflame; + pl->flamelength = maxflame; break; case TILE_TRIGGER: - pl->flags|=FLG_CONTROL; + pl->flags |= FLG_CONTROL; break; case TILE_SKATES: if (pl->speed < SPEEDSTART) { pl->speed = SPEEDSTART; } else { - pl->speed+=SPEEDDELTA; + pl->speed += SPEEDDELTA; } - if(pl->speed>SPEEDMAX) pl->speed=SPEEDMAX; + if (pl->speed > SPEEDMAX) pl->speed = SPEEDMAX; break; case TILE_TURTLE: - pl->speed=SPEEDTURTLE; - pl->speedturtle_timeout=SPEEDTURTLE_TIMEOUT; + pl->speed = SPEEDTURTLE; + pl->speedturtle_timeout = SPEEDTURTLE_TIMEOUT; break; } } -static void doplayer(player *pl) { +static void doplayer(player* pl) { /* int last; */ int color; /* int speed; */ - int px,py; + int px, py; int there; int flags; int what; - if(pl->controller==-1) - what=myaction; + if (pl->controller == -1) + what = myaction; else - what=actions[pl->controller]; + what = actions[pl->controller]; - flags=pl->flags; - if(flags&FLG_DEAD) return; - color=pl->color; + flags = pl->flags; + if (flags & FLG_DEAD) return; + color = pl->color; /* last=pl->doing; */ - pl->doing=what; + pl->doing = what; /* speed=pl->speed; */ - px=screentoarrayx(pl->xpos); - py=screentoarrayy(pl->ypos); - there=field[py][px]; + px = screentoarrayx(pl->xpos); + py = screentoarrayy(pl->ypos); + there = field[py][px]; - if(what==ACT_QUIT) { + if (what == ACT_QUIT) { killplayer(pl); list_del(&pl->list_all_players); return; } - if(there==FIELD_BONUS) { - playsound((myrand()&1) ? 1 : 5); - applybonus(pl,info[py][px]); - } else if(there==FIELD_FLAME) { - flame *fl = info[py][px]; + if (there == FIELD_BONUS) { + playsound((myrand() & 1) ? 1 : 5); + applybonus(pl, info[py][px]); + } else if (there == FIELD_FLAME) { + flame* fl = info[py][px]; if (fl->owner == pl) { pl->kills--; } else { @@ -791,35 +763,32 @@ static void doplayer(player *pl) { return; } -// if(what&ACT_TURBO) speed<<=2; - if(what&ACT_PRIMARY) { - if(there==FIELD_EMPTY && pl->bombsavailable) - dropbomb(pl,px,py, - (flags&FLG_CONTROL) ? BOMB_CONTROLLED :BOMB_NORMAL); + // if(what&ACT_TURBO) speed<<=2; + if (what & ACT_PRIMARY) { + if (there == FIELD_EMPTY && pl->bombsavailable) dropbomb(pl, px, py, (flags & FLG_CONTROL) ? BOMB_CONTROLLED : BOMB_NORMAL); } - if(what&ACT_SECONDARY && (flags&FLG_CONTROL)) - detonatecontrolled(pl); + if (what & ACT_SECONDARY && (flags & FLG_CONTROL)) detonatecontrolled(pl); - switch(what&ACT_MASK) { + switch (what & ACT_MASK) { case ACT_UP: - trymove(pl,0,-1); - pl->figcount=(pl->figcount+1)%15; - pl->figure=walking[color]+pl->figcount+15; + trymove(pl, 0, -1); + pl->figcount = (pl->figcount + 1) % 15; + pl->figure = walking[color] + pl->figcount + 15; break; case ACT_DOWN: - trymove(pl,0,1); - pl->figcount=(pl->figcount+1)%15; - pl->figure=walking[color]+pl->figcount+30; + trymove(pl, 0, 1); + pl->figcount = (pl->figcount + 1) % 15; + pl->figure = walking[color] + pl->figcount + 30; break; case ACT_LEFT: - trymove(pl,-1,0); - pl->figcount=(pl->figcount+1)%15; - pl->figure=walking[color]+pl->figcount+45; + trymove(pl, -1, 0); + pl->figcount = (pl->figcount + 1) % 15; + pl->figure = walking[color] + pl->figcount + 45; break; case ACT_RIGHT: - trymove(pl,1,0); - pl->figcount=(pl->figcount+1)%15; - pl->figure=walking[color]+pl->figcount; + trymove(pl, 1, 0); + pl->figcount = (pl->figcount + 1) % 15; + pl->figure = walking[color] + pl->figcount; break; case ACT_NONE: break; @@ -846,26 +815,29 @@ static void processquits(void) { if (network != NETWORK_SLAVE) return; for (i = 0; i < MAXNETNODES; ++i) { - if (netnodes[i].used && actions[i]==ACT_QUIT) - netnodes[i].used=0; + if (netnodes[i].used && actions[i] == ACT_QUIT) netnodes[i].used = 0; } } static int getaction(void) { int what; - what=ACT_NONE; - if(checkpressed(MYLEFT)) what=ACT_LEFT; - else if(checkpressed(MYRIGHT)) what=ACT_RIGHT; - else if(checkpressed(MYDOWN)) what=ACT_DOWN; - else if(checkpressed(MYUP)) what=ACT_UP; - else if(checkdown(13)) what=ACT_ENTER; - else if(checkdown(0x1b)) what=ACT_QUIT; + what = ACT_NONE; + if (checkpressed(MYLEFT)) + what = ACT_LEFT; + else if (checkpressed(MYRIGHT)) + what = ACT_RIGHT; + else if (checkpressed(MYDOWN)) + what = ACT_DOWN; + else if (checkpressed(MYUP)) + what = ACT_UP; + else if (checkdown(13)) + what = ACT_ENTER; + else if (checkdown(0x1b)) + what = ACT_QUIT; - if(checkdown(' ')) - what|=ACT_PRIMARY; - if(checkdown('b')) - what|=ACT_SECONDARY; + if (checkdown(' ')) what |= ACT_PRIMARY; + if (checkdown('b')) what |= ACT_SECONDARY; return what; } @@ -883,7 +855,7 @@ static int iterate(void) { gfxunlock(); myaction = getaction(); - if (NETWORK_NONE == network && myaction==ACT_QUIT) return CODE_QUIT; + if (NETWORK_NONE == network && myaction == ACT_QUIT) return CODE_QUIT; if (NETWORK_NONE == network) { gountil = mycount + 1; /* single step in singly player mode */ } else { @@ -894,9 +866,9 @@ static int iterate(void) { ++mycount; if (NETWORK_NONE != network) { i = gountil - mycount; - if(i >= ACTIONHIST) // too far behind + if (i >= ACTIONHIST) // too far behind goto leave_game; - memcpy(actions, actionblock+i*MAXNETNODES, MAXNETNODES); + memcpy(actions, actionblock + i * MAXNETNODES, MAXNETNODES); if (actions[myslot] == ACT_QUIT) return CODE_QUIT; } processbombs(); @@ -908,13 +880,13 @@ static int iterate(void) { processplayers(); } -/* - if(!(rand()&127)) - { - i=gtime(); - while(gtime()-i<100); - } -*/ + /* + if(!(rand()&127)) + { + i=gtime(); + while(gtime()-i<100); + } + */ drawbombs(); drawbonus(); @@ -926,23 +898,22 @@ static int iterate(void) { plotsprites(); copyup(); if (list_empty(&activegeneric)) { - player *pl; + player* pl; int deadplayers = 0; i = 0; list_for_each_entry(pl, &activeplayers, list) { - if(!(pl->flags & FLG_DEAD)) + if (!(pl->flags & FLG_DEAD)) ++i; else deadplayers++; } - if (deadplayers > 0 && (!i || (NETWORK_NONE != network && i==1))) { + if (deadplayers > 0 && (!i || (NETWORK_NONE != network && i == 1))) { ++deathcount; - if(deathcount==25) - return CODE_ALLDEAD; + if (deathcount == 25) return CODE_ALLDEAD; } else - deathcount=0; + deathcount = 0; } return CODE_CONT; @@ -952,7 +923,7 @@ leave_game: /* client disconnect/timeout: send ACT_QUIT to master */ return CODE_QUIT; } -void set_game_options(GameOptions *options) { +void set_game_options(GameOptions* options) { gameoptions = *options; } @@ -964,7 +935,7 @@ void run_single_player(void) { initplayers(); do { initgame(); - while(!(code=iterate()) && !exitflag) ++framecount; + while (!(code = iterate()) && !exitflag) ++framecount; } while (code != CODE_QUIT && !exitflag); } @@ -975,6 +946,6 @@ void run_network_game(void) { initplayers(); do { initgame(); - while (!(code=iterate()) && !exitflag) ++framecount; + while (!(code = iterate()) && !exitflag) ++framecount; } while (code != CODE_QUIT && !exitflag); } diff --git a/src/game.h b/src/game.h index 084feb5..98dc7f0 100644 --- a/src/game.h +++ b/src/game.h @@ -1,11 +1,13 @@ #ifndef GAME_H #define GAME_H +#include "bomber.h" + #define FRACTION 9 -#define SPEEDDELTA (1<<(FRACTION-1)) -#define SPEEDMAX (10< #include #include #include -#include #include "bomber.h" #include "gfx.h" #define MAXCOLORS 256 -int usedcolors=0; -SDL_Surface *thescreen; +int usedcolors = 0; +SDL_Surface* thescreen; SDL_Color themap[256]; -uchar *videomem; +uchar* videomem; int stride; -int mousex,mousey,mouseb; -uchar mustlock=0,locked=0; -uchar *block64; +int mousex, mousey, mouseb; +uchar mustlock = 0, locked = 0; +uchar* block64; -int buttonstate=0,buttondown=0; -int mxpos,mypos; +int buttonstate = 0, buttondown = 0; +int mxpos, mypos; -int pressedcodes[KEYMAX],downcodes[KEYMAX],numpressed,numdown; +int pressedcodes[KEYMAX], downcodes[KEYMAX], numpressed, numdown; -void dumpgfx() -{ +void dumpgfx() { usedcolors = 0; } -static int bestmatch(int red,int green,int blue) -{ -int i; -int bestcolor,bestdelta=0; -int rdelta,gdelta,bdelta; -int delta; +static int bestmatch(int red, int green, int blue) { + int i; + int bestcolor, bestdelta = 0; + int rdelta, gdelta, bdelta; + int delta; - i=0; - bestcolor=-1; - while(igs_pic; - for(i=0;i<256;i++) counts[i]=0; - i=gs->gs_xsize*gs->gs_ysize; - while(i--) - counts[*p++]++; - cnt=0; - gs->gs_inout[0]=0; - for(i=1;i<256;i++) - { - if(counts[i]) - { + p = gs->gs_pic; + for (i = 0; i < 256; i++) counts[i] = 0; + i = gs->gs_xsize * gs->gs_ysize; + while (i--) counts[*p++]++; + cnt = 0; + gs->gs_inout[0] = 0; + for (i = 1; i < 256; i++) { + if (counts[i]) { cnt++; - p=gs->gs_colormap+i+i+i; - red=*p++; - green=*p++; - blue=*p++; - for(j=0;jgs_inout[i]=j; + p = gs->gs_colormap + i + i + i; + red = *p++; + green = *p++; + blue = *p++; + for (j = 0; j < usedcolors; j++) { + if (red == themap[j].r && green == themap[j].g && blue == themap[j].b) { + gs->gs_inout[i] = j; break; } } - if(j==usedcolors) - { - if(usedcolorsgs_inout[i]=usedcolors; + if (j == usedcolors) { + if (usedcolors < MAXCOLORS) { + themap[j].r = red; + themap[j].g = green; + themap[j].b = blue; + gs->gs_inout[i] = usedcolors; ++usedcolors; } else - gs->gs_inout[i]=bestmatch(red,green,blue); + gs->gs_inout[i] = bestmatch(red, green, blue); } } } updatemap(); } -static uchar *compressfig(uchar *put,gfxset *gs, - int sourcex,int sourcey,int sizex,int sizey) -{ -int j,gswidth; -uchar *p, *p2,pixel,*map1; -int dx,dy; +static uchar* compressfig(uchar* put, gfxset* gs, int sourcex, int sourcey, int sizex, int sizey) { + int j, gswidth; + uchar *p, *p2, pixel, *map1; + int dx, dy; - gswidth=gs->gs_xsize; - map1=gs->gs_inout; - p=gs->gs_pic+sourcex+gswidth*sourcey; - for(dy=0;dygs_xsize; + map1 = gs->gs_inout; + p = gs->gs_pic + sourcex + gswidth * sourcey; + for (dy = 0; dy < sizey; dy++) { + p2 = p; + j = 0; + for (dx = 0; dx <= sizex; dx++) { + if (dx < sizex) + pixel = *p2; + else + pixel = 0; ++p2; - if(pixel) + if (pixel) ++j; - else if(j) - { - *put++=j; - *put++=dx-j; - *put++=dy; - p2-=j+1; - while(j) - *put++=map1[*p2++],--j; + else if (j) { + *put++ = j; + *put++ = dx - j; + *put++ = dy; + p2 -= j + 1; + while (j) *put++ = map1[*p2++], --j; ++p2; } } - p+=gswidth; + p += gswidth; } - *put++=0; + *put++ = 0; return put; } -static void gfxfetchsingle(figure *fig,gfxset *gs,int sourcex,int sourcey,int sizex,int sizey) -{ -uchar *p,*p2; -int dx,dy; -/* uchar *map1; */ -int gswidth; -int minx,miny,maxx,maxy; -int tx,ty; +static void gfxfetchsingle(figure* fig, gfxset* gs, int sourcex, int sourcey, int sizex, int sizey) { + uchar *p, *p2; + int dx, dy; + /* uchar *map1; */ + int gswidth; + int minx, miny, maxx, maxy; + int tx, ty; /* map1=gs->gs_inout; */ - gswidth=gs->gs_xsize; - p=gs->gs_pic+sourcex+gswidth*sourcey; - minx=miny=maxx=maxy=-1; - for(dy=0;dymaxy) maxy=ty; - tx=sourcex+dx; - if(minx==-1 || txmaxx) maxx=tx; + gswidth = gs->gs_xsize; + p = gs->gs_pic + sourcex + gswidth * sourcey; + minx = miny = maxx = maxy = -1; + for (dy = 0; dy < sizey; dy++) { + p2 = p; + ty = sourcey + dy; + for (dx = 0; dx < sizex; dx++) { + if (!*p2++) continue; + if (miny == -1 || ty < miny) miny = ty; + if (maxy == -1 || ty > maxy) maxy = ty; + tx = sourcex + dx; + if (minx == -1 || tx < minx) minx = tx; + if (maxx == -1 || tx > maxx) maxx = tx; } - p+=gswidth; + p += gswidth; } - if(minx==-1) - { - minx=maxx=sourcex; - miny=maxy=sourcey; + if (minx == -1) { + minx = maxx = sourcex; + miny = maxy = sourcey; } - fig->xdelta=minx-sourcex; - fig->ydelta=miny-sourcey; + fig->xdelta = minx - sourcex; + fig->ydelta = miny - sourcey; - sourcex=minx; - sourcey=miny; - fig->xsize=sizex=maxx-minx+1; - fig->ysize=sizey=maxy-miny+1; - - p=compressfig(block64,gs,sourcex,sourcey,sizex,sizey); - fig->graphics=malloc(p-block64); - if(fig->graphics) - memcpy(fig->graphics,block64,p-block64); - + sourcex = minx; + sourcey = miny; + fig->xsize = sizex = maxx - minx + 1; + fig->ysize = sizey = maxy - miny + 1; + p = compressfig(block64, gs, sourcex, sourcey, sizex, sizey); + fig->graphics = malloc(p - block64); + if (fig->graphics) memcpy(fig->graphics, block64, p - block64); } //(gfxset *gs,figure *fig,int sourcex,int sourcey,int sizex,int sizey) -void gfxfetch(gfxset *gs,figure *fig,int num) -{ -int x,y; -int xsize,ysize; -int fxsize,fysize; -unsigned char *p,*p2; +void gfxfetch(gfxset* gs, figure* fig, int num) { + int x, y; + int xsize, ysize; + int fxsize, fysize; + unsigned char *p, *p2; - xsize=gs->gs_xsize; - ysize=gs->gs_ysize; - p2=p=gs->gs_pic+xsize+1; - fxsize=2; - while(*p++==0) ++fxsize; - fysize=2; - while(*p2==0) ++fysize,p2+=xsize; - x=fxsize; - y=0; - while(num--) - { - gfxfetchsingle(fig,gs,x,y,fxsize,fysize); - x+=fxsize; - if(x>xsize-fxsize) - { - x=0; - y+=fysize; - if(y>ysize-fysize) - y=0; + xsize = gs->gs_xsize; + ysize = gs->gs_ysize; + p2 = p = gs->gs_pic + xsize + 1; + fxsize = 2; + while (*p++ == 0) ++fxsize; + fysize = 2; + while (*p2 == 0) ++fysize, p2 += xsize; + x = fxsize; + y = 0; + while (num--) { + gfxfetchsingle(fig, gs, x, y, fxsize, fysize); + x += fxsize; + if (x > xsize - fxsize) { + x = 0; + y += fysize; + if (y > ysize - fysize) y = 0; } fig++; } } -void solidfetch(gfxset *gs,solid *dest) -{ -int xsize,ysize; -int i; -unsigned char *p,*map; -uchar *gfx; +void solidfetch(gfxset* gs, solid* dest) { + int xsize, ysize; + int i; + unsigned char *p, *map; + uchar* gfx; - memset(dest,0,sizeof(solid)); - xsize=gs->gs_xsize; - ysize=gs->gs_ysize; + memset(dest, 0, sizeof(solid)); + xsize = gs->gs_xsize; + ysize = gs->gs_ysize; - i=xsize*ysize; - gfx=dest->graphics=malloc(i); - if(!gfx) return; - dest->xsize=xsize; - dest->ysize=ysize; - map=gs->gs_inout; - memcpy(gfx,gs->gs_pic,i); - p=gfx; - while(i--) - { - if(*p) *p=map[*p]; + i = xsize * ysize; + gfx = dest->graphics = malloc(i); + if (!gfx) return; + dest->xsize = xsize; + dest->ysize = ysize; + map = gs->gs_inout; + memcpy(gfx, gs->gs_pic, i); + p = gfx; + while (i--) { + if (*p) *p = map[*p]; ++p; } } -void solidcopyany(solid *src,solid *dest,int destx,int desty,int sizex,int sizey) -{ -int xmax,ymax; -int j; -uchar *p1,*p2; -int w; +void solidcopyany(solid* src, solid* dest, int destx, int desty, int sizex, int sizey) { + int xmax, ymax; + int j; + uchar *p1, *p2; + int w; - xmax=src->xsize; - ymax=src->ysize; - if(destx>=xmax || desty>=ymax || destx+sizex<=0 || desty+sizey<=0) - return; - if(destx<0) - { - sizex+=destx; - destx=0; + xmax = src->xsize; + ymax = src->ysize; + if (destx >= xmax || desty >= ymax || destx + sizex <= 0 || desty + sizey <= 0) return; + if (destx < 0) { + sizex += destx; + destx = 0; } - if(desty<0) - { - sizey+=desty; - desty=0; + if (desty < 0) { + sizey += desty; + desty = 0; } - if(destx+sizex>xmax) - sizex=xmax-destx; - if(desty+sizey>ymax) - sizey=ymax-desty; + if (destx + sizex > xmax) sizex = xmax - destx; + if (desty + sizey > ymax) sizey = ymax - desty; - if(dest) - { - w=dest->xsize; - p1=dest->graphics+desty*w+destx; - } else - { + if (dest) { + w = dest->xsize; + p1 = dest->graphics + desty * w + destx; + } else { gfxlock(); - w=stride; - p1=videomem+desty*stride+destx; + w = stride; + p1 = videomem + desty * stride + destx; } - p2=src->graphics+desty*xmax+destx; - for(j=0;jgraphics + desty * xmax + destx; + for (j = 0; j < sizey; ++j) { + memcpy(p1, p2, sizex); + p1 += w; + p2 += xmax; } - } -void solidcopy(solid *solid,int destx,int desty,int sizex,int sizey) -{ - solidcopyany(solid,0,destx,desty,sizex,sizey); +void solidcopy(solid* solid, int destx, int desty, int sizex, int sizey) { + solidcopyany(solid, 0, destx, desty, sizex, sizey); } -void drawfigureany(int x,int y,figure *fig,solid *dest) -{ -int run; -int dx,dy; -int xsize,ysize; -int clipx,clipy,w; -unsigned char *pc,*p,*p2,*take; +void drawfigureany(int x, int y, figure* fig, solid* dest) { + int run; + int dx, dy; + int xsize, ysize; + int clipx, clipy, w; + unsigned char *pc, *p, *p2, *take; - take=fig->graphics; - if(dest) - { - w=clipx=dest->xsize; - clipy=dest->ysize; - pc=dest->graphics; - } else - { + take = fig->graphics; + if (dest) { + w = clipx = dest->xsize; + clipy = dest->ysize; + pc = dest->graphics; + } else { gfxlock(); - w=stride; - clipx=IXSIZE; - clipy=IYSIZE; - pc=videomem; + w = stride; + clipx = IXSIZE; + clipy = IYSIZE; + pc = videomem; } - dx=fig->xdelta; - dy=fig->ydelta; - xsize=fig->xsize; - ysize=fig->ysize; - x+=dx; - y+=dy; - if(x>=0 && y>=0 && x<=clipx-xsize && y<=clipy-ysize) - { - while((run=*take++)) - { - dx=*((signed char *)take); ++take; - dy=*((signed char *)take); ++take; - p=pc+w*(y+dy)+x+dx; - while(run--) - *p++=*take++; + dx = fig->xdelta; + dy = fig->ydelta; + xsize = fig->xsize; + ysize = fig->ysize; + x += dx; + y += dy; + if (x >= 0 && y >= 0 && x <= clipx - xsize && y <= clipy - ysize) { + while ((run = *take++)) { + dx = *((signed char*) take); + ++take; + dy = *((signed char*) take); + ++take; + p = pc + w * (y + dy) + x + dx; + while (run--) *p++ = *take++; } - } else - { - while((run=*take++)) - { - dx=*((signed char *)take); ++take; - dy=*((signed char *)take); ++take; - dx+=x; - dy+=y; - p2=take; - take+=run; - if(dy<0 || dy>=clipy) continue; - if(dx>=clipx) continue; - if(dx<0) - { - p2-=dx; - run+=dx; - dx=0; - } else if(dx+run>clipx) - run=clipx-dx; - p=pc+w*dy+dx; - if(run) - memcpy(p,p2,run); + } else { + while ((run = *take++)) { + dx = *((signed char*) take); + ++take; + dy = *((signed char*) take); + ++take; + dx += x; + dy += y; + p2 = take; + take += run; + if (dy < 0 || dy >= clipy) continue; + if (dx >= clipx) continue; + if (dx < 0) { + p2 -= dx; + run += dx; + dx = 0; + } else if (dx + run > clipx) + run = clipx - dx; + p = pc + w * dy + dx; + if (run) memcpy(p, p2, run); } } } -void drawfigure(int destx,int desty,figure *fig) -{ - drawfigureany(destx,desty,fig,0); +void drawfigure(int destx, int desty, figure* fig) { + drawfigureany(destx, desty, fig, 0); } -void copyup() -{ +void copyup() { gfxunlock(); SDL_UpdateRect(thescreen, 0, 0, 0, 0); - needwhole=0; + needwhole = 0; } -void copyupxy(int x,int y) -{ +void copyupxy(int x, int y) { gfxunlock(); - SDL_UpdateRect(thescreen,x,y,24,24); + SDL_UpdateRect(thescreen, x, y, 24, 24); } -void copyupxysize(int x,int y,int xsize,int ysize) -{ +void copyupxysize(int x, int y, int xsize, int ysize) { gfxunlock(); - SDL_UpdateRect(thescreen,x,y,xsize,ysize); + SDL_UpdateRect(thescreen, x, y, xsize, ysize); } @@ -410,347 +358,463 @@ void copyupxysize(int x,int y,int xsize,int ysize) void opengfx(void) { unsigned long videoflags; - themap[0].r=0; - themap[0].g=0; - themap[0].b=0; - usedcolors=1; + themap[0].r = 0; + themap[0].g = 0; + themap[0].b = 0; + usedcolors = 1; - block64=malloc(65536); - if(!block64) - { + block64 = malloc(65536); + if (!block64) { printf("Couldn't allocate block64\n"); exit(50); } - if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0 ) - { - fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) { + fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } - videoflags = SDL_SWSURFACE|SDL_HWPALETTE; //|SDL_FULLSCREEN; + videoflags = SDL_SWSURFACE | SDL_HWPALETTE; //|SDL_FULLSCREEN; thescreen = SDL_SetVideoMode(IXSIZE, IYSIZE, 8, videoflags); - if ( thescreen == NULL ) - { - fprintf(stderr, "Couldn't set display mode: %s\n", - SDL_GetError()); + if (thescreen == NULL) { + fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError()); SDL_Quit(); exit(5); } - stride=thescreen->pitch; - videomem=thescreen->pixels; - mustlock=SDL_MUSTLOCK(thescreen); - locked=0; + stride = thescreen->pitch; + videomem = thescreen->pixels; + mustlock = SDL_MUSTLOCK(thescreen); + locked = 0; SDL_ShowCursor(0); } -void closegfx(void) -{ +void closegfx(void) { SDL_Quit(); } -int checkpressed(int code) -{ -int *p,i; - i=numpressed; - p=pressedcodes; - while(i--) - if(*p++==code) return 1; +int checkpressed(int code) { + int *p, i; + i = numpressed; + p = pressedcodes; + while (i--) + if (*p++ == code) return 1; return 0; } -int checkdown(int code) -{ -int *p,i; - i=numdown; - p=downcodes; - while(i--) - if(*p++==code) return 1; +int checkdown(int code) { + int *p, i; + i = numdown; + p = downcodes; + while (i--) + if (*p++ == code) return 1; return 0; } -int checkbutton(int button) -{ - return buttonstate & (1<', -SDLK_COMMA,'<', -SDLK_BACKQUOTE,'~', -SDLK_BACKSPACE,8, -SDLK_TAB,9, -SDLK_DELETE,MYDELETE, -SDLK_RETURN,13, -SDLK_F1,MYF1+MYSHIFTED, -SDLK_F2,MYF2+MYSHIFTED, -SDLK_F3,MYF3+MYSHIFTED, -SDLK_F4,MYF4+MYSHIFTED, -SDLK_F5,MYF5+MYSHIFTED, -SDLK_F6,MYF6+MYSHIFTED, -SDLK_F7,MYF7+MYSHIFTED, -SDLK_F8,MYF8+MYSHIFTED, -SDLK_F9,MYF9+MYSHIFTED, -SDLK_F10,MYF10+MYSHIFTED, -SDLK_ESCAPE,0x1b, -SDLK_LEFT,MYLEFT+MYSHIFTED, -SDLK_RIGHT,MYRIGHT+MYSHIFTED, -SDLK_UP,MYUP+MYSHIFTED, -SDLK_DOWN,MYDOWN+MYSHIFTED, -SDLK_PAGEUP,MYPAGEUP, -SDLK_PAGEDOWN,MYPAGEDOWN, -SDLK_SPACE,' ', -SDLK_HOME,MYHOME, -SDLK_END,MYEND, -SDLK_LALT,MYALTL, -SDLK_RALT,MYALTR, -ENDMARK -}; -static int looklist(int code,int *list) -{ - while((unsigned int)*list!=ENDMARK) { - if(*list==code) - return list[1]; - list+=2; +int sdlinoutnormal[] = { + SDLK_0, + '0', + SDLK_1, + '1', + SDLK_2, + '2', + SDLK_3, + '3', + SDLK_4, + '4', + SDLK_5, + '5', + SDLK_6, + '6', + SDLK_7, + '7', + SDLK_8, + '8', + SDLK_9, + '9', + SDLK_a, + 'a', + SDLK_b, + 'b', + SDLK_c, + 'c', + SDLK_d, + 'd', + SDLK_e, + 'e', + SDLK_f, + 'f', + SDLK_g, + 'g', + SDLK_h, + 'h', + SDLK_i, + 'i', + SDLK_j, + 'j', + SDLK_k, + 'k', + SDLK_l, + 'l', + SDLK_m, + 'm', + SDLK_n, + 'n', + SDLK_o, + 'o', + SDLK_p, + 'p', + SDLK_q, + 'q', + SDLK_r, + 'r', + SDLK_s, + 's', + SDLK_t, + 't', + SDLK_u, + 'u', + SDLK_v, + 'v', + SDLK_w, + 'w', + SDLK_x, + 'x', + SDLK_y, + 'y', + SDLK_z, + 'z', + SDLK_MINUS, + '-', + SDLK_EQUALS, + '=', + SDLK_LEFTBRACKET, + '[', + SDLK_RIGHTBRACKET, + ']', + SDLK_SEMICOLON, + ';', + SDLK_QUOTE, + '\'', + SDLK_SLASH, + '/', + SDLK_PERIOD, + '.', + SDLK_COMMA, + ',', + SDLK_BACKQUOTE, + '`', + SDLK_BACKSPACE, + 8, + SDLK_TAB, + 9, + SDLK_DELETE, + MYDELETE, + SDLK_RETURN, + 13, + SDLK_F1, + MYF1, + SDLK_F2, + MYF2, + SDLK_F3, + MYF3, + SDLK_F4, + MYF4, + SDLK_F5, + MYF5, + SDLK_F6, + MYF6, + SDLK_F7, + MYF7, + SDLK_F8, + MYF8, + SDLK_F9, + MYF9, + SDLK_F10, + MYF10, + SDLK_ESCAPE, + 0x1b, + SDLK_LEFT, + MYLEFT, + SDLK_RIGHT, + MYRIGHT, + SDLK_UP, + MYUP, + SDLK_DOWN, + MYDOWN, + SDLK_PAGEUP, + MYPAGEUP, + SDLK_PAGEDOWN, + MYPAGEDOWN, + SDLK_SPACE, + ' ', + SDLK_HOME, + MYHOME, + SDLK_END, + MYEND, + SDLK_LALT, + MYALTL, + SDLK_RALT, + MYALTR, + ENDMARK}; +int sdlinoutshifted[] = { + SDLK_0, + ')', + SDLK_1, + '!', + SDLK_2, + '@', + SDLK_3, + '#', + SDLK_4, + '$', + SDLK_5, + '%', + SDLK_6, + '^', + SDLK_7, + '&', + SDLK_8, + '*', + SDLK_9, + '(', + SDLK_a, + 'A', + SDLK_b, + 'B', + SDLK_c, + 'C', + SDLK_d, + 'D', + SDLK_e, + 'E', + SDLK_f, + 'F', + SDLK_g, + 'G', + SDLK_h, + 'H', + SDLK_i, + 'I', + SDLK_j, + 'J', + SDLK_k, + 'K', + SDLK_l, + 'L', + SDLK_m, + 'M', + SDLK_n, + 'N', + SDLK_o, + 'O', + SDLK_p, + 'P', + SDLK_q, + 'Q', + SDLK_r, + 'R', + SDLK_s, + 'S', + SDLK_t, + 'T', + SDLK_u, + 'U', + SDLK_v, + 'V', + SDLK_w, + 'W', + SDLK_x, + 'X', + SDLK_y, + 'Y', + SDLK_z, + 'Z', + SDLK_MINUS, + '_', + SDLK_EQUALS, + '+', + SDLK_LEFTBRACKET, + '{', + SDLK_RIGHTBRACKET, + '}', + SDLK_SEMICOLON, + ':', + SDLK_QUOTE, + '"', + SDLK_SLASH, + '?', + SDLK_PERIOD, + '>', + SDLK_COMMA, + '<', + SDLK_BACKQUOTE, + '~', + SDLK_BACKSPACE, + 8, + SDLK_TAB, + 9, + SDLK_DELETE, + MYDELETE, + SDLK_RETURN, + 13, + SDLK_F1, + MYF1 + MYSHIFTED, + SDLK_F2, + MYF2 + MYSHIFTED, + SDLK_F3, + MYF3 + MYSHIFTED, + SDLK_F4, + MYF4 + MYSHIFTED, + SDLK_F5, + MYF5 + MYSHIFTED, + SDLK_F6, + MYF6 + MYSHIFTED, + SDLK_F7, + MYF7 + MYSHIFTED, + SDLK_F8, + MYF8 + MYSHIFTED, + SDLK_F9, + MYF9 + MYSHIFTED, + SDLK_F10, + MYF10 + MYSHIFTED, + SDLK_ESCAPE, + 0x1b, + SDLK_LEFT, + MYLEFT + MYSHIFTED, + SDLK_RIGHT, + MYRIGHT + MYSHIFTED, + SDLK_UP, + MYUP + MYSHIFTED, + SDLK_DOWN, + MYDOWN + MYSHIFTED, + SDLK_PAGEUP, + MYPAGEUP, + SDLK_PAGEDOWN, + MYPAGEDOWN, + SDLK_SPACE, + ' ', + SDLK_HOME, + MYHOME, + SDLK_END, + MYEND, + SDLK_LALT, + MYALTL, + SDLK_RALT, + MYALTR, + ENDMARK}; +static int looklist(int code, int* list) { + while ((unsigned int) *list != ENDMARK) { + if (*list == code) return list[1]; + list += 2; } return -1; } -static int mapkey(int code,int qual) -{ +static int mapkey(int code, int qual) { - if(qual & KMOD_SHIFT) - code=looklist(code,sdlinoutshifted); + if (qual & KMOD_SHIFT) + code = looklist(code, sdlinoutshifted); else - code=looklist(code,sdlinoutnormal); - if(code<0) return -1; + code = looklist(code, sdlinoutnormal); + if (code < 0) return -1; - if(qual & KMOD_ALT) - code|=MYALTED; + if (qual & KMOD_ALT) code |= MYALTED; return code; } -static void markkey(int code,int status) -{ -int i; -int *ip; +static void markkey(int code, int status) { + int i; + int* ip; - if(status) - { - if(numdown>1; - mousey=event.button.y>>1; - mouseb=bs; + bs &= ~(1 << (event.button.button - 1)); + mousex = event.button.x >> 1; + mousey = event.button.y >> 1; + mouseb = bs; break; case SDL_MOUSEBUTTONDOWN: - bs|=1<<(event.button.button-1); - mousex=event.button.x>>1; - mousey=event.button.y>>1; - mouseb=bs; + bs |= 1 << (event.button.button - 1); + mousex = event.button.x >> 1; + mousey = event.button.y >> 1; + mouseb = bs; break; case SDL_MOUSEMOTION: - mousex=event.motion.x>>1; - mousey=event.motion.y>>1; + mousex = event.motion.x >> 1; + mousey = event.motion.y >> 1; break; case SDL_QUIT: exitflag = 1; @@ -815,58 +879,45 @@ int i,*ip,code; } */ -static void drawrect(int x,int y,int xs,int ys,int c) -{ -uchar *p; +static void drawrect(int x, int y, int xs, int ys, int c) { + uchar* p; gfxlock(); - p=videomem+y*stride+x; - while(ys--) - { - memset(p,c,xs); - p+=stride; + p = videomem + y * stride + x; + while (ys--) { + memset(p, c, xs); + p += stride; } } -void greyrect(int x,int y,int xsize,int ysize) -{ -static int greycolor=-1; - if(greycolor==-1) - greycolor=bestmatch(0,0,0x70); - drawrect(x,y,xsize,ysize,greycolor); +void greyrect(int x, int y, int xsize, int ysize) { + static int greycolor = -1; + if (greycolor == -1) greycolor = bestmatch(0, 0, 0x70); + drawrect(x, y, xsize, ysize, greycolor); } -void clear() -{ -int j; -uchar *p; +void clear() { + int j; + uchar* p; gfxlock(); - p=videomem; - for(j=0;j 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); diff --git a/src/list.h b/src/list.h index 450ed60..1d5ad03 100644 --- a/src/list.h +++ b/src/list.h @@ -1,10 +1,16 @@ #ifndef LIST_H #define LIST_H +#include + /* 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 @@ -36,11 +42,10 @@ void things_list_clear(listhead *head); /* listhead member must be the first mem * @head: the head for your list. * @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), */ \ - &pos->member != (head); \ - pos = list_entry(pos->member.next, __typeof__(*pos), member)) +#define list_for_each_entry(pos, head, member) \ + 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)) /** * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry @@ -49,11 +54,9 @@ void things_list_clear(listhead *head); /* listhead member must be the first mem * @head:<->the head for your list. * @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); \ - pos = n, n = list_entry(n->member.next, __typeof__(*n), member)) +#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); \ + pos = n, n = list_entry(n->member.next, __typeof__(*n), member)) #define list_empty(head) ((head) == (head)->next) diff --git a/src/matcher.c b/src/matcher.c index e308e4d..98e7119 100644 --- a/src/matcher.c +++ b/src/matcher.c @@ -1,17 +1,17 @@ +#include +#include +#include +#include +#include #include #include #include -#include -#include #include -#include -#include -#include -#include #include +#include +#include +#include #include -#include -#include #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) - { - struct timeval timeout; - fd_set readfds; - int res; + 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; - FD_ZERO(&readfds); - 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); - return size; + 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; + } + 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) && counterreg.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;i2) want=atoi(argv[i]+2); - else if(i+1 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(size0) // avoid year 203x bug... + now = longtime(); + if (now - purgetime > 0) // avoid year 203x bug... { purge(purgetime); - purgetime+=TIMETOLIVE; + purgetime += TIMETOLIVE; } } } diff --git a/src/menu.c b/src/menu.c index b6b2ed5..3983875 100644 --- a/src/menu.c +++ b/src/menu.c @@ -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> 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 master packets */ - PKT_JOIN, /* 4 bytes version #, 4 bytes joinunique #, 16 bytes name */ - PKT_QUIT, /* 4 bytes unique # */ + PKT_JOIN, /* 4 bytes version #, 4 bytes joinunique #, 16 bytes name */ + PKT_QUIT, /* 4 bytes unique # */ /* master -> slave packets */ - PKT_INVITE, /* 4 bytes unique #, 1 byte your slot (0xff for kick, no data following it) #, any # of 1:slot,16:name sets (-1 end) */ - PKT_BEGIN, /* clone of INVITE */ - 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 */ + PKT_INVITE, /* 4 bytes unique #, 1 byte your slot (0xff for kick, no data following it) #, any # of 1:slot,16:name sets (-1 end) */ + PKT_BEGIN, /* clone of INVITE */ + 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 */ /* slave -> master packets */ - PKT_MYDATA, /* 4 bytes unique #,4 bytes frame #, 1 byte data */ + PKT_MYDATA, /* 4 bytes unique #,4 bytes frame #, 1 byte data */ /* master -> slave packets */ - PKT_STEP, /* 4 bytes unique #, 4 bytes frame #, history x MAXNETNODES bytes ACT_* */ + PKT_STEP, /* 4 bytes unique #, 4 bytes frame #, history x MAXNETNODES bytes ACT_* */ PKT_INVALID = 0xff }; @@ -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>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 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; @@ -339,7 +324,7 @@ int networktraffic(void) { sendmine(mydatacount); for (;;) { - /* if we got already one packet we only wait 3msec, otherwise 30msec */ + /* if we got already one packet we only wait 3msec, otherwise 30msec */ long cur = gtime(); if (count >= 0 && cur - now > 3) break; if (exitflag || cur - now > 30) break; @@ -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) 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; } } diff --git a/src/network.h b/src/network.h index 2cdbbcf..41679f0 100644 --- a/src/network.h +++ b/src/network.h @@ -1,13 +1,11 @@ #ifndef NETWORK_H #define NETWORK_H -#include -#include #include +#include +#include -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]; diff --git a/src/sound.c b/src/sound.c index 06dc3d1..0c2e76a 100644 --- a/src/sound.c +++ b/src/sound.c @@ -1,9 +1,9 @@ -#include -#include -#include -#include #include #include +#include +#include +#include +#include #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>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;iname && 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); } } diff --git a/src/utils.c b/src/utils.c index a9e391a..4d48705 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,17 +1,17 @@ -#include "bomber.h" #include "utils.h" +#include "bomber.h" #include "gfx.h" #include -#include -#include #include +#include +#include #include -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"); } diff --git a/src/utils.h b/src/utils.h index 45b69b8..a561ddd 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,6 +1,8 @@ #ifndef UTILS_H #define UTILS_H +#include + 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;