xerbia/xerbia.c

250 lines
7.3 KiB
C
Raw Permalink Normal View History

2024-06-16 05:41:47 -04:00
#include <xolatile/xtandard.c>
enum { wheat, gold, wood, stone, resources };
enum { granary, mine, storehouse, quarry, constructions };
enum {
reply_quit, reply_help, reply_report, reply_status, reply_build, reply_train, reply_trade, reply_plant,
2024-06-16 19:37:57 -04:00
reply_turn,
replies
};
2024-06-16 20:05:15 -04:00
static int population = 0;
static int reputation = 0;
static int migration = 0;
2024-06-16 12:29:39 -04:00
static int resource [resources] = { 0 };
static int construction [constructions] = { 0 };
static char * resource_name [] = { "wheat", "gold", "wood", "stone" };
static char * construction_name [] = { "granary", "mine", "storehouse", "quarry" };
static char * reply_name [] = {
2024-06-16 19:37:57 -04:00
"quit", "help", "report", "status", "build", "train", "trade", "plant",
"turn"
};
static char * reply_text [] = {
"Quit game.",
"Print reply strings and their explanation, like this one.",
"Request a report from your court advisors about the state of your fortress.",
"Request a meeting with lords and merchants about affairs in your fiefdom.",
"Propose what kind of construction should be built this month to your architect.",
"Spend more time training with your warriors this entire month.",
"Discuss what goods should be sold or bought this month with merchant guild.",
2024-06-16 19:37:57 -04:00
"Order what kind of plants should your peasants harvest this month.",
"Submit your monthly strategy to the council and wait until next month."
};
2024-06-16 09:18:51 -04:00
static int construction_price [constructions] [resources] = {
{ 0, 10, 60, 30 },
{ 0, 120, 60, 10 },
{ 0, 30, 10, 60 },
{ 0, 60, 30, 10 }
};
2024-06-16 12:29:39 -04:00
static void separate (void) {
print ("/0------------------------------------------------------------------------------------------/-\n");
}
2024-06-16 15:47:45 -04:00
static int query (char * names [], int count) {
char input [1024] = "";
2024-06-17 07:41:05 -04:00
int index = 0;
2024-06-16 15:47:45 -04:00
requery: for (index = 0; index < 1024; ++index) {
in (& input [index], 1);
if (input [index] == '\n') {
break;
}
}
input [index] = '\0';
for (index = 0; index < count; ++index) {
if (string_compare (input, names [index]) == true) {
return (index);
}
}
2024-06-16 19:37:57 -04:00
print ("Incorrect /1reply/-, type '/4help/-' to list replies or look at message above.\n");
2024-06-16 15:47:45 -04:00
goto requery;
}
static void print_help (void) {
int index;
2024-06-16 05:41:47 -04:00
for (index = 0; index < replies; ++index) {
2024-06-16 19:37:57 -04:00
print ("/4%s/- /0<>/- %s\n", reply_name [index], reply_text [index]);
2024-06-16 05:41:47 -04:00
}
2024-06-16 12:29:39 -04:00
separate ();
2024-06-16 05:41:47 -04:00
}
2024-06-16 06:02:32 -04:00
static void print_resources (void) {
2024-06-16 09:18:51 -04:00
int index;
for (index = 0; index < resources; ++index) {
if (resource [index] == 0) {
print ("You don't have any /1%s/-.\n", resource_name [index]);
} else if (resource [index] == 1) {
2024-06-16 19:37:57 -04:00
print ("You have /41/- unit of /4%s/-.\n", resource_name [index]);
2024-06-16 09:18:51 -04:00
} else {
2024-06-16 19:37:57 -04:00
print ("You have /4%i/- units of type /4%s/-.\n", resource [index], resource_name [index]);
2024-06-16 09:18:51 -04:00
}
}
2024-06-16 12:29:39 -04:00
separate ();
2024-06-16 09:18:51 -04:00
}
static void print_constructions (void) {
int index;
for (index = 0; index < constructions; ++index) {
if (construction [index] == 0) {
print ("You don't have any construction of type /1%s/-.\n", construction_name [index]);
} else if (construction [index] == 1) {
2024-06-16 19:37:57 -04:00
print ("You have /41/- construction of type /4%s/-.\n", construction_name [index]);
2024-06-16 09:18:51 -04:00
} else {
2024-06-16 19:37:57 -04:00
print ("You have /4%i/- constructions of type /4%s/-.\n", construction [index], construction_name [index]);
2024-06-16 09:18:51 -04:00
}
}
2024-06-16 12:29:39 -04:00
separate ();
}
static void print_statistics (void) {
2024-06-16 20:05:15 -04:00
print ("Reputation = %i\n", reputation);
print ("Migration = %i\n", migration);
print ("Population = %i\n", population);
2024-06-16 12:29:39 -04:00
separate ();
2024-06-16 06:02:32 -04:00
}
2024-06-16 15:47:45 -04:00
static void build_construction (void) {
int index;
int reply;
for (index = 0; index < constructions; ++index) {
2024-06-16 16:13:33 -04:00
int price;
2024-06-16 19:37:57 -04:00
print ("/0->/- build /4%s/- (", construction_name [index]);
2024-06-16 16:13:33 -04:00
for (price = 0; price < resources; ++price) {
if (construction_price [index] [price] > 0) {
2024-06-16 19:37:57 -04:00
print ("%s ", resource_name [price]);
2024-06-16 16:13:33 -04:00
print ((resource [price] < construction_price [index] [price]) ? "/1" : "/2");
2024-06-16 19:37:57 -04:00
print ("%i/-", construction_price [index] [price]);
2024-06-16 16:13:33 -04:00
}
if ((price > 0) && (price < resources - 1)) {
print (", ");
}
}
print (");\n");
2024-06-16 15:47:45 -04:00
}
2024-06-16 16:13:33 -04:00
separate ();
2024-06-16 15:47:45 -04:00
reply = query (construction_name, constructions);
2024-06-16 16:13:33 -04:00
separate ();
2024-06-16 15:47:45 -04:00
for (index = 0; index < resources; ++index) {
if (resource [index] < construction_price [reply] [index]) {
print ("You don't have enough /1%s/-, need /1%i/- more.\n", resource_name [index], construction_price [reply] [index] - resource [index]);
print ("Your architect humbly refused to build /1%s/-.\n", construction_name [reply]);
separate ();
return;
}
}
2024-06-16 15:47:45 -04:00
for (index = 0; index < resources; ++index) {
resource [index] -= construction_price [reply] [index];
}
2024-06-16 15:47:45 -04:00
construction [reply] += 1;
2024-06-16 15:47:45 -04:00
print ("Construction of /2%s/- was completed successfully.\n", construction_name [reply]);
separate ();
}
2024-06-16 19:37:57 -04:00
static void compute_turn (void) {
char * problem [] = { "rats", "thieves", "rotting", "cracking" };
int index;
2024-06-16 20:05:15 -04:00
print ("Your strategy has been submitted to the council, now you can only wait...\n");
print ("One month later, your advisor brings your short report on activities in your fortress.\n");
2024-06-16 19:37:57 -04:00
for (index = 0; index < resources; ++index) {
int gained = construction [index] * 20;
int lost = random (1, 10);
resource [index] += gained - lost;
print ("Your fortress gained /2%i/- units of %s, but lost /1%i/- due to %s.\n", gained, resource_name [index], lost, problem [index]);
}
2024-06-16 20:05:15 -04:00
reputation += random (-3, 3) + resource [gold] / 120 + resource [wheat] / 60;
migration = random (0, 6) - resource [wheat] / 120;
population += migration;
resource [wheat] -= population / 3;
resource [gold] -= ((reputation < 0) ? -reputation : reputation) / 3;
print ("Beside that, /1%i/- units of wheat was spent on feeding the people.\n", population / 3);
print ("Also, only /1%i/- gold was spent inside castle.\n", ((reputation < 0) ? -reputation : reputation) / 3);
2024-06-16 19:37:57 -04:00
separate ();
}
2024-06-16 05:41:47 -04:00
int main (void) {
2024-06-16 12:29:39 -04:00
int index;
2024-06-16 13:31:34 -04:00
int reply;
2024-06-16 12:29:39 -04:00
2024-06-16 20:05:15 -04:00
reputation = random (0, 10);
migration = random (0, 10);
population = random (6, 12) * 20 + migration;
2024-06-16 12:29:39 -04:00
for (index = 0; index < resources; ++index) {
resource [index] = random (6, 12) * 20;
}
for (index = 0; index < constructions; ++index) {
construction [index] = random (1, 2);
}
2024-06-16 16:13:33 -04:00
separate ();
2024-06-16 19:37:57 -04:00
print ("/0-- Xerbia is clone of /7Sumerian Game/0, made for fun in readable and formatted ANSI C./-\n");
print ("/0-- Original game was designed by /7Mabel Addis/0 and programmed by /7William McKay/0 in 1964./-\n");
print ("/0-- /-\n");
print ("/0-- /4Ognjen 'xolatile' Milan Robovic/-\n");
2024-06-16 16:13:33 -04:00
2024-06-16 15:47:45 -04:00
for (reply = reply_help; reply != reply_quit; reply = query (reply_name, replies)) {
2024-06-16 13:31:34 -04:00
separate ();
2024-06-16 12:29:39 -04:00
2024-06-16 14:51:03 -04:00
switch (reply) {
2024-06-16 15:47:45 -04:00
case reply_help: print_help (); break;
case reply_report: print_resources (); break;
case reply_status: print_statistics (); break;
case reply_build: build_construction (); break;
2024-06-16 19:37:57 -04:00
case reply_turn: compute_turn (); break;
2024-06-16 15:47:45 -04:00
default: break;
2024-06-16 14:51:03 -04:00
}
2024-06-16 13:31:34 -04:00
}
2024-06-16 12:29:39 -04:00
separate ();
2024-06-16 16:13:33 -04:00
print_constructions ();
print ("/0The end!/-\n");
2024-06-16 12:29:39 -04:00
separate ();
2024-06-16 05:41:47 -04:00
return (log_success);
}