From 6b768b455451b915314304fef48fea8fc365df3b Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 30 Apr 2016 21:52:01 +0100 Subject: [PATCH] Experimental new gameplay features. --- data/app/config.json | 5 +++++ src/battle/bullets.c | 35 ++++++++++++++++++++++++++++++++--- src/battle/bullets.h | 1 + src/battle/player.c | 8 +++++++- src/structs.h | 7 +++++++ src/system/init.c | 25 +++++++++++++++++++------ 6 files changed, 71 insertions(+), 10 deletions(-) diff --git a/data/app/config.json b/data/app/config.json index 8642c09..7c0e30a 100644 --- a/data/app/config.json +++ b/data/app/config.json @@ -33,5 +33,10 @@ "CONTROL_NEXT_FIGHTER" : 5, "CONTROL_SCREENSHOT" : 0 } + }, + "gameplay" : { + "friendlyFire" : 0, + "autoSwitchPlayerTarget" : 0, + "missileReTarget" : 0 } } diff --git a/src/battle/bullets.c b/src/battle/bullets.c index 71f8760..7fcebf0 100644 --- a/src/battle/bullets.c +++ b/src/battle/bullets.c @@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void huntTarget(Bullet *b); static void checkCollisions(Bullet *b); static void resizeDrawList(void); +static void selectNewTarget(Bullet *b); static Bullet bulletDef[BT_MAX]; static Bullet **bulletsToDraw; @@ -165,7 +166,7 @@ static void checkCollisions(Bullet *b) if (b->owner != e && e->health > 0 && collision(b->x - b->w / 2, b->y - b->h / 2, b->w, b->h, e->x - e->w / 2, e->y - e->h / 2, e->w, e->h)) { - if (b->owner->side == e->side) + if (b->owner->side == e->side && !app.gameplay.friendlyFire) { b->damage = 0; } @@ -325,7 +326,35 @@ static void huntTarget(Bullet *b) } else { - b->target = NULL; + selectNewTarget(b); + } +} + +static void selectNewTarget(Bullet *b) +{ + int i; + Entity *e, **candidates; + + b->target = NULL; + + candidates = getAllEntsWithin(b->x - (SCREEN_WIDTH / 2), b->y - (SCREEN_HEIGHT / 2), SCREEN_WIDTH, SCREEN_HEIGHT, NULL); + + if (app.gameplay.missileReTarget) + { + for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) + { + if (e->type == ET_FIGHTER && e->side != b->owner->side && e->health > 0) + { + b->target = e; + + if (b->target == player) + { + playSound(SND_INCOMING); + } + + return; + } + } } } @@ -429,7 +458,7 @@ void fireMissile(Entity *owner) playBattleSound(b->sound, owner->x, owner->y); - if (owner->target == player) + if (b->target == player) { playSound(SND_INCOMING); } diff --git a/src/battle/bullets.h b/src/battle/bullets.h index 5146af4..daa77e5 100644 --- a/src/battle/bullets.h +++ b/src/battle/bullets.h @@ -49,6 +49,7 @@ extern void awardTrophy(char *id); extern int isOnBattleScreen(int x, int y, int w, int h); extern int getDistance(int x1, int y1, int x2, int y2); +extern App app; extern Battle battle; extern Colors colors; extern Entity *player; diff --git a/src/battle/player.c b/src/battle/player.c index 32525d1..fa82792 100644 --- a/src/battle/player.c +++ b/src/battle/player.c @@ -35,6 +35,7 @@ static void handleMouse(void); static void preFireMissile(void); static void applyRestrictions(void); static int isPriorityMissionTarget(Entity *e, int dist, int closest); +static int targetOutOfRange(void); static int selectedPlayerIndex; static int availableGuns[BT_MAX]; @@ -107,7 +108,7 @@ void doPlayer(void) handleMouse(); - if (!player->target || player->target->health <= 0 || player->target->systemPower <= 0) + if (!player->target || player->target->health <= 0 || player->target->systemPower <= 0 || targetOutOfRange()) { selectTarget(); } @@ -159,6 +160,11 @@ void doPlayer(void) } } +static int targetOutOfRange(void) +{ + return (app.gameplay.autoSwitchPlayerTarget && getDistance(player->x, player->y, player->target->x, player->target->y) > SCREEN_WIDTH * 2); +} + static void applyRestrictions(void) { if (game.currentMission->challengeData.noMissiles) diff --git a/src/structs.h b/src/structs.h index ccc76ad..9cd1319 100644 --- a/src/structs.h +++ b/src/structs.h @@ -383,6 +383,12 @@ struct Trophy { Trophy *next; }; +typedef struct { + int friendlyFire; + int autoSwitchPlayerTarget; + int missileReTarget; +} Gameplay; + typedef struct { StarSystem starSystemHead; Mission challengeMissionHead; @@ -453,6 +459,7 @@ typedef struct { int fullscreen; int musicVolume; int soundVolume; + Gameplay gameplay; Mouse mouse; int keyboard[MAX_KEYBOARD_KEYS]; SDL_Texture *backBuffer; diff --git a/src/system/init.c b/src/system/init.c index 8f4503b..652695d 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -216,12 +216,15 @@ static void loadConfig(void) { loadConfigFile(configFilename); } + + /* so that the player doesn't get confused if this is a new game */ + saveConfig(); } static void loadConfigFile(char *filename) { int i; - cJSON *root, *controlsJSON, *node; + cJSON *root, *controlsJSON, *node, *gameplayJSON; char *text; text = readFile(filename); @@ -257,19 +260,24 @@ static void loadConfigFile(char *filename) node = node->next; } } + + gameplayJSON = cJSON_GetObjectItem(root, "gameplay"); + if (gameplayJSON) + { + app.gameplay.friendlyFire = cJSON_GetObjectItem(gameplayJSON, "friendlyFire")->valueint; + app.gameplay.autoSwitchPlayerTarget = cJSON_GetObjectItem(gameplayJSON, "autoSwitchPlayerTarget")->valueint; + app.gameplay.missileReTarget = cJSON_GetObjectItem(gameplayJSON, "missileReTarget")->valueint; + } cJSON_Delete(root); free(text); - - /* so that the player doesn't get confused if this is a new game */ - saveConfig(); } void saveConfig(void) { int i; char *out, *configFilename; - cJSON *root, *controlsJSON, *keysJSON, *mouseJSON; + cJSON *root, *controlsJSON, *keysJSON, *mouseJSON, *gameplayJSON; configFilename = getSaveFilePath(CONFIG_FILENAME); @@ -297,8 +305,13 @@ void saveConfig(void) controlsJSON = cJSON_CreateObject(); cJSON_AddItemToObject(controlsJSON, "keys", keysJSON); cJSON_AddItemToObject(controlsJSON, "mouse", mouseJSON); - cJSON_AddItemToObject(root, "controls", controlsJSON); + + gameplayJSON = cJSON_CreateObject(); + cJSON_AddNumberToObject(gameplayJSON, "friendlyFire", app.gameplay.friendlyFire); + cJSON_AddNumberToObject(gameplayJSON, "autoSwitchPlayerTarget", app.gameplay.autoSwitchPlayerTarget); + cJSON_AddNumberToObject(gameplayJSON, "missileReTarget", app.gameplay.missileReTarget); + cJSON_AddItemToObject(root, "gameplay", gameplayJSON); out = cJSON_Print(root);