253 lignes
7.3 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 <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include "common.h"
  27. static void start_game (game *);
  28. int main (int argc, char **argv)
  29. {
  30. game *g = malloc(sizeof(game));
  31. srand(time(NULL));
  32. if(ee_init())
  33. {
  34. return 1;
  35. }
  36. /* Initialize our program */
  37. g->w = ee_get_width();
  38. g->h = ee_get_height();
  39. /* Go ! */
  40. start_game(g);
  41. /* Clean up */
  42. ee_end();
  43. return 0;
  44. }
  45. static void start_game (game *g)
  46. {
  47. int quit = 0;
  48. int poz = 0;
  49. int skip = 0;
  50. int purcompteur = 0;
  51. box *pausebox = NULL;
  52. g->sf = create_starfield(g);
  53. g->wp = malloc(sizeof(weapons));
  54. g->ex = malloc(sizeof(explosions));
  55. g->bo = malloc(sizeof(bonus));
  56. g->t = create_tunnel(g, g->w, g->h);
  57. g->p = create_player(g);
  58. g->al = malloc(sizeof(aliens));
  59. init_bonus(g, g->bo);
  60. init_weapons(g, g->wp);
  61. init_explosions(g, g->ex);
  62. init_aliens(g, g->al);
  63. /* Temporary stuff */
  64. g->t->w = 25;
  65. while(!quit)
  66. {
  67. char key;
  68. while((key = ee_get_key()))
  69. {
  70. switch(key)
  71. {
  72. case 'q':
  73. quit = 1;
  74. break;
  75. case 'p':
  76. poz = !poz;
  77. if(poz)
  78. {
  79. pausebox = create_box(g, g->w / 2, g->h / 2,
  80. g->w - 16, 8);
  81. }
  82. else
  83. {
  84. free_box(pausebox);
  85. }
  86. break;
  87. case '\t':
  88. ceo_alert(g);
  89. poz = 1;
  90. break;
  91. case 's':
  92. skip = 1;
  93. break;
  94. default:
  95. if(g->p->dead)
  96. {
  97. break;
  98. }
  99. switch(key)
  100. {
  101. case 'h':
  102. g->p->vx = -2;
  103. break;
  104. case 'j':
  105. if(g->p->y < g->h - 2) g->p->y += 1;
  106. break;
  107. case 'k':
  108. if(g->p->y > 1) g->p->y -= 1;
  109. break;
  110. case 'l':
  111. g->p->vx = 2;
  112. break;
  113. case 'n':
  114. if(g->p->special >= COST_NUKE)
  115. {
  116. g->p->special -= COST_NUKE;
  117. add_weapon(g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, 0, WEAPON_NUKE);
  118. }
  119. break;
  120. case 'f':
  121. if(g->p->special >= COST_FRAGBOMB)
  122. {
  123. g->p->special -= COST_FRAGBOMB;
  124. add_weapon(g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, -16, WEAPON_FRAGBOMB);
  125. }
  126. break;
  127. case 'b':
  128. if(g->p->special >= COST_BEAM)
  129. {
  130. g->p->special -= COST_BEAM;
  131. add_weapon(g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, 0, WEAPON_BEAM);
  132. }
  133. break;
  134. case ' ':
  135. if(g->p->weapon == 0)
  136. {
  137. g->p->weapon = 4;
  138. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
  139. add_weapon(g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
  140. /* Extra schtuph */
  141. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER);
  142. add_weapon(g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER);
  143. /* More schtuph */
  144. add_weapon(g, g->wp, (g->p->x + 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  145. add_weapon(g, g->wp, (g->p->x + 4) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  146. /* Even more schtuph */
  147. add_weapon(g, g->wp, (g->p->x + 2) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  148. add_weapon(g, g->wp, (g->p->x + 3) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  149. /* Extra schtuph */
  150. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, -32, 0, WEAPON_SEEKER);
  151. add_weapon(g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 32, 0, WEAPON_SEEKER);
  152. /* MORE SCHTUPH! */
  153. add_weapon(g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, -16, WEAPON_BOMB);
  154. }
  155. break;
  156. }
  157. }
  158. }
  159. if(!poz || skip)
  160. {
  161. skip = 0;
  162. /* XXX: to be removed */
  163. if(ee_rand(0,10) == 0)
  164. {
  165. int list[3] = { ALIEN_FOO, ALIEN_BAR, ALIEN_BAZ };
  166. add_alien(g, g->al, 0, rand() % g->h / 2, list[ee_rand(0,3)]);
  167. }
  168. /* Update game rules */
  169. if(g->t->right[1] - g->t->left[1] == g->t->w)
  170. {
  171. g->t->w = 85 - g->t->w;
  172. }
  173. /* Scroll and update positions */
  174. collide_player_tunnel(g, g->p, g->t, g->ex);
  175. update_player(g, g->p);
  176. collide_player_tunnel(g, g->p, g->t, g->ex);
  177. update_starfield(g, g->sf);
  178. update_bonus(g, g->bo);
  179. update_aliens(g, g->al);
  180. collide_weapons_tunnel(g, g->wp, g->t, g->ex);
  181. collide_weapons_aliens(g, g->wp, g->al, g->ex);
  182. update_weapons(g, g->wp);
  183. collide_weapons_tunnel(g, g->wp, g->t, g->ex);
  184. collide_weapons_aliens(g, g->wp, g->al, g->ex);
  185. update_explosions(g, g->ex);
  186. update_tunnel(g, g->t);
  187. }
  188. /* Clear screen */
  189. ee_clear();
  190. /* Print starfield, tunnel, aliens, player and explosions */
  191. draw_starfield(g, g->sf);
  192. draw_aliens(g, g->al);
  193. draw_tunnel(g, g->t);
  194. draw_bonus(g, g->bo);
  195. draw_explosions(g, g->ex);
  196. draw_weapons(g, g->wp);
  197. draw_player(g, g->p);
  198. draw_status(g);
  199. /* Print pause box if needed */
  200. if(poz)
  201. {
  202. pausebox->frame++;
  203. draw_box(g, pausebox);
  204. }
  205. /* Refresh */
  206. ee_refresh();
  207. purcompteur++;
  208. }
  209. if(pausebox)
  210. {
  211. free_box(pausebox);
  212. }
  213. free_starfield(g, g->sf);
  214. free_tunnel(g->t);
  215. free_player(g->p);
  216. }