From 2824e22b26d51d0a98bd75dbbb98abba34e323ed Mon Sep 17 00:00:00 2001 From: David Meyer Date: Sun, 14 Jun 2020 15:04:08 +0900 Subject: [PATCH] Initial upload of work-in-progress --- hammurabi.c | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 hammurabi.c diff --git a/hammurabi.c b/hammurabi.c new file mode 100644 index 0000000..8d3ad70 --- /dev/null +++ b/hammurabi.c @@ -0,0 +1,201 @@ +/* + * hammurabi - ancient Sumerian city-state resource management game + * + * Copyright 2020 David Meyer +JMJ + * + */ +#include +#include +#include +#include + +#define MAXYEARS 10 +#define INITPOP 100 +#define INITGRAIN 2800 +#define INITLAND 1000 + +typedef enum {FALSE, TRUE} Boolean; + +char MSGDIV[] = +"------------------------------<<<<>>>>-----------------------------------\n"; + +char MSGHELP[] = +"Help text here\n\ +Land price varies between 17 and 26 bushels of grain per acre.\n\ +Planting two acres requires one bushel of grain.\n\ +One person can farm ten acres of land.\n\ +One person needs to eat 20 bushels of grain per year to stay healthy.\n\ +"; + +char MSGSPLASH[] = +" ___ ___ ___. .__ \n\ + / | \\_____ _____ _____ __ ______________ \\_ |__ |__|\n\ + / ~ \\__ \\ / \\ / \\| | \\_ __ \\__ \\ | __ \\| |\n\ + \\ Y // __ \\| Y Y \\ Y Y \\ | /| | \\// __ \\| \\_\\ \\ |\n\ + \\___|_ /(____ /__|_| /__|_| /____/ |__| (____ /___ /__|\n\ + \\/ \\/ \\/ \\/ \\/ \\/ \n\ + ,.__.,\n\ + /,',.`.\\\n\ + ____|:|db|:|____\n\ + ___//\"\"\"|:|88|:|\"\"\"\\\\___\n\ + _____//\":=_=_=_=|--|=_=_=_=:\"\\\\_____\n\ + __/.-._//__|L_L_L_L|--|L_L_L_L|__\\\\_.-.\\__\n\ + //\"||m|:.-. .-. .-.,.__.,.-. .-. .-.:|m||\"\\\\\n\ + // ||8||: : L_L_L_/.',.`.\\_L_L_L : :||8|| \\\\\n\ + /'=======\"L.='.::'`:|:|db|:|:'`::.`=.L\"=======`\\\n\ + | .---. .--. |_:==' |:|88|:| `==:_| .--. .---. |\n\ + | | : |_.='/=======|:--:|=======\\`=._| : | |\n\ + | | _,=' ||==.=.==|:--:|==.=.==|| `=._ | |\n\ + |_:='_______|| || || |:--:| || || ||_______`=:_|\n\ + || || || |:--:| || || ||\n\ + ''-------|:--:|-------''\n\ + ':==:'\n\ +"; + +#define MAXHONOR 9 +char *HONORIFIC[] = {"Great", "Mighty", "Wise", "Benevolent", "Merciful", "Just", "Sublime", + "Glorious", "Serene", "Majestic"}; + +Boolean planyear(void); +void printgreeting(void); +void printlandprice(void); +void printstatus(void); +void printyearrept(void); +Boolean scanyes(char *prompt); +void setlandprice(void); + +int Year = 0; +int Population = INITPOP; +int Grain = INITGRAIN; +int Land = INITLAND; + +int consctl; + +int foodgrain, landsale, plantland; + +int harvyield, landprice, newpop, plaguedeath, popfed, ratgrain, starvedeath, starvepct, year; + +int totstarved = 0, totstarvepct = 0; + +void +main(void) +{ + consctl = open("/dev/consctl", OWRITE); + if(consctl == -1) exits("can't open consctl: %r\n"); + srand(time(0)); + + print(MSGSPLASH); + if(scanyes("Would you like instructions?") == TRUE) print(MSGHELP); + + printgreeting(); + printstatus(); + setlandprice(); + printlandprice(); + + for(year = 1; year <= MAXYEARS; ++ year) + { + if(planyear() == TRUE) + { + Grain = Grain + (landsale * landprice) - (plantland / 2) - foodgrain; + Land = Land - landsale; + + harvyield = nrand(5) + 1; + switch(nrand(5)+1) + { + case 1: + ratgrain = Grain; + break; + case 3: + ratgrain = Grain / 3; + break; + case 5: + ratgrain = Grain / 5; + break; + default: + ratgrain = 0; + } + Grain = Grain + (plantland * harvyield) - ratgrain; + + newpop = (nrand(5) + 1) * (20 * Land + Grain) / Population / 100 + 1; + popfed = foodgrain / 20; + starvedeath = ((popfed < Population) ? (Population - popfed) : 0); + totstarved = totstarved + starvedeath; + starvepct = 100 * starvedeath / Population; + totstarvepct = totstarvepct + starvepct; + Population = Population + newpop - starvedeath; + plaguedeath = ((nrand(100) < 15) ? (Population / 2) : 0); + Population = Population - plaguedeath; + + /* impeachement check */ + + printgreeting(); + printyearrept(); + } + else break; + } + print("The end\n"); + exits(0); +} + +Boolean +planyear(void) +{ + +} + +void +printgreeting(void) +{ + print(MSGDIV); + print("O %s One, I beg to report,\n", HONORIFIC[nrand(MAXHONOR + 1)]); +} + +void +printlandprice(void) +{ + print("The price of land is %d bushels of grain per acre.\n", landprice); +} + +void +printstatus(void) +{ + print("The population is %d.\n", Population); + print("You own %d acres.\n", Land); + print("You have %d bushels of grain.\n", Grain); +} + +void +printyearrept(void) +{ + print("In Year %d,\n", year); + print("%d bushels were harvested per acre, for a total harvest of %d bushels.\n", + harvyield, plantland * harvyield); + if(ratgrain > 0) print("Rats ate %d bushels of grain.\n", ratgrain); + if(newpop > 0) print("The population increased by %d.\n", newpop); + +} + +Boolean +scanyes(char *prompt) +{ + int in = 0; + + if(write(consctl, "rawon", 5) != 5) exits("\ncan't turn off echo\n"); + while(in != 'y' && in != 'n') + { + print("%s [y/n] ", prompt); + in = getchar(); + if(in == EOF) exits("\nerror reading terminal input\n"); + print("%c\n", in); + } + if(write(consctl, "rawoff", 6) != 6) exits("\ncan't turn on echo\n"); + print("\n"); + return (in == 'y' ? TRUE : FALSE); +} + +void +setlandprice(void) +{ + landprice = nrand(10) + 17; +} +