+ Moved ee_get_key() here. * libee/math.c: + Moved r00t() here and renamed it to ee_sqrt(). + Moved GET_RAND() here and renamed it to ee_rand(). * src/math.c: + Removed this deprecated file. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/ttyvaders/trunk@90 92316355-f0b4-4df1-b90c-862c8a59935fmaster
@@ -15,6 +15,6 @@ CPPFLAGS_ncurses = -DUSE_NCURSES | |||||
endif | endif | ||||
lib_LIBRARIES = libee.a | 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) | libee_a_CPPFLAGS = $(CPPFLAGS_slang) $(CPPFLAGS_ncurses) | ||||
@@ -204,25 +204,3 @@ void ee_end(void) | |||||
#endif | #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; | |||||
} | |||||
@@ -72,3 +72,6 @@ void ee_end(void); | |||||
char ee_get_key(void); | char ee_get_key(void); | ||||
int ee_rand(int, int); | |||||
int ee_sqrt(int); | |||||
@@ -0,0 +1,48 @@ | |||||
/* | |||||
* libee ASCII-Art library | |||||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||||
* 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; | |||||
} | |||||
@@ -1,6 +1,6 @@ | |||||
/* | /* | ||||
* ttyvaders Textmode shoot'em up | |||||
* Copyright (c) 2002 Sam Hocevar <sam@zoy.org> | |||||
* libee ASCII-Art library | |||||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||||
* All Rights Reserved | * All Rights Reserved | ||||
* | * | ||||
* $Id$ | * $Id$ | ||||
@@ -20,17 +20,28 @@ | |||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||||
*/ | */ | ||||
#include "common.h" | |||||
#include "config.h" | |||||
int r00t(int a) | |||||
#include <stdlib.h> | |||||
#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) | if(a <= 0) | ||||
{ | { | ||||
return 0; | return 0; | ||||
} | } | ||||
x = a > 100000 ? 1000 : a > 1000 ? 100 : a > 10 ? 10 : 1; | |||||
/* Newton's method. Three iterations would be more than enough. */ | /* Newton's method. Three iterations would be more than enough. */ | ||||
x = (x * x + a) / x / 2; | x = (x * x + a) / x / 2; | ||||
x = (x * x + a) / x / 2; | x = (x * x + a) / x / 2; |
@@ -27,7 +27,6 @@ ttyvaders_SOURCES = \ | |||||
common.h \ | common.h \ | ||||
explosions.c \ | explosions.c \ | ||||
main.c \ | main.c \ | ||||
math.c \ | |||||
overlay.c \ | overlay.c \ | ||||
player.c \ | player.c \ | ||||
starfield.c \ | starfield.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); | add_explosion(g, g->ex, al->x[i], al->y[i], 0, 0, EXPLOSION_MEDIUM); | ||||
al->type[i] = ALIEN_NONE; | 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 */ | /* Update coordinates */ | ||||
@@ -125,12 +125,12 @@ void collide_weapons_tunnel(game *g, weapons *wp, tunnel *t, explosions *ex) | |||||
if(x - 2 <= t->left[y-j]) | 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); | 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]) | 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); | 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; | break; | ||||
@@ -47,7 +47,6 @@ | |||||
/* | /* | ||||
* Useful macros | * 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_MAX(a,b) ((a)>(b)?(a):(b)) | ||||
#define GET_MIN(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 draw_explosions(game *, explosions *); | ||||
void update_explosions(game *, explosions *); | void update_explosions(game *, explosions *); | ||||
/* | |||||
* From math.c | |||||
*/ | |||||
int r00t(int); | |||||
/* | /* | ||||
* From overlay.c | * From overlay.c | ||||
*/ | */ | ||||
@@ -73,7 +73,7 @@ void draw_explosions(game *g, explosions *ex) | |||||
#if 0 | #if 0 | ||||
ee_color(GREEN); | ee_color(GREEN); | ||||
ee_goto(ex->x[i] + 3, ex->y[i]); | ee_goto(ex->x[i] + 3, ex->y[i]); | ||||
switch(GET_RAND(0,3)) | |||||
switch(ee_rand(0,3)) | |||||
{ | { | ||||
case 0: | case 0: | ||||
ee_putchar('p'); | ee_putchar('p'); | ||||
@@ -182,11 +182,11 @@ static void start_game (game *g) | |||||
skip = 0; | skip = 0; | ||||
/* XXX: to be removed */ | /* 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 }; | 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 */ | /* Update game rules */ | ||||
@@ -33,11 +33,11 @@ starfield * create_starfield(game *g) | |||||
for(i = 0; i < STARS; i++) | 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; | return s; | ||||
@@ -66,11 +66,11 @@ void update_starfield(game *g, starfield *s) | |||||
{ | { | ||||
if(s[i].x < 0) | 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].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) | else if(s[i].y < g->h-1) | ||||
{ | { | ||||
@@ -169,8 +169,8 @@ void update_tunnel(game *g, tunnel *t) | |||||
} | } | ||||
/* Generate new values */ | /* 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 */ | /* Check in which direction we need to alter tunnel */ | ||||
if(t->right[1] - t->left[1] < t->w) | if(t->right[1] - t->left[1] < t->w) | ||||
@@ -172,7 +172,7 @@ void update_weapons(game *g, weapons *wp) | |||||
/* Normalize direction */ | /* Normalize direction */ | ||||
if(dx | dy) | if(dx | dy) | ||||
{ | { | ||||
int norm = r00t(dx * dx + 4 * dy * dy); | |||||
int norm = ee_sqrt(dx * dx + 4 * dy * dy); | |||||
dx = dx * 32 / norm; | dx = dx * 32 / norm; | ||||
dy = dy * 32 / norm; | dy = dy * 32 / norm; | ||||
} | } | ||||
@@ -184,7 +184,7 @@ void update_weapons(game *g, weapons *wp) | |||||
/* Normalize speed */ | /* Normalize speed */ | ||||
if(dx | dy) | 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->vx[i] = dx * 32 / norm; | ||||
wp->vy[i] = dy * 32 / norm; | wp->vy[i] = dy * 32 / norm; | ||||
} | } | ||||