From 7d9bf097b9f77514fe8a2cd002d5c15ea38737fa Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Fri, 12 Aug 2011 18:24:35 +1000 Subject: [PATCH] Replace discrete calls to newtxwin() etc. with calls to txdlgbox() --- src/exch.c | 106 ++++++++++++++----------------------------- src/fileio.c | 64 ++++++++++---------------- src/game.c | 39 ++++++---------- src/intf.h | 2 + src/move.c | 125 +++++++++++++-------------------------------------- 5 files changed, 104 insertions(+), 232 deletions(-) diff --git a/src/exch.c b/src/exch.c index 9535ff5..c623e95 100644 --- a/src/exch.c +++ b/src/exch.c @@ -338,15 +338,10 @@ void visit_bank (void) case '1': // Borrow money from the Bank if (credit_limit == 0.0) { - newtxwin(7, 50, 8, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " Insufficient Credit Limit "); - center(curwin, 3, attr_error_highlight, - "The Bank will not lend you any more money"); - - wait_for_key(curwin, 5, attr_error_waitforkey); - deltxwin(); - + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " Insufficient Credit Limit ", + "The Bank will not lend you any more money."); } else { int x, y, n; int ret; @@ -390,25 +385,15 @@ void visit_bank (void) case '2': // Repay a debt if (player[current_player].debt == 0.0) { - newtxwin(7, 50, 8, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " No Debt "); - center(curwin, 3, attr_error_highlight, - "You have no debt to repay"); - - wait_for_key(curwin, 5, attr_error_waitforkey); - deltxwin(); - + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " No Debt ", + "You have no debt to repay."); } else if (player[current_player].cash == 0.0) { - newtxwin(7, 60, 8, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " No Cash "); - center(curwin, 3, attr_error_highlight, - "You have no cash with which to repay the debt!"); - - wait_for_key(curwin, 5, attr_error_waitforkey); - deltxwin(); - + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " No Cash ", + "You have no cash with which to repay the debt!"); } else { int x, y, n; int ret; @@ -593,25 +578,15 @@ void trade_shares (int num, bool *bid_used) maxshares = player[current_player].cash / company[num].share_price; if (company[num].max_stock - company[num].stock_issued == 0) { - newtxwin(7, 50, 8, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " No Shares Available "); - center(curwin, 3, attr_error_highlight, - "No more shares are available for purchase"); - - wait_for_key(curwin, 5, attr_error_waitforkey); - deltxwin(); - + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " No Shares Available ", + "No more shares are available for purchase."); } else if (maxshares <= 0) { - newtxwin(7, 50, 8, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " Insufficient Cash "); - center(curwin, 3, attr_error_highlight, - "Not enough cash to purchase shares"); - - wait_for_key(curwin, 5, attr_error_waitforkey); - deltxwin(); - + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " Insufficient Cash ", + "Not enough cash to purchase shares."); } else { maxshares = MIN(maxshares, company[num].max_stock - company[num].stock_issued); @@ -641,15 +616,10 @@ void trade_shares (int num, bool *bid_used) // Sell stock back to company maxshares = player[current_player].stock_owned[num]; if (maxshares == 0) { - newtxwin(7, 50, 8, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " No Shares "); - center(curwin, 3, attr_error_highlight, - "You do not have any shares to sell"); - - wait_for_key(curwin, 5, attr_error_waitforkey); - deltxwin(); - + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " No Shares ", + "You do not have any shares to sell."); } else { wbkgd(curwin, attr_normal_window); werase(curwin); @@ -683,25 +653,17 @@ void trade_shares (int num, bool *bid_used) *bid_used = true; if (maxshares == 0) { - newtxwin(8, 50, 8, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " No Shares Issued "); - center(curwin, 3, attr_error_highlight, "%s", company[num].name); - center(curwin, 4, attr_error_highlight, - "has refused to issue more shares"); - - wait_for_key(curwin, 6, attr_error_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " No Shares Issued ", + "%s has refused\nto issue more shares.", + company[num].name); } else { - newtxwin(8, 50, 8, WCENTER, true, attr_normal_window); - - center(curwin, 1, attr_title, " Shares Issued "); - center(curwin, 3, attr_highlight, "%s", company[num].name); - center(curwin, 4, attr_highlight, "has issued %'ld more shares", - maxshares); - - wait_for_key(curwin, 6, attr_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_normal_window, + attr_title, attr_normal, attr_highlight, 0, + attr_waitforkey, " Shares Issued ", + "%s has issued\n^{%'ld^} more shares.", + company[num].name, maxshares); } break; diff --git a/src/fileio.c b/src/fileio.c index 6721854..bcf41b6 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -240,28 +240,19 @@ bool load_game (int num) if (errno == ENOENT) { // File not found - newtxwin(7, 40, 9, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " Game Not Found "); - center(curwin, 3, attr_error_highlight, - "Game %d has not been saved to disk", num); - - wait_for_key(curwin, 5, attr_error_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 50, 9, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " Game Not Found ", + "Game %d has not been saved to disk.", num); } else { // Some other file error saved_errno = errno; - - newtxwin(9, 70, 9, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " Game Not Loaded "); - center(curwin, 3, attr_error_highlight, - "Game %d could not be loaded from disk", num); - center(curwin, 5, attr_error_normal, "File %s: %s", filename, - strerror(saved_errno)); - - wait_for_key(curwin, 7, attr_error_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 60, 9, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, + attr_error_normal, 0, attr_error_waitforkey, + " Game Not Loaded ", + "Game %d could not be loaded from disk.\n\n" + "^{File %s: %s^}", num, filename, strerror(saved_errno)); } free(buf); @@ -459,17 +450,13 @@ bool save_game (int num) ; // Do nothing: directory already exists } else { // Data directory could not be created - - newtxwin(9, 70, 7, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " Game Not Saved "); - center(curwin, 3, attr_error_highlight, - "Game %d could not be saved to disk", num); - center(curwin, 5, attr_error_normal, "Directory %s: %s", - data_dir, strerror(saved_errno)); - - wait_for_key(curwin, 7, attr_error_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 60, 7, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, + attr_error_normal, 0, attr_error_waitforkey, + " Game Not Saved ", + "Game %d could not be saved to disk.\n\n" + "^{Directory %s: %s^}", num, data_dir, + strerror(saved_errno)); free(buf); return false; @@ -484,17 +471,12 @@ bool save_game (int num) if (file == NULL) { // File could not be opened for writing saved_errno = errno; - - newtxwin(9, 70, 7, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " Game Not Saved "); - center(curwin, 3, attr_error_highlight, - "Game %d could not be saved to disk", num); - center(curwin, 5, attr_error_normal, "File %s: %s", filename, - strerror(saved_errno)); - - wait_for_key(curwin, 7, attr_error_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 60, 7, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, + attr_error_normal, 0, attr_error_waitforkey, + " Game Not Saved ", + "Game %d could not be saved to disk.\n\n" + "^{File %s: %s^}", num, filename, strerror(saved_errno)); free(buf); free(filename); diff --git a/src/game.c b/src/game.c index 3e13573..a56d1c4 100644 --- a/src/game.c +++ b/src/game.c @@ -207,14 +207,11 @@ void init_game (void) first_player = randi(number_players); current_player = first_player; - newtxwin(7, 50, 8, WCENTER, true, attr_normal_window); - - center(curwin, 2, attr_normal, "The first player to go is"); - center(curwin, 3, attr_highlight, "%s", - player[first_player].name); - - wait_for_key(curwin, 5, attr_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 50, 8, WCENTER, attr_normal_window, + attr_title, attr_normal, attr_highlight, 0, + attr_waitforkey, " First Player ", + "The first player to go is ^{%s^}.", + player[first_player].name); txrefresh(); } } @@ -497,30 +494,20 @@ void end_game (void) err_exit_nomem(); } - newtxwin(7, 40, 9, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " Game Over "); - center(curwin, 3, attr_error_highlight, "The game is over after %d turns", - turn_number - 1); - - wait_for_key(curwin, 5, attr_error_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 50, 9, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " Game Over ", + "The game is over after %d turns.", turn_number - 1); for (i = 0; i < number_players; i++) { show_status(i); } if (number_players == 1) { - l_strfmon(buf, BUFSIZE, "%1n", total_value(0)); - - newtxwin(9, 60, 8, WCENTER, true, attr_normal_window); - - center(curwin, 1, attr_title, " Total Value "); - center2(curwin, 4, attr_normal, attr_highlight, - "Your total value was ", "%s", buf); - - wait_for_key(curwin, 7, attr_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 60, 8, WCENTER, attr_normal_window, + attr_title, attr_normal, attr_highlight, 0, attr_waitforkey, + " Total Value ", "Your total value was ^{%N^}.", + total_value(0)); } else { // Sort players on the basis of total value for (i = 0; i < number_players; i++) { diff --git a/src/intf.h b/src/intf.h index fa2176c..1809355 100644 --- a/src/intf.h +++ b/src/intf.h @@ -55,6 +55,8 @@ #define WCENTER -1 // Centre the new window +#define MAX_DLG_LINES 10 // Default maximum lines in dialog box + // Check if resizing events are supported #ifdef KEY_RESIZE diff --git a/src/move.c b/src/move.c index 3db159b..49f3aa9 100644 --- a/src/move.c +++ b/src/move.c @@ -632,58 +632,23 @@ void next_player (void) void bankrupt_player (bool forced) { - bool longname; - int i; - - - /* It would be nice if we had functions that would do word-wrapping - for us automatically! */ - - longname = (strlen(player[current_player].name) > 20); if (forced) { - newtxwin(longname ? 9 : 8, 54, 7, WCENTER, true, attr_error_window); + txdlgbox(MAX_DLG_LINES, 50, 7, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " Bankruptcy Court ", + "%s has been declared bankrupt by the Interstellar Trading Bank.", + player[current_player].name); } else { - newtxwin(longname ? 8 : 7, 50, 7, WCENTER, true, attr_error_window); + txdlgbox(MAX_DLG_LINES, 50, 7, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, 0, 0, + attr_error_waitforkey, " Bankruptcy Court ", + "%s has declared bankruptcy.", player[current_player].name); } - - center(curwin, 1, attr_error_title, " Bankruptcy Court "); - - if (forced) { - if (longname) { - center(curwin, 3, attr_error_highlight, "%s", - player[current_player].name); - center(curwin, 4, attr_error_highlight, - "has been declared bankrupt by the"); - center(curwin, 5, attr_error_highlight, - "Interstellar Trading Bank"); - } else { - center(curwin, 3, attr_error_highlight, - "%s has been declared bankrupt", - player[current_player].name); - center(curwin, 4, attr_error_highlight, - "by the Interstellar Trading Bank"); - } - } else { - if (longname) { - center(curwin, 3, attr_error_highlight, "%s", - player[current_player].name); - center(curwin, 4, attr_error_highlight, - "has declared bankruptcy"); - } else { - center(curwin, 3, attr_error_highlight, - "%s has declared bankruptcy", - player[current_player].name); - } - } - - wait_for_key(curwin, getmaxy(curwin) - 2, attr_error_waitforkey); - - deltxwin(); txrefresh(); // Confiscate all assets belonging to player player[current_player].in_game = false; - for (i = 0; i < MAX_COMPANIES; i++) { + for (int i = 0; i < MAX_COMPANIES; i++) { company[i].stock_issued -= player[current_player].stock_owned[i]; player[current_player].stock_owned[i] = 0; } @@ -692,7 +657,7 @@ void bankrupt_player (bool forced) // Is anyone still left in the game? bool all_out = true; - for (i = 0; i < number_players; i++) { + for (int i = 0; i < number_players; i++) { if (player[i].in_game) { all_out = false; break; @@ -742,16 +707,10 @@ void try_start_new_company (int x, int y) } else { // Create the new company - newtxwin(8, 50, 7, WCENTER, true, attr_normal_window); - - center(curwin, 1, attr_title, " New Company "); - center(curwin, 3, attr_normal, "A new company has been formed!"); - center2(curwin, 4, attr_normal, attr_highlight, "Its name is ", - "%s", company[i].name); - - wait_for_key(curwin, 6, attr_waitforkey); - - deltxwin(); + txdlgbox(MAX_DLG_LINES, 50, 7, WCENTER, attr_normal_window, + attr_title, attr_normal, attr_highlight, 0, attr_waitforkey, + " New Company ", "A new company has been formed!\n" + "Its name is ^{%s^}.", company[i].name); txrefresh(); galaxy_map[x][y] = COMPANY_TO_MAP(i); @@ -958,19 +917,15 @@ void adjust_values (void) if (company[which].on_map) { if (randf() < ALL_ASSETS_TAKEN) { - newtxwin(10, 60, 6, WCENTER, true, attr_error_window); - - center(curwin, 1, attr_error_title, " Bankruptcy Court "); - center(curwin, 3, attr_error_highlight, "%s has been declared", - company[which].name); - center(curwin, 4, attr_error_highlight, - "bankrupt by the Interstellar Trading Bank."); - - center(curwin, 6, attr_error_window, - "All assets have been taken to repay outstanding loans."); - - wait_for_key(curwin, 8, attr_error_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 60, 6, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, + attr_error_normal, 0, attr_error_waitforkey, + " Bankruptcy Court ", + "%s has been declared bankrupt " + "by the Interstellar Trading Bank.\n\n" + "^{All assets have been taken " + "to repay outstanding loans.^}", + company[which].name); txrefresh(); } else { @@ -1090,31 +1045,15 @@ void adjust_values (void) // Check if a player's debt is too large if (total_value(current_player) <= -MAX_OVERDRAFT) { - double impounded; - char *buf; + double impounded = MIN(player[current_player].cash, + player[current_player].debt); - buf = malloc(BUFSIZE); - if (buf == NULL) { - err_exit_nomem(); - } - - impounded = MIN(player[current_player].cash, - player[current_player].debt); - - newtxwin(8, 60, 7, WCENTER, true, attr_error_window); - center(curwin, 1, attr_error_title, " Interstellar Trading Bank "); - - l_strfmon(buf, BUFSIZE, "%1n", player[current_player].debt); - center(curwin, 3, attr_error_highlight, - "Your debt has amounted to %s", buf); - - l_strfmon(buf, BUFSIZE, "%1n", impounded); - center3(curwin, 4, attr_error_normal, attr_error_normal, - attr_error_highlight, "The Bank has impounded ", - " from your cash", "%s", buf); - - wait_for_key(curwin, 6, attr_error_waitforkey); - deltxwin(); + txdlgbox(MAX_DLG_LINES, 60, 7, WCENTER, attr_error_window, + attr_error_title, attr_error_highlight, attr_error_normal, + 0, attr_error_waitforkey, " Interstellar Trading Bank ", + "Your debt has amounted to %N!\n" + "^{The Bank has impounded ^}%N^{ from your cash.^}", + player[current_player].debt, impounded); txrefresh(); player[current_player].cash -= impounded;