main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <unistd.h>
  5. #include <ncurses.h>
  6. #ifndef CTRL_KEY
  7. #define CTRL_KEY(c) ((c) & 037)
  8. #endif
  9. #include "econ.h"
  10. static void print_companies(Economy* ec);
  11. static void print_mines(Economy* ec);
  12. static void print_parcels(Economy* ec);
  13. static void print_people(Economy* ec);
  14. static void print_factories(Economy* ec);
  15. static void print_stores(Economy* ec);
  16. static void print_map(Economy* ec);
  17. int main(int argc, char* argv[]) {
  18. int ch;
  19. // ncurses stuff
  20. initscr();
  21. raw();
  22. timeout(0); // nonblocking i/o
  23. keypad(stdscr, TRUE);
  24. noecho();
  25. // curs_set(0); // hide the cursor
  26. start_color();
  27. // init_pair(1, COLOR_WHITE, COLOR_BLACK | A_BOLD);
  28. assume_default_colors( COLOR_WHITE, COLOR_BLACK | A_BOLD);
  29. Economy ec;
  30. Economy_init(&ec);
  31. /*
  32. Economy_AddActor(&ec, "Nobody", 100);
  33. Economy_AddActor(&ec, "A", 100);
  34. Economy_AddActor(&ec, "B", 100);
  35. Economy_AddActor(&ec, "C", 100);
  36. Economy_AddActor(&ec, "D", 100);
  37. Economy_AddActor(&ec, "E", 100);
  38. Economy_AddCashflow(&ec, 30, 0, 1, 1, "Pay me");
  39. for(int i = 0; g_assets[i].id != UINT32_MAX; i++) {
  40. Economy_AddAsset(&ec, &g_assets[i]);
  41. }
  42. */
  43. int tab = 0;
  44. // entityid_t companyid = Econ_CreateCompany(&ec, "Initech");
  45. Econ_CreatePerson(&ec, "Adam");
  46. Econ_CreatePerson(&ec, "Bob");
  47. Econ_CreatePerson(&ec, "Chuck");
  48. Econ_CreatePerson(&ec, "Don");
  49. Econ_CreatePerson(&ec, "Earl");
  50. Econ_CreatePerson(&ec, "Frank");
  51. Econ_CreatePerson(&ec, "Garret");
  52. Econ_CreatePerson(&ec, "Hank");
  53. Econ_CreatePerson(&ec, "Irving");
  54. Econ_CreatePerson(&ec, "John");
  55. Econ_CreatePerson(&ec, "Karl");
  56. Econ_CreatePerson(&ec, "Lenny");
  57. Econ_CreatePerson(&ec, "Matt");
  58. Econ_CreatePerson(&ec, "Nigel");
  59. Econ_CreatePerson(&ec, "Oscar");
  60. Econ_CreatePerson(&ec, "Peter");
  61. Econ_CreatePerson(&ec, "Quentin");
  62. Econ_CreatePerson(&ec, "Randy");
  63. // entityid_t mineID = Econ_CreateMine(&ec, 4, "Big Hole");
  64. // Mine* mine = Econ_GetEntity(&ec, mineID)->userData;
  65. // mine->owner = companyid;
  66. for(int n = 1; n <= 1000; ) {
  67. clear();
  68. mvprintw(0, 5, "Tick %d\n", n);
  69. switch(tab) {
  70. case 0: print_companies(&ec); break;
  71. case 1: print_mines(&ec); break;
  72. case 2: print_factories(&ec); break;
  73. case 3: print_stores(&ec); break;
  74. case 4: print_parcels(&ec); break;
  75. case 5: print_people(&ec); break;
  76. case 6: print_map(&ec); break;
  77. }
  78. while(1) {
  79. ch = getch();
  80. if(ch == -1) {
  81. usleep(10000);
  82. continue;
  83. }
  84. break;
  85. }
  86. if(ch == CTRL_KEY('c')) {
  87. break;
  88. }
  89. mvprintw(18, 2, "char %d, %d", ch, tab);
  90. if(ch == KEY_LEFT) {
  91. tab = (tab + 6) % 7;
  92. }
  93. else if(ch == KEY_RIGHT) {
  94. tab = (tab + 1) % 7;
  95. }
  96. if(ch == ' ') {
  97. Economy_tick(&ec);
  98. n++;
  99. }
  100. }
  101. endwin();
  102. return 0;
  103. }
  104. static void print_parcels(Economy* ec) {
  105. int n = 0;
  106. int rows, cols;
  107. getmaxyx(stdscr, rows, cols);
  108. mvprintw(0, 20, "Parcels");
  109. VECMP_EACH(&ec->Parcel, mi, m) {
  110. mvprintw(n + 1, 2, "%ld| minerals: %d, owner: %d\n", mi, m->minerals, m->owner);
  111. if(++n >= rows - 2) return;
  112. }
  113. }
  114. static void print_mines(Economy* ec) {
  115. int n = 0;
  116. int rows, cols;
  117. getmaxyx(stdscr, rows, cols);
  118. mvprintw(0, 20, "Mines");
  119. VECMP_EACH(&ec->Mine, mi, m) {
  120. mvprintw(n + 1, 2, "%ld| metals: %d\n", m->id, m->metals);
  121. if(++n >= rows - 2) return;
  122. }
  123. }
  124. static void print_people(Economy* ec) {
  125. int n = 0;
  126. int rows, cols;
  127. getmaxyx(stdscr, rows, cols);
  128. mvprintw(0, 20, "People");
  129. VECMP_EACH(&ec->Person, pi, p) {
  130. Entity* e = Econ_GetEntity(ec, p->id);
  131. Money* m = Econ_GetCompMoney(ec, e->money);
  132. mvprintw(n + 1, 2, " %d| %s,\t cash: %d\n", p->id, p->name, m->cash);
  133. if(++n >= rows - 2) return;
  134. }
  135. }
  136. static void print_companies(Economy* ec) {
  137. int n = 0;
  138. int rows, cols;
  139. getmaxyx(stdscr, rows, cols);
  140. mvprintw(0, 20, "Companies");
  141. VECMP_EACH(&ec->Company, pi, c) {
  142. Entity* e = Econ_GetEntity(ec, c->id);
  143. Money* m = Econ_GetCompMoney(ec, e->money);
  144. mvprintw(n + 1, 2, " %d| cash: %d, MIT: %d, par: %d, mine: %d, fact: %d, store: %d \n",
  145. c->id, m->cash, c->metalsInTransit, VEC_LEN(&c->parcels),
  146. VEC_LEN(&c->mines), VEC_LEN(&c->factories), VEC_LEN(&c->stores));
  147. if(++n >= rows - 2) return;
  148. }
  149. }
  150. static void print_stores(Economy* ec) {
  151. int n = 0;
  152. int rows, cols;
  153. getmaxyx(stdscr, rows, cols);
  154. mvprintw(0, 20, "Stores");
  155. VECMP_EACH(&ec->Store, pi, p) {
  156. Entity* e = Econ_GetEntity(ec, p->id);
  157. Money* m = Econ_GetCompMoney(ec, e->money);
  158. Store* s = e->userData;
  159. mvprintw(n + 1, 2, " %d| widgets: %d, sales %d\n", p->id, s->widgets, s->sales);
  160. if(++n >= rows - 2) return;
  161. }
  162. }
  163. static void print_factories(Economy* ec) {
  164. int n = 0;
  165. int rows, cols;
  166. getmaxyx(stdscr, rows, cols);
  167. mvprintw(0, 20, "Factories");
  168. VECMP_EACH(&ec->Factory, pi, p) {
  169. Entity* e = Econ_GetEntity(ec, p->id);
  170. Factory* f = e->userData;
  171. mvprintw(n + 1, 2, " %d| metals: %d, widgets: %d\n", p->id, f->metals, f->widgets);
  172. if(++n >= rows - 2) return;
  173. }
  174. }
  175. static void print_map(Economy* ec) {
  176. int rows, cols;
  177. getmaxyx(stdscr, rows, cols);
  178. for(int y = 0; y < 64; y++) {
  179. for(int x = 0; x < 64; x++) {
  180. attron(COLOR_PAIR(2));
  181. mvaddch(y, x*2, ' ');
  182. mvaddch(y, x*2+1, ' ');
  183. attroff(COLOR_PAIR(2));
  184. }
  185. }
  186. }
  187. //
  188. // static void farmer_fn(Ec) {
  189. //
  190. // }
  191. //