| 1 | /////////////////////////////////////////////// |
| 2 | // |
| 3 | // Snipe2d ludum dare 48h compo entry |
| 4 | // |
| 5 | // Jari Komppa aka Sol |
| 6 | // http://iki.fi/sol |
| 7 | // |
| 8 | /////////////////////////////////////////////// |
| 9 | // License |
| 10 | /////////////////////////////////////////////// |
| 11 | // |
| 12 | // This software is provided 'as-is', without any express or implied |
| 13 | // warranty. In no event will the authors be held liable for any damages |
| 14 | // arising from the use of this software. |
| 15 | // |
| 16 | // Permission is granted to anyone to use this software for any purpose, |
| 17 | // including commercial applications, and to alter it and redistribute it |
| 18 | // freely, subject to the following restrictions: |
| 19 | // |
| 20 | // 1. The origin of this software must not be misrepresented; you must not |
| 21 | // claim that you wrote the original software. If you use this software |
| 22 | // in a product, an acknowledgment in the product documentation would be |
| 23 | // appreciated but is not required. |
| 24 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 25 | // misrepresented as being the original software. |
| 26 | // 3. This notice may not be removed or altered from any source distribution. |
| 27 | // |
| 28 | // (eg. same as ZLIB license) |
| 29 | // |
| 30 | /////////////////////////////////////////////// |
| 31 | // |
| 32 | // Houses are taken from a satellite picture of glasgow. |
| 33 | // |
| 34 | // The sources are a mess, as I didn't even try to do anything |
| 35 | // really organized here.. and hey, it's a 48h compo =) |
| 36 | // |
| 37 | //#include <windows.h> |
| 38 | #include <iostream> |
| 39 | #include <stdlib.h> |
| 40 | #include <limits.h> |
| 41 | #include <math.h> |
| 42 | #include <string.h> |
| 43 | #include <libgen.h> |
| 44 | #include <SDL/SDL.h> |
| 45 | #include <SDL/SDL_image.h> |
| 46 | #include <SDL/SDL_mixer.h> |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <fcntl.h> |
| 50 | #include <unistd.h> |
| 51 | #include <stdio.h> |
| 52 | #include <string.h> |
| 53 | #include <stdarg.h> |
| 54 | #include "petopt.h" |
| 55 | |
| 56 | #ifdef HAVE_CONFIG_H |
| 57 | # include "config.h" |
| 58 | #endif |
| 59 | |
| 60 | // When trying to debug a segfault, you REALLY, REALLY need to be able to |
| 61 | // switch windows. :( |
| 62 | #ifdef DEBUG_MODE |
| 63 | #undef SDL_GRAB_ON |
| 64 | #define SDL_GRAB_ON SDL_GRAB_OFF |
| 65 | #endif |
| 66 | // Config: |
| 67 | |
| 68 | #define SHIFT_AMOUNT 12 |
| 69 | #define CAMERA_STEPS |
| 70 | #define UNREAL_DITHER |
| 71 | #define WOBBLE 2 |
| 72 | #define DISPLAY_LOGO_SCREEN |
| 73 | #define DISPLAY_GAMEOVER_SCREEN |
| 74 | //#define CAMERA_RECOIL |
| 75 | //#define RECYCLE_PEDESTRIANS |
| 76 | #define PLAY_AUDIO |
| 77 | |
| 78 | enum COLORSET { |
| 79 | COLOR_BLACK = 0, |
| 80 | COLOR_WHITE, |
| 81 | COLOR_GREEN, |
| 82 | COLOR_YELLOW, |
| 83 | COLOR_RED |
| 84 | }; |
| 85 | |
| 86 | enum CHARTYPES |
| 87 | { |
| 88 | CHAR_BADGUY = 0, |
| 89 | CHAR_VIP = 1, |
| 90 | CHAR_PEDESTRIAN = 2 |
| 91 | }; |
| 92 | |
| 93 | enum SOUNDCHAN { |
| 94 | SOUNDCHAN_EFFECT = 0, |
| 95 | SOUNDCHAN_MUSIC = 1, |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | /* Reason for game over */ |
| 100 | typedef enum OES_ENDREASON { |
| 101 | OESREASON_NONE = 0, /* game in progress */ |
| 102 | OESREASON_NEGLIGENT, /* let a VIP die. */ |
| 103 | OESREASON_AWOL, /* left post (voluntary game end). */ |
| 104 | OESREASON_FRAG, /* hit VIP. */ |
| 105 | OESREASON_QUIT, /* forceful quit. */ |
| 106 | } OES_ENDREASON; |
| 107 | |
| 108 | |
| 109 | /* Game states */ |
| 110 | typedef enum OES_GAMESTATE { |
| 111 | OESGAME_CHAOS = 0, /* uninitialized. */ |
| 112 | OESGAME_PLAY, /* initialized and in play. */ |
| 113 | OESGAME_PAUSED, /* paused. */ |
| 114 | OESGAME_UNPAUSED, /* un-paused, reinitalize system resources (devices). */ |
| 115 | } OES_GAMESTATE; |
| 116 | |
| 117 | |
| 118 | /* data types go here. */ |
| 119 | |
| 120 | typedef struct character_ |
| 121 | { |
| 122 | float mX, mY; // position |
| 123 | float mXi, mYi; // direction |
| 124 | float mSpeed; // speed of this AI |
| 125 | int mType; // AI type |
| 126 | int mTTL; // time to live, for normal pedestrians |
| 127 | int mTarget; // target exit point or target character |
| 128 | int mNextWaypoint; // waypoint towards which this AI is walking |
| 129 | int mLastWaypoints[7]; // last 7 waypoints to kill loops |
| 130 | int mLastWaypoint; // counter for above array |
| 131 | } CHARACTER; |
| 132 | |
| 133 | typedef struct waypointstruc |
| 134 | { |
| 135 | int mX,mY; |
| 136 | int *mConnection; |
| 137 | int mConnections; |
| 138 | } WAYPOINT; |
| 139 | |
| 140 | typedef struct spawnpointstruc |
| 141 | { |
| 142 | int mX, mY; |
| 143 | int mClosestWaypoint; |
| 144 | int mType; |
| 145 | } SPAWNPOINT; |
| 146 | |
| 147 | typedef struct prefs_ |
| 148 | { |
| 149 | char *homedir; |
| 150 | char *datadir; |
| 151 | char *cfgpath; |
| 152 | char *scorepath; |
| 153 | char *bindpath; |
| 154 | char *keyspath; |
| 155 | FILE *f; |
| 156 | char verbose; |
| 157 | char fullscreen; |
| 158 | char audio; |
| 159 | char difficulty; |
| 160 | char joystick; |
| 161 | char *wwwbrowser; |
| 162 | } PREFS; |
| 163 | |
| 164 | typedef struct scores_ |
| 165 | { |
| 166 | char easy_n[10][9]; |
| 167 | int easy_s[10]; |
| 168 | char medium_n[10][9]; |
| 169 | int medium_s[10]; |
| 170 | char hard_n[10][9]; |
| 171 | int hard_s[10]; |
| 172 | } SCORES; |
| 173 | |
| 174 | typedef struct BUTTONS { |
| 175 | SDL_Surface *startgame; |
| 176 | SDL_Surface *startgameh; |
| 177 | SDL_Surface *fullscreen; |
| 178 | SDL_Surface *fullscreenh; |
| 179 | SDL_Surface *window; |
| 180 | SDL_Surface *windowh; |
| 181 | SDL_Surface *audioon; |
| 182 | SDL_Surface *audiooff; |
| 183 | SDL_Surface *audioonh; |
| 184 | SDL_Surface *audiooffh; |
| 185 | SDL_Surface *easy; |
| 186 | SDL_Surface *easyh; |
| 187 | SDL_Surface *medium; |
| 188 | SDL_Surface *mediumh; |
| 189 | SDL_Surface *hard; |
| 190 | SDL_Surface *hardh; |
| 191 | SDL_Surface *hiscores; |
| 192 | SDL_Surface *hiscoresh; |
| 193 | SDL_Surface *prefs; |
| 194 | SDL_Surface *prefsh; |
| 195 | SDL_Surface *quit; |
| 196 | SDL_Surface *quith; |
| 197 | SDL_Surface *resume; |
| 198 | SDL_Surface *resumeh; |
| 199 | } BUTTONS; |
| 200 | |
| 201 | #if 0 |
| 202 | class SniperBGM { |
| 203 | Mix_Music *BGM; |
| 204 | int playp; |
| 205 | int pausep; |
| 206 | int startTime, stopTime; |
| 207 | float bookmark; |
| 208 | static const int FadeInTime = 3000; |
| 209 | static const int FadeOutTime = 1000; |
| 210 | static const float RepeatJumpPos = 0.0; |
| 211 | public: |
| 212 | SniperBGM(); |
| 213 | SniperBGM(Mix_Music *m); |
| 214 | ~SniperBGM(); |
| 215 | const SniperBGM & operator=(Mix_Music *m); |
| 216 | operator Mix_Music*() const; |
| 217 | void use(Mix_Music *m); |
| 218 | void start(); |
| 219 | void stimulate(); |
| 220 | void depress(); |
| 221 | void stop(); |
| 222 | void pause(); |
| 223 | void pause(int newstate); |
| 224 | void jumpto(float pos); |
| 225 | void rewind(); |
| 226 | void loop(); |
| 227 | void release(); |
| 228 | void grab(); |
| 229 | }; |
| 230 | #endif /* 0 */ |
| 231 | |
| 232 | /* Phasing out class SniperBGM */ |
| 233 | typedef struct sniperbgm_t { |
| 234 | Mix_Music *BGM; |
| 235 | int playp; |
| 236 | int pausep; |
| 237 | int time_start; |
| 238 | int time_stop; |
| 239 | float bookmark; |
| 240 | } sniperbgm_t; |
| 241 | |
| 242 | #define sniperbgm_FadeInTime 3000 |
| 243 | #define sniperbgm_FadeOutTime 1000 |
| 244 | #define sniperbgm_RepeatJumpPos 0.0 |
| 245 | |
| 246 | sniperbgm_t * sniperbgm_init (sniperbgm_t *); |
| 247 | sniperbgm_t * sniperbgm_init_music (sniperbgm_t *, Mix_Music *); |
| 248 | sniperbgm_t * sniperbgm_destroy (sniperbgm_t *); |
| 249 | void sniperbgm_delete (sniperbgm_t *); |
| 250 | Mix_Music * sniperbgm_music (sniperbgm_t *); |
| 251 | int sniperbgm_use (sniperbgm_t *, Mix_Music *); |
| 252 | void sniperbgm_start (sniperbgm_t *); |
| 253 | void sniperbgm_stimulate (sniperbgm_t *); |
| 254 | void sniperbgm_depress (sniperbgm_t *); |
| 255 | void sniperbgm_stop (sniperbgm_t *); |
| 256 | void sniperbgm_pause (sniperbgm_t *, int); |
| 257 | void sniperbgm_jumpto (sniperbgm_t *, float); |
| 258 | void sniperbgm_rewind (sniperbgm_t *); |
| 259 | void sniperbgm_loop (sniperbgm_t *); |
| 260 | void sniperbgm_release (sniperbgm_t *); |
| 261 | void sniperbgm_grab (sniperbgm_t *); |
| 262 | |
| 263 | |
| 264 | |
| 265 | /* character counter/generator state */ |
| 266 | typedef struct charcount_t { |
| 267 | int spawn; /* SpawnCount */ |
| 268 | int count; /* Count */ |
| 269 | int dead; /* Killed */ |
| 270 | int goal; /* GottenToSafety */ |
| 271 | float time; /* SpawnTime */ |
| 272 | float period; /* SpawnTimePeriod */ |
| 273 | } charcount_t; |
| 274 | |
| 275 | #if 0 |
| 276 | typedef struct soundeffect_t { |
| 277 | Mix_Chunk *data; |
| 278 | int len; |
| 279 | int ofs; |
| 280 | } soundeffect_t; |
| 281 | #endif /* 0 */ |
| 282 | |
| 283 | /* game state */ |
| 284 | typedef struct orbitalsniper_t { |
| 285 | SDL_Surface *Screen; |
| 286 | SDL_Surface *Map; |
| 287 | SDL_Surface *AIMap; |
| 288 | SDL_Surface *Font[5]; |
| 289 | SDL_Surface *CharSprite; |
| 290 | |
| 291 | int ControlState; |
| 292 | float MouseX; |
| 293 | float MouseY; |
| 294 | float MouseZ; |
| 295 | float CoordScale; |
| 296 | |
| 297 | int WobbleIndex; |
| 298 | float WobbleX; |
| 299 | float WobbleY; |
| 300 | float CenterX; |
| 301 | float CenterY; |
| 302 | |
| 303 | int verbosity; |
| 304 | int Reloading; |
| 305 | int ReloadTime; |
| 306 | int StartTick; |
| 307 | int GameStartTick; |
| 308 | int GameOverTicks; |
| 309 | OES_ENDREASON GameOverReason; |
| 310 | int FrameCount; |
| 311 | int LastTick; |
| 312 | int Score; |
| 313 | float ScoreMod; /* score modifier. */ |
| 314 | |
| 315 | charcount_t vip; /* keep alive at all costs. */ |
| 316 | charcount_t baddy; /* BadGuys */ |
| 317 | charcount_t pedestrian; /* collateral damage. */ |
| 318 | |
| 319 | int num_characters; |
| 320 | CHARACTER *characters; /* List of all characters in game */ |
| 321 | int num_spawnpoints; |
| 322 | SPAWNPOINT *spawnpoints; /* List of spawnpoints */ |
| 323 | int num_waypoints; |
| 324 | WAYPOINT *waypoints; /* List of waypoints */ |
| 325 | CHARACTER * SightedCharacter; |
| 326 | SCORES HiScore; /* high scores list */ |
| 327 | |
| 328 | // soundeffect_t AudioZoomSample; |
| 329 | // soundeffect_t AudioFireSample; |
| 330 | Mix_Chunk *AudioZoomOut; |
| 331 | Mix_Chunk *AudioZoomIn; |
| 332 | Mix_Chunk *AudioFire; |
| 333 | |
| 334 | int GameState; |
| 335 | SDL_AudioSpec *sdlaudio; |
| 336 | // SniperBGM BGM; |
| 337 | sniperbgm_t *BGM; |
| 338 | int GrabP; /* mouse grab */ |
| 339 | int BeforePauseTick; /* keep spawn-timing across pauses. */ |
| 340 | int AfterPauseTick; |
| 341 | int SemiPause; /* double-click timer. */ |
| 342 | |
| 343 | char *mediaPath; |
| 344 | // int unreal_dither[]; |
| 345 | } ORBITALSNIPER; |
| 346 | |
| 347 | |
| 348 | /* globals. */ |
| 349 | |
| 350 | extern BUTTONS gButton; |
| 351 | extern int unreal_dither[]; |
| 352 | extern char *mediaPath; |
| 353 | |
| 354 | extern ORBITALSNIPER Game; |
| 355 | |
| 356 | |
| 357 | /* function prototypes. */ |
| 358 | extern float distance(float aX1, float aY1, float aX2, float aY2); |
| 359 | extern float distance_wp(int aWaypoint, float aX, float aY); |
| 360 | extern float distance_wpwp(int aWaypoint1, int aWaypoint2); |
| 361 | extern void handle_ai(CHARACTER &c); |
| 362 | extern int spawn_ai(int aType); |
| 363 | |
| 364 | extern void print(int aXofs, int aYofs, int aColor, const char *aString, ...); |
| 365 | extern void printShadow(int aXofs, int aYofs, const char *aString, ...); |
| 366 | extern void zoom(SDL_Surface * src, float ofsx, float ofsy, float scale); |
| 367 | extern void zoom_unreal(SDL_Surface * src, float ofsx, float ofsy, float scale); |
| 368 | extern void drawLine(SDL_Surface * aTarget, int x1,int y1,int x2, int y2, int clr); |
| 369 | extern void target(); |
| 370 | extern void init(); |
| 371 | extern void init_logoscreen(); |
| 372 | extern void logoscreen(); |
| 373 | extern void precalc_ai(); |
| 374 | extern void shoot(); |
| 375 | extern void gameoverscreen(int aReason); |
| 376 | |
| 377 | extern void drawCharacter(CHARACTER &c); |
| 378 | |
| 379 | extern void configure_difficulty(); |
| 380 | |
| 381 | /* setup and teardown */ |
| 382 | int oes_setup (); |
| 383 | void oes_teardown (); |
| 384 | int oes_suspend(); |
| 385 | int oes_resume(); |
| 386 | |
| 387 | /* Preferences */ |
| 388 | PREFS *prefs_init (PREFS *); |
| 389 | PREFS *prefs_destroy (PREFS *); |
| 390 | void prefs_delete (PREFS *); |
| 391 | PREFS *prefs_load (PREFS *); |
| 392 | PREFS *prefs_save (PREFS *); |
| 393 | PREFS *prefs_create (PREFS *); |
| 394 | |
| 395 | extern void show_hiscores(); |
| 396 | extern void init_hiscores(); |
| 397 | extern void process_hiscore(); |
| 398 | extern void save_hiscores(); |
| 399 | |
| 400 | extern void prefs(); |
| 401 | |
| 402 | |
| 403 | void draw_button (SDL_Surface *b, int x, int y); |
| 404 | |
| 405 | // FIXME: everybody should use this. |
| 406 | SDL_Surface *oes_load_img (const char *path); |
| 407 | |
| 408 | bool hovering (int x, int y, int w, int h); |
| 409 | |
| 410 | |
| 411 | /* drawing */ |
| 412 | void oes_fillrect (SDL_Surface *, int x0, int y0, int x1, int y1, int color); |
| 413 | |
| 414 | int draw_gameover (SDL_Surface *, const SDL_Rect *); |
| 415 | int draw_hiscores (SDL_Surface *, const SDL_Rect *); |
| 416 | |
| 417 | |
| 418 | /* S-Expressions (libsexpr) */ |
| 419 | |
| 420 | #define PAIRP(se) ((se)->ty == SEXP_LIST) |
| 421 | #define ATOMP(se) ((se)->ty == SEXP_VALUE) |
| 422 | #define CAR(se) (ATOMP(se) ? se : hd_sexp(se)) |
| 423 | #define CDR(se) (ATOMP(se) ? next_sexp(se) : tl_sexp(se)) |
| 424 | |
| 425 | int prefs_load (); |
| 426 | int prefs_save (); |
| 427 | int prefs_init (); |
| 428 | |
| 429 | |
| 430 | int oes_game (); /* main game loop */ |
| 431 | |
| 432 | #include "ui.h" |
| 433 | |
| 434 | |
| 435 |