| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2013 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "shots.h" |
| 20 | |
| 21 | #include "player.h" |
| 22 | #include "sprite.h" |
| 23 | #include "video.h" |
| 24 | #include "varz.h" |
| 25 | |
| 26 | // I'm pretty sure the last extra entry is never used. |
| 27 | PlayerShotDataType playerShotData[MAX_PWEAPON + 1]; /* [1..MaxPWeapon+1] */ |
| 28 | JE_byte shotAvail[MAX_PWEAPON]; /* [1..MaxPWeapon] */ /*0:Avail 1-255:Duration left*/ |
| 29 | |
| 30 | void simulate_player_shots(void) |
| 31 | { |
| 32 | /* Player Shot Images */ |
| 33 | for (int z = 0; z < MAX_PWEAPON; z++) |
| 34 | { |
| 35 | if (shotAvail[z] != 0) |
| 36 | { |
| 37 | shotAvail[z]--; |
| 38 | if (z != MAX_PWEAPON - 1) |
| 39 | { |
| 40 | PlayerShotDataType* shot = &playerShotData[z]; |
| 41 | |
| 42 | shot->shotXM += shot->shotXC; |
| 43 | |
| 44 | if (shot->shotXM <= 100) |
| 45 | shot->shotX += shot->shotXM; |
| 46 | |
| 47 | shot->shotYM += shot->shotYC; |
| 48 | shot->shotY += shot->shotYM; |
| 49 | |
| 50 | if (shot->shotYM > 100) |
| 51 | { |
| 52 | shot->shotY -= 120; |
| 53 | shot->shotY += player[0].delta_y_shot_move; |
| 54 | } |
| 55 | |
| 56 | if (shot->shotComplicated != 0) |
| 57 | { |
| 58 | shot->shotDevX += shot->shotDirX; |
| 59 | shot->shotX += shot->shotDevX; |
| 60 | |
| 61 | if (abs(shot->shotDevX) == shot->shotCirSizeX) |
| 62 | shot->shotDirX = -shot->shotDirX; |
| 63 | |
| 64 | shot->shotDevY += shot->shotDirY; |
| 65 | shot->shotY += shot->shotDevY; |
| 66 | |
| 67 | if (abs(shot->shotDevY) == shot->shotCirSizeY) |
| 68 | shot->shotDirY = -shot->shotDirY; |
| 69 | /*Double Speed Circle Shots - add a second copy of above loop*/ |
| 70 | } |
| 71 | |
| 72 | int tempShotX = shot->shotX; |
| 73 | int tempShotY = shot->shotY; |
| 74 | |
| 75 | if (shot->shotX < 0 || shot->shotX > 140 || |
| 76 | shot->shotY < 0 || shot->shotY > 170) |
| 77 | { |
| 78 | shotAvail[z] = 0; |
| 79 | goto draw_player_shot_loop_end; |
| 80 | } |
| 81 | |
| 82 | /* if (shot->shotTrail != 255) |
| 83 | { |
| 84 | if (shot->shotTrail == 98) |
| 85 | { |
| 86 | JE_setupExplosion(shot->shotX - shot->shotXM, shot->shotY - shot->shotYM, shot->shotTrail); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | JE_setupExplosion(shot->shotX, shot->shotY, shot->shotTrail); |
| 91 | } |
| 92 | }*/ |
| 93 | |
| 94 | JE_word anim_frame = shot->shotGr + shot->shotAni; |
| 95 | if (++shot->shotAni == shot->shotAniMax) |
| 96 | shot->shotAni = 0; |
| 97 | |
| 98 | if (anim_frame < 60000) |
| 99 | { |
| 100 | if (anim_frame > 1000) |
| 101 | anim_frame = anim_frame % 1000; |
| 102 | if (anim_frame > 500) |
| 103 | blit_sprite2(VGAScreen, tempShotX+1, tempShotY, spriteSheet12, anim_frame - 500); |
| 104 | else |
| 105 | blit_sprite2(VGAScreen, tempShotX+1, tempShotY, spriteSheet8, anim_frame); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | draw_player_shot_loop_end: |
| 110 | ; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static const JE_word linkMultiGr[17] /* [0..16] */ = |
| 116 | {77,221,183,301,1,282,164,202,58,201,163,281,39,300,182,220,77}; |
| 117 | static const JE_word linkSonicGr[17] /* [0..16] */ = |
| 118 | {85,242,131,303,47,284,150,223,66,224,149,283,9,302,130,243,85}; |
| 119 | static const JE_word linkMult2Gr[17] /* [0..16] */ = |
| 120 | {78,299,295,297,2,278,276,280,59,279,275,277,40,296,294,298,78}; |
| 121 | |
| 122 | void player_shot_set_direction(JE_integer shot_id, uint weapon_id, JE_real direction) |
| 123 | { |
| 124 | PlayerShotDataType* shot = &playerShotData[shot_id]; |
| 125 | |
| 126 | shot->shotXM = -roundf(sinf(direction) * shot->shotYM); |
| 127 | shot->shotYM = -roundf(cosf(direction) * shot->shotYM); |
| 128 | |
| 129 | // Some weapons have sprites for each direction, use those. |
| 130 | int rounded_dir; |
| 131 | |
| 132 | switch (weapon_id) |
| 133 | { |
| 134 | case 27: |
| 135 | case 32: |
| 136 | case 10: |
| 137 | rounded_dir = roundf(direction * (16 / (2 * M_PI))); /*16 directions*/ |
| 138 | shot->shotGr = linkMultiGr[rounded_dir]; |
| 139 | break; |
| 140 | case 28: |
| 141 | case 33: |
| 142 | case 11: |
| 143 | rounded_dir = roundf(direction * (16 / (2 * M_PI))); /*16 directions*/ |
| 144 | shot->shotGr = linkSonicGr[rounded_dir]; |
| 145 | break; |
| 146 | case 30: |
| 147 | case 35: |
| 148 | case 14: |
| 149 | if (direction > M_PI_2 && direction < M_PI + M_PI_2) |
| 150 | { |
| 151 | shot->shotYC = 1; |
| 152 | } |
| 153 | break; |
| 154 | case 38: |
| 155 | case 22: |
| 156 | rounded_dir = roundf(direction * (16 / (2 * M_PI))); /*16 directions*/ |
| 157 | shot->shotGr = linkMult2Gr[rounded_dir]; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | bool player_shot_move_and_draw( |
| 163 | int shot_id, bool* out_is_special, |
| 164 | int* out_shotx, int* out_shoty, |
| 165 | JE_integer* out_shot_damage, JE_byte* out_blast_filter, |
| 166 | JE_byte* out_chain, JE_byte* out_playerNum, |
| 167 | JE_word* out_special_radiusw, JE_word* out_special_radiush) |
| 168 | { |
| 169 | PlayerShotDataType* shot = &playerShotData[shot_id]; |
| 170 | |
| 171 | shotAvail[shot_id]--; |
| 172 | if (shot_id != MAX_PWEAPON - 1) |
| 173 | { |
| 174 | shot->shotXM += shot->shotXC; |
| 175 | shot->shotX += shot->shotXM; |
| 176 | JE_integer tmp_shotXM = shot->shotXM; |
| 177 | |
| 178 | if (shot->shotXM > 100) |
| 179 | { |
| 180 | if (shot->shotXM == 101) |
| 181 | { |
| 182 | shot->shotX -= 101; |
| 183 | shot->shotX += player[shot->playerNumber-1].delta_x_shot_move; |
| 184 | shot->shotY += player[shot->playerNumber-1].delta_y_shot_move; |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | shot->shotX -= 120; |
| 189 | shot->shotX += player[shot->playerNumber-1].delta_x_shot_move; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | shot->shotYM += shot->shotYC; |
| 194 | shot->shotY += shot->shotYM; |
| 195 | |
| 196 | if (shot->shotYM > 100) |
| 197 | { |
| 198 | shot->shotY -= 120; |
| 199 | shot->shotY += player[shot->playerNumber-1].delta_y_shot_move; |
| 200 | } |
| 201 | |
| 202 | if (shot->shotComplicated != 0) |
| 203 | { |
| 204 | shot->shotDevX += shot->shotDirX; |
| 205 | shot->shotX += shot->shotDevX; |
| 206 | |
| 207 | if (abs(shot->shotDevX) == shot->shotCirSizeX) |
| 208 | shot->shotDirX = -shot->shotDirX; |
| 209 | |
| 210 | shot->shotDevY += shot->shotDirY; |
| 211 | shot->shotY += shot->shotDevY; |
| 212 | |
| 213 | if (abs(shot->shotDevY) == shot->shotCirSizeY) |
| 214 | shot->shotDirY = -shot->shotDirY; |
| 215 | |
| 216 | /*Double Speed Circle Shots - add a second copy of above loop*/ |
| 217 | } |
| 218 | |
| 219 | *out_shotx = shot->shotX; |
| 220 | *out_shoty = shot->shotY; |
| 221 | |
| 222 | if (shot->shotX < -34 || shot->shotX > 290 || |
| 223 | shot->shotY < -15 || shot->shotY > 190) |
| 224 | { |
| 225 | shotAvail[shot_id] = 0; |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | if (shot->shotTrail != 255) |
| 230 | { |
| 231 | if (shot->shotTrail == 98) |
| 232 | JE_setupExplosion(shot->shotX - shot->shotXM, shot->shotY - shot->shotYM, 0, shot->shotTrail, false, false); |
| 233 | else |
| 234 | JE_setupExplosion(shot->shotX, shot->shotY, 0, shot->shotTrail, false, false); |
| 235 | } |
| 236 | |
| 237 | if (shot->aimAtEnemy != 0) |
| 238 | { |
| 239 | if (--shot->aimDelay == 0) |
| 240 | { |
| 241 | shot->aimDelay = shot->aimDelayMax; |
| 242 | |
| 243 | if (enemyAvail[shot->aimAtEnemy - 1] != 1) |
| 244 | { |
| 245 | if (shot->shotX < enemy[shot->aimAtEnemy - 1].ex) |
| 246 | shot->shotXM++; |
| 247 | else |
| 248 | shot->shotXM--; |
| 249 | |
| 250 | if (shot->shotY < enemy[shot->aimAtEnemy - 1].ey) |
| 251 | shot->shotYM++; |
| 252 | else |
| 253 | shot->shotYM--; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | if (shot->shotXM > 0) |
| 258 | shot->shotXM++; |
| 259 | else |
| 260 | shot->shotXM--; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | JE_word sprite_frame = shot->shotGr + shot->shotAni; |
| 266 | if (++shot->shotAni == shot->shotAniMax) |
| 267 | shot->shotAni = 0; |
| 268 | |
| 269 | *out_shot_damage = shot->shotDmg; |
| 270 | *out_blast_filter = shot->shotBlastFilter; |
| 271 | *out_chain = shot->chainReaction; |
| 272 | *out_playerNum = shot->playerNumber; |
| 273 | |
| 274 | *out_is_special = sprite_frame > 60000; |
| 275 | |
| 276 | if (*out_is_special) |
| 277 | { |
| 278 | blit_sprite_blend(VGAScreen, *out_shotx+1, *out_shoty, OPTION_SHAPES, sprite_frame - 60001); |
| 279 | |
| 280 | *out_special_radiusw = sprite(OPTION_SHAPES, sprite_frame - 60001)->width / 2; |
| 281 | *out_special_radiush = sprite(OPTION_SHAPES, sprite_frame - 60001)->height / 2; |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | if (sprite_frame > 1000) |
| 286 | { |
| 287 | JE_doSP(*out_shotx+1 + 6, *out_shoty + 6, 5, 3, (sprite_frame / 1000) << 4); |
| 288 | sprite_frame = sprite_frame % 1000; |
| 289 | } |
| 290 | if (sprite_frame > 500) |
| 291 | { |
| 292 | if (background2 && *out_shoty + shadowYDist < 190 && tmp_shotXM < 100) |
| 293 | blit_sprite2_darken(VGAScreen, *out_shotx+1, *out_shoty + shadowYDist, spriteSheet12, sprite_frame - 500); |
| 294 | blit_sprite2(VGAScreen, *out_shotx+1, *out_shoty, spriteSheet12, sprite_frame - 500); |
| 295 | } |
| 296 | else |
| 297 | { |
| 298 | if (background2 && *out_shoty + shadowYDist < 190 && tmp_shotXM < 100) |
| 299 | blit_sprite2_darken(VGAScreen, *out_shotx+1, *out_shoty + shadowYDist, spriteSheet8, sprite_frame); |
| 300 | blit_sprite2(VGAScreen, *out_shotx+1, *out_shoty, spriteSheet8, sprite_frame); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | JE_integer player_shot_create(JE_word portNum, uint bay_i, JE_word PX, JE_word PY, JE_word mouseX, JE_word mouseY, JE_word wpNum, JE_byte playerNum) |
| 309 | { |
| 310 | static const JE_byte soundChannel[11] /* [1..11] */ = {0, 2, 4, 4, 2, 2, 5, 5, 1, 4, 1}; |
| 311 | |
| 312 | // Bounds check |
| 313 | if (portNum > PORT_NUM || wpNum <= 0 || wpNum > WEAP_NUM) |
| 314 | return MAX_PWEAPON; |
| 315 | |
| 316 | const JE_WeaponType* weapon = &weapons[wpNum]; |
| 317 | |
| 318 | if (power < weaponPort[portNum].poweruse) |
| 319 | return MAX_PWEAPON; |
| 320 | power -= weaponPort[portNum].poweruse; |
| 321 | |
| 322 | if (weapon->sound > 0) |
| 323 | soundQueue[soundChannel[bay_i]] = weapon->sound; |
| 324 | |
| 325 | int shot_id = MAX_PWEAPON; |
| 326 | /*Rot*/ |
| 327 | for (int multi_i = 1; multi_i <= weapon->multi; multi_i++) |
| 328 | { |
| 329 | for (shot_id = 0; shot_id < MAX_PWEAPON; shot_id++) |
| 330 | if (shotAvail[shot_id] == 0) |
| 331 | break; |
| 332 | if (shot_id == MAX_PWEAPON) |
| 333 | return MAX_PWEAPON; |
| 334 | |
| 335 | if (shotMultiPos[bay_i] == weapon->max || shotMultiPos[bay_i] > 8) |
| 336 | shotMultiPos[bay_i] = 1; |
| 337 | else |
| 338 | shotMultiPos[bay_i]++; |
| 339 | |
| 340 | PlayerShotDataType* shot = &playerShotData[shot_id]; |
| 341 | shot->chainReaction = 0; |
| 342 | |
| 343 | shot->playerNumber = playerNum; |
| 344 | |
| 345 | shot->shotAni = 0; |
| 346 | |
| 347 | shot->shotComplicated = weapon->circlesize != 0; |
| 348 | |
| 349 | if (weapon->circlesize == 0) |
| 350 | { |
| 351 | shot->shotDevX = 0; |
| 352 | shot->shotDirX = 0; |
| 353 | shot->shotDevY = 0; |
| 354 | shot->shotDirY = 0; |
| 355 | shot->shotCirSizeX = 0; |
| 356 | shot->shotCirSizeY = 0; |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | JE_byte circsize = weapon->circlesize; |
| 361 | |
| 362 | if (circsize > 19) |
| 363 | { |
| 364 | JE_byte circsize_mod20 = circsize % 20; |
| 365 | shot->shotCirSizeX = circsize_mod20; |
| 366 | shot->shotDevX = circsize_mod20 >> 1; |
| 367 | |
| 368 | circsize = circsize / 20; |
| 369 | shot->shotCirSizeY = circsize; |
| 370 | shot->shotDevY = circsize >> 1; |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | shot->shotCirSizeX = circsize; |
| 375 | shot->shotCirSizeY = circsize; |
| 376 | shot->shotDevX = circsize >> 1; |
| 377 | shot->shotDevY = circsize >> 1; |
| 378 | } |
| 379 | shot->shotDirX = 1; |
| 380 | shot->shotDirY = -1; |
| 381 | } |
| 382 | |
| 383 | shot->shotTrail = weapon->trail; |
| 384 | |
| 385 | if (weapon->attack[shotMultiPos[bay_i]-1] > 99 && weapon->attack[shotMultiPos[bay_i]-1] < 250) |
| 386 | { |
| 387 | shot->chainReaction = weapon->attack[shotMultiPos[bay_i]-1] - 100; |
| 388 | shot->shotDmg = 1; |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | shot->shotDmg = weapon->attack[shotMultiPos[bay_i]-1]; |
| 393 | } |
| 394 | |
| 395 | shot->shotBlastFilter = weapon->shipblastfilter; |
| 396 | |
| 397 | JE_integer tmp_by = weapon->by[shotMultiPos[bay_i]-1]; |
| 398 | |
| 399 | /*Note: Only front selection used for player shots...*/ |
| 400 | |
| 401 | shot->shotX = PX + weapon->bx[shotMultiPos[bay_i]-1]; |
| 402 | |
| 403 | shot->shotY = PY + tmp_by; |
| 404 | shot->shotYC = -weapon->acceleration; |
| 405 | shot->shotXC = weapon->accelerationx; |
| 406 | |
| 407 | shot->shotXM = weapon->sx[shotMultiPos[bay_i]-1]; |
| 408 | |
| 409 | // Not sure what this field does exactly. |
| 410 | JE_byte del = weapon->del[shotMultiPos[bay_i]-1]; |
| 411 | |
| 412 | if (del == 121) |
| 413 | { |
| 414 | shot->shotTrail = 0; |
| 415 | del = 255; |
| 416 | } |
| 417 | |
| 418 | shot->shotGr = weapon->sg[shotMultiPos[bay_i]-1]; |
| 419 | if (shot->shotGr == 0) |
| 420 | shotAvail[shot_id] = 0; |
| 421 | else |
| 422 | shotAvail[shot_id] = del; |
| 423 | |
| 424 | if (del > 100 && del < 120) |
| 425 | shot->shotAniMax = (del - 100 + 1); |
| 426 | else |
| 427 | shot->shotAniMax = weapon->weapani + 1; |
| 428 | |
| 429 | if (del == 99 || del == 98) |
| 430 | { |
| 431 | tmp_by = PX - mouseX; |
| 432 | if (tmp_by < -5) |
| 433 | tmp_by = -5; |
| 434 | else if (tmp_by > 5) |
| 435 | tmp_by = 5; |
| 436 | shot->shotXM += tmp_by; |
| 437 | } |
| 438 | |
| 439 | if (del == 99 || del == 100) |
| 440 | { |
| 441 | tmp_by = PY - mouseY - weapon->sy[shotMultiPos[bay_i]-1]; |
| 442 | if (tmp_by < -4) |
| 443 | tmp_by = -4; |
| 444 | else if (tmp_by > 4) |
| 445 | tmp_by = 4; |
| 446 | shot->shotYM = tmp_by; |
| 447 | } |
| 448 | else if (weapon->sy[shotMultiPos[bay_i]-1] == 98) |
| 449 | { |
| 450 | shot->shotYM = 0; |
| 451 | shot->shotYC = -1; |
| 452 | } |
| 453 | else if (weapon->sy[shotMultiPos[bay_i]-1] > 100) |
| 454 | { |
| 455 | shot->shotYM = weapon->sy[shotMultiPos[bay_i]-1]; |
| 456 | shot->shotY -= player[shot->playerNumber-1].delta_y_shot_move; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | shot->shotYM = -weapon->sy[shotMultiPos[bay_i]-1]; |
| 461 | } |
| 462 | |
| 463 | if (weapon->sx[shotMultiPos[bay_i]-1] > 100) |
| 464 | { |
| 465 | shot->shotXM = weapon->sx[shotMultiPos[bay_i]-1]; |
| 466 | shot->shotX -= player[shot->playerNumber-1].delta_x_shot_move; |
| 467 | if (shot->shotXM == 101) |
| 468 | shot->shotY -= player[shot->playerNumber-1].delta_y_shot_move; |
| 469 | } |
| 470 | |
| 471 | if (weapon->aim > 5) /*Guided Shot*/ |
| 472 | { |
| 473 | uint best_dist = 65000; |
| 474 | JE_byte closest_enemy = 0; |
| 475 | /*Find Closest Enemy*/ |
| 476 | for (x = 0; x < 100; x++) |
| 477 | { |
| 478 | if (enemyAvail[x] != 1 && !enemy[x].scoreitem) |
| 479 | { |
| 480 | y = abs(enemy[x].ex - shot->shotX) + abs(enemy[x].ey - shot->shotY); |
| 481 | if (y < best_dist) |
| 482 | { |
| 483 | best_dist = y; |
| 484 | closest_enemy = x + 1; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | shot->aimAtEnemy = closest_enemy; |
| 489 | shot->aimDelay = 5; |
| 490 | shot->aimDelayMax = weapon->aim - 5; |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | shot->aimAtEnemy = 0; |
| 495 | } |
| 496 | |
| 497 | shotRepeat[bay_i] = weapon->shotrepeat; |
| 498 | } |
| 499 | |
| 500 | return shot_id; |
| 501 | } |