From 4087e122fe1a3c3398875350799051bc9a2a9366 Mon Sep 17 00:00:00 2001 From: Matt Molyneaux Date: Tue, 1 Jun 2021 09:32:50 +0100 Subject: [PATCH 1/2] Use enums for members that clearly should have been so from the beginning The compiler can catch more mistakes and the code is easier to read Fixes #21 --- apricots/ai.cpp | 700 +++++++++++++++++++++--------------------- apricots/all.cpp | 40 +-- apricots/apricots.h | 215 +++++++------ apricots/collide.cpp | 49 ++- apricots/drak.cpp | 6 +- apricots/drawall.cpp | 2 +- apricots/fall.cpp | 140 ++++----- apricots/game.cpp | 40 +-- apricots/init.cpp | 37 +-- apricots/sampleio.cpp | 10 +- apricots/setup.cpp | 518 ++++++++++++++----------------- 11 files changed, 839 insertions(+), 918 deletions(-) diff --git a/apricots/ai.cpp b/apricots/ai.cpp index 4610821..e827720 100644 --- a/apricots/ai.cpp +++ b/apricots/ai.cpp @@ -17,11 +17,11 @@ void followtarget(plane &p, int &jx, int &jy, int rx, int ry, bool reverse) { if (p.y < GAME_HEIGHT - 160) { if ((rx > 0) && (p.d == 13)) { targetd = -1; - p.coms = 9; + p.coms = plane::Coms::GO_LEFT_PLUS; } if ((rx < 0) && (p.d == 5)) { targetd = 1; - p.coms = 8; + p.coms = plane::Coms::GO_RIGHT_MINUS; } } // Afterburner if available @@ -46,384 +46,374 @@ void followtarget(plane &p, int &jx, int &jy, int rx, int ry, bool reverse) { void computer_ai(gamedata &g, plane &p, int &jx, int &jy, bool &jb) { switch (g.p().land) { - case 0: // Landed Plane (takes off) - jy = -1; - break; - - case 1: // Taking off plane (accelerates) - jy = -1; - if (p.s > 5.0 * GAME_SPEED) { - jx = sign(9 - p.d); - } - break; - - case 2: // Flying plane - if (p.rotate == 0) { - if ((p.coms == 0) && (p.state == 0)) { - // Stall Avoidance - if (((p.y < 24.0) && (p.ys < 0.0)) || (p.s < 3.0 * GAME_SPEED)) - p.coms = 4; - // Drak mothership avoidance - if (g.drakms.exist == 1) { - double dx = p.x - g.drakms.x + (p.xs - g.drakms.xs) * 4.0 / GAME_SPEED; - if ((dx > -40.0) && (dx < 136.0)) { - if ((p.y > 46.0) && (p.y < 106.0) && ((p.ys < 0.0) || (p.y < 86.0))) - p.coms = 4; - if ((p.y <= 46.0) && (p.ys >= 0.0) && ((p.ys > 0.0) || (p.y > 26.0))) - p.coms = 1; - } - } - // Groundheight tracking - int px = int(p.x + 8) / 16; - if (((p.d == 5) || (p.d == 13)) && - ((p.y + 20.0 > g.gamemap.steepheight[px]) || (p.y + 20.0 > g.gamemap.steepheight[px + 1]))) - p.coms = 1; - if ((p.d > 2) && (p.d < 8)) - px = clamp(px - 1, 0, MAP_W * 2 - 1); - if ((p.d > 10) && (p.d < 16)) - px = clamp(px + 1, 0, MAP_W * 2 - 1); - if (p.ys < 0.0) { - if ((p.y + 5.0 > g.gamemap.smoothheight[px]) || (p.y + 5.0 > g.gamemap.smoothheight[px + 1])) - p.coms = 1; - } else { - if ((p.y + 40.0 > g.gamemap.smoothheight[px]) || (p.y + 40.0 > g.gamemap.smoothheight[px + 1])) - p.coms = 1; - } - if ((p.y - 40.0 > g.gamemap.realheight[px]) || (p.y - 40.0 > g.gamemap.realheight[px + 1])) - p.coms = 6; - // Landing runway approach - if (p.targetx == -10) { - int dx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx; - if ((dx > 10) && (dx < -10 + g.base[p.side].runwaylength) && - ((p.d == 6) || (p.d == 7) || (p.d == 11) || (p.d == 12))) { - p.coms = 0; - if (g.base[p.side].planey - int(p.y) < 50) - p.coms = 11; - } - } - // Side of map avoidance - if (int(p.x) < 100) - p.coms = 2; - if (int(p.x) > GAME_WIDTH - 116) - p.coms = 3; + case plane::LandingState::LANDED: + jy = -1; + break; + case plane::LandingState::TAKING_OFF: + jy = -1; + if (p.s > 5.0 * GAME_SPEED) { + jx = sign(9 - p.d); } - // Recover from stall - if (p.state == 1) - p.coms = 5; - // Choose action depending on coms - switch (p.coms) { - case 0: { - // Action! - // Land if out of shots and bombs - if ((p.ammo == 0) && (p.bombs == 0)) - p.targetx = -10; - // Land if mission is done - if ((g.mission < 2) && (p.score >= g.targetscore)) - p.targetx = -10; - if ((g.mission == 2) && (p.targetscore == 0)) - p.targetx = -10; - // Drak fighters don't land - if ((p.targetx == -10) && (p.drak)) - p.targetx = 0; - // Check for gunthreat and change target if target not already a gun - if ((p.gunthreat > 0) && (p.bombs > 0)) { - if (p.targetx > 0) { - if (g.gamemap.b[p.targetx].type != 5) { - p.targetx = p.gunthreat; - p.targety = g.gamemap.b[p.targetx].y; + break; + case plane::LandingState::FLYING: + if (p.rotate == 0) { + if ((p.coms == plane::Coms::ACTION) && (p.state == 0)) { + // Stall Avoidance + if (((p.y < 24.0) && (p.ys < 0.0)) || (p.s < 3.0 * GAME_SPEED)) + p.coms = plane::Coms::LEVEL_OFF; + // Drak mothership avoidance + if (g.drakms.exist == 1) { + double dx = p.x - g.drakms.x + (p.xs - g.drakms.xs) * 4.0 / GAME_SPEED; + if ((dx > -40.0) && (dx < 136.0)) { + if ((p.y > 46.0) && (p.y < 106.0) && ((p.ys < 0.0) || (p.y < 86.0))) + p.coms = plane::Coms::LEVEL_OFF; + if ((p.y <= 46.0) && (p.ys >= 0.0) && ((p.ys > 0.0) || (p.y > 26.0))) + p.coms = plane::Coms::GO_UP; } + } + // Groundheight tracking + int px = int(p.x + 8) / 16; + if (((p.d == 5) || (p.d == 13)) && + ((p.y + 20.0 > g.gamemap.steepheight[px]) || (p.y + 20.0 > g.gamemap.steepheight[px + 1]))) + p.coms = plane::Coms::GO_UP; + if ((p.d > 2) && (p.d < 8)) + px = clamp(px - 1, 0, MAP_W * 2 - 1); + if ((p.d > 10) && (p.d < 16)) + px = clamp(px + 1, 0, MAP_W * 2 - 1); + if (p.ys < 0.0) { + if ((p.y + 5.0 > g.gamemap.smoothheight[px]) || (p.y + 5.0 > g.gamemap.smoothheight[px + 1])) + p.coms = plane::Coms::GO_UP; } else { - p.targetx = p.gunthreat; - p.targety = g.gamemap.b[p.targetx].y; + if ((p.y + 40.0 > g.gamemap.smoothheight[px]) || (p.y + 40.0 > g.gamemap.smoothheight[px + 1])) + p.coms = plane::Coms::GO_UP; + } + if ((p.y - 40.0 > g.gamemap.realheight[px]) || (p.y - 40.0 > g.gamemap.realheight[px + 1])) + p.coms = plane::Coms::GO_DOWN; + // Landing runway approach + if (p.targetx == -10) { + int dx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx; + if ((dx > 10) && (dx < -10 + g.base[p.side].runwaylength) && + ((p.d == 6) || (p.d == 7) || (p.d == 11) || (p.d == 12))) { + p.coms = plane::Coms::ACTION; + if (g.base[p.side].planey - int(p.y) < 50) + p.coms = plane::Coms::START_LANDING; + } } + // Side of map avoidance + if (int(p.x) < 100) + p.coms = plane::Coms::GO_RIGHT; + if (int(p.x) > GAME_WIDTH - 116) + p.coms = plane::Coms::GO_LEFT; } - // Find target if no target exists - if (p.targetx == 0) { - // Try a building - p.targetx = int(drand() * (MAP_W * 2 - 3)) + 2; - if ((g.gamemap.b[p.targetx].type < 3) || (g.gamemap.b[p.targetx].points < 0) || - (g.gamemap.b[p.targetx].side == p.side) || (p.bombs == 0) || - ((g.gamemap.b[p.targetx].side == 0) && (g.mission == 2))) { - // Building unsuitable target so try plane instead - p.targetx = 0; - int tryplaneid = int(drand() * g.planes) + 1; - if ((((g.mission != 2) && (int(drand() * 2) == 1)) || (int(drand() * 30) == 1) || (p.drak)) && - (p.ammo > 0)) { + // Recover from stall + if (p.state == 1) + p.coms = plane::Coms::GO_DOWN; + switch (p.coms) { + case plane::Coms::ACTION: { + // Land if out of shots and bombs + if ((p.ammo == 0) && (p.bombs == 0)) + p.targetx = -10; + // Land if mission is done + if ((g.mission < 2) && (p.score >= g.targetscore)) + p.targetx = -10; + if ((g.mission == 2) && (p.targetscore == 0)) + p.targetx = -10; + // Drak fighters don't land + if ((p.targetx == -10) && (p.drak)) + p.targetx = 0; + // Check for gunthreat and change target if target not already a gun + if ((p.gunthreat > 0) && (p.bombs > 0)) { + if (p.targetx > 0) { + if (g.gamemap.b[p.targetx].type != 5) { + p.targetx = p.gunthreat; + p.targety = g.gamemap.b[p.targetx].y; + } + } else { + p.targetx = p.gunthreat; + p.targety = g.gamemap.b[p.targetx].y; + } + } + // Find target if no target exists + if (p.targetx == 0) { + // Try a building + p.targetx = int(drand() * (MAP_W * 2 - 3)) + 2; + if ((g.gamemap.b[p.targetx].type < 3) || (g.gamemap.b[p.targetx].points < 0) || + (g.gamemap.b[p.targetx].side == p.side) || (p.bombs == 0) || + ((g.gamemap.b[p.targetx].side == 0) && (g.mission == 2))) { + // Building unsuitable target so try plane instead + p.targetx = 0; + int tryplaneid = int(drand() * g.planes) + 1; + if ((((g.mission != 2) && (int(drand() * 2) == 1)) || (int(drand() * 30) == 1) || (p.drak)) && + (p.ammo > 0)) { + g.dp.reset(); + while (g.dp.next()) { + // Check if plane is suitable target and select if so + if ((g.dp().id == tryplaneid) && (g.dp().side != p.side) && (g.dp().state < 2) && (!g.dp().hide)) + p.targetx = -tryplaneid; + } + } + } else { + // Building suitable so becomes target + p.targety = g.gamemap.b[p.targetx].y; + } + // Set new cruiseheight if changed target + if (p.cruiseheight == 0) + p.cruiseheight = 20 + int(drand() * (GAME_HEIGHT - 176)); + if (p.targetx == 0) { + // Chase false target + if (p.xs > 0) { + followtarget(p, jx, jy, -160, 0, false); + } else { + followtarget(p, jx, jy, 160, 0, false); + } + // If still no target for too long then land + p.targety++; + if (p.targety == int(200 / GAME_SPEED)) + p.targetx = -10; + } + } + // Target is building + if (p.targetx > 0) { + int rx = int(p.x) - 16 * p.targetx + 8; + int ry = int(p.y) + 40 - p.targety; + followtarget(p, jx, jy, rx, ry, ((abs(rx) < 110) && (g.gamemap.b[p.targetx].type == 5))); + // Check to see if can still bomb the building + if ((g.gamemap.b[p.targetx].type == 0) || (p.bombs == 0)) { + p.targetx = 0; + p.targety = 0; + p.cruiseheight = 0; + } + } + // Target is plane + if ((p.targetx < 0) && (p.targetx > -10)) { g.dp.reset(); while (g.dp.next()) { - // Check if plane is suitable target and select if so - if ((g.dp().id == tryplaneid) && (g.dp().side != p.side) && (g.dp().state < 2) && (!g.dp().hide)) - p.targetx = -tryplaneid; + if (g.dp().id == -p.targetx) { + // Check to see if can still shoot plane + if ((g.dp().state > 1) || (p.ammo == 0) || ((g.dp().hide) && (int(drand() * 40) == 1))) { + p.targetx = 0; + p.targety = 0; + p.cruiseheight = 0; + } else { + int rx = int(p.x - g.dp().x); + int ry = int(p.y - g.dp().y); + followtarget(p, jx, jy, rx, ry, (rx * rx + ry * ry < 2500)); + } + } } } - } else { - // Building suitable so becomes target - p.targety = g.gamemap.b[p.targetx].y; + // Target is runway + if (p.targetx == -10) { + int rx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx - g.base[p.side].runwaylength / 2; + int ry = int(p.y) - g.base[p.side].planey; + followtarget(p, jx, jy, rx, ry, false); + } + break; } - // Set new cruiseheight if changed target - if (p.cruiseheight == 0) - p.cruiseheight = 20 + int(drand() * (GAME_HEIGHT - 176)); - if (p.targetx == 0) { - // Chase false target - if (p.xs > 0) { - followtarget(p, jx, jy, -160, 0, false); - } else { - followtarget(p, jx, jy, 160, 0, false); + case plane::Coms::GO_UP: { + if ((p.d > 3) && (p.d < 9)) + jx = 1; + if ((p.d > 9) && (p.d < 15)) + jx = -1; + if (p.d == 9) { + if (int(p.x) < 100) + jx = -1; + if (int(p.x) > GAME_WIDTH - 116) + jx = 1; + if (jx == 0) { + int px = int(p.x + 8) / 16 - 1; + jx = sign(g.gamemap.smoothheight[px] - g.gamemap.smoothheight[px + 3]); + if (jx == 0) + jx = int(drand() * 2.0) * 2 - 1; + } } - // If still no target for too long then land - p.targety++; - if (p.targety == int(200 / GAME_SPEED)) - p.targetx = -10; + p.coms = plane::Coms::ACTION; + break; } - } - // Target is building - if (p.targetx > 0) { - int rx = int(p.x) - 16 * p.targetx + 8; - int ry = int(p.y) + 40 - p.targety; - followtarget(p, jx, jy, rx, ry, ((abs(rx) < 110) && (g.gamemap.b[p.targetx].type == 5))); - // Check to see if can still bomb the building - if ((g.gamemap.b[p.targetx].type == 0) || (p.bombs == 0)) { - p.targetx = 0; - p.targety = 0; - p.cruiseheight = 0; + case plane::Coms::GO_RIGHT: { + // Go right + if ((p.d == 1) || (p.d > 13)) + jx = 1; + if ((p.d > 7) && (p.d < 13)) + jx = -1; + if ((p.d < 8) && (p.d > 1)) { + jx = 1; + if (p.s < 4.0 * GAME_SPEED) + jx = -1; + if (int(p.y) < 34) + jx = -1; + int px = int(p.x + 8) / 16; + if ((int(p.y) + 45 > g.gamemap.realheight[px]) && (int(p.y) + 45 > g.gamemap.realheight[px + 1])) + jx = 1; + } + if (jx == 1) + p.coms = plane::Coms::GO_RIGHT_PLUS; + if (jx == -1) + p.coms = plane::Coms::GO_RIGHT_MINUS; + if (p.d == 13) + p.coms = plane::Coms::ACTION; + break; } - } - // Target is plane - if ((p.targetx < 0) && (p.targetx > -10)) { - g.dp.reset(); - while (g.dp.next()) { - if (g.dp().id == -p.targetx) { - // Check to see if can still shoot plane - if ((g.dp().state > 1) || (p.ammo == 0) || ((g.dp().hide) && (int(drand() * 40) == 1))) { - p.targetx = 0; - p.targety = 0; - p.cruiseheight = 0; - } else { - int rx = int(p.x - g.dp().x); - int ry = int(p.y - g.dp().y); - followtarget(p, jx, jy, rx, ry, (rx * rx + ry * ry < 2500)); - } + case plane::Coms::GO_LEFT: { + if (p.d < 5) + jx = -1; + if ((p.d > 5) && (p.d < 11)) + jx = 1; + if (p.d > 10) { + jx = -1; + if (p.s < 4.0 * GAME_SPEED) + jx = 1; + if (int(p.y) < 34) + jx = 1; + int px = int(p.x + 8) / 16; + if ((int(p.y) + 45 > g.gamemap.realheight[px]) && (int(p.y) + 45 > g.gamemap.realheight[px + 1])) + jx = -1; } + if (jx == 1) + p.coms = plane::Coms::GO_LEFT_PLUS; + if (jx == -1) + p.coms = plane::Coms::GO_LEFT_MINUS; + if (p.d == 5) + p.coms = plane::Coms::ACTION; + break; } - } - // Target is runway - if (p.targetx == -10) { - int rx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx - g.base[p.side].runwaylength / 2; - int ry = int(p.y) - g.base[p.side].planey; - followtarget(p, jx, jy, rx, ry, false); - } - break; - } - case 1: { - // Go upwards - if ((p.d > 3) && (p.d < 9)) - jx = 1; - if ((p.d > 9) && (p.d < 15)) - jx = -1; - if (p.d == 9) { - if (int(p.x) < 100) - jx = -1; - if (int(p.x) > GAME_WIDTH - 116) - jx = 1; - if (jx == 0) { - int px = int(p.x + 8) / 16 - 1; - jx = sign(g.gamemap.smoothheight[px] - g.gamemap.smoothheight[px + 3]); - if (jx == 0) + case plane::Coms::LEVEL_OFF: { + if (p.d < 8) + jx = -1; + if (p.d > 10) + jx = 1; + if (p.d == 1) jx = int(drand() * 2.0) * 2 - 1; + if ((p.d > 3) && (p.d < 15)) + p.coms = plane::Coms::ACTION; + break; } - } - p.coms = 0; - break; - } - case 2: { - // Go right - if ((p.d == 1) || (p.d > 13)) - jx = 1; - if ((p.d > 7) && (p.d < 13)) - jx = -1; - if ((p.d < 8) && (p.d > 1)) { - jx = 1; - if (p.s < 4.0 * GAME_SPEED) - jx = -1; - if (int(p.y) < 34) - jx = -1; - int px = int(p.x + 8) / 16; - if ((int(p.y) + 45 > g.gamemap.realheight[px]) && (int(p.y) + 45 > g.gamemap.realheight[px + 1])) - jx = 1; - } - if (jx == 1) - p.coms = 7; - if (jx == -1) - p.coms = 8; - if (p.d == 13) - p.coms = 0; - break; - } - case 3: { - // Go left - if (p.d < 5) - jx = -1; - if ((p.d > 5) && (p.d < 11)) - jx = 1; - if (p.d > 10) { - jx = -1; - if (p.s < 4.0 * GAME_SPEED) - jx = 1; - if (int(p.y) < 34) - jx = 1; - int px = int(p.x + 8) / 16; - if ((int(p.y) + 45 > g.gamemap.realheight[px]) && (int(p.y) + 45 > g.gamemap.realheight[px + 1])) - jx = -1; - } - if (jx == 1) - p.coms = 9; - if (jx == -1) - p.coms = 10; - if (p.d == 5) - p.coms = 0; - break; - } - case 4: { - // Level off - if (p.d < 8) - jx = -1; - if (p.d > 10) - jx = 1; - if (p.d == 1) - jx = int(drand() * 2.0) * 2 - 1; - if ((p.d > 3) && (p.d < 15)) - p.coms = 0; - break; - } - case 5: { - // Go down - if (p.d < 9) - jx = -1; - if (p.d > 9) - jx = 1; - if (p.d == 1) - jx = int(drand() * 2.0) * 2 - 1; - if ((p.d == 9) && (p.s > 3.0 * GAME_SPEED)) - p.coms = 0; - break; - } - case 6: { - // Go upwards lots - if ((p.d > 1) && (p.d < 9)) - jx = 1; - if (p.d > 9) - jx = -1; - if (p.d == 9) { - if (int(p.x) < 100) - jx = -1; - if (int(p.x) > GAME_WIDTH - 116) - jx = 1; - if (jx == 0) { - int px = int(p.x + 8) / 16 - 1; - jx = sign(g.gamemap.smoothheight[px] - g.gamemap.smoothheight[px + 3]); - if (jx == 0) + case plane::Coms::GO_DOWN: { + // Go down + if (p.d < 9) + jx = -1; + if (p.d > 9) + jx = 1; + if (p.d == 1) jx = int(drand() * 2.0) * 2 - 1; + if ((p.d == 9) && (p.s > 3.0 * GAME_SPEED)) + p.coms = plane::Coms::ACTION; + break; } + case plane::Coms::GO_UP_LOTS: { + if ((p.d > 1) && (p.d < 9)) + jx = 1; + if (p.d > 9) + jx = -1; + if (p.d == 9) { + if (int(p.x) < 100) + jx = -1; + if (int(p.x) > GAME_WIDTH - 116) + jx = 1; + if (jx == 0) { + int px = int(p.x + 8) / 16 - 1; + jx = sign(g.gamemap.smoothheight[px] - g.gamemap.smoothheight[px + 3]); + if (jx == 0) + jx = int(drand() * 2.0) * 2 - 1; + } + } + p.coms = plane::Coms::ACTION; + break; + } + case plane::Coms::GO_RIGHT_PLUS: { + if (p.d != 13) { + jx = 1; + } else { + p.coms = plane::Coms::ACTION; + } + break; + } + case plane::Coms::GO_RIGHT_MINUS: { + // Go right -ve + if (p.d != 13) { + jx = -1; + } else { + p.coms = plane::Coms::ACTION; + } + break; + } + case plane::Coms::GO_LEFT_PLUS: { + if (p.d != 5) { + jx = 1; + } else { + p.coms = plane::Coms::ACTION; + } + break; + } + case plane::Coms::GO_LEFT_MINUS: { + if (p.d != 5) { + jx = -1; + } else { + p.coms = plane::Coms::ACTION; + } + break; + } + case plane::Coms::START_LANDING: { + if (p.d == 6) + jx = -1; + if (p.d == 12) + jx = 1; + int dx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx; + if ((dx < 10) || (dx > -10 + g.base[p.side].runwaylength)) + p.coms = plane::Coms::ACTION; + break; + } + default: + switch_bad_default("plane.coms", __FILE__, __LINE__); + break; } - p.coms = 0; - break; - } - case 7: { - // Go right +ve - if (p.d != 13) { - jx = 1; - } else { - p.coms = 0; - } - break; - } - case 8: { - // Go right -ve - if (p.d != 13) { - jx = -1; - } else { - p.coms = 0; - } - break; - } - case 9: { - // Go left +ve - if (p.d != 5) { - jx = 1; - } else { - p.coms = 0; - } - break; - } - case 10: { - // Go left -ve - if (p.d != 5) { - jx = -1; - } else { - p.coms = 0; - } - break; - } - case 11: { - // Landing - if (p.d == 6) - jx = -1; - if (p.d == 12) - jx = 1; - int dx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx; - if ((dx < 10) || (dx > -10 + g.base[p.side].runwaylength)) - p.coms = 0; - break; - } - default: - switch_bad_default("plane.coms", __FILE__, __LINE__); - break; + // Wiggle to avoid gunfire + if ((p.gunthreat > 0) && (jx == 0) && (!p.hide)) + jx = int(drand() * 2.0) * 2 - 1; } - // Wiggle to avoid gunfire - if ((p.gunthreat > 0) && (jx == 0) && (!p.hide)) - jx = int(drand() * 2.0) * 2 - 1; - } - // Afterburner for jet if speed slow - if ((p.burner) && (p.s < 5.0 * GAME_SPEED)) - jy = -1; - // Stealth ability - if (p.stealth) - jy = -1; - if (p.shotdelay == 0) { - // Launch bomb - if (p.targetx > 0) { - double rx = p.x - double(16 * p.targetx) + 8.0 + p.xs; - double ry = p.y - double(p.targety) + 14.0 + p.ys; - double a = (p.ys + 2.0 * GAME_SPEED) * (p.ys + 2.0 * GAME_SPEED) - (ry * 0.2) * GAME_SPEED * GAME_SPEED; - if (a > 0) { - if (abs(int(rx + p.xs * 10.0 / (GAME_SPEED * GAME_SPEED) * (-p.ys - 2.0 * GAME_SPEED + sqrt(a)))) < 6) - jy = 1; + // Afterburner for jet if speed slow + if ((p.burner) && (p.s < 5.0 * GAME_SPEED)) + jy = -1; + // Stealth ability + if (p.stealth) + jy = -1; + if (p.shotdelay == 0) { + // Launch bomb + if (p.targetx > 0) { + double rx = p.x - double(16 * p.targetx) + 8.0 + p.xs; + double ry = p.y - double(p.targety) + 14.0 + p.ys; + double a = (p.ys + 2.0 * GAME_SPEED) * (p.ys + 2.0 * GAME_SPEED) - (ry * 0.2) * GAME_SPEED * GAME_SPEED; + if (a > 0) { + if (abs(int(rx + p.xs * 10.0 / (GAME_SPEED * GAME_SPEED) * (-p.ys - 2.0 * GAME_SPEED + sqrt(a)))) < 6) + jy = 1; + } } - } - // Fire shot - if ((p.s < 8.0 * GAME_SPEED) && (p.ammo > 0)) { - g.dp.reset(); - while (g.dp.next()) { - if ((g.dp().side != p.side) && (g.dp().state < 2) && (!g.dp().hide)) { - double rx = p.x - g.dp().x; - double ry = p.y - g.dp().y; - double d = sqrt(rx * rx + ry * ry); - if (d < 100.0) { - double smartsine = ((ry * g.dp().xs - rx * g.dp().ys) / (8.0 * GAME_SPEED) / d); - smartsine = clamp(smartsine, -1.0, 1.0); - double smartangle = asin(smartsine); - double angle = atan2(rx, ry); - int bogied = wrap((int(((angle - smartangle) / PI * 8.0) + 1.5)), 1, 17); - if (bogied == p.d) - jb = true; + // Fire shot + if ((p.s < 8.0 * GAME_SPEED) && (p.ammo > 0)) { + g.dp.reset(); + while (g.dp.next()) { + if ((g.dp().side != p.side) && (g.dp().state < 2) && (!g.dp().hide)) { + double rx = p.x - g.dp().x; + double ry = p.y - g.dp().y; + double d = sqrt(rx * rx + ry * ry); + if (d < 100.0) { + double smartsine = ((ry * g.dp().xs - rx * g.dp().ys) / (8.0 * GAME_SPEED) / d); + smartsine = clamp(smartsine, -1.0, 1.0); + double smartangle = asin(smartsine); + double angle = atan2(rx, ry); + int bogied = wrap((int(((angle - smartangle) / PI * 8.0) + 1.5)), 1, 17); + if (bogied == p.d) + jb = true; + } } } } } - } - break; - default: - switch_bad_default("plane.land", __FILE__, __LINE__); - break; + break; + case plane::LandingState::LANDING: + break; + default: + switch_bad_default("plane.land", __FILE__, __LINE__); + break; } } diff --git a/apricots/all.cpp b/apricots/all.cpp index 9ae5119..0e59e57 100644 --- a/apricots/all.cpp +++ b/apricots/all.cpp @@ -67,24 +67,24 @@ void animate_explosions(linkedlist &explosion) { explosion().time++; int maxtime = 0; switch (explosion().type) { - case 0: - maxtime = int(14 / GAME_SPEED); - break; - case 1: - maxtime = int(22 / GAME_SPEED); - break; - case 2: - maxtime = int(4 / GAME_SPEED); - break; - case 3: - maxtime = int(20 / GAME_SPEED); - break; - case 4: - maxtime = int(5 / GAME_SPEED); - break; - default: - switch_bad_default("explosion.type", __FILE__, __LINE__); - break; + case firetype::ZERO: + maxtime = int(14 / GAME_SPEED); + break; + case firetype::ONE: + maxtime = int(22 / GAME_SPEED); + break; + case firetype::TWO: + maxtime = int(4 / GAME_SPEED); + break; + case firetype::THREE: + maxtime = int(20 / GAME_SPEED); + break; + case firetype::FOUR: + maxtime = int(5 / GAME_SPEED); + break; + default: + switch_bad_default("firetype.type", __FILE__, __LINE__); + break; } if (explosion().time == maxtime) explosion.kill(); @@ -159,7 +159,7 @@ void plane_collisions(gamedata &g) { if (g.p().state < 2) g.p().score -= 25; g.p().state = 2; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; @@ -190,7 +190,7 @@ void plane_collisions(gamedata &g) { if (g.p().state < 2) g.p().score -= 25; g.p().state = 2; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; diff --git a/apricots/apricots.h b/apricots/apricots.h index 5305607..19927c3 100644 --- a/apricots/apricots.h +++ b/apricots/apricots.h @@ -23,6 +23,7 @@ #include "shape.h" #include #include +#include #include #include #include @@ -79,10 +80,17 @@ const building FIRTREE = {1, 0, 0, 0, 258, 259, -10, 0, 0, 143, 143}; const building PALMTREE = {1, 0, 0, 0, 192, 193, -10, 0, 0, 143, 222}; struct firetype { + enum Type { + ZERO, + ONE, + TWO, + THREE, + FOUR, + }; int x; int y; int time = 0; - int type = 0; + Type type = ZERO; }; struct smoketype { @@ -152,12 +160,6 @@ struct map { shape ground; }; -struct info { - int planetype; - int basetype; - int control; -}; - struct drakmstype { double x; double y; @@ -231,14 +233,39 @@ struct airbase { }; struct plane { + enum LandingState { + LANDED, + TAKING_OFF, + FLYING, + LANDING, + }; + enum Coms { + ACTION, + GO_UP, + GO_RIGHT, + GO_LEFT, + LEVEL_OFF, + GO_DOWN, + GO_UP_LOTS, + GO_RIGHT_PLUS, + GO_RIGHT_MINUS, + GO_LEFT_PLUS, + GO_LEFT_MINUS, + START_LANDING, + }; + enum Control { + COMPUTER, + PLAYER_ONE, + PLAYER_TWO, + }; double x; double y; double xs; double ys; double s; int d; - int control; - int land; + Control control; + LandingState land; int state; int crash; int id; @@ -260,7 +287,7 @@ struct plane { bool drak; int score; int targetscore; - int coms; // + Coms coms; // int targetx; // int targety; // Computer AI stuff int cruiseheight; // @@ -283,6 +310,12 @@ struct planeclone { int buildingwin; }; +struct info { + int planetype; + int basetype; + plane::Control control; +}; + struct gamedata { int planes; int towers; @@ -342,101 +375,73 @@ const airbase SHOOTY_AIRBASE(80, 80, 6, 80, 13, 0, 0, 0, GUN, FUEL, GUN, CONTROL HANGAR, GUN, FUEL, GUN); const airbase TWOGUN_AIRBASE(48, 96, 5, 128, 5, 0, 0, 0, RADAR, GUN, HANGAR, NB, NB, NB, NB, NB, NB, NB, CONTROLTOWER, GUN, RADAR); +const array AIRBASES = { + EMPTY_AIRBASE, + STANDARD_AIRBASE, + REVERSED_AIRBASE, + LITTLE_AIRBASE, + LONG_AIRBASE, + ORIGINAL_AIRBASE, + SHOOTY_AIRBASE, + TWOGUN_AIRBASE, +}; // Sample definitions - -const int SOUND_ENGINE = 0; -const int SOUND_JET = 1; -const int SOUND_EXPLODE = 2; -const int SOUND_GROUNDHIT = 3; -const int SOUND_FUELEXPLODE = 4; -const int SOUND_SHOT = 5; -const int SOUND_GUNSHOT = 6; -const int SOUND_BOMB = 7; -const int SOUND_SPLASH = 8; -const int SOUND_LASER = 9; -const int SOUND_STALL = 10; -const int SOUND_GUNSHOT2 = 11; -const int SOUND_BURNER = 12; -const int SOUND_FINISH = 13; +enum Sounds { + SOUND_ENGINE, + SOUND_JET, + SOUND_EXPLODE, + SOUND_GROUNDHIT, + SOUND_FUELEXPLODE, + SOUND_SHOT, + SOUND_GUNSHOT, + SOUND_BOMB, + SOUND_SPLASH, + SOUND_LASER, + SOUND_STALL, + SOUND_GUNSHOT2, + SOUND_BURNER, + SOUND_FINISH, +}; +const int SOUNDS_COUNT = 14; + +inline const array SOUND_NAMES = { + "engine.wav", + "jet.wav", + "explode.wav", + "groundhit.wav", + "fuelexplode.wav", + "shot.wav", + "gunshot.wav", + "bomb.wav", + "splash.wav", + "laser.wav", + "stall.wav", + "gunshot2.wav", + "afterburner.wav", + "finish.wav", +}; // Plane definitions -const plane SPITFIRE = {0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 122, - 0, - int(3 / GAME_SPEED) - 1, - false, - false, - false, - false, - 0, - 8, - 8, - 4, - 4, - 143, - SOUND_ENGINE, - false, - 0, - 0, - 0, - 0, - 0, - 0, - 0}; -const plane JET = {0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 76, 0, int(2 / GAME_SPEED) - 1, - false, true, false, false, 0, 12, 12, 5, 5, 140, SOUND_JET, false, 0, 0, 0, - 0, 0, 0, 0}; +const plane NULLPLANE = { + 0.0, 0.0, 0.0, 0.0, 0.0, 0, plane::Control::COMPUTER, plane::LandingState::LANDED, 0, 0, 0, 0, 0, 0, 0, false, false, false, false, 0, 0, 0, 0, 0, 0, SOUND_ENGINE, false, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; +const plane SPITFIRE = { + 0.0, 0.0, 0.0, 0.0, 0.0, 0, plane::Control::COMPUTER, plane::LandingState::LANDED, 0, 0, 0, 0, 122, 0, int(3 / GAME_SPEED) - 1, false, false, false, false, 0, 8, 8, 4, 4, 143, SOUND_ENGINE, false, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; +const plane JET = { + 0.0, 0.0, 0.0, 0.0, 0.0, 0, plane::Control::COMPUTER, plane::LandingState::LANDED, 0, 0, 0, 0, 76, 0, int(2 / GAME_SPEED) - 1, false, true, false, false, 0, 12, 12, 5, 5, 140, SOUND_JET, false, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; const plane STEALTH = { - 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 174, 0, int(4 / GAME_SPEED) - 1, - false, false, false, true, 0, 3, 3, 6, 6, 235, SOUND_JET, false, 0, 0, 0, - 0, 0, 0, 0}; -const plane DRAK_FIGHTER = {0.0, - 0.0, - 0.0, - 2.0 * GAME_SPEED, - 2.0 * GAME_SPEED, - 9, - 0, - 2, - 1, - 0, - 0, - 0, - 259, - 0, - int(1 / GAME_SPEED) - 1, - false, - true, - false, - false, - 0, - 1000, - 1000, - 0, - 0, - 235, - SOUND_JET, - true, - 0, - 0, - 0, - 0, - 0, - 0, - 0}; + 0.0, 0.0, 0.0, 0.0, 0.0, 0, plane::Control::COMPUTER, plane::LandingState::LANDED, 0, 0, 0, 0, 174, 0, int(4 / GAME_SPEED) - 1, false, false, false, true, 0, 3, 3, 6, 6, 235, SOUND_JET, false, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; +const plane DRAK_FIGHTER = { + 0.0, 0.0, 0.0, 2.0 * GAME_SPEED, 2.0 * GAME_SPEED, 9, plane::Control::COMPUTER, plane::LandingState::FLYING, 1, 0, 0, 0, 259, 0, int(1 / GAME_SPEED) - 1, false, true, false, false, 0, 1000, 1000, 0, 0, 235, SOUND_JET, true, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; + +const array PLANES = { + NULLPLANE, + SPITFIRE, + JET, + STEALTH, + DRAK_FIGHTER, +}; // Drak mothership initial state @@ -454,13 +459,23 @@ const drakguntype DGUN_SIDE_LEFT = { const drakguntype DGUN_SIDE_RIGHT = { 0, 84, 20, 11, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 303, 302, 301, 300, 299, 298, 297, 296, 0, 0}}; +// Land types +enum LandType { + HILAND, + LOLAND, + BEACH, + PORTLEFT, + PORTRIGHT, + SEA, +}; + // Function prototypes Uint32 time_left(Uint32); bool fall_collision(gamedata &, falltype &); double drand(); int getConfig(string, string, int, int, int); -int randomland(int, int); +LandType randomland(LandType, LandType); int sign(int); int wrap(int, int, int); string find_config_file(); diff --git a/apricots/collide.cpp b/apricots/collide.cpp index 62a413e..702f60f 100644 --- a/apricots/collide.cpp +++ b/apricots/collide.cpp @@ -11,14 +11,14 @@ void detect_collisions(gamedata &g) { // Check for ground collision - if (g.p().land == 2) { + if (g.p().land == plane::LandingState::FLYING) { if ((g.gamemap.ground.collide(0, 0, g.images[g.p().image + g.p().d], (int)g.p().x, (int)g.p().y)) || (int(g.p().x) < -16) || (int(g.p().x) > GAME_WIDTH)) { // Check for landing plane int dx = int(g.p().x) - 32 * g.base[g.p().side].mapx - g.base[g.p().side].runwayx; if ((dx > -5) && (dx < 5 + g.base[g.p().side].runwaylength) && (g.p().state == 0) && (!g.p().drak) && ((g.p().d == 12) || (g.p().d == 11) || (g.p().d == 7) || (g.p().d == 6))) { - g.p().land = 3; + g.p().land = plane::LandingState::LANDING; g.p().y = g.base[g.p().side].planey; g.p().d = 18 - g.base[g.p().side].planed; g.p().xs = 2.0 * g.xmove[g.p().d] * GAME_SPEED; @@ -26,7 +26,7 @@ void detect_collisions(gamedata &g) { } else { // Crashed g.p().score -= 25; - // Mission 1 scoreloss + // Mission 1 score loss if ((g.mission == 1) && (g.p().score > g.targetscore - 200)) { g.p().score = g.targetscore - 200; } @@ -43,8 +43,7 @@ void detect_collisions(gamedata &g) { firetype splash; splash.x = int(g.p().x); splash.y = GAME_HEIGHT - 20; - splash.time = 0; - splash.type = 3; + splash.type = firetype::Type::THREE; g.explosion.add(splash); g.sound.play(SOUND_SPLASH); } else { // hits land @@ -52,9 +51,9 @@ void detect_collisions(gamedata &g) { if ((g.p().y > 3.0) && (g.p().x > 0) && (g.p().x < GAME_WIDTH - 16)) { g.images[g.p().image + 17].blit(g.gamescreen, (int)g.p().x, (int)g.p().y - 3); } - firetype boom = {int(g.p().x), int(g.p().y), 0, 0}; + firetype boom = {int(g.p().x), int(g.p().y)}; g.explosion.add(boom); - firetype fire = {int(g.p().x), int(g.p().y) - 7, int(210 / GAME_SPEED), 0}; + firetype fire = {int(g.p().x), int(g.p().y) - 7, int(210 / GAME_SPEED)}; g.flame.add(fire); for (int i = 0; i < 3; i++) { falltype shrapnel = {}; @@ -78,7 +77,7 @@ void detect_collisions(gamedata &g) { g.images[g.p().image + g.p().d], (int)g.p().x, (int)g.p().y))) { // Crashed g.p().score -= 25; - // Mission 1 scoreloss + // Mission 1 score loss if ((g.mission == 1) && (g.p().score > g.targetscore - 200)) { g.p().score = g.targetscore - 200; } @@ -89,7 +88,7 @@ void detect_collisions(gamedata &g) { g.p().s = 0.0; // Damage to drak mothership g.drakms.damage++; - firetype boom = {int(g.p().x), int(g.p().y), 0, 0}; + firetype boom = {int(g.p().x), int(g.p().y)}; g.explosion.add(boom); for (int i = 0; i < 3; i++) { falltype shrapnel = {}; @@ -118,7 +117,7 @@ void detect_collisions(gamedata &g) { if (g.images[g.gamemap.b[x].image].collide(g.gamemap.b[x].x, g.gamemap.b[x].y, g.images[g.p().image + g.p().d], (int)g.p().x, (int)g.p().y)) { g.p().state = 2; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; @@ -139,7 +138,7 @@ void detect_collisions(gamedata &g) { int ty = g.gamemap.b[x].y - g.gamemap.b[x].towersize * 16; if (g.images[197].collide(g.gamemap.b[x].x, ty, g.images[g.p().image + g.p().d], (int)g.p().x, (int)g.p().y)) { g.p().state = 2; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; @@ -174,13 +173,11 @@ void detect_collisions(gamedata &g) { } } g.p().state = 2; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().s = 0.0; firetype boom; boom.x = int(g.p().x); boom.y = int(g.p().y); - boom.time = 0; - boom.type = 0; g.explosion.add(boom); g.shot.kill(); g.sound.play(SOUND_EXPLODE); @@ -198,15 +195,13 @@ void detect_collisions(gamedata &g) { g.p().score -= 50; g.p().score += 50; g.p().state = 2; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; firetype boom; boom.x = int(g.p().x); boom.y = int(g.p().y); - boom.time = 0; - boom.type = 0; g.explosion.add(boom); // Reset laser flags if (g.drakgun().type == -1) @@ -228,15 +223,13 @@ void detect_collisions(gamedata &g) { (int)g.p().y)) { g.p().score -= 10; g.p().state = 2; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; firetype boom; boom.x = int(g.p().x); boom.y = int(g.p().y); - boom.time = 0; - boom.type = 0; g.explosion.add(boom); g.sound.play(SOUND_EXPLODE); } @@ -258,15 +251,13 @@ void detect_collisions(gamedata &g) { } } g.p().state = 2; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; firetype boom; boom.x = int(g.p().x); boom.y = int(g.p().y); - boom.time = 0; - boom.type = 0; g.explosion.add(boom); g.fall.kill(); g.sound.play(SOUND_EXPLODE); @@ -294,9 +285,9 @@ void killbuilding(gamedata &g, building &b) { } if (b.image != 71) { // not a fuel canister if (b.type != 1) { // not a tree - firetype boom = {b.x, b.y, 0, 0}; + firetype boom = {b.x, b.y}; g.explosion.add(boom); - firetype fire = {b.x, b.y, 0, 0}; + firetype fire = {b.x, b.y}; g.flame.add(fire); } for (int i = 0; i < 3; i++) { @@ -317,9 +308,9 @@ void killbuilding(gamedata &g, building &b) { } g.sound.play(SOUND_EXPLODE); } else { // fuel blows up - firetype boom = {int(b.x - 8), int(b.y - 16), 0, 1}; + firetype boom = {int(b.x - 8), int(b.y - 16), 0, firetype::Type::ONE}; g.explosion.add(boom); - firetype fire = {b.x, b.y, 0, 0}; + firetype fire = {b.x, b.y}; g.flame.add(fire); fire.x = b.x - 8; fire.time = int(drand() * 10.0 * GAME_SPEED); @@ -413,11 +404,11 @@ void killtower(gamedata &g, building &b, double xs, double ys, int h, int side) g.fall.add(shrapnel); } // Explosion at top - firetype boom = {b.x, b.y - h * 16, 0, 0}; + firetype boom = {b.x, b.y - h * 16}; g.explosion.add(boom); // Fire if tower levelled if (h == 0) { - firetype fire = {b.x, b.y, 0, 0}; + firetype fire = {b.x, b.y}; g.flame.add(fire); } // Recalculate intelligence map diff --git a/apricots/drak.cpp b/apricots/drak.cpp index d73dec7..7cb6b80 100644 --- a/apricots/drak.cpp +++ b/apricots/drak.cpp @@ -73,13 +73,11 @@ void fire_laser(gamedata &g, drakguntype &drakgun, drakmstype &drakms, sampleio while (g.p.next()) { if ((int(g.p().y) > lasery) && (abs(int(g.p().x) - laserx) < 6) && (g.p().state < 2)) { g.p().score -= 25; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; g.p().s = 0; firetype boom; boom.x = int(g.p().x); boom.y = int(g.p().y); - boom.type = 0; - boom.time = 0; g.explosion.add(boom); } } @@ -316,8 +314,6 @@ void drak_main(gamedata &g) { firetype boom; boom.x = x + int(drand() * 16) - 8; boom.y = int(g.drakms.y); - boom.time = 0; - boom.type = 0; g.explosion.add(boom); for (int m = 1; m <= 2; m++) { falltype shrapnel = {}; diff --git a/apricots/drawall.cpp b/apricots/drawall.cpp index 5bf9e8b..0aec5ce 100644 --- a/apricots/drawall.cpp +++ b/apricots/drawall.cpp @@ -259,7 +259,7 @@ void drawall(gamedata &g) { bool found = false; g.p.reset(); while (g.p.next()) { - if ((g.p().control == 0) && (g.p().side > 0)) { + if ((g.p().control == plane::Control::COMPUTER) && (g.p().side > 0)) { if (!found) { bestp = g.p(); found = true; diff --git a/apricots/fall.cpp b/apricots/fall.cpp index 7e2fce4..dfd2e54 100644 --- a/apricots/fall.cpp +++ b/apricots/fall.cpp @@ -11,56 +11,53 @@ bool fall_collision(gamedata &g, falltype &fall) { if ((g.gamemap.ground.collide(0, 0, g.images[fall.image], (int)fall.x, (int)fall.y)) || (int(fall.x) < -16) || (int(fall.x) > GAME_WIDTH) || (int(fall.y) > GAME_HEIGHT)) { switch (fall.type) { - case 0: - break; - case 1: // Shrapnel - { - int px = clamp(int((fall.x + 18.0) / 32), 0, MAP_W - 1); - if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (fall.y > GAME_HEIGHT - 11)) { // hits sea - firetype splash; - splash.x = int(fall.x) - 5; - splash.y = GAME_HEIGHT - 20; - splash.time = 0; - splash.type = 4; - g.explosion.add(splash); - } else { // hits land - firetype boom; - boom.x = int(fall.x); - boom.y = int(fall.y); - boom.time = 0; - boom.type = 2; - g.explosion.add(boom); - } - } break; - - case 2: - case 3: // Large bits and bombs - if (fall.type == 3) - fall.x -= 5; + case firetype::Type::ZERO: + break; + case firetype::Type::ONE: // Shrapnel { - int px = clamp(int((fall.x + 24.0) / 32), 0, MAP_W - 1); - if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (fall.y > GAME_HEIGHT - 21)) { // hits sea + int px = clamp(int((fall.x + 18.0) / 32), 0, MAP_W - 1); + if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (fall.y > GAME_HEIGHT - 11)) { // hits sea firetype splash; - splash.x = int(fall.x); + splash.x = int(fall.x) - 5; splash.y = GAME_HEIGHT - 20; splash.time = 0; - splash.type = 3; + splash.type = firetype::Type::FOUR; g.explosion.add(splash); - g.sound.play(SOUND_SPLASH); } else { // hits land firetype boom; boom.x = int(fall.x); boom.y = int(fall.y); boom.time = 0; - boom.type = 0; + boom.type = firetype::Type::TWO; g.explosion.add(boom); - g.sound.play(SOUND_GROUNDHIT); } - } - break; - default: - switch_bad_default("fall.type", __FILE__, __LINE__); - break; + } break; + + case firetype::Type::TWO: + case firetype::Type::THREE: // Large bits and bombs + if (fall.type == 3) + fall.x -= 5; + { + int px = clamp(int((fall.x + 24.0) / 32), 0, MAP_W - 1); + if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (fall.y > GAME_HEIGHT - 21)) { // hits sea + firetype splash; + splash.x = int(fall.x); + splash.y = GAME_HEIGHT - 20; + splash.type = firetype::Type::THREE; + g.explosion.add(splash); + g.sound.play(SOUND_SPLASH); + } else { // hits land + firetype boom; + boom.x = int(fall.x); + boom.y = int(fall.y); + g.explosion.add(boom); + g.sound.play(SOUND_GROUNDHIT); + } + } + break; + default: + switch_bad_default("firetype", __FILE__, __LINE__); + break; } return true; } @@ -70,48 +67,45 @@ bool fall_collision(gamedata &g, falltype &fall) { if ((g.drakms.exist == 1) && (g.images[318].collide((int)g.drakms.x, (int)g.drakms.y, g.images[fall.image], (int)fall.x, (int)fall.y))) { switch (fall.type) { - case 0: - break; - case 1: // Shrapnel - { - firetype boom; - boom.x = int(fall.x); - boom.y = int(fall.y); - boom.time = 0; - boom.type = 2; - g.explosion.add(boom); - } break; - - case 2: - case 3: // Large bits and bombs - if (fall.type == 3) - fall.x -= 5; + case firetype::Type::ZERO: + break; + case firetype::Type::ONE: // Shrapnel { firetype boom; boom.x = int(fall.x); boom.y = int(fall.y); - boom.time = 0; - boom.type = 0; + boom.type = firetype::Type::TWO; g.explosion.add(boom); - } - g.sound.play(SOUND_GROUNDHIT); - // Damage mothership - g.drakms.damage += 2; - // Find which plane (if any) the fall belongs to - if (fall.side > 0) { - g.p.reset(); - while (g.p.next()) { - if (fall.side == g.p().side) { + } break; + + case firetype::Type::TWO: + case firetype::Type::THREE: // Large bits and bombs + if (fall.type == 3) + fall.x -= 5; + { + firetype boom; + boom.x = int(fall.x); + boom.y = int(fall.y); + g.explosion.add(boom); + } + g.sound.play(SOUND_GROUNDHIT); + // Damage mothership + g.drakms.damage += 2; + // Find which plane (if any) the fall belongs to + if (fall.side > 0) { + g.p.reset(); + while (g.p.next()) { + if (fall.side == g.p().side) { - // Add to score - g.p().score += 40; + // Add to score + g.p().score += 40; + } } } - } - break; - default: - switch_bad_default("fall.type", __FILE__, __LINE__); - break; + break; + default: + switch_bad_default("firetype", __FILE__, __LINE__); + break; } return true; } @@ -198,8 +192,6 @@ bool fall_collision(gamedata &g, falltype &fall) { firetype boom; boom.x = int(fall.x); boom.y = int(fall.y); - boom.time = 0; - boom.type = 0; g.explosion.add(boom); // Remove laser flags if (g.drakgun().type == -1) diff --git a/apricots/game.cpp b/apricots/game.cpp index 1152c72..c5af93b 100644 --- a/apricots/game.cpp +++ b/apricots/game.cpp @@ -105,7 +105,7 @@ void act(gamedata &g, int jx, int jy, bool jb) { // Start Engine if (jy == -1) { g.p().s = 0.3 * GAME_SPEED * GAME_SPEED; - g.p().land = 1; + g.p().land = plane::LandingState::TAKING_OFF; if ((g.p().control) > 0) { g.sound.volume(g.p().control - 1, 0.0); g.sound.loop(g.p().control - 1, g.p().enginesample); @@ -121,16 +121,16 @@ void act(gamedata &g, int jx, int jy, bool jb) { if ((jx == -1) && (g.p().s > 2.0 * GAME_SPEED) && (g.base[g.p().side].planed == 13)) { g.p().d++; g.p().rotate = g.p().maxrotate; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; } if ((jx == 1) && (g.p().s > 2.0 * GAME_SPEED) && (g.base[g.p().side].planed == 5)) { g.p().d--; g.p().rotate = g.p().maxrotate; - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; } // Off end of runway if (abs(int(g.p().x - g.base[g.p().side].planex)) > g.base[g.p().side].runwaylength) { - g.p().land = 2; + g.p().land = plane::LandingState::FLYING; } break; @@ -178,10 +178,10 @@ void act(gamedata &g, int jx, int jy, bool jb) { } break; - case 3: // Landing plane + case plane::LandingState::LANDING: if ((((g.p().x - g.base[g.p().side].planex) < 2.0) && (g.base[g.p().side].planed == 13)) || (((g.p().x - g.base[g.p().side].planex) > -2.0) && (g.base[g.p().side].planed == 5))) { - g.p().land = 0; + g.p().land = plane::LandingState::LANDED; g.p().x = g.base[g.p().side].planex; g.p().y = g.base[g.p().side].planey; g.p().d = g.base[g.p().side].planed; @@ -190,7 +190,7 @@ void act(gamedata &g, int jx, int jy, bool jb) { g.p().s = 0; g.p().ammo = g.p().maxammo; g.p().bombs = g.p().maxbombs; - g.p().coms = 0; + g.p().coms = plane::Coms::ACTION; g.p().targetx = 0; g.p().targety = 0; g.p().cruiseheight = 0; @@ -255,7 +255,7 @@ void act(gamedata &g, int jx, int jy, bool jb) { g.p().state = 0; g.p().xs = g.p().s * g.xmove[g.p().d]; g.p().ys = g.p().s * g.ymove[g.p().d]; - g.p().coms = 0; + g.p().coms = plane::Coms::ACTION; if ((g.p().control) > 0) { double volume = g.p().s / (6.0 * GAME_SPEED); g.sound.volume(g.p().control - 1, volume * 0.5); @@ -287,7 +287,7 @@ void act(gamedata &g, int jx, int jy, bool jb) { if (g.p().crash == int(70 / GAME_SPEED)) { if (!g.p().drak) { // Respawn plane to runway - g.p().land = 0; + g.p().land = plane::LandingState::LANDED; g.p().state = 0; g.p().rotate = 0; g.p().crash = 0; @@ -299,7 +299,7 @@ void act(gamedata &g, int jx, int jy, bool jb) { g.p().s = 0; g.p().ammo = g.p().maxammo; g.p().bombs = g.p().maxbombs; - g.p().coms = 0; + g.p().coms = plane::Coms::ACTION; g.p().targetx = 0; g.p().targety = 0; g.p().cruiseheight = 0; @@ -367,16 +367,16 @@ void keyboard(const Uint8 *keys, int &jx, int &jy, bool &jb, SDL_Scancode up, SD void control(gamedata &g, const Uint8 *keys, int &jx, int &jy, bool &jb) { switch (g.p().control) { - case 1: // Player 1 - keyboard(keys, jx, jy, jb, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, - SDL_SCANCODE_RETURN); - break; - case 2: // Player 2 - keyboard(keys, jx, jy, jb, SDL_SCANCODE_S, SDL_SCANCODE_X, SDL_SCANCODE_Z, SDL_SCANCODE_C, SDL_SCANCODE_LCTRL); - break; - default: // Computer controlled - computer_ai(g, g.p(), jx, jy, jb); - break; + case 1: // Player 1 + keyboard(keys, jx, jy, jb, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, + SDL_SCANCODE_RETURN); + break; + case 2: // Player 2 + keyboard(keys, jx, jy, jb, SDL_SCANCODE_S, SDL_SCANCODE_X, SDL_SCANCODE_Z, SDL_SCANCODE_C, SDL_SCANCODE_LCTRL); + break; + default: // Computer controlled + computer_ai(g, g.p(), jx, jy, jb); + break; } } diff --git a/apricots/init.cpp b/apricots/init.cpp index bbbe1e7..1898a96 100644 --- a/apricots/init.cpp +++ b/apricots/init.cpp @@ -22,7 +22,7 @@ #include "apricots.h" -// Display setup (double buffered with two playfields) +// Display setup void setup_display(gamedata &g) { SDL_Window *window = @@ -133,26 +133,13 @@ void load_shapes(gamedata &g, shape images[]) { void init_sound(sampleio &sound) { - char filenames[14][255]; - for (int i = 0; i < 14; i++) { + char filenames[SOUNDS_COUNT][255]; + for (int i = 0; i < SOUNDS_COUNT; i++) { strcpy(filenames[i], AP_PATH); + strcat(filenames[i], SOUND_NAMES[i]); } - strcat(filenames[0], "engine.wav"); - strcat(filenames[1], "jet.wav"); - strcat(filenames[2], "explode.wav"); - strcat(filenames[3], "groundhit.wav"); - strcat(filenames[4], "fuelexplode.wav"); - strcat(filenames[5], "shot.wav"); - strcat(filenames[6], "gunshot.wav"); - strcat(filenames[7], "bomb.wav"); - strcat(filenames[8], "splash.wav"); - strcat(filenames[9], "laser.wav"); - strcat(filenames[10], "stall.wav"); - strcat(filenames[11], "gunshot2.wav"); - strcat(filenames[12], "afterburner.wav"); - strcat(filenames[13], "finish.wav"); - - sound.init(14, filenames, 2, 6); + + sound.init(SOUNDS_COUNT, filenames, 2, 6); } // Initialize the game constants @@ -363,12 +350,12 @@ void init_gamedata(gamedata &g) { g.planeinfo[6].basetype = getConfig(config, "BASE6", 1, 1, 7); // Control: 1=Player 1, 2=Player 2, 0=AI - g.planeinfo[1].control = getConfig(config, "CONTROL1", 1, 0, 2); - g.planeinfo[2].control = getConfig(config, "CONTROL2", 0, 0, 2); - g.planeinfo[3].control = getConfig(config, "CONTROL3", 0, 0, 2); - g.planeinfo[4].control = getConfig(config, "CONTROL4", 0, 0, 2); - g.planeinfo[5].control = getConfig(config, "CONTROL5", 0, 0, 2); - g.planeinfo[6].control = getConfig(config, "CONTROL6", 0, 0, 2); + g.planeinfo[1].control = (plane::Control) getConfig(config, "CONTROL1", 1, 0, 2); + g.planeinfo[2].control = (plane::Control) getConfig(config, "CONTROL2", 0, 0, 2); + g.planeinfo[3].control = (plane::Control) getConfig(config, "CONTROL3", 0, 0, 2); + g.planeinfo[4].control = (plane::Control) getConfig(config, "CONTROL4", 0, 0, 2); + g.planeinfo[5].control = (plane::Control) getConfig(config, "CONTROL5", 0, 0, 2); + g.planeinfo[6].control = (plane::Control) getConfig(config, "CONTROL6", 0, 0, 2); // Error check int count[3]; count[0] = 0; diff --git a/apricots/sampleio.cpp b/apricots/sampleio.cpp index 65d816b..d908b37 100644 --- a/apricots/sampleio.cpp +++ b/apricots/sampleio.cpp @@ -234,11 +234,11 @@ ALboolean sampleio ::sourceisplaying(ALuint sid) { #endif switch (state) { - case AL_PLAYING: - case AL_PAUSED: - return AL_TRUE; - default: - break; + case AL_PLAYING: + case AL_PAUSED: + return AL_TRUE; + default: + break; } return AL_FALSE; } diff --git a/apricots/setup.cpp b/apricots/setup.cpp index 1e266c2..724978e 100644 --- a/apricots/setup.cpp +++ b/apricots/setup.cpp @@ -8,54 +8,23 @@ #include "apricots.h" -// Create airbases +// Land generation randomizer +LandType randomland(LandType landa, LandType landb) { + if (drand() > 0.5) + return landa; + return landb; +} +// Create airbases void create_airbases(airbase base[], info planeinfo[], int planes) { - base[0] = EMPTY_AIRBASE; for (int n = 1; n <= planes; n++) { - switch (planeinfo[n].basetype) { - case 1: - base[n] = STANDARD_AIRBASE; - break; - case 2: - base[n] = REVERSED_AIRBASE; - break; - case 3: - base[n] = LITTLE_AIRBASE; - break; - case 4: - base[n] = LONG_AIRBASE; - break; - case 5: - base[n] = ORIGINAL_AIRBASE; - break; - case 6: - base[n] = SHOOTY_AIRBASE; - break; - case 7: - base[n] = TWOGUN_AIRBASE; - break; - default: - switch_bad_default("info.basetype", __FILE__, __LINE__); - break; - } + base[n] = AIRBASES[planeinfo[n].basetype]; } } -// Land generation randomizer - -int randomland(int landa, int landb) { - - if (drand() > 0.5) - return landa; - return landb; -} - // Create the game map - void setup_map(map &gamemap, int planes, airbase base[], bool flatground[]) { - // Arrange airbase flags (to pin out airbase locations) int airbaseflag[MAP_W]; for (int x1 = 0; x1 < MAP_W; x1++) { @@ -87,16 +56,8 @@ void setup_map(map &gamemap, int planes, airbase base[], bool flatground[]) { } } - // Setup landtypes - const int HILAND = 0; - const int LOLAND = 1; - const int BEACH = 2; - const int PORTLEFT = 3; - const int PORTRIGHT = 4; - const int SEA = 5; - int height = 0; - int land = 0; + LandType land = HILAND; // Do left of map int r; @@ -123,248 +84,251 @@ void setup_map(map &gamemap, int planes, airbase base[], bool flatground[]) { // Do middle portion of map for (int x = 1; x < MAP_W - 1; x++) { switch (land) { - case HILAND: - gamemap.groundheight[x] = 32 * height + 9; - flatground[x * 2] = true; - r = int(drand() * 6.0); - if ((airbaseflag[x] == 2) && (height == MAP_H - 3)) - r = 5; - if ((airbaseflag[x] == 2) && (height == MAP_H - 2) && ((r == 2) || (r == 4))) - r = 5; - if (airbaseflag[x] == 1) - r = 0; - if ((height == MAP_H - 3) && ((r == 2) || (r == 4))) - r = 0; - if ((height == MAP_H - 1) && (r == 5)) - r = 0; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - gamemap.image[x][height] = 9; - flatground[x * 2 + 1] = true; - break; - case 1: - gamemap.image[x][height] = 8; - land = LOLAND; - break; - case 2: - gamemap.image[x][height] = 10; - gamemap.image[x][height - 1] = 11; - height--; - land = LOLAND; - break; - case 3: - gamemap.image[x][height] = 20; - land = LOLAND; - break; - case 4: - gamemap.image[x][height] = 15; - land = randomland(HILAND, LOLAND); - gamemap.image[x][height - 1] = 19 - (2 * land); - height--; - break; - case 5: - gamemap.image[x][height] = 18; - if ((height != MAP_H - 2) || (drand() > 0.33) || (airbaseflag[x] == 2)) { - land = randomland(HILAND, LOLAND); - gamemap.image[x][height + 1] = 14 - (10 * land); - height++; - } else { - land = SEA; - gamemap.image[x][height + 1] = 28; - height++; + case HILAND: + gamemap.groundheight[x] = 32 * height + 9; + flatground[x * 2] = true; + r = int(drand() * 6.0); + if ((airbaseflag[x] == 2) && (height == MAP_H - 3)) + r = 5; + if ((airbaseflag[x] == 2) && (height == MAP_H - 2) && ((r == 2) || (r == 4))) + r = 5; + if (airbaseflag[x] == 1) + r = 0; + if ((height == MAP_H - 3) && ((r == 2) || (r == 4))) + r = 0; + if ((height == MAP_H - 1) && (r == 5)) + r = 0; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + gamemap.image[x][height] = 9; + flatground[x * 2 + 1] = true; + break; + case 1: + gamemap.image[x][height] = 8; + land = LOLAND; + break; + case 2: + gamemap.image[x][height] = 10; + gamemap.image[x][height - 1] = 11; + height--; + land = LOLAND; + break; + case 3: + gamemap.image[x][height] = 20; + land = LOLAND; + break; + case 4: + gamemap.image[x][height] = 15; + land = randomland(HILAND, LOLAND); + gamemap.image[x][height - 1] = 19 - (2 * land); + height--; + break; + case 5: + gamemap.image[x][height] = 18; + if ((height != MAP_H - 2) || (drand() > 0.33) || (airbaseflag[x] == 2)) { + land = randomland(HILAND, LOLAND); + gamemap.image[x][height + 1] = 14 - (10 * land); + height++; + } else { + land = SEA; + gamemap.image[x][height + 1] = 28; + height++; + } + break; } break; - } - break; - case LOLAND: - gamemap.groundheight[x] = 32 * height + 22; - flatground[x * 2] = true; - r = int(drand() * 6.0); - if ((airbaseflag[x] == 2) && (height == MAP_H - 3)) - r = 2 + 3 * int(drand() * 2.0); - if ((airbaseflag[x] == 2) && (height == MAP_H - 2) && (r == 4)) - r = 5; - if (airbaseflag[x] == 1) - r = 0; - if ((height == MAP_H - 3) && (r == 4)) - r = 0; - if ((height == MAP_H - 1) && (r == 5)) - r = 2; - if (((airbaseflag[x] == 2) || (x == MAP_W - 2)) && (height == MAP_H - 1) && (r == 2)) - r = 0; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - gamemap.image[x][height] = 5; - flatground[x * 2 + 1] = true; - break; - case 1: - gamemap.image[x][height] = 7; - land = HILAND; - break; - case 2: - if (height < MAP_H - 1) { - gamemap.image[x][height] = 13; - gamemap.image[x][height + 1] = 12; - height++; - land = HILAND; - } else { - land = randomland(BEACH, PORTLEFT); - if (airbaseflag[x] == 3) - land = BEACH; - gamemap.image[x][height] = 6 + (8 * land); - if (land == PORTLEFT) + case LOLAND: + gamemap.groundheight[x] = 32 * height + 22; + flatground[x * 2] = true; + r = int(drand() * 6.0); + if ((airbaseflag[x] == 2) && (height == MAP_H - 3)) + r = 2 + 3 * int(drand() * 2.0); + if ((airbaseflag[x] == 2) && (height == MAP_H - 2) && (r == 4)) + r = 5; + if (airbaseflag[x] == 1) + r = 0; + if ((height == MAP_H - 3) && (r == 4)) + r = 0; + if ((height == MAP_H - 1) && (r == 5)) + r = 2; + if (((airbaseflag[x] == 2) || (x == MAP_W - 2)) && (height == MAP_H - 1) && (r == 2)) + r = 0; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + gamemap.image[x][height] = 5; flatground[x * 2 + 1] = true; + break; + case 1: + gamemap.image[x][height] = 7; + land = HILAND; + break; + case 2: + if (height < MAP_H - 1) { + gamemap.image[x][height] = 13; + gamemap.image[x][height + 1] = 12; + height++; + land = HILAND; + } else { + land = randomland(BEACH, PORTLEFT); + if (airbaseflag[x] == 3) + land = BEACH; + gamemap.image[x][height] = 6 + (8 * land); + if (land == PORTLEFT) + flatground[x * 2 + 1] = true; + } + break; + case 3: + gamemap.image[x][height] = 21; + land = HILAND; + break; + case 4: + gamemap.image[x][height] = 6; + land = randomland(HILAND, LOLAND); + gamemap.image[x][height - 1] = 19 - (2 * land); + height--; + break; + case 5: + gamemap.image[x][height] = 16; + if ((height != MAP_H - 2) || (drand() > 0.33) || (airbaseflag[x] == 2)) { + land = randomland(HILAND, LOLAND); + gamemap.image[x][height + 1] = 14 - (10 * land); + height++; + } else { + land = SEA; + gamemap.image[x][height + 1] = 28; + height++; + } + break; } break; - case 3: - gamemap.image[x][height] = 21; - land = HILAND; - break; - case 4: - gamemap.image[x][height] = 6; - land = randomland(HILAND, LOLAND); - gamemap.image[x][height - 1] = 19 - (2 * land); - height--; - break; - case 5: - gamemap.image[x][height] = 16; - if ((height != MAP_H - 2) || (drand() > 0.33) || (airbaseflag[x] == 2)) { - land = randomland(HILAND, LOLAND); - gamemap.image[x][height + 1] = 14 - (10 * land); - height++; - } else { - land = SEA; - gamemap.image[x][height + 1] = 28; - height++; + + case BEACH: + gamemap.groundheight[x] = GAME_HEIGHT - 4; + flatground[x * 2] = true; + r = int(drand() * 3.0); + if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) + r = 1; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + gamemap.image[x][height] = 24; + flatground[x * 2 + 1] = true; + break; + case 1: + gamemap.image[x][height] = 23; + land = LOLAND; + break; + case 2: + gamemap.image[x][height] = 25; + land = SEA; + break; } break; - } - break; - case BEACH: - gamemap.groundheight[x] = GAME_HEIGHT - 4; - flatground[x * 2] = true; - r = int(drand() * 3.0); - if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) - r = 1; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - gamemap.image[x][height] = 24; - flatground[x * 2 + 1] = true; - break; - case 1: - gamemap.image[x][height] = 23; - land = LOLAND; + case PORTLEFT: + gamemap.groundheight[x] = GAME_HEIGHT - 10; + flatground[x * 2] = true; + r = int(drand() * 3.0); + if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) + r = 1; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + gamemap.image[x][height] = 32; + flatground[x * 2 + 1] = true; + break; + case 1: + case 2: + gamemap.image[x][height] = 33; + land = SEA; + break; + } break; - case 2: - gamemap.image[x][height] = 25; - land = SEA; + + case PORTRIGHT: + gamemap.groundheight[x] = GAME_HEIGHT - 10; + flatground[x * 2] = true; + r = int(drand() * 3.0); + if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) + r = 1; + if (r == 0) { + gamemap.image[x][height] = 32; + flatground[x * 2 + 1] = true; + } else if (r > 0) { + gamemap.image[x][height] = 31; + land = LOLAND; + flatground[x * 2 + 1] = true; + } break; - } - break; - case PORTLEFT: - gamemap.groundheight[x] = GAME_HEIGHT - 10; - flatground[x * 2] = true; - r = int(drand() * 3.0); - if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) - r = 1; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - gamemap.image[x][height] = 32; - flatground[x * 2 + 1] = true; + case SEA: + gamemap.groundheight[x] = GAME_HEIGHT - 2; + r = int(drand() * 4.0); + if (airbaseflag[x] == 2) + r = 3; + if (airbaseflag[x] == 3) + r = 2; + if ((r == 2) && (x == MAP_W - 2)) + r = 0; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + case 1: + gamemap.image[x][height] = 27; + break; + case 2: + land = randomland(BEACH, PORTRIGHT); + gamemap.image[x][height] = 18 + (4 * land); + break; + case 3: + gamemap.image[x][height] = 29; + land = randomland(HILAND, LOLAND); + gamemap.image[x][height - 1] = 19 - (2 * land); + height--; + break; + } break; - case 1: - case 2: - gamemap.image[x][height] = 33; - land = SEA; + default: + switch_bad_default("land", __FILE__, __LINE__); break; - } - break; + } + } - case PORTRIGHT: - gamemap.groundheight[x] = GAME_HEIGHT - 10; - flatground[x * 2] = true; - r = int(drand() * 3.0); - if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) - r = 1; - if (r == 0) { - gamemap.image[x][height] = 32; - flatground[x * 2 + 1] = true; - } else if (r > 0) { - gamemap.image[x][height] = 31; - land = LOLAND; - flatground[x * 2 + 1] = true; - } + // Do right of map + switch (land) { + case HILAND: + gamemap.image[MAP_W - 1][height] = 15; + gamemap.groundheight[MAP_W - 1] = 32 * height + 9; + flatground[(MAP_W - 1) * 2] = true; + break; + case LOLAND: + gamemap.image[MAP_W - 1][height] = 6; + gamemap.groundheight[MAP_W - 1] = 32 * height + 22; + flatground[(MAP_W - 1) * 2] = true; break; - case SEA: - gamemap.groundheight[x] = GAME_HEIGHT - 2; - r = int(drand() * 4.0); - if (airbaseflag[x] == 2) - r = 3; - if (airbaseflag[x] == 3) - r = 2; - if ((r == 2) && (x == MAP_W - 2)) - r = 0; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - case 1: - gamemap.image[x][height] = 27; - break; - case 2: - land = randomland(BEACH, PORTRIGHT); - gamemap.image[x][height] = 18 + (4 * land); - break; - case 3: - gamemap.image[x][height] = 29; - land = randomland(HILAND, LOLAND); - gamemap.image[x][height - 1] = 19 - (2 * land); - height--; - break; - } + gamemap.image[MAP_W - 1][height] = 29; + gamemap.groundheight[MAP_W - 1] = GAME_HEIGHT - 2; break; + case BEACH: + case PORTLEFT: + case PORTRIGHT: default: switch_bad_default("land", __FILE__, __LINE__); break; - } - } - - // Do right of map - switch (land) { - case HILAND: - gamemap.image[MAP_W - 1][height] = 15; - gamemap.groundheight[MAP_W - 1] = 32 * height + 9; - flatground[(MAP_W - 1) * 2] = true; - break; - case LOLAND: - gamemap.image[MAP_W - 1][height] = 6; - gamemap.groundheight[MAP_W - 1] = 32 * height + 22; - flatground[(MAP_W - 1) * 2] = true; - break; - case SEA: - gamemap.image[MAP_W - 1][height] = 29; - gamemap.groundheight[MAP_W - 1] = GAME_HEIGHT - 2; - break; - default: - switch_bad_default("land", __FILE__, __LINE__); - break; } for (int y = 0; y < height; y++) { gamemap.image[MAP_W - 1][y] = 3; @@ -723,21 +687,7 @@ void setup_planes(linkedlist &p, linkedlist &dp, airbase base for (int n = 1; n <= planes; n++) { // Create plane - plane newplane; - switch (planeinfo[n].planetype) { - case 1: - newplane = SPITFIRE; - break; - case 2: - newplane = JET; - break; - case 3: - newplane = STEALTH; - break; - default: - switch_bad_default("info.planetype", __FILE__, __LINE__); - break; - } + plane newplane = PLANES[planeinfo[n].planetype]; newplane.x = base[n].planex; newplane.y = base[n].planey; newplane.d = base[n].planed; From 76da55711a3983b52552d89e0349256990d6361a Mon Sep 17 00:00:00 2001 From: Matt Molyneaux Date: Tue, 12 Dec 2023 00:24:29 +0000 Subject: [PATCH 2/2] Fix lint issues - fix error that only appears on Ubuntu's cppcheck - add make rule to actually fix clang-format errors - fix indent errors picked up by clang-format --- Makefile.am | 9 +- apricots/ai.cpp | 684 +++++++++++++++++++++--------------------- apricots/all.cpp | 36 +-- apricots/apricots.h | 214 ++++++++++--- apricots/collide.cpp | 21 +- apricots/drak.cpp | 8 +- apricots/fall.cpp | 146 +++++---- apricots/game.cpp | 20 +- apricots/init.cpp | 12 +- apricots/sampleio.cpp | 10 +- apricots/setup.cpp | 448 +++++++++++++-------------- 11 files changed, 862 insertions(+), 746 deletions(-) diff --git a/Makefile.am b/Makefile.am index 15e5c66..7274407 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,14 +8,19 @@ dist-hook: dist-ChangeLog CPPCHECK_CMD = cppcheck --std=c++17 --error-exitcode=1 -CLANG_FORMAT_CMD = clang-format -style=file -Werror -i -dry-run +CLANG_FORMAT_CMD = clang-format -style=file -Werror -i +CLANG_FORMAT_CHECK_CMD = ${CLANG_FORMAT_CMD} -dry-run .PHONY: cppcheck cppcheck: ${CPPCHECK_CMD} ${SUBDIRS} +.PHONY: clang-format-fix +clang-format-fix: + find ${SUBDIRS} \( -name \*.h -o -name \*.cpp \) -print0 | xargs -0 -n1 ${CLANG_FORMAT_CMD} + .PHONY: clang-format clang-format: - find ${SUBDIRS} \( -name \*.h -o -name \*.cpp \) -print0 | xargs -0 -n1 ${CLANG_FORMAT_CMD} + find ${SUBDIRS} \( -name \*.h -o -name \*.cpp \) -print0 | xargs -0 -n1 ${CLANG_FORMAT_CHECK_CMD} check-local: cppcheck clang-format diff --git a/apricots/ai.cpp b/apricots/ai.cpp index e827720..efb4c80 100644 --- a/apricots/ai.cpp +++ b/apricots/ai.cpp @@ -46,374 +46,374 @@ void followtarget(plane &p, int &jx, int &jy, int rx, int ry, bool reverse) { void computer_ai(gamedata &g, plane &p, int &jx, int &jy, bool &jb) { switch (g.p().land) { - case plane::LandingState::LANDED: - jy = -1; - break; - case plane::LandingState::TAKING_OFF: - jy = -1; - if (p.s > 5.0 * GAME_SPEED) { - jx = sign(9 - p.d); - } - break; - case plane::LandingState::FLYING: - if (p.rotate == 0) { - if ((p.coms == plane::Coms::ACTION) && (p.state == 0)) { - // Stall Avoidance - if (((p.y < 24.0) && (p.ys < 0.0)) || (p.s < 3.0 * GAME_SPEED)) - p.coms = plane::Coms::LEVEL_OFF; - // Drak mothership avoidance - if (g.drakms.exist == 1) { - double dx = p.x - g.drakms.x + (p.xs - g.drakms.xs) * 4.0 / GAME_SPEED; - if ((dx > -40.0) && (dx < 136.0)) { - if ((p.y > 46.0) && (p.y < 106.0) && ((p.ys < 0.0) || (p.y < 86.0))) - p.coms = plane::Coms::LEVEL_OFF; - if ((p.y <= 46.0) && (p.ys >= 0.0) && ((p.ys > 0.0) || (p.y > 26.0))) - p.coms = plane::Coms::GO_UP; - } - } - // Groundheight tracking - int px = int(p.x + 8) / 16; - if (((p.d == 5) || (p.d == 13)) && - ((p.y + 20.0 > g.gamemap.steepheight[px]) || (p.y + 20.0 > g.gamemap.steepheight[px + 1]))) - p.coms = plane::Coms::GO_UP; - if ((p.d > 2) && (p.d < 8)) - px = clamp(px - 1, 0, MAP_W * 2 - 1); - if ((p.d > 10) && (p.d < 16)) - px = clamp(px + 1, 0, MAP_W * 2 - 1); - if (p.ys < 0.0) { - if ((p.y + 5.0 > g.gamemap.smoothheight[px]) || (p.y + 5.0 > g.gamemap.smoothheight[px + 1])) - p.coms = plane::Coms::GO_UP; - } else { - if ((p.y + 40.0 > g.gamemap.smoothheight[px]) || (p.y + 40.0 > g.gamemap.smoothheight[px + 1])) + case plane::LandingState::LANDED: + jy = -1; + break; + case plane::LandingState::TAKING_OFF: + jy = -1; + if (p.s > 5.0 * GAME_SPEED) { + jx = sign(9 - p.d); + } + break; + case plane::LandingState::FLYING: + if (p.rotate == 0) { + if ((p.coms == plane::Coms::ACTION) && (p.state == 0)) { + // Stall Avoidance + if (((p.y < 24.0) && (p.ys < 0.0)) || (p.s < 3.0 * GAME_SPEED)) + p.coms = plane::Coms::LEVEL_OFF; + // Drak mothership avoidance + if (g.drakms.exist == 1) { + double dx = p.x - g.drakms.x + (p.xs - g.drakms.xs) * 4.0 / GAME_SPEED; + if ((dx > -40.0) && (dx < 136.0)) { + if ((p.y > 46.0) && (p.y < 106.0) && ((p.ys < 0.0) || (p.y < 86.0))) + p.coms = plane::Coms::LEVEL_OFF; + if ((p.y <= 46.0) && (p.ys >= 0.0) && ((p.ys > 0.0) || (p.y > 26.0))) p.coms = plane::Coms::GO_UP; } - if ((p.y - 40.0 > g.gamemap.realheight[px]) || (p.y - 40.0 > g.gamemap.realheight[px + 1])) - p.coms = plane::Coms::GO_DOWN; - // Landing runway approach - if (p.targetx == -10) { - int dx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx; - if ((dx > 10) && (dx < -10 + g.base[p.side].runwaylength) && - ((p.d == 6) || (p.d == 7) || (p.d == 11) || (p.d == 12))) { - p.coms = plane::Coms::ACTION; - if (g.base[p.side].planey - int(p.y) < 50) - p.coms = plane::Coms::START_LANDING; - } - } - // Side of map avoidance - if (int(p.x) < 100) - p.coms = plane::Coms::GO_RIGHT; - if (int(p.x) > GAME_WIDTH - 116) - p.coms = plane::Coms::GO_LEFT; } - // Recover from stall - if (p.state == 1) + // Groundheight tracking + int px = int(p.x + 8) / 16; + if (((p.d == 5) || (p.d == 13)) && + ((p.y + 20.0 > g.gamemap.steepheight[px]) || (p.y + 20.0 > g.gamemap.steepheight[px + 1]))) + p.coms = plane::Coms::GO_UP; + if ((p.d > 2) && (p.d < 8)) + px = clamp(px - 1, 0, MAP_W * 2 - 1); + if ((p.d > 10) && (p.d < 16)) + px = clamp(px + 1, 0, MAP_W * 2 - 1); + if (p.ys < 0.0) { + if ((p.y + 5.0 > g.gamemap.smoothheight[px]) || (p.y + 5.0 > g.gamemap.smoothheight[px + 1])) + p.coms = plane::Coms::GO_UP; + } else { + if ((p.y + 40.0 > g.gamemap.smoothheight[px]) || (p.y + 40.0 > g.gamemap.smoothheight[px + 1])) + p.coms = plane::Coms::GO_UP; + } + if ((p.y - 40.0 > g.gamemap.realheight[px]) || (p.y - 40.0 > g.gamemap.realheight[px + 1])) p.coms = plane::Coms::GO_DOWN; - switch (p.coms) { - case plane::Coms::ACTION: { - // Land if out of shots and bombs - if ((p.ammo == 0) && (p.bombs == 0)) - p.targetx = -10; - // Land if mission is done - if ((g.mission < 2) && (p.score >= g.targetscore)) - p.targetx = -10; - if ((g.mission == 2) && (p.targetscore == 0)) - p.targetx = -10; - // Drak fighters don't land - if ((p.targetx == -10) && (p.drak)) - p.targetx = 0; - // Check for gunthreat and change target if target not already a gun - if ((p.gunthreat > 0) && (p.bombs > 0)) { - if (p.targetx > 0) { - if (g.gamemap.b[p.targetx].type != 5) { - p.targetx = p.gunthreat; - p.targety = g.gamemap.b[p.targetx].y; - } - } else { - p.targetx = p.gunthreat; - p.targety = g.gamemap.b[p.targetx].y; - } - } - // Find target if no target exists - if (p.targetx == 0) { - // Try a building - p.targetx = int(drand() * (MAP_W * 2 - 3)) + 2; - if ((g.gamemap.b[p.targetx].type < 3) || (g.gamemap.b[p.targetx].points < 0) || - (g.gamemap.b[p.targetx].side == p.side) || (p.bombs == 0) || - ((g.gamemap.b[p.targetx].side == 0) && (g.mission == 2))) { - // Building unsuitable target so try plane instead - p.targetx = 0; - int tryplaneid = int(drand() * g.planes) + 1; - if ((((g.mission != 2) && (int(drand() * 2) == 1)) || (int(drand() * 30) == 1) || (p.drak)) && - (p.ammo > 0)) { - g.dp.reset(); - while (g.dp.next()) { - // Check if plane is suitable target and select if so - if ((g.dp().id == tryplaneid) && (g.dp().side != p.side) && (g.dp().state < 2) && (!g.dp().hide)) - p.targetx = -tryplaneid; - } - } - } else { - // Building suitable so becomes target - p.targety = g.gamemap.b[p.targetx].y; - } - // Set new cruiseheight if changed target - if (p.cruiseheight == 0) - p.cruiseheight = 20 + int(drand() * (GAME_HEIGHT - 176)); - if (p.targetx == 0) { - // Chase false target - if (p.xs > 0) { - followtarget(p, jx, jy, -160, 0, false); - } else { - followtarget(p, jx, jy, 160, 0, false); - } - // If still no target for too long then land - p.targety++; - if (p.targety == int(200 / GAME_SPEED)) - p.targetx = -10; - } - } - // Target is building - if (p.targetx > 0) { - int rx = int(p.x) - 16 * p.targetx + 8; - int ry = int(p.y) + 40 - p.targety; - followtarget(p, jx, jy, rx, ry, ((abs(rx) < 110) && (g.gamemap.b[p.targetx].type == 5))); - // Check to see if can still bomb the building - if ((g.gamemap.b[p.targetx].type == 0) || (p.bombs == 0)) { - p.targetx = 0; - p.targety = 0; - p.cruiseheight = 0; - } - } - // Target is plane - if ((p.targetx < 0) && (p.targetx > -10)) { - g.dp.reset(); - while (g.dp.next()) { - if (g.dp().id == -p.targetx) { - // Check to see if can still shoot plane - if ((g.dp().state > 1) || (p.ammo == 0) || ((g.dp().hide) && (int(drand() * 40) == 1))) { - p.targetx = 0; - p.targety = 0; - p.cruiseheight = 0; - } else { - int rx = int(p.x - g.dp().x); - int ry = int(p.y - g.dp().y); - followtarget(p, jx, jy, rx, ry, (rx * rx + ry * ry < 2500)); - } - } - } - } - // Target is runway - if (p.targetx == -10) { - int rx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx - g.base[p.side].runwaylength / 2; - int ry = int(p.y) - g.base[p.side].planey; - followtarget(p, jx, jy, rx, ry, false); - } - break; - } - case plane::Coms::GO_UP: { - if ((p.d > 3) && (p.d < 9)) - jx = 1; - if ((p.d > 9) && (p.d < 15)) - jx = -1; - if (p.d == 9) { - if (int(p.x) < 100) - jx = -1; - if (int(p.x) > GAME_WIDTH - 116) - jx = 1; - if (jx == 0) { - int px = int(p.x + 8) / 16 - 1; - jx = sign(g.gamemap.smoothheight[px] - g.gamemap.smoothheight[px + 3]); - if (jx == 0) - jx = int(drand() * 2.0) * 2 - 1; - } - } + // Landing runway approach + if (p.targetx == -10) { + int dx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx; + if ((dx > 10) && (dx < -10 + g.base[p.side].runwaylength) && + ((p.d == 6) || (p.d == 7) || (p.d == 11) || (p.d == 12))) { p.coms = plane::Coms::ACTION; - break; - } - case plane::Coms::GO_RIGHT: { - // Go right - if ((p.d == 1) || (p.d > 13)) - jx = 1; - if ((p.d > 7) && (p.d < 13)) - jx = -1; - if ((p.d < 8) && (p.d > 1)) { - jx = 1; - if (p.s < 4.0 * GAME_SPEED) - jx = -1; - if (int(p.y) < 34) - jx = -1; - int px = int(p.x + 8) / 16; - if ((int(p.y) + 45 > g.gamemap.realheight[px]) && (int(p.y) + 45 > g.gamemap.realheight[px + 1])) - jx = 1; - } - if (jx == 1) - p.coms = plane::Coms::GO_RIGHT_PLUS; - if (jx == -1) - p.coms = plane::Coms::GO_RIGHT_MINUS; - if (p.d == 13) - p.coms = plane::Coms::ACTION; - break; + if (g.base[p.side].planey - int(p.y) < 50) + p.coms = plane::Coms::START_LANDING; } - case plane::Coms::GO_LEFT: { - if (p.d < 5) - jx = -1; - if ((p.d > 5) && (p.d < 11)) - jx = 1; - if (p.d > 10) { - jx = -1; - if (p.s < 4.0 * GAME_SPEED) - jx = 1; - if (int(p.y) < 34) - jx = 1; - int px = int(p.x + 8) / 16; - if ((int(p.y) + 45 > g.gamemap.realheight[px]) && (int(p.y) + 45 > g.gamemap.realheight[px + 1])) - jx = -1; + } + // Side of map avoidance + if (int(p.x) < 100) + p.coms = plane::Coms::GO_RIGHT; + if (int(p.x) > GAME_WIDTH - 116) + p.coms = plane::Coms::GO_LEFT; + } + // Recover from stall + if (p.state == 1) + p.coms = plane::Coms::GO_DOWN; + switch (p.coms) { + case plane::Coms::ACTION: { + // Land if out of shots and bombs + if ((p.ammo == 0) && (p.bombs == 0)) + p.targetx = -10; + // Land if mission is done + if ((g.mission < 2) && (p.score >= g.targetscore)) + p.targetx = -10; + if ((g.mission == 2) && (p.targetscore == 0)) + p.targetx = -10; + // Drak fighters don't land + if ((p.targetx == -10) && (p.drak)) + p.targetx = 0; + // Check for gunthreat and change target if target not already a gun + if ((p.gunthreat > 0) && (p.bombs > 0)) { + if (p.targetx > 0) { + if (g.gamemap.b[p.targetx].type != 5) { + p.targetx = p.gunthreat; + p.targety = g.gamemap.b[p.targetx].y; } - if (jx == 1) - p.coms = plane::Coms::GO_LEFT_PLUS; - if (jx == -1) - p.coms = plane::Coms::GO_LEFT_MINUS; - if (p.d == 5) - p.coms = plane::Coms::ACTION; - break; - } - case plane::Coms::LEVEL_OFF: { - if (p.d < 8) - jx = -1; - if (p.d > 10) - jx = 1; - if (p.d == 1) - jx = int(drand() * 2.0) * 2 - 1; - if ((p.d > 3) && (p.d < 15)) - p.coms = plane::Coms::ACTION; - break; - } - case plane::Coms::GO_DOWN: { - // Go down - if (p.d < 9) - jx = -1; - if (p.d > 9) - jx = 1; - if (p.d == 1) - jx = int(drand() * 2.0) * 2 - 1; - if ((p.d == 9) && (p.s > 3.0 * GAME_SPEED)) - p.coms = plane::Coms::ACTION; - break; + } else { + p.targetx = p.gunthreat; + p.targety = g.gamemap.b[p.targetx].y; } - case plane::Coms::GO_UP_LOTS: { - if ((p.d > 1) && (p.d < 9)) - jx = 1; - if (p.d > 9) - jx = -1; - if (p.d == 9) { - if (int(p.x) < 100) - jx = -1; - if (int(p.x) > GAME_WIDTH - 116) - jx = 1; - if (jx == 0) { - int px = int(p.x + 8) / 16 - 1; - jx = sign(g.gamemap.smoothheight[px] - g.gamemap.smoothheight[px + 3]); - if (jx == 0) - jx = int(drand() * 2.0) * 2 - 1; + } + // Find target if no target exists + if (p.targetx == 0) { + // Try a building + p.targetx = int(drand() * (MAP_W * 2 - 3)) + 2; + if ((g.gamemap.b[p.targetx].type < 3) || (g.gamemap.b[p.targetx].points < 0) || + (g.gamemap.b[p.targetx].side == p.side) || (p.bombs == 0) || + ((g.gamemap.b[p.targetx].side == 0) && (g.mission == 2))) { + // Building unsuitable target so try plane instead + p.targetx = 0; + int tryplaneid = int(drand() * g.planes) + 1; + if ((((g.mission != 2) && (int(drand() * 2) == 1)) || (int(drand() * 30) == 1) || (p.drak)) && + (p.ammo > 0)) { + g.dp.reset(); + while (g.dp.next()) { + // Check if plane is suitable target and select if so + if ((g.dp().id == tryplaneid) && (g.dp().side != p.side) && (g.dp().state < 2) && (!g.dp().hide)) + p.targetx = -tryplaneid; } } - p.coms = plane::Coms::ACTION; - break; - } - case plane::Coms::GO_RIGHT_PLUS: { - if (p.d != 13) { - jx = 1; - } else { - p.coms = plane::Coms::ACTION; - } - break; + } else { + // Building suitable so becomes target + p.targety = g.gamemap.b[p.targetx].y; } - case plane::Coms::GO_RIGHT_MINUS: { - // Go right -ve - if (p.d != 13) { - jx = -1; + // Set new cruiseheight if changed target + if (p.cruiseheight == 0) + p.cruiseheight = 20 + int(drand() * (GAME_HEIGHT - 176)); + if (p.targetx == 0) { + // Chase false target + if (p.xs > 0) { + followtarget(p, jx, jy, -160, 0, false); } else { - p.coms = plane::Coms::ACTION; + followtarget(p, jx, jy, 160, 0, false); } - break; + // If still no target for too long then land + p.targety++; + if (p.targety == int(200 / GAME_SPEED)) + p.targetx = -10; } - case plane::Coms::GO_LEFT_PLUS: { - if (p.d != 5) { - jx = 1; - } else { - p.coms = plane::Coms::ACTION; - } - break; + } + // Target is building + if (p.targetx > 0) { + int rx = int(p.x) - 16 * p.targetx + 8; + int ry = int(p.y) + 40 - p.targety; + followtarget(p, jx, jy, rx, ry, ((abs(rx) < 110) && (g.gamemap.b[p.targetx].type == 5))); + // Check to see if can still bomb the building + if ((g.gamemap.b[p.targetx].type == 0) || (p.bombs == 0)) { + p.targetx = 0; + p.targety = 0; + p.cruiseheight = 0; } - case plane::Coms::GO_LEFT_MINUS: { - if (p.d != 5) { - jx = -1; - } else { - p.coms = plane::Coms::ACTION; + } + // Target is plane + if ((p.targetx < 0) && (p.targetx > -10)) { + g.dp.reset(); + while (g.dp.next()) { + if (g.dp().id == -p.targetx) { + // Check to see if can still shoot plane + if ((g.dp().state > 1) || (p.ammo == 0) || ((g.dp().hide) && (int(drand() * 40) == 1))) { + p.targetx = 0; + p.targety = 0; + p.cruiseheight = 0; + } else { + int rx = int(p.x - g.dp().x); + int ry = int(p.y - g.dp().y); + followtarget(p, jx, jy, rx, ry, (rx * rx + ry * ry < 2500)); + } } - break; } - case plane::Coms::START_LANDING: { - if (p.d == 6) - jx = -1; - if (p.d == 12) - jx = 1; - int dx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx; - if ((dx < 10) || (dx > -10 + g.base[p.side].runwaylength)) - p.coms = plane::Coms::ACTION; - break; + } + // Target is runway + if (p.targetx == -10) { + int rx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx - g.base[p.side].runwaylength / 2; + int ry = int(p.y) - g.base[p.side].planey; + followtarget(p, jx, jy, rx, ry, false); + } + break; + } + case plane::Coms::GO_UP: { + if ((p.d > 3) && (p.d < 9)) + jx = 1; + if ((p.d > 9) && (p.d < 15)) + jx = -1; + if (p.d == 9) { + if (int(p.x) < 100) + jx = -1; + if (int(p.x) > GAME_WIDTH - 116) + jx = 1; + if (jx == 0) { + int px = int(p.x + 8) / 16 - 1; + jx = sign(g.gamemap.smoothheight[px] - g.gamemap.smoothheight[px + 3]); + if (jx == 0) + jx = int(drand() * 2.0) * 2 - 1; } - default: - switch_bad_default("plane.coms", __FILE__, __LINE__); - break; } - // Wiggle to avoid gunfire - if ((p.gunthreat > 0) && (jx == 0) && (!p.hide)) + p.coms = plane::Coms::ACTION; + break; + } + case plane::Coms::GO_RIGHT: { + // Go right + if ((p.d == 1) || (p.d > 13)) + jx = 1; + if ((p.d > 7) && (p.d < 13)) + jx = -1; + if ((p.d < 8) && (p.d > 1)) { + jx = 1; + if (p.s < 4.0 * GAME_SPEED) + jx = -1; + if (int(p.y) < 34) + jx = -1; + int px = int(p.x + 8) / 16; + if ((int(p.y) + 45 > g.gamemap.realheight[px]) && (int(p.y) + 45 > g.gamemap.realheight[px + 1])) + jx = 1; + } + if (jx == 1) + p.coms = plane::Coms::GO_RIGHT_PLUS; + if (jx == -1) + p.coms = plane::Coms::GO_RIGHT_MINUS; + if (p.d == 13) + p.coms = plane::Coms::ACTION; + break; + } + case plane::Coms::GO_LEFT: { + if (p.d < 5) + jx = -1; + if ((p.d > 5) && (p.d < 11)) + jx = 1; + if (p.d > 10) { + jx = -1; + if (p.s < 4.0 * GAME_SPEED) + jx = 1; + if (int(p.y) < 34) + jx = 1; + int px = int(p.x + 8) / 16; + if ((int(p.y) + 45 > g.gamemap.realheight[px]) && (int(p.y) + 45 > g.gamemap.realheight[px + 1])) + jx = -1; + } + if (jx == 1) + p.coms = plane::Coms::GO_LEFT_PLUS; + if (jx == -1) + p.coms = plane::Coms::GO_LEFT_MINUS; + if (p.d == 5) + p.coms = plane::Coms::ACTION; + break; + } + case plane::Coms::LEVEL_OFF: { + if (p.d < 8) + jx = -1; + if (p.d > 10) + jx = 1; + if (p.d == 1) jx = int(drand() * 2.0) * 2 - 1; + if ((p.d > 3) && (p.d < 15)) + p.coms = plane::Coms::ACTION; + break; } - // Afterburner for jet if speed slow - if ((p.burner) && (p.s < 5.0 * GAME_SPEED)) - jy = -1; - // Stealth ability - if (p.stealth) - jy = -1; - if (p.shotdelay == 0) { - // Launch bomb - if (p.targetx > 0) { - double rx = p.x - double(16 * p.targetx) + 8.0 + p.xs; - double ry = p.y - double(p.targety) + 14.0 + p.ys; - double a = (p.ys + 2.0 * GAME_SPEED) * (p.ys + 2.0 * GAME_SPEED) - (ry * 0.2) * GAME_SPEED * GAME_SPEED; - if (a > 0) { - if (abs(int(rx + p.xs * 10.0 / (GAME_SPEED * GAME_SPEED) * (-p.ys - 2.0 * GAME_SPEED + sqrt(a)))) < 6) - jy = 1; + case plane::Coms::GO_DOWN: { + // Go down + if (p.d < 9) + jx = -1; + if (p.d > 9) + jx = 1; + if (p.d == 1) + jx = int(drand() * 2.0) * 2 - 1; + if ((p.d == 9) && (p.s > 3.0 * GAME_SPEED)) + p.coms = plane::Coms::ACTION; + break; + } + case plane::Coms::GO_UP_LOTS: { + if ((p.d > 1) && (p.d < 9)) + jx = 1; + if (p.d > 9) + jx = -1; + if (p.d == 9) { + if (int(p.x) < 100) + jx = -1; + if (int(p.x) > GAME_WIDTH - 116) + jx = 1; + if (jx == 0) { + int px = int(p.x + 8) / 16 - 1; + jx = sign(g.gamemap.smoothheight[px] - g.gamemap.smoothheight[px + 3]); + if (jx == 0) + jx = int(drand() * 2.0) * 2 - 1; } } - // Fire shot - if ((p.s < 8.0 * GAME_SPEED) && (p.ammo > 0)) { - g.dp.reset(); - while (g.dp.next()) { - if ((g.dp().side != p.side) && (g.dp().state < 2) && (!g.dp().hide)) { - double rx = p.x - g.dp().x; - double ry = p.y - g.dp().y; - double d = sqrt(rx * rx + ry * ry); - if (d < 100.0) { - double smartsine = ((ry * g.dp().xs - rx * g.dp().ys) / (8.0 * GAME_SPEED) / d); - smartsine = clamp(smartsine, -1.0, 1.0); - double smartangle = asin(smartsine); - double angle = atan2(rx, ry); - int bogied = wrap((int(((angle - smartangle) / PI * 8.0) + 1.5)), 1, 17); - if (bogied == p.d) - jb = true; - } + p.coms = plane::Coms::ACTION; + break; + } + case plane::Coms::GO_RIGHT_PLUS: { + if (p.d != 13) { + jx = 1; + } else { + p.coms = plane::Coms::ACTION; + } + break; + } + case plane::Coms::GO_RIGHT_MINUS: { + // Go right -ve + if (p.d != 13) { + jx = -1; + } else { + p.coms = plane::Coms::ACTION; + } + break; + } + case plane::Coms::GO_LEFT_PLUS: { + if (p.d != 5) { + jx = 1; + } else { + p.coms = plane::Coms::ACTION; + } + break; + } + case plane::Coms::GO_LEFT_MINUS: { + if (p.d != 5) { + jx = -1; + } else { + p.coms = plane::Coms::ACTION; + } + break; + } + case plane::Coms::START_LANDING: { + if (p.d == 6) + jx = -1; + if (p.d == 12) + jx = 1; + int dx = int(p.x) - 32 * g.base[p.side].mapx - g.base[p.side].runwayx; + if ((dx < 10) || (dx > -10 + g.base[p.side].runwaylength)) + p.coms = plane::Coms::ACTION; + break; + } + default: + switch_bad_default("plane.coms", __FILE__, __LINE__); + break; + } + // Wiggle to avoid gunfire + if ((p.gunthreat > 0) && (jx == 0) && (!p.hide)) + jx = int(drand() * 2.0) * 2 - 1; + } + // Afterburner for jet if speed slow + if ((p.burner) && (p.s < 5.0 * GAME_SPEED)) + jy = -1; + // Stealth ability + if (p.stealth) + jy = -1; + if (p.shotdelay == 0) { + // Launch bomb + if (p.targetx > 0) { + double rx = p.x - double(16 * p.targetx) + 8.0 + p.xs; + double ry = p.y - double(p.targety) + 14.0 + p.ys; + double a = (p.ys + 2.0 * GAME_SPEED) * (p.ys + 2.0 * GAME_SPEED) - (ry * 0.2) * GAME_SPEED * GAME_SPEED; + if (a > 0) { + if (abs(int(rx + p.xs * 10.0 / (GAME_SPEED * GAME_SPEED) * (-p.ys - 2.0 * GAME_SPEED + sqrt(a)))) < 6) + jy = 1; + } + } + // Fire shot + if ((p.s < 8.0 * GAME_SPEED) && (p.ammo > 0)) { + g.dp.reset(); + while (g.dp.next()) { + if ((g.dp().side != p.side) && (g.dp().state < 2) && (!g.dp().hide)) { + double rx = p.x - g.dp().x; + double ry = p.y - g.dp().y; + double d = sqrt(rx * rx + ry * ry); + if (d < 100.0) { + double smartsine = ((ry * g.dp().xs - rx * g.dp().ys) / (8.0 * GAME_SPEED) / d); + smartsine = clamp(smartsine, -1.0, 1.0); + double smartangle = asin(smartsine); + double angle = atan2(rx, ry); + int bogied = wrap((int(((angle - smartangle) / PI * 8.0) + 1.5)), 1, 17); + if (bogied == p.d) + jb = true; } } } } - break; - case plane::LandingState::LANDING: - break; - default: - switch_bad_default("plane.land", __FILE__, __LINE__); - break; + } + break; + case plane::LandingState::LANDING: + break; + default: + switch_bad_default("plane.land", __FILE__, __LINE__); + break; } } diff --git a/apricots/all.cpp b/apricots/all.cpp index 0e59e57..d505d0d 100644 --- a/apricots/all.cpp +++ b/apricots/all.cpp @@ -67,24 +67,24 @@ void animate_explosions(linkedlist &explosion) { explosion().time++; int maxtime = 0; switch (explosion().type) { - case firetype::ZERO: - maxtime = int(14 / GAME_SPEED); - break; - case firetype::ONE: - maxtime = int(22 / GAME_SPEED); - break; - case firetype::TWO: - maxtime = int(4 / GAME_SPEED); - break; - case firetype::THREE: - maxtime = int(20 / GAME_SPEED); - break; - case firetype::FOUR: - maxtime = int(5 / GAME_SPEED); - break; - default: - switch_bad_default("firetype.type", __FILE__, __LINE__); - break; + case firetype::ZERO: + maxtime = int(14 / GAME_SPEED); + break; + case firetype::ONE: + maxtime = int(22 / GAME_SPEED); + break; + case firetype::TWO: + maxtime = int(4 / GAME_SPEED); + break; + case firetype::THREE: + maxtime = int(20 / GAME_SPEED); + break; + case firetype::FOUR: + maxtime = int(5 / GAME_SPEED); + break; + default: + switch_bad_default("firetype.type", __FILE__, __LINE__); + break; } if (explosion().time == maxtime) explosion.kill(); diff --git a/apricots/apricots.h b/apricots/apricots.h index 19927c3..01dc2ba 100644 --- a/apricots/apricots.h +++ b/apricots/apricots.h @@ -376,14 +376,8 @@ const airbase SHOOTY_AIRBASE(80, 80, 6, 80, 13, 0, 0, 0, GUN, FUEL, GUN, CONTROL const airbase TWOGUN_AIRBASE(48, 96, 5, 128, 5, 0, 0, 0, RADAR, GUN, HANGAR, NB, NB, NB, NB, NB, NB, NB, CONTROLTOWER, GUN, RADAR); const array AIRBASES = { - EMPTY_AIRBASE, - STANDARD_AIRBASE, - REVERSED_AIRBASE, - LITTLE_AIRBASE, - LONG_AIRBASE, - ORIGINAL_AIRBASE, - SHOOTY_AIRBASE, - TWOGUN_AIRBASE, + EMPTY_AIRBASE, STANDARD_AIRBASE, REVERSED_AIRBASE, LITTLE_AIRBASE, + LONG_AIRBASE, ORIGINAL_AIRBASE, SHOOTY_AIRBASE, TWOGUN_AIRBASE, }; // Sample definitions @@ -405,42 +399,186 @@ enum Sounds { }; const int SOUNDS_COUNT = 14; -inline const array SOUND_NAMES = { - "engine.wav", - "jet.wav", - "explode.wav", - "groundhit.wav", - "fuelexplode.wav", - "shot.wav", - "gunshot.wav", - "bomb.wav", - "splash.wav", - "laser.wav", - "stall.wav", - "gunshot2.wav", - "afterburner.wav", - "finish.wav", +inline const array SOUND_NAMES = { + "engine.wav", "jet.wav", "explode.wav", "groundhit.wav", "fuelexplode.wav", "shot.wav", "gunshot.wav", + "bomb.wav", "splash.wav", "laser.wav", "stall.wav", "gunshot2.wav", "afterburner.wav", "finish.wav", }; // Plane definitions -const plane NULLPLANE = { - 0.0, 0.0, 0.0, 0.0, 0.0, 0, plane::Control::COMPUTER, plane::LandingState::LANDED, 0, 0, 0, 0, 0, 0, 0, false, false, false, false, 0, 0, 0, 0, 0, 0, SOUND_ENGINE, false, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; -const plane SPITFIRE = { - 0.0, 0.0, 0.0, 0.0, 0.0, 0, plane::Control::COMPUTER, plane::LandingState::LANDED, 0, 0, 0, 0, 122, 0, int(3 / GAME_SPEED) - 1, false, false, false, false, 0, 8, 8, 4, 4, 143, SOUND_ENGINE, false, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; -const plane JET = { - 0.0, 0.0, 0.0, 0.0, 0.0, 0, plane::Control::COMPUTER, plane::LandingState::LANDED, 0, 0, 0, 0, 76, 0, int(2 / GAME_SPEED) - 1, false, true, false, false, 0, 12, 12, 5, 5, 140, SOUND_JET, false, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; -const plane STEALTH = { - 0.0, 0.0, 0.0, 0.0, 0.0, 0, plane::Control::COMPUTER, plane::LandingState::LANDED, 0, 0, 0, 0, 174, 0, int(4 / GAME_SPEED) - 1, false, false, false, true, 0, 3, 3, 6, 6, 235, SOUND_JET, false, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; -const plane DRAK_FIGHTER = { - 0.0, 0.0, 0.0, 2.0 * GAME_SPEED, 2.0 * GAME_SPEED, 9, plane::Control::COMPUTER, plane::LandingState::FLYING, 1, 0, 0, 0, 259, 0, int(1 / GAME_SPEED) - 1, false, true, false, false, 0, 1000, 1000, 0, 0, 235, SOUND_JET, true, 0, 0, plane::Coms::ACTION, 0, 0, 0, 0}; +const plane NULLPLANE = {0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0, + plane::Control::COMPUTER, + plane::LandingState::LANDED, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + false, + false, + false, + false, + 0, + 0, + 0, + 0, + 0, + 0, + SOUND_ENGINE, + false, + 0, + 0, + plane::Coms::ACTION, + 0, + 0, + 0, + 0}; +const plane SPITFIRE = {0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0, + plane::Control::COMPUTER, + plane::LandingState::LANDED, + 0, + 0, + 0, + 0, + 122, + 0, + int(3 / GAME_SPEED) - 1, + false, + false, + false, + false, + 0, + 8, + 8, + 4, + 4, + 143, + SOUND_ENGINE, + false, + 0, + 0, + plane::Coms::ACTION, + 0, + 0, + 0, + 0}; +const plane JET = {0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0, + plane::Control::COMPUTER, + plane::LandingState::LANDED, + 0, + 0, + 0, + 0, + 76, + 0, + int(2 / GAME_SPEED) - 1, + false, + true, + false, + false, + 0, + 12, + 12, + 5, + 5, + 140, + SOUND_JET, + false, + 0, + 0, + plane::Coms::ACTION, + 0, + 0, + 0, + 0}; +const plane STEALTH = {0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0, + plane::Control::COMPUTER, + plane::LandingState::LANDED, + 0, + 0, + 0, + 0, + 174, + 0, + int(4 / GAME_SPEED) - 1, + false, + false, + false, + true, + 0, + 3, + 3, + 6, + 6, + 235, + SOUND_JET, + false, + 0, + 0, + plane::Coms::ACTION, + 0, + 0, + 0, + 0}; +const plane DRAK_FIGHTER = {0.0, + 0.0, + 0.0, + 2.0 * GAME_SPEED, + 2.0 * GAME_SPEED, + 9, + plane::Control::COMPUTER, + plane::LandingState::FLYING, + 1, + 0, + 0, + 0, + 259, + 0, + int(1 / GAME_SPEED) - 1, + false, + true, + false, + false, + 0, + 1000, + 1000, + 0, + 0, + 235, + SOUND_JET, + true, + 0, + 0, + plane::Coms::ACTION, + 0, + 0, + 0, + 0}; const array PLANES = { - NULLPLANE, - SPITFIRE, - JET, - STEALTH, - DRAK_FIGHTER, + NULLPLANE, SPITFIRE, JET, STEALTH, DRAK_FIGHTER, }; // Drak mothership initial state diff --git a/apricots/collide.cpp b/apricots/collide.cpp index 702f60f..7a503e5 100644 --- a/apricots/collide.cpp +++ b/apricots/collide.cpp @@ -40,10 +40,7 @@ void detect_collisions(gamedata &g) { if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (g.p().y > GAME_HEIGHT - 21)) { // hits sea - firetype splash; - splash.x = int(g.p().x); - splash.y = GAME_HEIGHT - 20; - splash.type = firetype::Type::THREE; + firetype splash = {int(g.p().x), GAME_HEIGHT - 20, 0, firetype::Type::THREE}; g.explosion.add(splash); g.sound.play(SOUND_SPLASH); } else { // hits land @@ -175,9 +172,7 @@ void detect_collisions(gamedata &g) { g.p().state = 2; g.p().land = plane::LandingState::FLYING; g.p().s = 0.0; - firetype boom; - boom.x = int(g.p().x); - boom.y = int(g.p().y); + firetype boom = {int(g.p().x), int(g.p().y)}; g.explosion.add(boom); g.shot.kill(); g.sound.play(SOUND_EXPLODE); @@ -199,9 +194,7 @@ void detect_collisions(gamedata &g) { g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; - firetype boom; - boom.x = int(g.p().x); - boom.y = int(g.p().y); + firetype boom = {int(g.p().x), int(g.p().y)}; g.explosion.add(boom); // Reset laser flags if (g.drakgun().type == -1) @@ -227,9 +220,7 @@ void detect_collisions(gamedata &g) { g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; - firetype boom; - boom.x = int(g.p().x); - boom.y = int(g.p().y); + firetype boom = {int(g.p().x), int(g.p().y)}; g.explosion.add(boom); g.sound.play(SOUND_EXPLODE); } @@ -255,9 +246,7 @@ void detect_collisions(gamedata &g) { g.p().xs = g.p().xs * 0.5; g.p().ys = g.p().ys * 0.5; g.p().s = 0.0; - firetype boom; - boom.x = int(g.p().x); - boom.y = int(g.p().y); + firetype boom = {int(g.p().x), int(g.p().y)}; g.explosion.add(boom); g.fall.kill(); g.sound.play(SOUND_EXPLODE); diff --git a/apricots/drak.cpp b/apricots/drak.cpp index 7cb6b80..f4fa818 100644 --- a/apricots/drak.cpp +++ b/apricots/drak.cpp @@ -75,9 +75,7 @@ void fire_laser(gamedata &g, drakguntype &drakgun, drakmstype &drakms, sampleio g.p().score -= 25; g.p().land = plane::LandingState::FLYING; g.p().s = 0; - firetype boom; - boom.x = int(g.p().x); - boom.y = int(g.p().y); + firetype boom = {int(g.p().x), int(g.p().y)}; g.explosion.add(boom); } } @@ -311,9 +309,7 @@ void drak_main(gamedata &g) { // Destroy drak mothership for (int i = 0; i < 6; i++) { int x = int(g.drakms.x) + i * 16; - firetype boom; - boom.x = x + int(drand() * 16) - 8; - boom.y = int(g.drakms.y); + firetype boom = {x + int(drand() * 16) - 8, int(g.drakms.y)}; g.explosion.add(boom); for (int m = 1; m <= 2; m++) { falltype shrapnel = {}; diff --git a/apricots/fall.cpp b/apricots/fall.cpp index dfd2e54..03e2eec 100644 --- a/apricots/fall.cpp +++ b/apricots/fall.cpp @@ -11,53 +11,48 @@ bool fall_collision(gamedata &g, falltype &fall) { if ((g.gamemap.ground.collide(0, 0, g.images[fall.image], (int)fall.x, (int)fall.y)) || (int(fall.x) < -16) || (int(fall.x) > GAME_WIDTH) || (int(fall.y) > GAME_HEIGHT)) { switch (fall.type) { - case firetype::Type::ZERO: - break; - case firetype::Type::ONE: // Shrapnel + case firetype::Type::ZERO: + break; + case firetype::Type::ONE: // Shrapnel + { + int px = clamp(int((fall.x + 18.0) / 32), 0, MAP_W - 1); + if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (fall.y > GAME_HEIGHT - 11)) { // hits sea + firetype splash; + splash.x = int(fall.x) - 5; + splash.y = GAME_HEIGHT - 20; + splash.time = 0; + splash.type = firetype::Type::FOUR; + g.explosion.add(splash); + } else { // hits land + firetype boom; + boom.x = int(fall.x); + boom.y = int(fall.y); + boom.time = 0; + boom.type = firetype::Type::TWO; + g.explosion.add(boom); + } + } break; + + case firetype::Type::TWO: + case firetype::Type::THREE: // Large bits and bombs + if (fall.type == 3) + fall.x -= 5; { - int px = clamp(int((fall.x + 18.0) / 32), 0, MAP_W - 1); - if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (fall.y > GAME_HEIGHT - 11)) { // hits sea - firetype splash; - splash.x = int(fall.x) - 5; - splash.y = GAME_HEIGHT - 20; - splash.time = 0; - splash.type = firetype::Type::FOUR; + int px = clamp(int((fall.x + 24.0) / 32), 0, MAP_W - 1); + if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (fall.y > GAME_HEIGHT - 21)) { // hits sea + firetype splash = {int(fall.x), GAME_HEIGHT - 20, 0, firetype::Type::THREE}; g.explosion.add(splash); + g.sound.play(SOUND_SPLASH); } else { // hits land - firetype boom; - boom.x = int(fall.x); - boom.y = int(fall.y); - boom.time = 0; - boom.type = firetype::Type::TWO; + firetype boom = {int(fall.x), int(fall.y)}; g.explosion.add(boom); + g.sound.play(SOUND_GROUNDHIT); } - } break; - - case firetype::Type::TWO: - case firetype::Type::THREE: // Large bits and bombs - if (fall.type == 3) - fall.x -= 5; - { - int px = clamp(int((fall.x + 24.0) / 32), 0, MAP_W - 1); - if ((g.gamemap.groundheight[px] == GAME_HEIGHT - 2) && (fall.y > GAME_HEIGHT - 21)) { // hits sea - firetype splash; - splash.x = int(fall.x); - splash.y = GAME_HEIGHT - 20; - splash.type = firetype::Type::THREE; - g.explosion.add(splash); - g.sound.play(SOUND_SPLASH); - } else { // hits land - firetype boom; - boom.x = int(fall.x); - boom.y = int(fall.y); - g.explosion.add(boom); - g.sound.play(SOUND_GROUNDHIT); - } - } - break; - default: - switch_bad_default("firetype", __FILE__, __LINE__); - break; + } + break; + default: + switch_bad_default("firetype", __FILE__, __LINE__); + break; } return true; } @@ -67,45 +62,40 @@ bool fall_collision(gamedata &g, falltype &fall) { if ((g.drakms.exist == 1) && (g.images[318].collide((int)g.drakms.x, (int)g.drakms.y, g.images[fall.image], (int)fall.x, (int)fall.y))) { switch (fall.type) { - case firetype::Type::ZERO: - break; - case firetype::Type::ONE: // Shrapnel + case firetype::Type::ZERO: + break; + case firetype::Type::ONE: // Shrapnel + { + firetype boom = {int(fall.x), int(fall.y), 0, firetype::Type::TWO}; + g.explosion.add(boom); + } break; + + case firetype::Type::TWO: + case firetype::Type::THREE: // Large bits and bombs + if (fall.type == 3) + fall.x -= 5; { - firetype boom; - boom.x = int(fall.x); - boom.y = int(fall.y); - boom.type = firetype::Type::TWO; + firetype boom = {int(fall.x), int(fall.y)}; g.explosion.add(boom); - } break; - - case firetype::Type::TWO: - case firetype::Type::THREE: // Large bits and bombs - if (fall.type == 3) - fall.x -= 5; - { - firetype boom; - boom.x = int(fall.x); - boom.y = int(fall.y); - g.explosion.add(boom); - } - g.sound.play(SOUND_GROUNDHIT); - // Damage mothership - g.drakms.damage += 2; - // Find which plane (if any) the fall belongs to - if (fall.side > 0) { - g.p.reset(); - while (g.p.next()) { - if (fall.side == g.p().side) { + } + g.sound.play(SOUND_GROUNDHIT); + // Damage mothership + g.drakms.damage += 2; + // Find which plane (if any) the fall belongs to + if (fall.side > 0) { + g.p.reset(); + while (g.p.next()) { + if (fall.side == g.p().side) { - // Add to score - g.p().score += 40; - } + // Add to score + g.p().score += 40; } } - break; - default: - switch_bad_default("firetype", __FILE__, __LINE__); - break; + } + break; + default: + switch_bad_default("firetype", __FILE__, __LINE__); + break; } return true; } @@ -189,9 +179,7 @@ bool fall_collision(gamedata &g, falltype &fall) { } } // Explosion - firetype boom; - boom.x = int(fall.x); - boom.y = int(fall.y); + firetype boom = {int(fall.x), int(fall.y)}; g.explosion.add(boom); // Remove laser flags if (g.drakgun().type == -1) diff --git a/apricots/game.cpp b/apricots/game.cpp index c5af93b..d4f5349 100644 --- a/apricots/game.cpp +++ b/apricots/game.cpp @@ -367,16 +367,16 @@ void keyboard(const Uint8 *keys, int &jx, int &jy, bool &jb, SDL_Scancode up, SD void control(gamedata &g, const Uint8 *keys, int &jx, int &jy, bool &jb) { switch (g.p().control) { - case 1: // Player 1 - keyboard(keys, jx, jy, jb, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, - SDL_SCANCODE_RETURN); - break; - case 2: // Player 2 - keyboard(keys, jx, jy, jb, SDL_SCANCODE_S, SDL_SCANCODE_X, SDL_SCANCODE_Z, SDL_SCANCODE_C, SDL_SCANCODE_LCTRL); - break; - default: // Computer controlled - computer_ai(g, g.p(), jx, jy, jb); - break; + case 1: // Player 1 + keyboard(keys, jx, jy, jb, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, + SDL_SCANCODE_RETURN); + break; + case 2: // Player 2 + keyboard(keys, jx, jy, jb, SDL_SCANCODE_S, SDL_SCANCODE_X, SDL_SCANCODE_Z, SDL_SCANCODE_C, SDL_SCANCODE_LCTRL); + break; + default: // Computer controlled + computer_ai(g, g.p(), jx, jy, jb); + break; } } diff --git a/apricots/init.cpp b/apricots/init.cpp index 1898a96..f4bb848 100644 --- a/apricots/init.cpp +++ b/apricots/init.cpp @@ -350,12 +350,12 @@ void init_gamedata(gamedata &g) { g.planeinfo[6].basetype = getConfig(config, "BASE6", 1, 1, 7); // Control: 1=Player 1, 2=Player 2, 0=AI - g.planeinfo[1].control = (plane::Control) getConfig(config, "CONTROL1", 1, 0, 2); - g.planeinfo[2].control = (plane::Control) getConfig(config, "CONTROL2", 0, 0, 2); - g.planeinfo[3].control = (plane::Control) getConfig(config, "CONTROL3", 0, 0, 2); - g.planeinfo[4].control = (plane::Control) getConfig(config, "CONTROL4", 0, 0, 2); - g.planeinfo[5].control = (plane::Control) getConfig(config, "CONTROL5", 0, 0, 2); - g.planeinfo[6].control = (plane::Control) getConfig(config, "CONTROL6", 0, 0, 2); + g.planeinfo[1].control = (plane::Control)getConfig(config, "CONTROL1", 1, 0, 2); + g.planeinfo[2].control = (plane::Control)getConfig(config, "CONTROL2", 0, 0, 2); + g.planeinfo[3].control = (plane::Control)getConfig(config, "CONTROL3", 0, 0, 2); + g.planeinfo[4].control = (plane::Control)getConfig(config, "CONTROL4", 0, 0, 2); + g.planeinfo[5].control = (plane::Control)getConfig(config, "CONTROL5", 0, 0, 2); + g.planeinfo[6].control = (plane::Control)getConfig(config, "CONTROL6", 0, 0, 2); // Error check int count[3]; count[0] = 0; diff --git a/apricots/sampleio.cpp b/apricots/sampleio.cpp index d908b37..65d816b 100644 --- a/apricots/sampleio.cpp +++ b/apricots/sampleio.cpp @@ -234,11 +234,11 @@ ALboolean sampleio ::sourceisplaying(ALuint sid) { #endif switch (state) { - case AL_PLAYING: - case AL_PAUSED: - return AL_TRUE; - default: - break; + case AL_PLAYING: + case AL_PAUSED: + return AL_TRUE; + default: + break; } return AL_FALSE; } diff --git a/apricots/setup.cpp b/apricots/setup.cpp index 724978e..9778e8a 100644 --- a/apricots/setup.cpp +++ b/apricots/setup.cpp @@ -84,251 +84,251 @@ void setup_map(map &gamemap, int planes, airbase base[], bool flatground[]) { // Do middle portion of map for (int x = 1; x < MAP_W - 1; x++) { switch (land) { - case HILAND: - gamemap.groundheight[x] = 32 * height + 9; - flatground[x * 2] = true; - r = int(drand() * 6.0); - if ((airbaseflag[x] == 2) && (height == MAP_H - 3)) - r = 5; - if ((airbaseflag[x] == 2) && (height == MAP_H - 2) && ((r == 2) || (r == 4))) - r = 5; - if (airbaseflag[x] == 1) - r = 0; - if ((height == MAP_H - 3) && ((r == 2) || (r == 4))) - r = 0; - if ((height == MAP_H - 1) && (r == 5)) - r = 0; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - gamemap.image[x][height] = 9; - flatground[x * 2 + 1] = true; - break; - case 1: - gamemap.image[x][height] = 8; - land = LOLAND; - break; - case 2: - gamemap.image[x][height] = 10; - gamemap.image[x][height - 1] = 11; - height--; - land = LOLAND; - break; - case 3: - gamemap.image[x][height] = 20; - land = LOLAND; - break; - case 4: - gamemap.image[x][height] = 15; - land = randomland(HILAND, LOLAND); - gamemap.image[x][height - 1] = 19 - (2 * land); - height--; - break; - case 5: - gamemap.image[x][height] = 18; - if ((height != MAP_H - 2) || (drand() > 0.33) || (airbaseflag[x] == 2)) { - land = randomland(HILAND, LOLAND); - gamemap.image[x][height + 1] = 14 - (10 * land); - height++; - } else { - land = SEA; - gamemap.image[x][height + 1] = 28; - height++; - } - break; - } + case HILAND: + gamemap.groundheight[x] = 32 * height + 9; + flatground[x * 2] = true; + r = int(drand() * 6.0); + if ((airbaseflag[x] == 2) && (height == MAP_H - 3)) + r = 5; + if ((airbaseflag[x] == 2) && (height == MAP_H - 2) && ((r == 2) || (r == 4))) + r = 5; + if (airbaseflag[x] == 1) + r = 0; + if ((height == MAP_H - 3) && ((r == 2) || (r == 4))) + r = 0; + if ((height == MAP_H - 1) && (r == 5)) + r = 0; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + gamemap.image[x][height] = 9; + flatground[x * 2 + 1] = true; break; - - case LOLAND: - gamemap.groundheight[x] = 32 * height + 22; - flatground[x * 2] = true; - r = int(drand() * 6.0); - if ((airbaseflag[x] == 2) && (height == MAP_H - 3)) - r = 2 + 3 * int(drand() * 2.0); - if ((airbaseflag[x] == 2) && (height == MAP_H - 2) && (r == 4)) - r = 5; - if (airbaseflag[x] == 1) - r = 0; - if ((height == MAP_H - 3) && (r == 4)) - r = 0; - if ((height == MAP_H - 1) && (r == 5)) - r = 2; - if (((airbaseflag[x] == 2) || (x == MAP_W - 2)) && (height == MAP_H - 1) && (r == 2)) - r = 0; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - gamemap.image[x][height] = 5; - flatground[x * 2 + 1] = true; - break; - case 1: - gamemap.image[x][height] = 7; - land = HILAND; - break; - case 2: - if (height < MAP_H - 1) { - gamemap.image[x][height] = 13; - gamemap.image[x][height + 1] = 12; - height++; - land = HILAND; - } else { - land = randomland(BEACH, PORTLEFT); - if (airbaseflag[x] == 3) - land = BEACH; - gamemap.image[x][height] = 6 + (8 * land); - if (land == PORTLEFT) - flatground[x * 2 + 1] = true; - } - break; - case 3: - gamemap.image[x][height] = 21; - land = HILAND; - break; - case 4: - gamemap.image[x][height] = 6; - land = randomland(HILAND, LOLAND); - gamemap.image[x][height - 1] = 19 - (2 * land); - height--; - break; - case 5: - gamemap.image[x][height] = 16; - if ((height != MAP_H - 2) || (drand() > 0.33) || (airbaseflag[x] == 2)) { - land = randomland(HILAND, LOLAND); - gamemap.image[x][height + 1] = 14 - (10 * land); - height++; - } else { - land = SEA; - gamemap.image[x][height + 1] = 28; - height++; - } - break; - } + case 1: + gamemap.image[x][height] = 8; + land = LOLAND; break; - - case BEACH: - gamemap.groundheight[x] = GAME_HEIGHT - 4; - flatground[x * 2] = true; - r = int(drand() * 3.0); - if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) - r = 1; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - gamemap.image[x][height] = 24; - flatground[x * 2 + 1] = true; - break; - case 1: - gamemap.image[x][height] = 23; - land = LOLAND; - break; - case 2: - gamemap.image[x][height] = 25; - land = SEA; - break; + case 2: + gamemap.image[x][height] = 10; + gamemap.image[x][height - 1] = 11; + height--; + land = LOLAND; + break; + case 3: + gamemap.image[x][height] = 20; + land = LOLAND; + break; + case 4: + gamemap.image[x][height] = 15; + land = randomland(HILAND, LOLAND); + gamemap.image[x][height - 1] = 19 - (2 * land); + height--; + break; + case 5: + gamemap.image[x][height] = 18; + if ((height != MAP_H - 2) || (drand() > 0.33) || (airbaseflag[x] == 2)) { + land = randomland(HILAND, LOLAND); + gamemap.image[x][height + 1] = 14 - (10 * land); + height++; + } else { + land = SEA; + gamemap.image[x][height + 1] = 28; + height++; } break; + } + break; - case PORTLEFT: - gamemap.groundheight[x] = GAME_HEIGHT - 10; - flatground[x * 2] = true; - r = int(drand() * 3.0); - if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) - r = 1; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - gamemap.image[x][height] = 32; + case LOLAND: + gamemap.groundheight[x] = 32 * height + 22; + flatground[x * 2] = true; + r = int(drand() * 6.0); + if ((airbaseflag[x] == 2) && (height == MAP_H - 3)) + r = 2 + 3 * int(drand() * 2.0); + if ((airbaseflag[x] == 2) && (height == MAP_H - 2) && (r == 4)) + r = 5; + if (airbaseflag[x] == 1) + r = 0; + if ((height == MAP_H - 3) && (r == 4)) + r = 0; + if ((height == MAP_H - 1) && (r == 5)) + r = 2; + if (((airbaseflag[x] == 2) || (x == MAP_W - 2)) && (height == MAP_H - 1) && (r == 2)) + r = 0; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + gamemap.image[x][height] = 5; + flatground[x * 2 + 1] = true; + break; + case 1: + gamemap.image[x][height] = 7; + land = HILAND; + break; + case 2: + if (height < MAP_H - 1) { + gamemap.image[x][height] = 13; + gamemap.image[x][height + 1] = 12; + height++; + land = HILAND; + } else { + land = randomland(BEACH, PORTLEFT); + if (airbaseflag[x] == 3) + land = BEACH; + gamemap.image[x][height] = 6 + (8 * land); + if (land == PORTLEFT) flatground[x * 2 + 1] = true; - break; - case 1: - case 2: - gamemap.image[x][height] = 33; - land = SEA; - break; } break; - - case PORTRIGHT: - gamemap.groundheight[x] = GAME_HEIGHT - 10; - flatground[x * 2] = true; - r = int(drand() * 3.0); - if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) - r = 1; - if (r == 0) { - gamemap.image[x][height] = 32; - flatground[x * 2 + 1] = true; - } else if (r > 0) { - gamemap.image[x][height] = 31; - land = LOLAND; - flatground[x * 2 + 1] = true; - } + case 3: + gamemap.image[x][height] = 21; + land = HILAND; break; - - case SEA: - gamemap.groundheight[x] = GAME_HEIGHT - 2; - r = int(drand() * 4.0); - if (airbaseflag[x] == 2) - r = 3; - if (airbaseflag[x] == 3) - r = 2; - if ((r == 2) && (x == MAP_W - 2)) - r = 0; - switch (r) { - default: - switch_bad_default("drand", __FILE__, __LINE__); - [[fallthrough]]; - case 0: - case 1: - gamemap.image[x][height] = 27; - break; - case 2: - land = randomland(BEACH, PORTRIGHT); - gamemap.image[x][height] = 18 + (4 * land); - break; - case 3: - gamemap.image[x][height] = 29; - land = randomland(HILAND, LOLAND); - gamemap.image[x][height - 1] = 19 - (2 * land); - height--; - break; + case 4: + gamemap.image[x][height] = 6; + land = randomland(HILAND, LOLAND); + gamemap.image[x][height - 1] = 19 - (2 * land); + height--; + break; + case 5: + gamemap.image[x][height] = 16; + if ((height != MAP_H - 2) || (drand() > 0.33) || (airbaseflag[x] == 2)) { + land = randomland(HILAND, LOLAND); + gamemap.image[x][height + 1] = 14 - (10 * land); + height++; + } else { + land = SEA; + gamemap.image[x][height + 1] = 28; + height++; } break; + } + break; + + case BEACH: + gamemap.groundheight[x] = GAME_HEIGHT - 4; + flatground[x * 2] = true; + r = int(drand() * 3.0); + if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) + r = 1; + switch (r) { default: - switch_bad_default("land", __FILE__, __LINE__); + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + gamemap.image[x][height] = 24; + flatground[x * 2 + 1] = true; break; - } - } + case 1: + gamemap.image[x][height] = 23; + land = LOLAND; + break; + case 2: + gamemap.image[x][height] = 25; + land = SEA; + break; + } + break; - // Do right of map - switch (land) { - case HILAND: - gamemap.image[MAP_W - 1][height] = 15; - gamemap.groundheight[MAP_W - 1] = 32 * height + 9; - flatground[(MAP_W - 1) * 2] = true; + case PORTLEFT: + gamemap.groundheight[x] = GAME_HEIGHT - 10; + flatground[x * 2] = true; + r = int(drand() * 3.0); + if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) + r = 1; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + gamemap.image[x][height] = 32; + flatground[x * 2 + 1] = true; + break; + case 1: + case 2: + gamemap.image[x][height] = 33; + land = SEA; + break; + } break; - case LOLAND: - gamemap.image[MAP_W - 1][height] = 6; - gamemap.groundheight[MAP_W - 1] = 32 * height + 22; - flatground[(MAP_W - 1) * 2] = true; + + case PORTRIGHT: + gamemap.groundheight[x] = GAME_HEIGHT - 10; + flatground[x * 2] = true; + r = int(drand() * 3.0); + if ((airbaseflag[x] > 1) || (x == MAP_W - 2)) + r = 1; + if (r == 0) { + gamemap.image[x][height] = 32; + flatground[x * 2 + 1] = true; + } else if (r > 0) { + gamemap.image[x][height] = 31; + land = LOLAND; + flatground[x * 2 + 1] = true; + } break; + case SEA: - gamemap.image[MAP_W - 1][height] = 29; - gamemap.groundheight[MAP_W - 1] = GAME_HEIGHT - 2; + gamemap.groundheight[x] = GAME_HEIGHT - 2; + r = int(drand() * 4.0); + if (airbaseflag[x] == 2) + r = 3; + if (airbaseflag[x] == 3) + r = 2; + if ((r == 2) && (x == MAP_W - 2)) + r = 0; + switch (r) { + default: + switch_bad_default("drand", __FILE__, __LINE__); + [[fallthrough]]; + case 0: + case 1: + gamemap.image[x][height] = 27; + break; + case 2: + land = randomland(BEACH, PORTRIGHT); + gamemap.image[x][height] = 18 + (4 * land); + break; + case 3: + gamemap.image[x][height] = 29; + land = randomland(HILAND, LOLAND); + gamemap.image[x][height - 1] = 19 - (2 * land); + height--; + break; + } break; - case BEACH: - case PORTLEFT: - case PORTRIGHT: default: switch_bad_default("land", __FILE__, __LINE__); break; + } + } + + // Do right of map + switch (land) { + case HILAND: + gamemap.image[MAP_W - 1][height] = 15; + gamemap.groundheight[MAP_W - 1] = 32 * height + 9; + flatground[(MAP_W - 1) * 2] = true; + break; + case LOLAND: + gamemap.image[MAP_W - 1][height] = 6; + gamemap.groundheight[MAP_W - 1] = 32 * height + 22; + flatground[(MAP_W - 1) * 2] = true; + break; + case SEA: + gamemap.image[MAP_W - 1][height] = 29; + gamemap.groundheight[MAP_W - 1] = GAME_HEIGHT - 2; + break; + case BEACH: + case PORTLEFT: + case PORTRIGHT: + default: + switch_bad_default("land", __FILE__, __LINE__); + break; } for (int y = 0; y < height; y++) { gamemap.image[MAP_W - 1][y] = 3;