diff --git a/libee/Makefile.am b/libee/Makefile.am index fb7e6e2..e7e769b 100644 --- a/libee/Makefile.am +++ b/libee/Makefile.am @@ -15,6 +15,6 @@ CPPFLAGS_ncurses = -DUSE_NCURSES endif lib_LIBRARIES = libee.a -libee_a_SOURCES = ee.c ee.h +libee_a_SOURCES = ee.c ee.h io.c math.c libee_a_CPPFLAGS = $(CPPFLAGS_slang) $(CPPFLAGS_ncurses) diff --git a/libee/ee.c b/libee/ee.c index 3512437..0706079 100644 --- a/libee/ee.c +++ b/libee/ee.c @@ -204,25 +204,3 @@ void ee_end(void) #endif } -char ee_get_key(void) -{ -#ifdef USE_SLANG - if(SLang_input_pending(0)) - { - return SLang_getkey(); - } -#elif USE_NCURSES - char key = getch(); - - if(key != ERR) - { - return key; - } -#else - return 0; - -#endif - - return 0; -} - diff --git a/libee/ee.h b/libee/ee.h index 61f13a1..37d1ad3 100644 --- a/libee/ee.h +++ b/libee/ee.h @@ -72,3 +72,6 @@ void ee_end(void); char ee_get_key(void); +int ee_rand(int, int); +int ee_sqrt(int); + diff --git a/libee/io.c b/libee/io.c new file mode 100644 index 0000000..a50a19f --- /dev/null +++ b/libee/io.c @@ -0,0 +1,48 @@ +/* + * libee ASCII-Art library + * Copyright (c) 2002, 2003 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "config.h" + +#include "ee.h" + +char ee_get_key(void) +{ +#ifdef USE_SLANG + if(SLang_input_pending(0)) + { + return SLang_getkey(); + } +#elif USE_NCURSES + char key = getch(); + + if(key != ERR) + { + return key; + } +#else + return 0; + +#endif + + return 0; +} + diff --git a/src/math.c b/libee/math.c similarity index 74% rename from src/math.c rename to libee/math.c index a2f9a2e..c87c32e 100644 --- a/src/math.c +++ b/libee/math.c @@ -1,6 +1,6 @@ /* - * ttyvaders Textmode shoot'em up - * Copyright (c) 2002 Sam Hocevar + * libee ASCII-Art library + * Copyright (c) 2002, 2003 Sam Hocevar * All Rights Reserved * * $Id$ @@ -20,17 +20,28 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include "common.h" +#include "config.h" -int r00t(int a) +#include + +#include "ee.h" + +int ee_rand(int min, int max) +{ + return min + (int)((1.0*(max-min)) * rand() / (RAND_MAX+1.0)); +} + +int ee_sqrt(int a) { - int x = a > 100000 ? 1000 : a > 1000 ? 100 : a > 10 ? 10 : 1; + int x; if(a <= 0) { return 0; } + x = a > 100000 ? 1000 : a > 1000 ? 100 : a > 10 ? 10 : 1; + /* Newton's method. Three iterations would be more than enough. */ x = (x * x + a) / x / 2; x = (x * x + a) / x / 2; diff --git a/src/Makefile.am b/src/Makefile.am index dd2c0f7..0d05759 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,7 +27,6 @@ ttyvaders_SOURCES = \ common.h \ explosions.c \ main.c \ - math.c \ overlay.c \ player.c \ starfield.c \ diff --git a/src/aliens.c b/src/aliens.c index 8f88976..1a9d896 100644 --- a/src/aliens.c +++ b/src/aliens.c @@ -72,7 +72,7 @@ void update_aliens(game *g, aliens *al) { add_explosion(g, g->ex, al->x[i], al->y[i], 0, 0, EXPLOSION_MEDIUM); al->type[i] = ALIEN_NONE; - add_bonus(g, g->bo, al->x[i], al->y[i], GET_RAND(0,5) ? BONUS_GREEN : BONUS_LIFE); + add_bonus(g, g->bo, al->x[i], al->y[i], ee_rand(0,5) ? BONUS_GREEN : BONUS_LIFE); } /* Update coordinates */ diff --git a/src/collide.c b/src/collide.c index d539d2d..a59b252 100644 --- a/src/collide.c +++ b/src/collide.c @@ -125,12 +125,12 @@ void collide_weapons_tunnel(game *g, weapons *wp, tunnel *t, explosions *ex) if(x - 2 <= t->left[y-j]) { add_explosion(g, ex, GET_MIN(t->left[y-j], x+3), y-j, 0, 1, EXPLOSION_SMALL); - t->left[y-j] -= GET_RAND(0,3); + t->left[y-j] -= ee_rand(0,3); } else if(x + 3 >= t->right[y-j]) { add_explosion(g, ex, GET_MAX(t->right[y-j], x-2), y-j, 0, 1, EXPLOSION_SMALL); - t->right[y-j] += GET_RAND(0,3); + t->right[y-j] += ee_rand(0,3); } } break; diff --git a/src/common.h b/src/common.h index bca55c0..0b7d1cb 100644 --- a/src/common.h +++ b/src/common.h @@ -47,7 +47,6 @@ /* * Useful macros */ -#define GET_RAND(p,q) ((p)+(int)((1.0*((q)-(p)))*rand()/(RAND_MAX+1.0))) #define GET_MAX(a,b) ((a)>(b)?(a):(b)) #define GET_MIN(a,b) ((a)<(b)?(a):(b)) @@ -186,11 +185,6 @@ void add_explosion(game *, explosions *, int, int, int, int, int); void draw_explosions(game *, explosions *); void update_explosions(game *, explosions *); -/* - * From math.c - */ -int r00t(int); - /* * From overlay.c */ diff --git a/src/explosions.c b/src/explosions.c index 09ef5f3..6ca9472 100644 --- a/src/explosions.c +++ b/src/explosions.c @@ -73,7 +73,7 @@ void draw_explosions(game *g, explosions *ex) #if 0 ee_color(GREEN); ee_goto(ex->x[i] + 3, ex->y[i]); - switch(GET_RAND(0,3)) + switch(ee_rand(0,3)) { case 0: ee_putchar('p'); diff --git a/src/main.c b/src/main.c index ec73fb5..86ae445 100644 --- a/src/main.c +++ b/src/main.c @@ -182,11 +182,11 @@ static void start_game (game *g) skip = 0; /* XXX: to be removed */ - if(GET_RAND(0,10) == 0) + if(ee_rand(0,10) == 0) { int list[3] = { ALIEN_FOO, ALIEN_BAR, ALIEN_BAZ }; - add_alien(g, g->al, 0, rand() % g->h / 2, list[GET_RAND(0,3)]); + add_alien(g, g->al, 0, rand() % g->h / 2, list[ee_rand(0,3)]); } /* Update game rules */ diff --git a/src/starfield.c b/src/starfield.c index 44342ec..85a3082 100644 --- a/src/starfield.c +++ b/src/starfield.c @@ -33,11 +33,11 @@ starfield * create_starfield(game *g) for(i = 0; i < STARS; i++) { - s[i].x = GET_RAND(0, g->w); - s[i].y = GET_RAND(0, g->h); - s[i].z = GET_RAND(1, 4); - s[i].c = GET_RAND(6, 8); - s[i].ch = GET_RAND(0, 2) ? '.' : '\''; + s[i].x = ee_rand(0, g->w); + s[i].y = ee_rand(0, g->h); + s[i].z = ee_rand(1, 4); + s[i].c = ee_rand(6, 8); + s[i].ch = ee_rand(0, 2) ? '.' : '\''; } return s; @@ -66,11 +66,11 @@ void update_starfield(game *g, starfield *s) { if(s[i].x < 0) { - s[i].x = GET_RAND(0, g->w); + s[i].x = ee_rand(0, g->w); s[i].y = 0; - s[i].z = GET_RAND(1, 3); - s[i].c = GET_RAND(6, 8); - s[i].ch = GET_RAND(0, 2) ? '.' : '\''; + s[i].z = ee_rand(1, 3); + s[i].c = ee_rand(6, 8); + s[i].ch = ee_rand(0, 2) ? '.' : '\''; } else if(s[i].y < g->h-1) { diff --git a/src/tunnel.c b/src/tunnel.c index 64e9425..955d63d 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -169,8 +169,8 @@ void update_tunnel(game *g, tunnel *t) } /* Generate new values */ - i = delta[GET_RAND(0,6)]; - j = delta[GET_RAND(0,6)]; + i = delta[ee_rand(0,6)]; + j = delta[ee_rand(0,6)]; /* Check in which direction we need to alter tunnel */ if(t->right[1] - t->left[1] < t->w) diff --git a/src/weapons.c b/src/weapons.c index 71e2ecd..36fbef1 100644 --- a/src/weapons.c +++ b/src/weapons.c @@ -172,7 +172,7 @@ void update_weapons(game *g, weapons *wp) /* Normalize direction */ if(dx | dy) { - int norm = r00t(dx * dx + 4 * dy * dy); + int norm = ee_sqrt(dx * dx + 4 * dy * dy); dx = dx * 32 / norm; dy = dy * 32 / norm; } @@ -184,7 +184,7 @@ void update_weapons(game *g, weapons *wp) /* Normalize speed */ if(dx | dy) { - int norm = r00t(dx * dx + 4 * dy * dy); + int norm = ee_sqrt(dx * dx + 4 * dy * dy); wp->vx[i] = dx * 32 / norm; wp->vy[i] = dy * 32 / norm; }