2011-06-14 04:19:01 -04:00
|
|
|
/************************************************************************
|
|
|
|
* *
|
|
|
|
* Star Traders: A Game of Interstellar Trading *
|
|
|
|
* Copyright (C) 1990-2011, John Zaitseff *
|
|
|
|
* *
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
2011-06-14 09:01:15 -04:00
|
|
|
Author: John Zaitseff <J.Zaitseff@zap.org.au>
|
2011-06-14 04:19:01 -04:00
|
|
|
$Id$
|
|
|
|
|
|
|
|
Star Traders is a simple game of interstellar trading, where the object
|
|
|
|
of the game is to create companies, buy and sell shares, borrow and
|
|
|
|
repay money, in order to become the wealthiest player (the winner).
|
|
|
|
|
2011-06-14 07:59:48 -04:00
|
|
|
This file, trader.c, contains the main program for Star Traders.
|
2011-06-14 04:19:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
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 3 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, see http://www.gnu.org/licenses/.
|
|
|
|
*/
|
2011-06-14 09:02:16 -04:00
|
|
|
|
|
|
|
|
2011-06-14 09:19:58 -04:00
|
|
|
#include "trader.h"
|
|
|
|
|
|
|
|
|
2011-06-28 23:15:26 -04:00
|
|
|
/************************************************************************
|
|
|
|
* Internal function declarations *
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]);
|
2011-07-04 01:45:03 -04:00
|
|
|
|
|
|
|
void process_cmdline (int argc, char *argv[]);
|
|
|
|
void show_version (void) __attribute__((noreturn));
|
|
|
|
void show_usage (int status) __attribute__((noreturn));
|
|
|
|
|
|
|
|
void init_program (void);
|
|
|
|
void end_program (void);
|
2011-06-28 23:15:26 -04:00
|
|
|
|
|
|
|
|
2011-06-14 09:02:16 -04:00
|
|
|
/************************************************************************
|
|
|
|
* Main program *
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
int main (int argc, char *argv[])
|
|
|
|
{
|
2011-06-15 08:34:28 -04:00
|
|
|
init_program_name(argv);
|
|
|
|
|
2011-07-02 00:23:35 -04:00
|
|
|
// Initialise the locale
|
|
|
|
if (setlocale(LC_ALL, "") == NULL) {
|
|
|
|
err_exit("could not set locale (check LANG, LC_ALL and LANGUAGE in environment)");
|
|
|
|
}
|
2011-06-30 06:31:19 -04:00
|
|
|
|
2011-06-28 23:15:26 -04:00
|
|
|
// Process command line arguments
|
|
|
|
process_cmdline(argc, argv);
|
|
|
|
|
2011-07-04 01:45:03 -04:00
|
|
|
// Set up the display, internal low-level routines, etc.
|
|
|
|
init_program();
|
2011-06-28 23:15:26 -04:00
|
|
|
|
2011-07-04 01:45:03 -04:00
|
|
|
// Play the actual game
|
|
|
|
init_game();
|
2011-07-14 01:11:53 -04:00
|
|
|
while (! quit_selected && ! abort_game && turn_number <= max_turn) {
|
2011-07-04 01:45:03 -04:00
|
|
|
select_moves();
|
|
|
|
get_move();
|
|
|
|
process_move();
|
|
|
|
exchange_stock();
|
|
|
|
next_player();
|
2011-06-30 06:31:19 -04:00
|
|
|
}
|
2011-07-04 01:45:03 -04:00
|
|
|
end_game();
|
2011-06-30 06:31:19 -04:00
|
|
|
|
2011-07-04 01:45:03 -04:00
|
|
|
// Finish up...
|
|
|
|
end_program();
|
2011-06-14 09:02:16 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2011-06-28 23:15:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* Command line processing *
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/* Constants for command line options */
|
|
|
|
|
2011-07-14 06:19:04 -04:00
|
|
|
enum options_char {
|
|
|
|
OPTION_NO_COLOR = 1,
|
|
|
|
};
|
|
|
|
|
2011-06-30 01:25:30 -04:00
|
|
|
static const char options_short[] = "hV";
|
|
|
|
/* -h --help
|
|
|
|
-V --version
|
2011-06-28 23:15:26 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static struct option const options_long[] = {
|
2011-07-14 06:19:04 -04:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "no-color", no_argument, NULL, OPTION_NO_COLOR },
|
|
|
|
{ "no-colour", no_argument, NULL, OPTION_NO_COLOR },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
2011-06-28 23:15:26 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------
|
|
|
|
Function: process_cmdline - Process command line arguments
|
|
|
|
Arguments: argc - Same as passed to main()
|
|
|
|
argv - Same as passed to main()
|
|
|
|
Returns: (nothing)
|
|
|
|
|
|
|
|
This function processes the command line arguments passed through argc
|
|
|
|
and argv, setting global variables as appropriate.
|
|
|
|
*/
|
|
|
|
|
2011-07-04 01:45:03 -04:00
|
|
|
void process_cmdline (int argc, char *argv[])
|
2011-06-28 23:15:26 -04:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
// Process arguments starting with "-" or "--"
|
2011-06-30 07:11:36 -04:00
|
|
|
opterr = true;
|
|
|
|
while (true) {
|
2011-06-28 23:15:26 -04:00
|
|
|
c = getopt_long(argc, argv, options_short, options_long, NULL);
|
|
|
|
if (c == EOF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
2011-07-14 06:19:04 -04:00
|
|
|
// -h, --help: show help
|
2011-06-28 23:15:26 -04:00
|
|
|
show_usage(EXIT_SUCCESS);
|
2011-07-14 06:19:04 -04:00
|
|
|
break;
|
2011-06-28 23:15:26 -04:00
|
|
|
|
|
|
|
case 'V':
|
2011-07-14 06:19:04 -04:00
|
|
|
// -V, --version: show version information
|
2011-06-28 23:15:26 -04:00
|
|
|
show_version();
|
2011-07-14 06:19:04 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_NO_COLOR:
|
|
|
|
// --no-color, --no-colour: don't use colour
|
|
|
|
option_no_color = true;
|
|
|
|
break;
|
2011-06-28 23:15:26 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
show_usage(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process remaining arguments
|
|
|
|
|
|
|
|
if ((optind < argc) && (argv[optind] != NULL)) {
|
2011-06-30 01:25:30 -04:00
|
|
|
if (argv[optind][0] == '-') {
|
2011-07-01 20:01:37 -04:00
|
|
|
fprintf(stderr, "%s: invalid operand `%s'\n", program_name(),
|
2011-06-30 01:25:30 -04:00
|
|
|
argv[optind]);
|
|
|
|
show_usage(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2011-07-04 01:45:03 -04:00
|
|
|
if ((strlen(argv[optind]) == 1) &&
|
|
|
|
(*argv[optind] >= '1') && (*argv[optind] <= '9')) {
|
2011-07-03 20:28:50 -04:00
|
|
|
game_num = *argv[optind] - '0';
|
|
|
|
} else {
|
2011-07-01 20:01:37 -04:00
|
|
|
fprintf(stderr, "%s: invalid game number `%s'\n",
|
2011-07-01 19:39:23 -04:00
|
|
|
program_name(), argv[optind]);
|
|
|
|
show_usage(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2011-06-30 01:25:30 -04:00
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((optind < argc) && (argv[optind] != NULL)) {
|
2011-07-01 20:01:37 -04:00
|
|
|
fprintf(stderr, "%s: extra operand `%s'\n", program_name(),
|
2011-06-30 01:25:30 -04:00
|
|
|
argv[optind]);
|
|
|
|
show_usage(EXIT_FAILURE);
|
2011-06-28 23:15:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------
|
|
|
|
Function: show_version - Show program version information
|
|
|
|
Arguments: (none)
|
|
|
|
Returns: (nothing)
|
|
|
|
|
|
|
|
This function displays version information about this program, then
|
|
|
|
terminates.
|
|
|
|
*/
|
|
|
|
|
2011-07-04 01:45:03 -04:00
|
|
|
void show_version (void)
|
2011-06-28 23:15:26 -04:00
|
|
|
{
|
2011-07-01 20:01:37 -04:00
|
|
|
printf("\
|
2011-06-28 23:15:26 -04:00
|
|
|
" PACKAGE_NAME " (%s) %s\n\
|
|
|
|
Copyright (C) %s, John Zaitseff.\n\
|
|
|
|
\n\
|
|
|
|
Star Traders is a simple game of interstellar trading, where the object\n\
|
|
|
|
of the game is to create companies, buy and sell shares, borrow and repay\n\
|
|
|
|
money, in order to become the wealthiest player (the winner).\n\
|
|
|
|
\n\
|
|
|
|
This program is free software that is distributed under the terms of the\n\
|
|
|
|
GNU General Public License, version 3 or later. You are welcome to\n\
|
|
|
|
modify and/or distribute it under certain conditions. This program has\n\
|
|
|
|
NO WARRANTY, to the extent permitted by law; see the License for details.\n\
|
2011-07-01 20:01:37 -04:00
|
|
|
", program_name(), PACKAGE_VERSION, "1990-2011");
|
2011-06-28 23:15:26 -04:00
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------
|
|
|
|
Function: show_usage - Show command line usage information
|
|
|
|
Arguments: status - Exit status
|
|
|
|
Returns: (nothing)
|
|
|
|
|
|
|
|
This function displays usage information to standard output or standard
|
|
|
|
error, then terminates.
|
|
|
|
*/
|
|
|
|
|
2011-07-04 01:45:03 -04:00
|
|
|
void show_usage (int status)
|
2011-06-28 23:15:26 -04:00
|
|
|
{
|
2011-07-01 20:01:37 -04:00
|
|
|
const char *pn = program_name();
|
|
|
|
|
2011-06-28 23:15:26 -04:00
|
|
|
if (status != EXIT_SUCCESS) {
|
2011-07-01 20:01:37 -04:00
|
|
|
fprintf(stderr, "%s: Try `%s --help' for more information.\n",
|
|
|
|
pn, pn);
|
2011-06-28 23:15:26 -04:00
|
|
|
} else {
|
2011-07-01 20:01:37 -04:00
|
|
|
printf("Usage: %s [OPTION ...] [GAME]\n", pn);
|
|
|
|
printf("\
|
2011-06-28 23:15:26 -04:00
|
|
|
Play Star Traders, a simple game of interstellar trading.\n\n\
|
2011-07-01 20:01:37 -04:00
|
|
|
");
|
|
|
|
printf("\
|
2011-06-28 23:15:26 -04:00
|
|
|
Options:\n\
|
2011-07-14 06:19:04 -04:00
|
|
|
-V, --version output version information and exit\n\
|
|
|
|
-h, --help display this help and exit\n\
|
|
|
|
--no-color don't use colour for displaying text\n\n\
|
2011-07-01 20:01:37 -04:00
|
|
|
");
|
|
|
|
printf("\
|
2011-06-28 23:15:26 -04:00
|
|
|
If GAME is specified as a number between 1 and 9, load and continue\n\
|
|
|
|
playing that game. If GAME is not specified, start a new game.\n\n\
|
2011-07-01 20:01:37 -04:00
|
|
|
");
|
2011-06-28 23:15:26 -04:00
|
|
|
|
|
|
|
#ifdef PACKAGE_AUTHOR
|
2011-07-01 20:01:37 -04:00
|
|
|
printf("Report bugs to %s <%s>.\n", PACKAGE_AUTHOR, PACKAGE_BUGREPORT);
|
2011-06-28 23:15:26 -04:00
|
|
|
#else
|
2011-07-01 20:01:37 -04:00
|
|
|
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
|
2011-06-28 23:15:26 -04:00
|
|
|
#endif
|
|
|
|
#ifdef PACKAGE_PACKAGER_BUG_REPORTS
|
2011-07-01 20:01:37 -04:00
|
|
|
printf("Report %s bugs to <%s>.\n", PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS);
|
2011-06-28 23:15:26 -04:00
|
|
|
#endif
|
|
|
|
#ifdef PACKAGE_URL
|
2011-07-02 07:19:51 -04:00
|
|
|
printf(PACKAGE_NAME " home page: <%s>.\n", PACKAGE_URL);
|
2011-06-28 23:15:26 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(status);
|
|
|
|
}
|
2011-07-04 01:45:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* Initialisation and deinitialisation function definitions *
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------
|
|
|
|
Function: init_program - Initialise program-wide functions
|
|
|
|
Arguments: (none)
|
|
|
|
Returns: (nothing)
|
|
|
|
|
|
|
|
This function initialises the terminal display, internal low-level
|
|
|
|
routines, etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void init_program (void)
|
|
|
|
{
|
|
|
|
// Initialise the random number generator
|
2011-07-04 03:13:29 -04:00
|
|
|
init_rand();
|
2011-07-04 01:45:03 -04:00
|
|
|
|
|
|
|
// Initialise the terminal display
|
|
|
|
init_screen();
|
|
|
|
|
|
|
|
// Initialise signal-handling functions
|
2011-07-11 02:14:07 -04:00
|
|
|
// @@@ To be completed
|
2011-07-04 01:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------
|
|
|
|
Function: end_program - Deinitialise program-wide functions
|
|
|
|
Arguments: (none)
|
|
|
|
Returns: (nothing)
|
|
|
|
|
|
|
|
This function finalises the terminal display, internal low-level
|
|
|
|
routines, etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void end_program (void)
|
|
|
|
{
|
|
|
|
end_screen();
|
|
|
|
}
|