| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file gamemode.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created level.c and menu.c |
| 10 | * - 3rd of February 2009: Renamed level.c to level.cpp and menu.c to menu.cpp |
| 11 | * - 9th March 2009: Created game.cpp from parts of menu.cpp and level.cpp |
| 12 | * - 18th July 2009: Created servergame.cpp from parts of game.cpp |
| 13 | * - 2nd August 2009: Created gamemode.cpp from parts of servergame.cpp |
| 14 | * |
| 15 | * @par Licence: |
| 16 | * Copyright (c) 2005-2017 AJ Thomson |
| 17 | * |
| 18 | * OpenJazz is distributed under the terms of |
| 19 | * the GNU General Public License, version 2.0 |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include "game.h" |
| 25 | #include "gamemode.h" |
| 26 | |
| 27 | #include "io/gfx/font.h" |
| 28 | #include "player/player.h" |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Outcome of player being hit |
| 33 | * |
| 34 | * @param source Player responsible for the hit |
| 35 | * @param victim Player victim of the hit |
| 36 | * |
| 37 | * @return Whether or not the hit should result in energy loss (true) |
| 38 | */ |
| 39 | bool GameMode::hit (Player *source, Player *victim) { |
| 40 | |
| 41 | OJ_UNUSED(source); |
| 42 | OJ_UNUSED(victim); |
| 43 | |
| 44 | return true; |
| 45 | |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Outcome of player being killed |
| 51 | * |
| 52 | * @param game The current game |
| 53 | * @param source Player responsible for the kill |
| 54 | * @param victim Player victim of the kill |
| 55 | * |
| 56 | * @return Whether or not the player should be be killed (true) |
| 57 | */ |
| 58 | bool GameMode::kill (Game* game, Player *source, Player *victim) { |
| 59 | |
| 60 | if (source && (victim == localPlayer)) game->score(source->getTeam()); |
| 61 | |
| 62 | return true; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Outcome of level being completed |
| 69 | * |
| 70 | * @param game The current game |
| 71 | * @param player Player that has completed level |
| 72 | * @param gridX X-coordinate (in tiles) of finishing position |
| 73 | * @param gridY Y-coordinate (in tiles) of finishing position |
| 74 | * |
| 75 | * @return Whether or not the level should end (true) |
| 76 | */ |
| 77 | bool GameMode::endOfLevel (Game* game, Player *player, int gridX, int gridY) { |
| 78 | |
| 79 | (void)player; |
| 80 | |
| 81 | game->setCheckpoint(gridX, gridY); |
| 82 | |
| 83 | return true; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * Outcome of time running out |
| 90 | */ |
| 91 | void GameMode::outOfTime () { |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Get the game mode type |
| 98 | * |
| 99 | * @return Game mode type (M_SINGLE) |
| 100 | */ |
| 101 | GameModeType SingleGameMode::getMode () { |
| 102 | |
| 103 | return M_SINGLE; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Choose a team for a new player |
| 110 | * |
| 111 | * @return New player's team (0) |
| 112 | */ |
| 113 | unsigned char SingleGameMode::chooseTeam () { |
| 114 | |
| 115 | return 0; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Draw the player's team's score (not in single-player mode) |
| 122 | * |
| 123 | * @param font Font to use to draw score |
| 124 | */ |
| 125 | void SingleGameMode::drawScore (Font* font) { |
| 126 | |
| 127 | (void)font; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Choose a team for a new player |
| 134 | * |
| 135 | * @return New player's team (0) |
| 136 | */ |
| 137 | unsigned char CooperativeGameMode::chooseTeam () { |
| 138 | |
| 139 | // All players are on the same team |
| 140 | |
| 141 | return 0; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Draw the player's team's score (not in cooperative mode) |
| 148 | * |
| 149 | * @param font Font to use to draw score |
| 150 | */ |
| 151 | void CooperativeGameMode::drawScore (Font* font) { |
| 152 | |
| 153 | (void)font; |
| 154 | |
| 155 | // Do nothing |
| 156 | |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * Choose a team for a new player |
| 162 | * |
| 163 | * @return New player's team (unique) |
| 164 | */ |
| 165 | unsigned char FreeForAllGameMode::chooseTeam () { |
| 166 | |
| 167 | // Every player is on a separate team |
| 168 | |
| 169 | int count; |
| 170 | unsigned char team; |
| 171 | |
| 172 | team = 1; |
| 173 | |
| 174 | // Find a team number higher than any other |
| 175 | for (count = nPlayers - 1; count >= 0; count--) { |
| 176 | |
| 177 | if (players[count].getTeam() > team) |
| 178 | team = players[count].getTeam() + 1; |
| 179 | |
| 180 | } |
| 181 | |
| 182 | return team; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * Draw the player's team's score |
| 189 | * |
| 190 | * @param font Font to use to draw score |
| 191 | */ |
| 192 | void FreeForAllGameMode::drawScore (Font* font) { |
| 193 | |
| 194 | font->showNumber(localPlayer->teamScore, 64, 4); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Choose a team for a new player |
| 201 | * |
| 202 | * @return New player's team (0 or 1) |
| 203 | */ |
| 204 | unsigned char TeamGameMode::chooseTeam () { |
| 205 | |
| 206 | // Players are split between two teams |
| 207 | |
| 208 | int count, difference; |
| 209 | |
| 210 | // Calculate team imbalance |
| 211 | |
| 212 | difference = 0; |
| 213 | |
| 214 | for (count = 0; count < nPlayers; count++) { |
| 215 | |
| 216 | if (players[count].getTeam()) difference++; |
| 217 | else difference--; |
| 218 | |
| 219 | } |
| 220 | |
| 221 | // Assign to the team with the least players |
| 222 | |
| 223 | if (difference >= 0) return 0; |
| 224 | |
| 225 | return 1; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /** |
| 231 | * Draw the player's team's score |
| 232 | * |
| 233 | * @param font Font to use to draw score |
| 234 | */ |
| 235 | void TeamGameMode::drawScore (Font* font) { |
| 236 | |
| 237 | font->showNumber(localPlayer->teamScore, 64, 4); |
| 238 | |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * Get the game mode type |
| 244 | * |
| 245 | * @return Game mode type (M_COOP) |
| 246 | */ |
| 247 | GameModeType CoopGameMode::getMode () { |
| 248 | |
| 249 | return M_COOP; |
| 250 | |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /** |
| 255 | * Get the game mode type |
| 256 | * |
| 257 | * @return Game mode type (M_BATTLE) |
| 258 | */ |
| 259 | GameModeType BattleGameMode::getMode () { |
| 260 | |
| 261 | return M_BATTLE; |
| 262 | |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Get the game mode type |
| 268 | * |
| 269 | * @return Game mode type (M_TEAMBATTLE) |
| 270 | */ |
| 271 | GameModeType TeamBattleGameMode::getMode () { |
| 272 | |
| 273 | return M_TEAMBATTLE; |
| 274 | |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * Get the game mode type |
| 280 | * |
| 281 | * @return Game mode type (M_RACE) |
| 282 | */ |
| 283 | GameModeType RaceGameMode::getMode () { |
| 284 | |
| 285 | return M_RACE; |
| 286 | |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /** |
| 291 | * Outcome of player being hit |
| 292 | * |
| 293 | * @param source Player responsible for the hit |
| 294 | * @param victim Player victim of the hit |
| 295 | * |
| 296 | * @return Whether or not the hit should result in energy loss (false) |
| 297 | */ |
| 298 | bool RaceGameMode::hit (Player *source, Player *victim) { |
| 299 | |
| 300 | OJ_UNUSED(source); |
| 301 | OJ_UNUSED(victim); |
| 302 | |
| 303 | return false; |
| 304 | |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /** |
| 309 | * Outcome of level being completed |
| 310 | * |
| 311 | * @param game The current game |
| 312 | * @param player Player that has completed level |
| 313 | * @param gridX X-coordinate (in tiles) of finishing position |
| 314 | * @param gridY Y-coordinate (in tiles) of finishing position |
| 315 | * |
| 316 | * @return Whether or not the level should end (false) |
| 317 | */ |
| 318 | bool RaceGameMode::endOfLevel (Game* game, Player *player, int gridX, int gridY) { |
| 319 | |
| 320 | (void)gridX; |
| 321 | (void)gridY; |
| 322 | |
| 323 | if (player == localPlayer) game->score(localPlayer->getTeam()); |
| 324 | |
| 325 | game->resetPlayer(player); |
| 326 | |
| 327 | return false; |
| 328 | |
| 329 | } |