incavead.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include <thread>
  2. #include <mutex>
  3. #include <sstream>
  4. #include <iostream>
  5. #include <math.h>
  6. #include "libmaudit/maudit.h"
  7. #include "debug_benchmark.h"
  8. #include "rcode.h"
  9. #include "serialize.h"
  10. #include "common_types.h"
  11. #include "common.h"
  12. #include "skins.h"
  13. ////
  14. #include "pstats.h"
  15. #include "damage.h"
  16. #include "constants.h"
  17. #include "terrain.h"
  18. #include "designs.h"
  19. #include "species.h"
  20. #include "vaults.h"
  21. #include "counters.h"
  22. #include "terrain_bank.h"
  23. #include "designs_bank.h"
  24. #include "species_bank.h"
  25. #include "vaults_bank.h"
  26. #include "celauto_bank.h"
  27. #include "levelskins.h"
  28. #include "nlp.h"
  29. #include "pstats_serialize.h"
  30. #include "gamestate.h"
  31. #include "mainloop_net.h"
  32. #include "bones.h"
  33. #include "uniques.h"
  34. #include "permafeats.h"
  35. #include "finance.h"
  36. //#include "configparser.h"
  37. namespace configparser {
  38. extern void parse_config(const std::string& filename, tag_mem_t& tagmem);
  39. }
  40. #include "utilstuff.h"
  41. #include "inventory.h"
  42. #include "player.h"
  43. #include "combat.h"
  44. #include "monster_ai.h"
  45. #include "vault_place.h"
  46. #include "game.h"
  47. void init_statics(const std::string& gamecfg) {
  48. tag_mem_t tagmem;
  49. configparser::parse_config(gamecfg, tagmem);
  50. ////
  51. }
  52. void client_mainloop(int client_fd, bool singleplayer, bool debug, size_t n_skin, bool fullwidth) {
  53. try {
  54. maudit::client_socket client(client_fd);
  55. typedef maudit::screen<maudit::client_socket> screen_t;
  56. screen_t screen(client);
  57. mainloop::Main<Game, GameState, GameOptions, screen_t> main(screen, debug, n_skin, fullwidth);
  58. main.mainloop(singleplayer);
  59. } catch (std::exception& e) {
  60. std::cerr << "Caught error: " << e.what() << std::endl;
  61. } catch (...) {
  62. }
  63. }
  64. void spectator_mainloop(int client_fd) {
  65. try {
  66. maudit::client_socket client(client_fd);
  67. typedef maudit::screen<maudit::client_socket> screen_t;
  68. screen_t screen(client);
  69. spectator::choose_and_watch(screen);
  70. } catch (std::exception& e) {
  71. std::cerr << "Caught error: " << e.what() << std::endl;
  72. } catch (...) {
  73. }
  74. }
  75. template <typename SOCKET>
  76. void serverloop(SOCKET& sock, bool debug, size_t nskin, bool fullwidth) {
  77. while (1) {
  78. int client = sock.accept();
  79. std::thread thr(client_mainloop, client, false, debug, nskin, fullwidth);
  80. thr.detach();
  81. }
  82. }
  83. template <typename SOCKET>
  84. void spectatorloop(SOCKET& sock) {
  85. while (1) {
  86. int client = sock.accept();
  87. std::thread thr(spectator_mainloop, client);
  88. thr.detach();
  89. }
  90. }
  91. void do_genmaps(int start, int end) {
  92. std::cout << "Generating a new set of maps. Please wait." << std::endl;
  93. for (int worldz = start; worldz <= end; ++worldz) {
  94. for (int worldx = -1; worldx <= 1; ++worldx) {
  95. for (int worldy = -1; worldy <= 1; ++worldy) {
  96. GameState state;
  97. state.neigh.init(Game::GRID_W, Game::GRID_H);
  98. state.grid.init(Game::GRID_W, Game::GRID_H);
  99. state.render.init(Game::GRID_W, Game::GRID_H);
  100. state.features.init();
  101. std::string filename = make_mapname(worldx, worldy, worldz);
  102. uint64_t fixedseed = make_fixedseed(worldx, worldy, worldz);
  103. std::cout << "=== Making map for: " << worldx << "," << worldy << "," << worldz << std::endl;
  104. const Levelskin& lev = levelskins().get(worldz);
  105. state.rng.init(fixedseed);
  106. vault_state_t vaultstate;
  107. generate_or_read_cached(filename, state, lev, worldx, worldy, worldz,
  108. lev.get_vaults_level(worldz),
  109. [](const std::string& msg) { std::cout << msg << std::endl; },
  110. vaultstate);
  111. }
  112. }
  113. }
  114. }
  115. int main(int argc, char** argv) {
  116. bool singleplayer = false;
  117. bool genmaps = false;
  118. bool debug = false;
  119. int graphics = 0;
  120. bool fullwidth = false;
  121. int genmaps_start = -9;
  122. int genmaps_end = 26;
  123. std::string gamecfg("game.cfg");
  124. for (int argi = 1; argi < argc; ++argi) {
  125. std::string arg(argv[argi]);
  126. if (arg == "-s" || arg == "--singleplayer") {
  127. singleplayer = true;
  128. } else if (arg == "-g" || arg == "--generate") {
  129. genmaps = true;
  130. } else if (arg == "-d" || arg == "--debug") {
  131. debug = true;
  132. } else if (arg == "-u" || arg == "--unicode") {
  133. graphics = 1;
  134. } else if (arg == "-t" || arg == "--tiles") {
  135. graphics = 2;
  136. fullwidth = true;
  137. } else if (arg == "-g" || arg == "--game") {
  138. ++argi;
  139. if (argi < argc) {
  140. gamecfg = argv[argi];
  141. }
  142. } else if (genmaps) {
  143. size_t n = arg.find(':');
  144. if (n != std::string::npos) {
  145. genmaps_start = std::stoi(arg.substr(0, n));
  146. genmaps_end = std::stoi(arg.substr(n+1));
  147. }
  148. }
  149. }
  150. maudit::server_socket server(20020);
  151. init_statics(gamecfg);
  152. bones::bones().load(constants().max_bones);
  153. uniques::uniques().load();
  154. uniques::items().load();
  155. permafeats::features().load(constants().max_permafeats);
  156. finance::supply().load(constants().monetary_supply_base, constants().min_money_value);
  157. if (genmaps) {
  158. do_genmaps(genmaps_start, genmaps_end);
  159. return 0;
  160. }
  161. if (singleplayer) {
  162. int client = server.accept();
  163. client_mainloop(client, true, false, graphics, fullwidth);
  164. } else {
  165. std::thread specl([&]() {
  166. maudit::server_socket specs(22222);
  167. spectatorloop(specs);
  168. });
  169. specl.detach();
  170. std::thread thru([&]() {
  171. maudit::server_socket serveru(20022);
  172. serverloop(serveru, debug, 1, false);
  173. });
  174. thru.detach();
  175. std::thread thrf([&]() {
  176. maudit::server_socket serverf(20028);
  177. serverloop(serverf, debug, 2, true);
  178. });
  179. thrf.detach();
  180. serverloop(server, debug, 0, false);
  181. }
  182. return 0;
  183. }