xerbia/xerbia.c
2024-06-16 15:47:45 -04:00

193 lines
5.5 KiB
C

#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,
replies
};
static int population = 0;
static int reputation = 0;
static int monthly_immigration = 0;
static int monthly_emigration = 0;
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 [] = {
"quit", "help", "report", "status", "build", "train", "trade", "plant"
};
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.",
"Order what kind of plants should your peasants harvest this month."
};
static int construction_price [constructions] [resources] = {
{ 0, 10, 60, 30 },
{ 0, 120, 60, 10 },
{ 0, 30, 10, 60 },
{ 0, 60, 30, 10 }
};
static void separate (void) {
print ("/0------------------------------------------------------------------------------------------/-\n");
}
static int query (char * names [], int count) {
char input [1024] = "";
int index = reply_help;
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);
}
}
print ("Incorrect /1reply/-, type '/3help/-' to list replies or look at message above.\n");
goto requery;
}
static void print_help (void) {
int index;
for (index = 0; index < replies; ++index) {
print ("/3%s/- /0<>/- %s\n", reply_name [index], reply_text [index]);
}
separate ();
}
static void print_resources (void) {
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) {
print ("You have /31/- unit of /3%s/-.\n", resource_name [index]);
} else {
print ("You have /3%i/- units of type /3%s/-.\n", resource [index], resource_name [index]);
}
}
separate ();
}
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) {
print ("You have /31/- construction of type /3%s/-.\n", construction_name [index]);
} else {
print ("You have /3%i/- constructions of type /3%s/-.\n", construction [index], construction_name [index]);
}
}
separate ();
}
static void print_statistics (void) {
print ("Reputation = %i\n", reputation);
print ("Monthly immigration = %i\n", monthly_immigration);
print ("Monthly emigration = %i\n", monthly_emigration);
print ("Population = %i\n", population);
separate ();
}
static void build_construction (void) {
int index;
int reply;
for (index = 0; index < constructions; ++index) {
print ("/0->/- build /3%s/- ()\n", construction_name [index]);
}
reply = query (construction_name, constructions);
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;
}
}
for (index = 0; index < resources; ++index) {
resource [index] -= construction_price [reply] [index];
}
construction [reply] += 1;
print ("Construction of /2%s/- was completed successfully.\n", construction_name [reply]);
separate ();
}
int main (void) {
int index;
int reply;
reputation = random (0, 10);
monthly_immigration = random (0, 10);
monthly_emigration = random (0, 10);
population = random (6, 12) * 20 + monthly_immigration - monthly_emigration;
for (index = 0; index < resources; ++index) {
resource [index] = random (6, 12) * 20;
}
for (index = 0; index < constructions; ++index) {
construction [index] = random (1, 2);
}
for (reply = reply_help; reply != reply_quit; reply = query (reply_name, replies)) {
separate ();
switch (reply) {
case reply_help: print_help (); break;
case reply_report: print_resources (); break;
case reply_status: print_statistics (); break;
case reply_build: build_construction (); break;
default: break;
}
}
print_constructions ();
separate ();
print ("/0The end!/-\n");
separate ();
return (log_success);
}