From ddc831b35386e5c2c6941d72d818ec1be057b96d Mon Sep 17 00:00:00 2001 From: Allanis Date: Wed, 27 Feb 2013 18:16:22 +0000 Subject: [PATCH] [Add] You can somewhat steal credits now. [Fix] Only board the ship once. --- src/board.c | 86 ++++++++++++++++++++++++++++++++++++++++++--------- src/pilot.h | 1 + src/toolkit.c | 20 ++++++++++++ src/toolkit.h | 3 ++ 4 files changed, 96 insertions(+), 14 deletions(-) diff --git a/src/board.c b/src/board.c index 2d78cb6..ca5bbaf 100644 --- a/src/board.c +++ b/src/board.c @@ -13,13 +13,16 @@ extern unsigned int player_target; static unsigned int board_credits = 0; // Penniez on the ship. +static unsigned int board_wid = 0; -static void player_unboard(char* str); +static void board_exit(char* str); +static void board_stealCreds(char* str); +static void board_fail(void); +static void board_update(void); // Attempt to board the players target. void player_board(void) { Pilot* p; - unsigned int wid; char str[128]; char cred[10]; @@ -34,29 +37,35 @@ void player_board(void) { player_message("You cannot board a ship that is not disabled!"); return; } - if(vect_dist(&player->solid->pos, &p->solid->pos) > + else if(vect_dist(&player->solid->pos, &p->solid->pos) > p->ship->gfx_space->sw * PILOT_SIZE_APROX) { player_message("You are too far away to board your target"); return; } - if((pow2(VX(player->solid->vel)-VX(p->solid->vel)) + + else if((pow2(VX(player->solid->vel)-VX(p->solid->vel)) + pow2(VY(player->solid->vel)-VY(p->solid->vel))) > (double)pow2(MAX_HYPERSPACE_VEL)) { player_message("You are going too fast to board the ship"); return; } - + else if(pilot_isFlag(p, PILOT_BOARDED)) { + player_message("Your target cannot be boarded again"); + return; + } + + // Pilot will be boarded. + pilot_setFlag(p, PILOT_BOARDED); player_message("Boarding ship %s", p->name); // Calculate credits based on ship price. board_credits = RNG(20*p->ship->price, 50*p->ship->price)/1000; // Create the boarding window. - wid = window_create("Boarding", -1, -1, BOARDING_WIDTH, BOARDING_HEIGHT); + board_wid = window_create("Boarding", -1, -1, BOARDING_WIDTH, BOARDING_HEIGHT); - window_addText(wid, 20, -30, 120, 60, + window_addText(board_wid, 20, -30, 120, 60, 0, "txtCargo", &gl_smallFont, &cDConsole, - "Credits:\n" + "SCreds:\n" "Cargo:\n"); credits2str(cred, board_credits); snprintf(str, 128, @@ -64,14 +73,63 @@ void player_board(void) { "%s\n", cred, "none"); - window_addText(wid, 80, -30, 120, 60, 0, "txtData", &gl_smallFont, &cBlack, str); + window_addText(board_wid, 80, -30, 120, 60, 0, "txtData", &gl_smallFont, &cBlack, str); - window_addButton(wid, 20, 20, 50, 30, "btnStealScred", "SCred", player_unboard); - window_addButton(wid, -20, 20, 60, 30, "btnBoardingClose", "Close", player_unboard); + window_addButton(board_wid, 20, 20, 50, 30, "btnStealScred", "SCred", board_exit); + window_addButton(board_wid, -20, 20, 50, 30, "btnBoardingClose", "Close", board_exit); } -static void player_unboard(char* str) { - if(strcmp(str, "btnBoardingClose")==0) - window_destroy(window_get("Boarding")); +static void board_exit(char* str) { + (void)str; + window_destroy(window_get("Boarding")); +} + +static void board_stealCreds(char* str) { + (void)str; + + if(board_credits == 0) { + player_message("The ship has no SCreds left"); + return; + } + + // Calculate success - TODO: make this based on crew. + if(RNG(0, 100) < 50) { + board_fail(); + return; + } + + credits += board_credits; + board_credits = 0; + board_update(); // Update the lack of credits. + player_message("You manage to steal the ship's credits"); +} + +// Failed to board. +static void board_fail(void) { + Pilot* p; + + if(RNG(0, 2)==0) { + p = pilot_get(player_target); + p->armour = -1; + player_message("You have tripped the ship's self destruct mechanism!"); + } else + player_message("The ship's security system locks you out!"); + + board_exit(NULL); +} + +// Update the cargo and credit fields. +static void board_update(void) { + char str[128]; + char cred[10]; + + credits2str(cred, board_credits); + + snprintf(str, 128, + "%s\n" + "%s\n", + cred, "none"); + + window_modifyText(board_wid, "txtData", str); } diff --git a/src/pilot.h b/src/pilot.h index 708a8f4..e328ac7 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -31,6 +31,7 @@ #define PILOT_HYP_PREP (1<<5) // Pilot is getting ready for hyperspace. #define PILOT_HYP_BEGIN (1<<6) // Pilot is starting engines. #define PILOT_HYPERSPACE (1<<7) // Pilot is in hyperspace. +#define PILOT_BOARDED (1<<8) // Pilot has been boarded already! #define PILOT_DISABLED (1<<9) // Pilot is disabled. #define PILOT_DEAD (1<<10) // Pilot is on it's death bed. #define PILOT_DELETE (1<<15) // Pilot will get delete asap. diff --git a/src/toolkit.c b/src/toolkit.c index cefaec1..0049ef8 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -15,6 +15,7 @@ typedef enum WidgetStatus_ { WIDGET_STATUS_NORMAL, WIDGET_STATUS_MOUSEOVER, WIDGET_STATUS_MOUSEDOWN, + WIDGET_STATUS_FOCUS } WidgetStatus; typedef struct Widget_ { @@ -75,6 +76,7 @@ static int mwindows = 0; static Widget* window_newWidget(Window* w); static void widget_cleanup(Widget* widget); static Window* window_wget(const unsigned int wid); +static Widget* window_getwgt(const unsigned int wid, char* name); // Input. static void toolkit_mouseEvent(SDL_Event* event); // Render. @@ -192,6 +194,22 @@ static Window* window_wget(const unsigned int wid) { return NULL; } +static Widget* window_getwgt(const unsigned int wid, char* name) { + int i; + Window* wdw = window_wget(wid); + for(i = 0; i < wdw->nwidgets; i++) + if(strcmp(wdw->widgets[i].name, name)==0) + return &wdw->widgets[i]; + return NULL; +} + +void window_modifyText(const unsigned int wid, char* name, char* newstring) { + Widget* wgt = window_getwgt(wid, name); + + if(wgt->dat.txt.text) free(wgt->dat.txt.text); + wgt->dat.txt.text = strdup(newstring); +} + // Check if a window exists. int window_exists(const char* wdwname) { int i; @@ -532,6 +550,8 @@ static void toolkit_renderButton(Widget* btn, double bx, double by) { dc = &cGrey40; oc = &cGrey20; break; + default: + break; } // Shaded base. diff --git a/src/toolkit.h b/src/toolkit.h index 8a33458..a697c53 100644 --- a/src/toolkit.h +++ b/src/toolkit.h @@ -19,6 +19,9 @@ void window_addText(const unsigned int wid, const int x, const int y, void window_addImage(const unsigned int wid, const int x, const int y, char* name, glTexture* image); +// Modification +void window_modifyText(const unsigned int wid, char* name, char* newstring); + // Get the window by name. int window_exists(const char* wdwname); unsigned int window_get(const char* wdwname);