Allow random planet and backgrounds for missions, using "AUTO" in mission definition.

This commit is contained in:
Steve 2016-02-29 14:42:46 +00:00
parent adb86d9f14
commit d869c22f36
11 changed files with 233 additions and 83 deletions

View File

@ -9,7 +9,7 @@ vpath %.h $(SEARCHPATH)
DEPS += defs.h structs.h DEPS += defs.h structs.h
OBJS += ai.o OBJS += ai.o
OBJS += battle.o bullets.o OBJS += background.o battle.o bullets.o
OBJS += capitalShips.o challengeHome.o challenges.o cJSON.o OBJS += capitalShips.o challengeHome.o challenges.o cJSON.o
OBJS += debris.o dev.o draw.o OBJS += debris.o dev.o draw.o
OBJS += effects.o entities.o extractionPoint.o OBJS += effects.o entities.o extractionPoint.o

View File

@ -1,8 +1,8 @@
{ {
"name" : "Destroy all Darts", "name" : "Destroy all Darts",
"description" : "Destroy all Darts", "description" : "Destroy all Darts",
"background" : "gfx/backgrounds/background03.jpg", "background" : "AUTO",
"planet" : "gfx/planets/spirit.png", "planet" : "AUTO",
"music" : "", "music" : "",
"player" : { "player" : {
"type" : "Nymph", "type" : "Nymph",
@ -14,7 +14,7 @@
}, },
"challenge" : { "challenge" : {
"timeLimit" : 30, "timeLimit" : 30,
"killLimit" : 3, "killLimit" : 5,
"challenges" : [ "challenges" : [
{ {
"type" : "CHALLENGE_TIME", "type" : "CHALLENGE_TIME",
@ -29,7 +29,7 @@
"side" : "SIDE_PIRATE", "side" : "SIDE_PIRATE",
"x" : 25, "x" : 25,
"y" : 22, "y" : 22,
"number" : 3, "number" : 5,
"scatter" : 1000 "scatter" : 1000
} }
] ]

View File

@ -1,8 +1,8 @@
{ {
"name" : "Destroy all Darts", "name" : "Destroy all Darts",
"description" : "Destroy all Darts", "description" : "Destroy all Darts",
"background" : "gfx/backgrounds/background03.jpg", "background" : "AUTO",
"planet" : "gfx/planets/spirit.png", "planet" : "AUTO",
"music" : "", "music" : "",
"player" : { "player" : {
"type" : "Nymph", "type" : "Nymph",
@ -33,7 +33,7 @@
"side" : "SIDE_PIRATE", "side" : "SIDE_PIRATE",
"x" : 25, "x" : 25,
"y" : 22, "y" : 22,
"number" : 3, "number" : 5,
"scatter" : 1000 "scatter" : 1000
} }
] ]

View File

@ -1,8 +1,8 @@
{ {
"name" : "Destroy all Darts", "name" : "Destroy all Darts",
"description" : "Destroy all Darts", "description" : "Destroy all Darts",
"background" : "gfx/backgrounds/background03.jpg", "background" : "AUTO",
"planet" : "gfx/planets/spirit.png", "planet" : "AUTO",
"music" : "", "music" : "",
"player" : { "player" : {
"type" : "Nymph", "type" : "Nymph",

130
src/draw/background.c Normal file
View File

@ -0,0 +1,130 @@
/*
Copyright (C) 2015-2016 Parallel Realities
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "background.h"
static PointF backgroundPoint[4];
char **backgrounds;
char **planets;
int numBackgrounds;
int numPlanets;
void initBackground(void)
{
char **filenames;
int i;
numBackgrounds = numPlanets = 0;
filenames = getFileList(getFileLocation("gfx/backgrounds"), &numBackgrounds);
backgrounds = malloc(sizeof(char*) * numBackgrounds);
for (i = 0 ; i < numBackgrounds ; i++)
{
backgrounds[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH);
sprintf(backgrounds[i], "gfx/backgrounds/%s", filenames[i]);
free(filenames[i]);
}
free(filenames);
filenames = getFileList("gfx/planets", &numPlanets);
planets = malloc(sizeof(char*) * numPlanets);
for (i = 0 ; i < numPlanets ; i++)
{
planets[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH);
sprintf(planets[i], "gfx/planets/%s", filenames[i]);
free(filenames[i]);
}
free(filenames);
backgroundPoint[0].x = -SCREEN_WIDTH / 2;
backgroundPoint[0].y = -SCREEN_HEIGHT / 2;
backgroundPoint[1].x = SCREEN_WIDTH / 2;
backgroundPoint[1].y = -SCREEN_HEIGHT / 2;
backgroundPoint[2].x = -SCREEN_WIDTH / 2;
backgroundPoint[2].y = SCREEN_HEIGHT / 2;
backgroundPoint[3].x = SCREEN_WIDTH / 2;
backgroundPoint[3].y = SCREEN_HEIGHT / 2;
}
void scrollBackground(float x, float y)
{
int i;
for (i = 0 ; i < 4 ; i++)
{
backgroundPoint[i].x += x;
backgroundPoint[i].y += y;
if (backgroundPoint[i].x < 0)
{
backgroundPoint[i].x += (SCREEN_WIDTH * 2);
}
if (backgroundPoint[i].x >= SCREEN_WIDTH)
{
backgroundPoint[i].x -= (SCREEN_WIDTH * 2);
}
if (backgroundPoint[i].y < 0)
{
backgroundPoint[i].y += (SCREEN_HEIGHT * 2);
}
if (backgroundPoint[i].y >= SCREEN_HEIGHT)
{
backgroundPoint[i].y -= (SCREEN_HEIGHT * 2);
}
}
}
void drawBackground(SDL_Texture *texture)
{
int i;
for (i = 0 ; i < 4 ; i++)
{
blitScaled(texture, backgroundPoint[i].x, backgroundPoint[i].y, SCREEN_WIDTH, SCREEN_HEIGHT);
}
}
char *getBackgroundTexture(int i)
{
return backgrounds[i % numBackgrounds];
}
char *getPlanetTexture(int i)
{
return planets[i % numPlanets];
}
void destroyBackground(void)
{
free(backgrounds);
free(planets);
}

25
src/draw/background.h Normal file
View File

@ -0,0 +1,25 @@
/*
Copyright (C) 2015-2016 Parallel Realities
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../common.h"
extern char **getFileList(char *dir, int *count);
extern char *getFileLocation(char *filename);
extern void blitScaled(SDL_Texture *texture, int x, int y, int w, int h);

View File

@ -20,23 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "draw.h" #include "draw.h"
static PointF backgroundPoint[4];
void initBackground(void)
{
backgroundPoint[0].x = -SCREEN_WIDTH / 2;
backgroundPoint[0].y = -SCREEN_HEIGHT / 2;
backgroundPoint[1].x = SCREEN_WIDTH / 2;
backgroundPoint[1].y = -SCREEN_HEIGHT / 2;
backgroundPoint[2].x = -SCREEN_WIDTH / 2;
backgroundPoint[2].y = SCREEN_HEIGHT / 2;
backgroundPoint[3].x = SCREEN_WIDTH / 2;
backgroundPoint[3].y = SCREEN_HEIGHT / 2;
}
void prepareScene(void) void prepareScene(void)
{ {
SDL_SetRenderTarget(app.renderer, app.backBuffer); SDL_SetRenderTarget(app.renderer, app.backBuffer);
@ -161,47 +144,6 @@ void drawFilledCircle(int cx, int cy, int radius, int r, int g, int b, int a)
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE); SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE);
} }
void scrollBackground(float x, float y)
{
int i;
for (i = 0 ; i < 4 ; i++)
{
backgroundPoint[i].x += x;
backgroundPoint[i].y += y;
if (backgroundPoint[i].x < 0)
{
backgroundPoint[i].x += (SCREEN_WIDTH * 2);
}
if (backgroundPoint[i].x >= SCREEN_WIDTH)
{
backgroundPoint[i].x -= (SCREEN_WIDTH * 2);
}
if (backgroundPoint[i].y < 0)
{
backgroundPoint[i].y += (SCREEN_HEIGHT * 2);
}
if (backgroundPoint[i].y >= SCREEN_HEIGHT)
{
backgroundPoint[i].y -= (SCREEN_HEIGHT * 2);
}
}
}
void drawBackground(SDL_Texture *texture)
{
int i;
for (i = 0 ; i < 4 ; i++)
{
blitScaled(texture, backgroundPoint[i].x, backgroundPoint[i].y, SCREEN_WIDTH, SCREEN_HEIGHT);
}
}
void saveScreenshot(void) void saveScreenshot(void)
{ {
static int i = 0; static int i = 0;

View File

@ -31,6 +31,8 @@ static unsigned long hashcode(const char *str);
static char **toTypeArray(char *types, int *numTypes); static char **toTypeArray(char *types, int *numTypes);
static void loadEpicData(cJSON *node); static void loadEpicData(cJSON *node);
static void loadChallengeData(cJSON *node); static void loadChallengeData(cJSON *node);
static char *getAutoBackground(char *filename);
static char *getAutoPlanet(char *filename);
Mission *loadMissionMeta(char *filename) Mission *loadMissionMeta(char *filename)
{ {
@ -108,7 +110,7 @@ Mission *loadMissionMeta(char *filename)
void loadMission(char *filename) void loadMission(char *filename)
{ {
cJSON *root; cJSON *root;
char *text, music[MAX_DESCRIPTION_LENGTH]; char *text, music[MAX_DESCRIPTION_LENGTH], *background, *planet;
float planetScale; float planetScale;
startSectionTransition(); startSectionTransition();
@ -117,20 +119,9 @@ void loadMission(char *filename)
text = readFile(getFileLocation(filename)); text = readFile(getFileLocation(filename));
srand(hashcode(filename));
root = cJSON_Parse(text); root = cJSON_Parse(text);
battle.background = getTexture(cJSON_GetObjectItem(root, "background")->valuestring); srand(hashcode(filename));
planetScale = 25 + (rand() % 100);
planetScale *= 0.01;
battle.planetTexture = getTexture(cJSON_GetObjectItem(root, "planet")->valuestring);
battle.planet.x = (SCREEN_WIDTH / 2) - (rand() % SCREEN_WIDTH) + (rand() % SCREEN_WIDTH);
battle.planet.y = (SCREEN_HEIGHT / 2) - (rand() % SCREEN_HEIGHT) + (rand() % SCREEN_HEIGHT);
SDL_QueryTexture(battle.planetTexture, NULL, NULL, &battle.planetWidth, &battle.planetHeight);
battle.planetWidth *= planetScale;
battle.planetHeight *= planetScale;
loadObjectives(cJSON_GetObjectItem(root, "objectives")); loadObjectives(cJSON_GetObjectItem(root, "objectives"));
@ -170,10 +161,33 @@ void loadMission(char *filename)
initScript(cJSON_GetObjectItem(root, "script")); initScript(cJSON_GetObjectItem(root, "script"));
free(text); /* planet and background loading must come last, so AUTO works properly */
background = cJSON_GetObjectItem(root, "background")->valuestring;
if (strcmp(background, "AUTO") == 0)
{
background = getAutoBackground(filename);
}
battle.background = getTexture(background);
planet = cJSON_GetObjectItem(root, "planet")->valuestring;
if (strcmp(planet, "AUTO") == 0)
{
planet = getAutoPlanet(filename);
}
planetScale = 75 + (rand() % 125);
planetScale *= 0.01;
battle.planetTexture = getTexture(planet);
battle.planet.x = (SCREEN_WIDTH / 2) - (rand() % SCREEN_WIDTH) + (rand() % SCREEN_WIDTH);
battle.planet.y = (SCREEN_HEIGHT / 2) - (rand() % SCREEN_HEIGHT) + (rand() % SCREEN_HEIGHT);
SDL_QueryTexture(battle.planetTexture, NULL, NULL, &battle.planetWidth, &battle.planetHeight);
battle.planetWidth *= planetScale;
battle.planetHeight *= planetScale;
srand(time(NULL)); srand(time(NULL));
free(text);
endSectionTransition(); endSectionTransition();
/* only increment num missions / challenges started if there are some (Free Flight excluded, for example) */ /* only increment num missions / challenges started if there are some (Free Flight excluded, for example) */
@ -201,6 +215,38 @@ void loadMission(char *filename)
playMusic(music); playMusic(music);
} }
static char *getAutoBackground(char *filename)
{
int hash;
if (!battle.challengeData.isChallenge)
{
hash = hashcode(game.selectedStarSystem);
}
else
{
hash = hashcode(filename);
}
return getBackgroundTexture(hash);
}
static char *getAutoPlanet(char *filename)
{
int hash;
if (!battle.challengeData.isChallenge)
{
hash = hashcode(game.selectedStarSystem);
}
else
{
hash = hashcode(filename);
}
return getPlanetTexture(hash);
}
void completeMission(void) void completeMission(void)
{ {
if (battle.status == MS_IN_PROGRESS) if (battle.status == MS_IN_PROGRESS)

View File

@ -51,6 +51,8 @@ extern void initMissionInfo(void);
extern char *getTranslatedString(char *string); extern char *getTranslatedString(char *string);
extern void updateStarSystemMissions(void); extern void updateStarSystemMissions(void);
extern void updateChallengeMissions(void); extern void updateChallengeMissions(void);
extern char *getBackgroundTexture(int n);
extern char *getPlanetTexture(int n);
extern Battle battle; extern Battle battle;
extern Entity *player; extern Entity *player;

View File

@ -132,7 +132,8 @@ void initGameSystem(void)
initChallenges, initChallenges,
initStats, initStats,
initBattle, initBattle,
initModalDialog initModalDialog,
initBackground
}; };
numInitFuns = sizeof(initFuncs) / sizeof(void*); numInitFuns = sizeof(initFuncs) / sizeof(void*);
@ -290,6 +291,8 @@ void cleanup(void)
destroyGalacticMap(); destroyGalacticMap();
destroyWidgets(); destroyWidgets();
destroyBackground();
TTF_Quit(); TTF_Quit();

View File

@ -47,6 +47,7 @@ extern void initStats(void);
extern void initStarSystems(void); extern void initStarSystems(void);
extern void initChallenges(void); extern void initChallenges(void);
extern void initWidgets(void); extern void initWidgets(void);
extern void initBackground(void);
extern void destroyLookups(void); extern void destroyLookups(void);
extern void destroyFonts(void); extern void destroyFonts(void);
extern void destroySounds(void); extern void destroySounds(void);
@ -60,6 +61,7 @@ extern void destroyBattle(void);
extern void destroyTextures(void); extern void destroyTextures(void);
extern void destroyGalacticMap(void); extern void destroyGalacticMap(void);
extern void destroyWidgets(void); extern void destroyWidgets(void);
extern void destroyBackground(void);
extern void expireTexts(int all); extern void expireTexts(int all);
extern void initInput(void); extern void initInput(void);
extern void initModalDialog(void); extern void initModalDialog(void);