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.

collide.c 8.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <stdlib.h>
  24. #include "common.h"
  25. void collide_weapons_tunnel(game *g, weapons *wp, tunnel *t, explosions *ex)
  26. {
  27. int i, j, x, y;
  28. for(i = 0; i < WEAPONS; i++)
  29. {
  30. x = wp->x[i] >> 4;
  31. y = wp->y[i] >> 4;
  32. switch(wp->type[i])
  33. {
  34. case WEAPON_LIGHTNING:
  35. case WEAPON_NONE:
  36. break;
  37. case WEAPON_SEEKER:
  38. case WEAPON_BOMB:
  39. case WEAPON_FRAGBOMB:
  40. if(y < 0 || y >= g->h)
  41. {
  42. break;
  43. }
  44. if(x <= t->left[y]
  45. || x >= t->right[y])
  46. {
  47. int damage = wp->type[i] == WEAPON_SEEKER ? 1 : 2;
  48. add_explosion(g, ex, x, y, 0, 1, wp->type[i] == WEAPON_SEEKER ? EXPLOSION_SMALL : EXPLOSION_MEDIUM);
  49. if(x <= t->left[y])
  50. {
  51. if(y-2 >= 0) t->left[y-2] -= damage - 1;
  52. if(y-1 >= 0) t->left[y-1] -= damage;
  53. t->left[y] -= damage + 1;
  54. if(y+1 < g->h) t->left[y+1] -= damage;
  55. if(y+2 < g->h) t->left[y+2] -= damage - 1;
  56. }
  57. else
  58. {
  59. if(y-2 >= 0) t->right[y-2] += damage - 1;
  60. if(y-1 >= 0) t->right[y-1] += damage;
  61. t->right[y] += damage + 1;
  62. if(y+1 < g->h) t->right[y+1] += damage;
  63. if(y+2 < g->h) t->right[y+2] += damage - 1;
  64. }
  65. if(wp->type[i] == WEAPON_FRAGBOMB)
  66. {
  67. wp->n[i] = -1;
  68. }
  69. else
  70. {
  71. wp->type[i] = WEAPON_NONE;
  72. }
  73. }
  74. break;
  75. case WEAPON_LASER:
  76. for(j = GET_MIN(0, wp->vy[i] >> 4);
  77. j < GET_MAX(0, wp->vy[i] >> 4);
  78. j++)
  79. {
  80. if(y+j >= g->h || y+j < 0)
  81. {
  82. continue;
  83. }
  84. if(x <= t->left[y+j] || x >= t->right[y+j])
  85. {
  86. add_explosion(g, ex, x, y+j, 0, 1, EXPLOSION_SMALL);
  87. wp->type[i] = WEAPON_NONE;
  88. if(x <= t->left[y+j])
  89. {
  90. if(y+j-1 >= 0) t->left[y+j-1]--;
  91. t->left[y+j] -= 2;
  92. if(y+j+1 < g->h) t->left[y+j+1]--;
  93. }
  94. else
  95. {
  96. if(y+j-1 >= 0) t->right[y+j-1]++;
  97. t->right[y+j] += 2;
  98. if(y+j+1 < g->h) t->right[y+j+1]++;
  99. }
  100. break;
  101. }
  102. }
  103. break;
  104. case WEAPON_BEAM:
  105. if(wp->n[i] > 19)
  106. {
  107. break;
  108. }
  109. j = (29 - wp->n[i]) * (29 - wp->n[i]) / 8;
  110. j = GET_MIN(y, j);
  111. for(; j > 0; j--)
  112. {
  113. if(x - 2 <= t->left[y-j])
  114. {
  115. add_explosion(g, ex, GET_MIN(t->left[y-j], x+3), y-j, 0, 1, EXPLOSION_SMALL);
  116. t->left[y-j] -= ee_rand(0,2);
  117. }
  118. else if(x + 3 >= t->right[y-j])
  119. {
  120. add_explosion(g, ex, GET_MAX(t->right[y-j], x-2), y-j, 0, 1, EXPLOSION_SMALL);
  121. t->right[y-j] += ee_rand(0,2);
  122. }
  123. }
  124. break;
  125. case WEAPON_NUKE:
  126. /* The nuke does not break the tunnel */
  127. break;
  128. }
  129. }
  130. }
  131. void collide_weapons_aliens(game *g, weapons *wp, aliens *al, explosions *ex)
  132. {
  133. int i, j, k, x, y;
  134. for(i = 0; i < WEAPONS; i++)
  135. {
  136. int ok = 0;
  137. int r;
  138. x = wp->x[i] >> 4;
  139. y = wp->y[i] >> 4;
  140. switch(wp->type[i])
  141. {
  142. case WEAPON_LIGHTNING:
  143. case WEAPON_NONE:
  144. break;
  145. case WEAPON_NUKE:
  146. /* Big nuke */
  147. r = (29 - wp->n[i]) * (29 - wp->n[i]) / 8;
  148. for(j = 0; j < ALIENS; j++)
  149. {
  150. if(al->type[j] == ALIEN_NONE || al->life[j] < 0)
  151. {
  152. continue;
  153. }
  154. if(wp->n[i] == 0 /* Nuke destroys _everything_ */ ||
  155. (al->x[j] - x) * (al->x[j] - x)
  156. + 4 * (al->y[j] - y) * (al->y[j] - y)
  157. <= r * r)
  158. {
  159. /* Kill alien, not nuke */
  160. al->life[j] -= 10;
  161. }
  162. }
  163. break;
  164. case WEAPON_BEAM:
  165. if(wp->n[i] > 19)
  166. {
  167. break;
  168. }
  169. r = (29 - wp->n[i]) * (29 - wp->n[i]) / 8;
  170. for(j = 0; j < ALIENS; j++)
  171. {
  172. if(al->type[j] == ALIEN_NONE || al->life[j] < 0)
  173. {
  174. continue;
  175. }
  176. if(x >= al->x[j] && x <= al->x[j] + 4
  177. && y >= al->y[j] + 2 && y-5-r <= al->y[j])
  178. {
  179. al->life[j] -= 4;
  180. }
  181. }
  182. break;
  183. case WEAPON_LASER:
  184. for(j = 0; j < ALIENS; j++)
  185. {
  186. if(al->type[j] == ALIEN_NONE || al->life[j] < 0)
  187. {
  188. continue;
  189. }
  190. for(k = GET_MIN(0, wp->vy[i] >> 4);
  191. k < GET_MAX(0, wp->vy[i] >> 4);
  192. k++)
  193. {
  194. if(x >= al->x[j] && x <= al->x[j] + 4
  195. && y+k >= al->y[j] && y+k <= al->y[j] + 2)
  196. {
  197. al->life[j]--;
  198. ok = 1;
  199. break;
  200. }
  201. }
  202. }
  203. if(ok)
  204. {
  205. add_explosion(g, ex, x, y+1, 0, 0, EXPLOSION_SMALL);
  206. wp->type[i] = WEAPON_NONE;
  207. }
  208. break;
  209. case WEAPON_SEEKER:
  210. case WEAPON_BOMB:
  211. case WEAPON_FRAGBOMB:
  212. for(j = 0; j < ALIENS; j++)
  213. {
  214. if(al->type[j] == ALIEN_NONE || al->life[j] < 0)
  215. {
  216. continue;
  217. }
  218. if(x >= al->x[j] && x <= al->x[j] + 4
  219. && y >= al->y[j] && y <= al->y[j] + 2)
  220. {
  221. al->life[j] -= wp->type[i] == WEAPON_SEEKER ? 1 : 5;
  222. ok = 1;
  223. }
  224. }
  225. if(ok)
  226. {
  227. add_explosion(g, ex, x, y+1, 0, 0, wp->type[i] == WEAPON_SEEKER ? EXPLOSION_SMALL : EXPLOSION_MEDIUM);
  228. if(wp->type[i] == WEAPON_FRAGBOMB)
  229. {
  230. wp->n[i] = -1;
  231. }
  232. else
  233. {
  234. wp->type[i] = WEAPON_NONE;
  235. }
  236. }
  237. break;
  238. }
  239. }
  240. }
  241. void collide_player_tunnel(game *g, player *p, tunnel *t, explosions *ex)
  242. {
  243. if(p->dead)
  244. {
  245. return;
  246. }
  247. if(p->x - 2 <= t->left[p->y])
  248. {
  249. p->x += 3;
  250. p->vx = 2;
  251. add_explosion(g, ex, p->x-1, p->y, 0, 0, EXPLOSION_SMALL);
  252. p->life -= 180;
  253. }
  254. else if(p->x + 3 >= t->right[p->y])
  255. {
  256. p->x -= 3;
  257. p->vx = -2;
  258. add_explosion(g, ex, p->x+2, p->y, 0, 0, EXPLOSION_SMALL);
  259. p->life -= 180;
  260. }
  261. }