You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

257 lines
7.4 KiB

  1. /*
  2. * ttyvaders Textmode shoot'em up
  3. * Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "config.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "common.h"
  28. static void start_game (game *);
  29. int main (int argc, char **argv)
  30. {
  31. game *g = malloc(sizeof(game));
  32. srand(time(NULL));
  33. if(ee_init())
  34. {
  35. return 1;
  36. }
  37. /* Initialize our program */
  38. g->w = ee_get_width();
  39. g->h = ee_get_height();
  40. intro();
  41. /* Go ! */
  42. start_game(g);
  43. /* Clean up */
  44. ee_end();
  45. return 0;
  46. }
  47. static void start_game (game *g)
  48. {
  49. int quit = 0;
  50. int poz = 0;
  51. int skip = 0;
  52. int purcompteur = 0;
  53. box *pausebox = NULL;
  54. g->sf = create_starfield(g);
  55. g->wp = malloc(sizeof(weapons));
  56. g->ex = malloc(sizeof(explosions));
  57. g->bo = malloc(sizeof(bonus));
  58. g->t = create_tunnel(g, g->w, g->h);
  59. g->p = create_player(g);
  60. g->al = malloc(sizeof(aliens));
  61. init_bonus(g, g->bo);
  62. init_weapons(g, g->wp);
  63. init_explosions(g, g->ex);
  64. init_aliens(g, g->al);
  65. /* Temporary stuff */
  66. g->t->w = 25;
  67. while(!quit)
  68. {
  69. char key;
  70. while((key = ee_get_key()))
  71. {
  72. switch(key)
  73. {
  74. case 'q':
  75. quit = 1;
  76. break;
  77. case 'p':
  78. poz = !poz;
  79. if(poz)
  80. {
  81. pausebox = create_box(g, g->w / 2, g->h / 2,
  82. g->w - 16, 8);
  83. }
  84. else
  85. {
  86. free_box(pausebox);
  87. }
  88. break;
  89. case '\t':
  90. ceo_alert(g);
  91. poz = 1;
  92. break;
  93. case 's':
  94. skip = 1;
  95. break;
  96. default:
  97. if(g->p->dead)
  98. {
  99. break;
  100. }
  101. switch(key)
  102. {
  103. case 'h':
  104. g->p->vx = -2;
  105. break;
  106. case 'j':
  107. if(g->p->y < g->h - 3) g->p->y += 1;
  108. break;
  109. case 'k':
  110. if(g->p->y > 2) g->p->y -= 1;
  111. break;
  112. case 'l':
  113. g->p->vx = 2;
  114. break;
  115. case 'n':
  116. if(g->p->special >= COST_NUKE)
  117. {
  118. g->p->special -= COST_NUKE;
  119. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, 0, WEAPON_NUKE);
  120. }
  121. break;
  122. case 'f':
  123. if(g->p->special >= COST_FRAGBOMB)
  124. {
  125. g->p->special -= COST_FRAGBOMB;
  126. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_FRAGBOMB);
  127. }
  128. break;
  129. case 'b':
  130. if(g->p->special >= COST_BEAM)
  131. {
  132. g->p->special -= COST_BEAM;
  133. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, 0, WEAPON_BEAM);
  134. }
  135. break;
  136. case ' ':
  137. if(g->p->weapon == 0)
  138. {
  139. g->p->weapon = 4;
  140. add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
  141. add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
  142. /* Extra schtuph */
  143. add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER);
  144. add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER);
  145. /* More schtuph */
  146. add_weapon(g, g->wp, (g->p->x - 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  147. add_weapon(g, g->wp, (g->p->x + 2) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  148. /* Even more schtuph */
  149. add_weapon(g, g->wp, g->p->x << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  150. add_weapon(g, g->wp, (g->p->x + 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  151. /* Extra schtuph */
  152. add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, -32, 0, WEAPON_SEEKER);
  153. add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 32, 0, WEAPON_SEEKER);
  154. /* MORE SCHTUPH! */
  155. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_BOMB);
  156. }
  157. break;
  158. }
  159. }
  160. }
  161. if(!poz || skip)
  162. {
  163. skip = 0;
  164. /* XXX: to be removed */
  165. if(ee_rand(0, 9) == 0)
  166. {
  167. int list[3] = { ALIEN_FOO, ALIEN_BAR, ALIEN_BAZ };
  168. add_alien(g, g->al, 0, rand() % g->h / 2, list[ee_rand(0,2)]);
  169. }
  170. /* Update game rules */
  171. if(g->t->right[1] - g->t->left[1] == g->t->w)
  172. {
  173. g->t->w = 85 - g->t->w;
  174. }
  175. /* Scroll and update positions */
  176. collide_player_tunnel(g, g->p, g->t, g->ex);
  177. update_player(g, g->p);
  178. collide_player_tunnel(g, g->p, g->t, g->ex);
  179. update_starfield(g, g->sf);
  180. update_bonus(g, g->bo);
  181. update_aliens(g, g->al);
  182. collide_weapons_tunnel(g, g->wp, g->t, g->ex);
  183. collide_weapons_aliens(g, g->wp, g->al, g->ex);
  184. update_weapons(g, g->wp);
  185. collide_weapons_tunnel(g, g->wp, g->t, g->ex);
  186. collide_weapons_aliens(g, g->wp, g->al, g->ex);
  187. update_explosions(g, g->ex);
  188. update_tunnel(g, g->t);
  189. }
  190. /* Clear screen */
  191. ee_clear();
  192. /* Print starfield, tunnel, aliens, player and explosions */
  193. draw_starfield(g, g->sf);
  194. draw_aliens(g, g->al);
  195. draw_tunnel(g, g->t);
  196. draw_bonus(g, g->bo);
  197. draw_explosions(g, g->ex);
  198. draw_weapons(g, g->wp);
  199. draw_player(g, g->p);
  200. draw_status(g);
  201. /* Print pause box if needed */
  202. if(poz)
  203. {
  204. pausebox->frame++;
  205. draw_box(g, pausebox);
  206. }
  207. /* Refresh */
  208. ee_refresh();
  209. purcompteur++;
  210. }
  211. if(pausebox)
  212. {
  213. free_box(pausebox);
  214. }
  215. free_starfield(g, g->sf);
  216. free_tunnel(g->t);
  217. free_player(g->p);
  218. }