main.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /*
  2. Space Construct v0.3-dev
  3. Beanzilla@21:4/110 on FsxNet
  4. Beanzilla@637:1/110 on HappyNet
  5. */
  6. #define VERSION_MAJOR 0
  7. #define VERSION_MINOR 3
  8. #ifndef VERSION_TYPE
  9. #define VERSION_TYPE "dev"
  10. #endif
  11. #define PATH_MAX 256
  12. #define PATH_SEP "/"
  13. #include <MagiDoor.h>
  14. #include <sqlite3.h>
  15. #if defined(_MSC_VER) || defined(WIN32)
  16. #define snprintf _snprintf
  17. #define strcasecmp _stricmp
  18. #include <winsock2.h>
  19. #ifndef _MSC_VER
  20. #define _MSC_VER 1
  21. #endif
  22. #else
  23. #include <arpa/inet.h>
  24. #endif
  25. // Standard C
  26. #include <time.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <ctype.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <unistd.h> // stat
  35. char * log_path = "logs";
  36. int inuse = 0; // Are any other copies of us running? (We are a single door!)
  37. int debug = 0; // Are we in debug mode?
  38. // User Structure (This includes guns, fuel, armor, shields, HP)
  39. typedef struct user_info {
  40. int uid; // Primary Key
  41. int experience; // Think about this as score
  42. // Was ship structure, now is part of user
  43. int guns; // Placed Guns, More means more damage (Deals from 1-2 damage per attack)
  44. int fuel; // How many "turns" do we have
  45. int metal; // Used to build more guns/armors/shields possibly fuel too (guns take 5, armor takes 3, shields take 7, maybe fuel takes 12)
  46. int armors; // Placed Armors (Each Armor takes 4 points of damage then breaks off)
  47. int shields; // Placed Shields (Each Shields takes 3 points of damage then is offline, regenerates 1 point per attack/turn must reach 50% of max to being protecting again)
  48. int hitpoints; // Total hitpoints till we die (Rules for taking damage, shields first then armor then hitpoints)
  49. int armorpoints; // Points we get from armors (Repairable but costs 1 metal per point, but can break if taking too much damage)
  50. int shieldpoints; // Points we get from shields (Self-Regenerates, and does not get destroyed)
  51. int shieldsup; // 0 means yes, anything else means shields are offline, deduct if not 0 for each attack.
  52. int laston; // 20200630 is 6-30-2020
  53. char nick[256]; // What they go by on score board, this allows duping since we really go by real
  54. char real[256]; // Their real name to match with drop file info, prevents someone from loging in as another
  55. } user_inf;
  56. // DateStamp Structure to provide date difference
  57. typedef struct dateTamp {
  58. int year; // YYYY, or number of years difference
  59. int month; // MM, or number of months difference
  60. int day; // DD, or number of days difference.
  61. int age; // Taking down to aproximate day count.
  62. } dT; // YYYYMMDD it in int form
  63. void dolog(char *fmt, ...) {
  64. // Low end Logging
  65. char buffer[PATH_MAX];
  66. struct tm *time_now;
  67. time_t timen;
  68. FILE *logfptr;
  69. timen = time(NULL);
  70. time_now = localtime(&timen);
  71. if (log_path != NULL) {
  72. snprintf(buffer, PATH_MAX, "%s%s%04d%02d%02d.log", log_path, PATH_SEP,
  73. time_now->tm_year + 1900, time_now->tm_mon + 1, time_now->tm_mday);
  74. } else {
  75. snprintf(buffer, PATH_MAX, "%04d%02d%02d.log", time_now->tm_year + 1900,
  76. time_now->tm_mon + 1, time_now->tm_mday);
  77. }
  78. logfptr = fopen(buffer, "a");
  79. if (!logfptr) {
  80. return;
  81. }
  82. va_list ap;
  83. va_start(ap, fmt);
  84. vsnprintf(buffer, 512, fmt, ap);
  85. va_end(ap);
  86. fprintf(logfptr, "%02d:%02d:%02d %s\n", time_now->tm_hour, time_now->tm_min,
  87. time_now->tm_sec, buffer);
  88. fclose(logfptr);
  89. }
  90. int dateStamp() {
  91. // YYYYMMDD \o/ In a INT so we can store it and compare against it!
  92. struct tm *time_now;
  93. time_t timen;
  94. int result = 0;
  95. timen = time(NULL);
  96. time_now = localtime(&timen);
  97. result += ((time_now->tm_year + 1900) * 10000);
  98. result += (time_now->tm_mon * 100) + 100;
  99. result += time_now->tm_mday;
  100. return result;
  101. }
  102. int compareDate(int dt) {
  103. // Returns integer of difference, from now.
  104. struct tm *time_now;
  105. time_t timen;
  106. int now = 0;
  107. timen = time(NULL);
  108. time_now = localtime(&timen);
  109. now += ((time_now->tm_year + 1900) * 10000);
  110. now += (time_now->tm_mon * 100) + 100;
  111. now += time_now->tm_mday;
  112. return (now - dt); // 10000 = 1 Year, 100 = 1 Month, 1 = 1 Day
  113. }
  114. dT formDate(int diff) {
  115. // Reverse process so we can compare exact days or get the structure back out too.
  116. dT result;
  117. result.year = 0;
  118. result.month = 0;
  119. result.day = 0;
  120. result.age = 0;
  121. int notDone = 1;
  122. while(notDone) {
  123. if(diff >= 10000) {
  124. diff -= 10000;
  125. result.year += 1;
  126. } else if(diff >= 100) {
  127. diff -= 100;
  128. result.month += 1;
  129. } else if(diff >= 1) {
  130. diff -= 1;
  131. result.day += 1;
  132. } else {
  133. notDone = 0;
  134. }
  135. } // Process Aprox day count
  136. if(result.year != 0) {
  137. result.age += (365 * result.year);
  138. } else if(result.month != 0) {
  139. result.age += (30 * result.month);
  140. } else if(result.day != 0) {
  141. result.age += result.day;
  142. }
  143. return result;
  144. }
  145. int db_test() {
  146. sqlite3 *db;
  147. sqlite3_stmt *stmt;
  148. char sqlbuffer[256];
  149. char strbuffer[256];
  150. int sizeofdb = 0;
  151. int rc = sqlite3_open("spaceconstruct.db3", &db);
  152. if(rc) { // Did we do a successful open?
  153. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  154. sqlite3_close(db);
  155. md_exit(1);
  156. }
  157. sqlite3_busy_timeout(db, 5000);
  158. // DB open
  159. strcpy(sqlbuffer, "SELECT * FROM user;");
  160. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer), &stmt, NULL);
  161. while(sqlite3_step(stmt) == SQLITE_ROW) {
  162. md_printf("`white`%s=%d %s=%s %s=%s %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d\r\n",
  163. sqlite3_column_name(stmt, 0), // int, uid
  164. sqlite3_column_int(stmt, 0),
  165. sqlite3_column_name(stmt, 1), // text, nick
  166. sqlite3_column_text(stmt, 1),
  167. sqlite3_column_name(stmt, 2), // text, real
  168. sqlite3_column_text(stmt, 2),
  169. sqlite3_column_name(stmt, 3), // int, experiece
  170. sqlite3_column_int(stmt, 3),
  171. sqlite3_column_name(stmt, 4), // int, metal
  172. sqlite3_column_int(stmt, 4),
  173. sqlite3_column_name(stmt, 5), // int, fuel
  174. sqlite3_column_int(stmt, 5),
  175. sqlite3_column_name(stmt, 6), // int, guns
  176. sqlite3_column_int(stmt, 6),
  177. sqlite3_column_name(stmt, 7), // int, armors
  178. sqlite3_column_int(stmt, 7),
  179. sqlite3_column_name(stmt, 8), // int, shields
  180. sqlite3_column_int(stmt, 8),
  181. sqlite3_column_name(stmt, 9), // int, armorpoints
  182. sqlite3_column_int(stmt, 9),
  183. sqlite3_column_name(stmt, 10), // int, shieldpoints
  184. sqlite3_column_int(stmt, 10),
  185. sqlite3_column_name(stmt, 11), // int, hitpoints
  186. sqlite3_column_int(stmt, 11),
  187. sqlite3_column_name(stmt, 12), // int, shieldsup
  188. sqlite3_column_int(stmt, 12),
  189. sqlite3_column_name(stmt, 13), // int, laston
  190. sqlite3_column_int(stmt, 13)
  191. );
  192. sizeofdb += 1;
  193. } // Clean up database
  194. sqlite3_finalize(stmt);
  195. sqlite3_close(db);
  196. return sizeofdb;
  197. }
  198. int check_lock() {
  199. // Checks stats of lock, (1 = Lock is in effect, 0 = No Lock established)
  200. struct stat s;
  201. if (stat("lock.flg", &s) == 0) {
  202. return 1;
  203. } else {
  204. return 0;
  205. }
  206. }
  207. void grab_lock() {
  208. // Attempt to grab lock
  209. int valid = check_lock();
  210. FILE *fhandle;
  211. if (valid == 0) {
  212. fhandle = fopen("lock.flg", "w");
  213. if(!fhandle) {
  214. dolog("E: Unable to make lock.flg!");
  215. fprintf(stderr, "Unable to establish lock!\r\n");
  216. md_exit(-1);
  217. }
  218. fprintf(fhandle, "I am in use already!\n");
  219. fclose(fhandle);
  220. } else {
  221. dolog("W: Lock already established!");
  222. }
  223. }
  224. void rel_lock() {
  225. // Attempt to release lock
  226. int valid = check_lock();
  227. if (valid == 1) {
  228. if (unlink("lock.flg") != 0) {
  229. dolog("E: Unable to release lock.flg!");
  230. fprintf(stderr, "Unable to release lock!\r\n");
  231. md_exit(-1);
  232. }
  233. } else {
  234. dolog("W: Lock already released!");
  235. }
  236. }
  237. void log_drop() {
  238. // Spits out info from Drop File:
  239. md_printf("`white`First=%s Last=%s Alias=%s TimeLeft=%d SecLevel=%d Location=%s Node=%d Socket=%d Sysop=%s\r\n",
  240. mdcontrol.user_firstname,
  241. mdcontrol.user_lastname,
  242. mdcontrol.user_alias,
  243. mdcontrol.user_timeleft,
  244. mdcontrol.user_seclevel,
  245. mdcontrol.user_location,
  246. mdcontrol.node,
  247. mdcontrol.socket,
  248. mdcontrol.sysop_name
  249. );
  250. }
  251. void paws() {
  252. // Aaah, DRY
  253. md_printf("`white`Press any key to continue...");
  254. md_getc();
  255. md_printf("\r\n");
  256. }
  257. int yesNo() {
  258. char ch;
  259. int done = 0;
  260. md_printf("`bright black`(`bright white`Y`bright black`)`white`es `bright black`(`bright red`N`bright black`)`red`o");
  261. ch = md_get_answer("YyNn\r");
  262. md_printf("\r\n");
  263. switch(tolower(ch)) {
  264. case 'y':
  265. done = 1;
  266. break;
  267. case '\r':
  268. case 'n':
  269. break;
  270. }
  271. return done;
  272. }
  273. int randrange(int min, int max){
  274. return min + rand() / (RAND_MAX / (max - min + 1) + 1);
  275. }
  276. int locate_player(char first[], char last[]) {
  277. // returns user id for given real name and 0 for no record found
  278. sqlite3 *db;
  279. sqlite3_stmt *stmt;
  280. char sqlbuffer[256];
  281. int result = 0;
  282. char name[256];
  283. int rc = sqlite3_open("spaceconstruct.db3", &db);
  284. if(rc) { // Did we do a successful open?
  285. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  286. sqlite3_close(db);
  287. md_exit(-1);
  288. }
  289. sqlite3_busy_timeout(db, 5000);
  290. // Form Real Name
  291. strcpy(name, "");
  292. strcat(name, first);
  293. strcat(name, " ");
  294. strcat(name, last);
  295. //md_printf("realname = '%s'\r\n", name);
  296. // Locating user with given name
  297. strcpy(sqlbuffer, "SELECT * from user where real=? COLLATE NOCASE;");
  298. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  299. sqlite3_bind_text(stmt, 1, name, strlen(name), SQLITE_STATIC);
  300. rc = sqlite3_step(stmt);
  301. if (rc == SQLITE_ROW) {
  302. //dolog("User=%s uid=%d", name, sqlite3_column_int(stmt, 0));
  303. result = sqlite3_column_int(stmt, 0);
  304. } else {
  305. dolog("W: Unable to locate user=%s", name);
  306. result = 0;
  307. } // Clean Up, return results
  308. sqlite3_finalize(stmt);
  309. sqlite3_close(db);
  310. return result;
  311. }
  312. user_inf load_player(int uuid) {
  313. // Returns a player Structure from database
  314. sqlite3 *db;
  315. sqlite3_stmt *stmt;
  316. char sqlbuffer[256];
  317. user_inf result;
  318. int rc = sqlite3_open("spaceconstruct.db3", &db);
  319. if(rc) { // Did we do a successful open?
  320. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  321. sqlite3_close(db);
  322. md_exit(-1);
  323. }
  324. sqlite3_busy_timeout(db, 5000);
  325. strcpy(sqlbuffer, "SELECT * FROM user WHERE uid=?;");
  326. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  327. sqlite3_bind_int(stmt, 1, uuid);
  328. rc = sqlite3_step(stmt);
  329. if (rc == SQLITE_ROW) {
  330. result.uid = sqlite3_column_int(stmt, 0);
  331. strcpy(result.nick, sqlite3_column_text(stmt, 1));
  332. strcpy(result.real, sqlite3_column_text(stmt, 2));
  333. result.experience = sqlite3_column_int(stmt, 3);
  334. result.metal = sqlite3_column_int(stmt, 4);
  335. result.fuel = sqlite3_column_int(stmt, 5);
  336. result.guns = sqlite3_column_int(stmt, 6);
  337. result.armors = sqlite3_column_int(stmt, 7);
  338. result.shields = sqlite3_column_int(stmt, 8);
  339. result.armorpoints = sqlite3_column_int(stmt, 9);
  340. result.shieldpoints = sqlite3_column_int(stmt, 10);
  341. result.hitpoints = sqlite3_column_int(stmt, 11);
  342. result.shieldsup = sqlite3_column_int(stmt, 12);
  343. result.laston = sqlite3_column_int(stmt, 13);
  344. } else {
  345. //dolog("E: Unable to locate user with id=%d got %s (%d)", uuid, sqlite3_errmsg(db), rc);
  346. sqlite3_finalize(stmt);
  347. sqlite3_close(db);
  348. result.uid = 0;
  349. return result;
  350. }
  351. sqlite3_finalize(stmt);
  352. sqlite3_close(db);
  353. return result;
  354. }
  355. void update_player(user_inf data) {
  356. sqlite3 *db;
  357. sqlite3_stmt *stmt;
  358. char sqlbuffer[1024];
  359. int rc = sqlite3_open("spaceconstruct.db3", &db);
  360. if(rc) { // Did we do a successful open?
  361. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  362. sqlite3_close(db);
  363. md_exit(-1);
  364. }
  365. sqlite3_busy_timeout(db, 5000);
  366. // Bad, don't do this... opens to SQL injection!
  367. //snprintf(sqlbuffer, 1024, "UPDATE user SET nick = '%s', experience = %d, metal = %d, fuel = %d, guns = %d, armors = %d, shields = %d, armorpoints = %d, shieldpoints = %d, hitpoints = %d WHERE uid=%d;",
  368. // data.nick, data.experience, data.metal, data.fuel, data.guns, data.armors, data.shields, data.armorpoints, data.shieldpoints, data.hitpoints, data.uid);
  369. strcpy(sqlbuffer, "UPDATE user SET nick=?, experience=?, metal=?, fuel=?, guns=?, armors=?, shields=?, armorpoints=?, shieldpoints=?, hitpoints=?, shieldsup=?, laston=? WHERE uid=?;");
  370. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  371. // Bind All data values
  372. sqlite3_bind_text(stmt, 1, data.nick, strlen(data.nick), SQLITE_STATIC);
  373. sqlite3_bind_int(stmt, 2, data.experience);
  374. sqlite3_bind_int(stmt, 3, data.metal);
  375. sqlite3_bind_int(stmt, 4, data.fuel);
  376. sqlite3_bind_int(stmt, 5, data.guns);
  377. sqlite3_bind_int(stmt, 6, data.armors);
  378. sqlite3_bind_int(stmt, 7, data.shields);
  379. sqlite3_bind_int(stmt, 8, data.armorpoints);
  380. sqlite3_bind_int(stmt, 9, data.shieldpoints);
  381. sqlite3_bind_int(stmt, 10, data.hitpoints);
  382. sqlite3_bind_int(stmt, 11, data.shieldsup);
  383. sqlite3_bind_int(stmt, 12, data.laston);
  384. sqlite3_bind_int(stmt, 13, data.uid);
  385. // Execute
  386. rc = sqlite3_step(stmt);
  387. if(rc != SQLITE_DONE) {
  388. dolog("E: failed updating player=%d got error %s (%d)", data.uid, sqlite3_errmsg(db), rc);
  389. sqlite3_finalize(stmt);
  390. sqlite3_close(db);
  391. md_exit(-1);
  392. }
  393. sqlite3_finalize(stmt);
  394. sqlite3_close(db);
  395. }
  396. int create_player(user_inf data) {
  397. sqlite3 *db;
  398. sqlite3_stmt *stmt;
  399. char sqlbuffer[1024];
  400. int rc = sqlite3_open("spaceconstruct.db3", &db);
  401. if(rc) { // Did we do a successful open?
  402. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  403. sqlite3_close(db);
  404. md_exit(-1);
  405. }
  406. sqlite3_busy_timeout(db, 5000);
  407. strcpy(sqlbuffer, "INSERT INTO user (nick, real, experience, metal, fuel, guns, armors, shields, armorpoints, shieldpoints, hitpoints, shieldsup, laston) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
  408. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  409. sqlite3_bind_text(stmt, 1, data.nick, strlen(data.nick), SQLITE_STATIC);
  410. sqlite3_bind_text(stmt, 2, data.real, strlen(data.real), SQLITE_STATIC);
  411. sqlite3_bind_int(stmt, 3, data.experience);
  412. sqlite3_bind_int(stmt, 4, data.metal);
  413. sqlite3_bind_int(stmt, 5, data.fuel);
  414. sqlite3_bind_int(stmt, 6, data.guns);
  415. sqlite3_bind_int(stmt, 7, data.armors);
  416. sqlite3_bind_int(stmt, 8, data.shields);
  417. sqlite3_bind_int(stmt, 9, data.armorpoints);
  418. sqlite3_bind_int(stmt, 10, data.shieldpoints);
  419. sqlite3_bind_int(stmt, 11, data.hitpoints);
  420. sqlite3_bind_int(stmt, 12, data.shieldsup);
  421. sqlite3_bind_int(stmt, 13, data.laston);
  422. rc = sqlite3_step(stmt);
  423. if(rc != SQLITE_DONE) {
  424. dolog("E: failed inserting player=%d got error %s (%d)", data.nick, sqlite3_errmsg(db), rc);
  425. sqlite3_finalize(stmt);
  426. sqlite3_close(db);
  427. md_exit(-1);
  428. }
  429. sqlite3_finalize(stmt);
  430. sqlite3_close(db);
  431. return 1; // Good
  432. }
  433. void delete_player(int uuid) {
  434. sqlite3 *db;
  435. sqlite3_stmt *stmt;
  436. char sqlbuffer[256];
  437. int rc = sqlite3_open("spaceconstruct.db3", &db);
  438. if(rc) { // Did we do a successful open?
  439. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  440. sqlite3_close(db);
  441. md_exit(-1);
  442. }
  443. sqlite3_busy_timeout(db, 5000);
  444. strcpy(sqlbuffer, "DELETE FROM user WHERE uid=?;");
  445. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  446. sqlite3_bind_int(stmt, 1, uuid);
  447. rc = sqlite3_step(stmt);
  448. if (rc != SQLITE_DONE){
  449. dolog("E: Unable to locate user with id=%d got %s (%d)", uuid, sqlite3_errmsg(db), rc);
  450. sqlite3_finalize(stmt);
  451. sqlite3_close(db);
  452. md_exit(-1);
  453. }
  454. sqlite3_finalize(stmt);
  455. sqlite3_close(db);
  456. }
  457. void delete_players() {
  458. sqlite3 *db;
  459. sqlite3_stmt *stmt;
  460. char sqlbuffer[256];
  461. int rc = sqlite3_open("spaceconstruct.db3", &db);
  462. if(rc) { // Did we do a successful open?
  463. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  464. sqlite3_close(db);
  465. md_exit(-1);
  466. }
  467. sqlite3_busy_timeout(db, 5000);
  468. strcpy(sqlbuffer, "DELETE FROM user;");
  469. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  470. rc = sqlite3_step(stmt);
  471. if (rc != SQLITE_DONE){
  472. dolog("E: Delete_players 1/2, Unexpected error got %s (%d)", sqlite3_errmsg(db), rc);
  473. sqlite3_finalize(stmt);
  474. sqlite3_close(db);
  475. md_exit(-1);
  476. }
  477. strcpy(sqlbuffer, "UPDATE sqlite_sequence SET seq = 0 WHERE name='user';");
  478. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  479. rc = sqlite3_step(stmt);
  480. if (rc != SQLITE_DONE){
  481. dolog("E: Delete_players 2/2, Unexpected error got %s (%d)", sqlite3_errmsg(db), rc);
  482. sqlite3_finalize(stmt);
  483. sqlite3_close(db);
  484. md_exit(-1);
  485. }
  486. sqlite3_finalize(stmt);
  487. sqlite3_close(db);
  488. }
  489. int display_all_players() {
  490. sqlite3 *db;
  491. sqlite3_stmt *stmt;
  492. char sqlbuffer[1024];
  493. int rc = sqlite3_open("spaceconstruct.db3", &db);
  494. if(rc) { // Did we do a successful open?
  495. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  496. sqlite3_close(db);
  497. md_exit(1);
  498. }
  499. sqlite3_busy_timeout(db, 5000);
  500. strcpy(sqlbuffer, "SELECT * from user ORDER BY experience DESC, nick DESC;");
  501. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  502. int total = 0;
  503. md_printf("`bright green`___ The Players ___\r\n");
  504. while(sqlite3_step(stmt) == SQLITE_ROW) {
  505. md_printf("`bright white` %s (%d)\r\n", sqlite3_column_text(stmt, 1), sqlite3_column_int(stmt, 3));
  506. total += 1;
  507. }
  508. if(total > 1) {
  509. md_printf("`green`There are a total of `bright green`%d`green` players\r\n", total);
  510. } else {
  511. md_printf("`green`There are a total of `bright green`%d`green` player\r\n", total);
  512. }
  513. sqlite3_finalize(stmt);
  514. sqlite3_close(db);
  515. return total;
  516. }
  517. user_inf build_menu(user_inf my) {
  518. int done = 0;
  519. int didUp = 0;
  520. char ch;
  521. if(my.metal != 0) {
  522. while(!done) {
  523. md_clr_scr();
  524. md_sendfile("ansis/sc_build.ans", FALSE);
  525. md_printf("`bright yellow`You have %d metal\r\n", my.metal);
  526. ch = md_get_answer("GgAaSsCcRr\r");
  527. switch(tolower(ch)) {
  528. case 'g':
  529. if(my.metal >= (4 * (my.guns + 1))) {
  530. my.metal -= (4 * (my.guns + 1));
  531. my.guns += 1;
  532. my.hitpoints += 1;
  533. didUp = 1; // We need to save
  534. md_printf("`bright green`Added a new gun!\r\n`white`");
  535. paws();
  536. } else {
  537. md_printf("`bright red`You don't have the %d metal for this!\r\n`white`", (4 * (my.guns + 1)));
  538. paws();
  539. }
  540. break;
  541. case 'a':
  542. if(my.metal >= (3 * (my.armors + 1))) {
  543. my.metal -= (3 * (my.armors + 1));
  544. my.armors += 1;
  545. my.hitpoints += 1;
  546. my.armorpoints += 8;
  547. didUp = 1; // We need to save
  548. md_printf("`bright green`Added a new armor!\r\n`white`");
  549. paws();
  550. } else {
  551. md_printf("`bright red`You don't have the %d metal for this!\r\n`white`", (3 * (my.armors + 1)));
  552. paws();
  553. }
  554. break;
  555. case 's':
  556. if(my.metal >= (6 * (my.shields + 1))) {
  557. my.metal -= (6 * (my.shields + 1));
  558. my.shields += 1;
  559. my.hitpoints += 1;
  560. if(my.shieldsup == 0) {
  561. my.shieldpoints += 3;
  562. } else {
  563. my.shieldpoints += 1;
  564. }
  565. didUp = 1; // We need to save
  566. md_printf("`bright green`Added a new shield!\r\n`white`");
  567. paws();
  568. } else {
  569. md_printf("`bright red`You don't have the %d metal for this!\r\n`white`", (6 * (my.shields + 1)));
  570. paws();
  571. }
  572. break;
  573. case '\r':
  574. case 'c':
  575. done = 1;
  576. break;
  577. case 'r':
  578. if(my.experience > 25) {
  579. if(my.metal > 25) {
  580. my.experience -= 25;
  581. my.metal -= 25;
  582. my.fuel += 5;
  583. didUp = 1;
  584. md_printf("`bright green`Made 5 fuel using 25 experience and 25 metal!\r\n");
  585. paws();
  586. } else {
  587. md_printf("`bright red`You need 25 experience and 25 metal to make fuel!\r\n");
  588. paws();
  589. }
  590. } else {
  591. md_printf("`bright red`You need 25 experience and 25 metal to make fuel!\r\n");
  592. paws();
  593. }
  594. break;
  595. }
  596. }
  597. if(didUp) {
  598. update_player(my);
  599. }
  600. return my;
  601. } else {
  602. return my;
  603. }
  604. }
  605. user_inf combat_menu(user_inf my, int targ_hp, int targ_dmg, int asteroid) {
  606. int done = 0;
  607. int playerWon = 0; // Did the player win? (1 = Yes, 2 = No, 3 = Flee)
  608. char ch;
  609. int armorRep = 0; // Can armor be repaired?
  610. int temp = 0;
  611. int temp1 = 0;
  612. int temp2 = 0;
  613. int temp_hp = targ_hp;
  614. int flee = 0;
  615. int fire = 0;
  616. int regen = 0;
  617. while(!done) {
  618. if(asteroid == 0) {
  619. md_printf("`bright white`Pirate has %d hitpoints left\r\n", targ_hp);
  620. } else if(asteroid == 1) {
  621. md_printf("`bright white`Asteroid has %d hitpoints left\r\n", targ_hp);
  622. } else if(asteroid == 2) {
  623. md_printf("`bright white`Unknown One has %d hitpoints left\r\n", targ_hp);
  624. }
  625. // Proccess Shields comming back online
  626. if(my.shieldsup != 0) {
  627. my.shieldsup -= 1;
  628. }
  629. // Proccess Crew repairing hull
  630. if(my.hitpoints < (my.guns + my.armors + my.shields + 2)) {
  631. regen = rand() % 100;
  632. if(regen >= 90) {
  633. my.hitpoints += 1;
  634. if(my.hitpoints > (my.guns + my.armors + my.shields + 2)) {
  635. my.hitpoints = (my.guns + my.armors + my.shields + 2);
  636. }
  637. md_printf("`bright green`Your crew repaired your hull for 1 point!\r\n");
  638. }
  639. }
  640. if(my.shieldsup == 0) {
  641. // Proccess Shield Regeneration
  642. if(my.shieldpoints < (my.shields * 3)) {
  643. regen = rand() % 100;
  644. if(regen >= 70) {
  645. my.shieldpoints += (my.shields + 1);
  646. if(my.shieldpoints > (my.shields * 3)) {
  647. my.shieldpoints = (my.shields * 3);
  648. }
  649. md_printf("`bright green`Your shields regenerate %d points!\r\n", my.shields);
  650. }
  651. }
  652. md_printf("`bright white`Your ship has `bright blue`%d`bright white` shields, %d armor and %d hitpoints left:\r\n", my.shieldpoints, my.armorpoints, my.hitpoints);
  653. } else {
  654. // Shields should still regenerate some
  655. if(my.shieldpoints < (my.shields * 3)) {
  656. regen = rand() % 100;
  657. if(regen >= 70) {
  658. my.shieldpoints += 1;
  659. if(my.shieldpoints > (my.shields * 3)) {
  660. my.shieldpoints = (my.shields * 3);
  661. }
  662. md_printf("`bright green`Your shields regenerate 1 points!\r\n");
  663. }
  664. }
  665. md_printf("`bright white`Your ship has `blue`%d`bright white` shields, %d armor and %d hitpoints left:\r\n", my.shieldpoints, my.armorpoints, my.hitpoints);
  666. }
  667. // Forming Combat Menu
  668. md_printf("`bright black`(`bright white`A`bright black`)`white`ttack\r\n");
  669. if(my.armorpoints < (my.armors * 8)) {
  670. md_printf("`bright black`(`bright white`R`bright black`)`white`epair Armor\r\n");
  671. armorRep = 1; // Yes we can repair armor
  672. }
  673. md_printf("`bright black`(`bright white`F`bright black`)`white`lee\r\n");
  674. // Proccess User action
  675. ch = md_get_answer("AaRrFf\r");
  676. switch(tolower(ch)) {
  677. case '\r':
  678. case 'a':
  679. fire = rand() % 100;
  680. if(asteroid == 1) { // Its a rock so allow player to hit it more frequent
  681. fire += 15;
  682. }
  683. if(fire >= 50) {
  684. targ_hp -= my.guns;
  685. if(targ_hp <= 0) {
  686. md_printf("`bright red`Target Destroyed!\r\n");
  687. done = 1;
  688. playerWon = 1;
  689. } else {
  690. md_printf("`bright red`Target took %d damage!\r\n", my.guns);
  691. }
  692. } else {
  693. md_printf("`white`You Missed!\r\n");
  694. }
  695. break;
  696. case 'r':
  697. if(armorRep) {
  698. if(my.metal > 0) {
  699. my.metal -= 1;
  700. my.armorpoints += 2;
  701. if(my.armorpoints < (my.armors * 8)) {
  702. my.armorpoints = (my.armors * 8);
  703. }
  704. md_printf("`bright green`Repaired 2 Armor points!\r\n");
  705. } else {
  706. md_printf("`bright red`You need metal inorder to repair!\r\n");
  707. }
  708. } else {
  709. md_printf("`bright yellow`You don't seem to need repairs!\r\n");
  710. }
  711. break;
  712. case 'f':
  713. flee = rand() % 100;
  714. if(asteroid == 1) {
  715. flee = 100; // Always allow fleeing from asteroid
  716. } else if(asteroid == 2) {
  717. flee = 0; // Never allow fleeing from Unknown One
  718. }
  719. if(flee >= 82) {
  720. playerWon = 3;
  721. done = 1;
  722. md_printf("`bright green`You ran away!\r\n");
  723. } else {
  724. md_printf("`bright red`Target found you!\r\n");
  725. }
  726. break;
  727. } // If Enemy is alive proccess incomming attack
  728. if(targ_hp > 0) {
  729. temp = targ_dmg;
  730. fire = rand() % 100;
  731. if(asteroid == 1) { // Yup it doesn't have gunz cuz it's rock.
  732. fire = 0;
  733. } else if(asteroid == 2) { // This is the Unknown One
  734. fire += 5;
  735. }
  736. if(fire >= 40) {
  737. // Shields
  738. if(my.shieldsup == 0 && my.shieldpoints != 0) {
  739. if(temp >= my.shieldpoints) {
  740. // Damage will lower shields!
  741. my.shieldsup = (my.shields + 2);
  742. temp -= my.shieldpoints;
  743. my.shieldpoints = 0;
  744. md_printf("`bright blue`Shields absorbed %d damage and went offline!\r\n`white`", targ_dmg);
  745. } else {
  746. // Damage to shields but not lower them
  747. temp1 = temp;
  748. temp -= my.shieldpoints;
  749. my.shieldpoints -= temp1;
  750. temp1 = 0;
  751. md_printf("`bright blue`Shields absorbed %d damage!\r\n`white`", targ_dmg);
  752. }
  753. }
  754. // shields now armor
  755. if(temp > 0) {
  756. if(my.armorpoints != 0) {
  757. // We do have armor
  758. if(temp >= my.armorpoints) {
  759. // Damage will destroy armor!
  760. temp2 = temp;
  761. temp -= my.armorpoints;
  762. my.armorpoints = 0;
  763. md_printf("`yellow`Armor took %d damage and broke!\r\n`white`", temp2);
  764. } else {
  765. // Nope armor is good
  766. temp1 = temp;
  767. temp2 = temp;
  768. temp -= my.armorpoints;
  769. my.armorpoints -= temp1;
  770. temp1 = 0;
  771. md_printf("`bright yellow`Armor took %d damage!\r\n`white`", temp2);
  772. }
  773. }
  774. }
  775. // armor now hull
  776. if(temp > 0) {
  777. if(temp < my.hitpoints) {
  778. temp1 = temp;
  779. temp -= my.hitpoints; // should be 0
  780. my.hitpoints -= temp1;
  781. md_printf("`bright red`Hull took %d damage!\r\n", temp1);
  782. } else {
  783. playerWon = 2; // Nope the target did us in
  784. done = 1;
  785. my.hitpoints = 0;
  786. md_printf("`red`Hull took %d damage and broke!\r\n", temp);
  787. }
  788. }
  789. }
  790. // firing
  791. }
  792. // Winning move?
  793. if(playerWon) {
  794. if(playerWon == 1) {
  795. // Player won
  796. my.experience += (temp_hp + targ_dmg);
  797. my.metal += temp_hp;
  798. if(asteroid == 1) { // If asteroid reward more experience and metal!
  799. my.experience += 2;
  800. my.metal += 2;
  801. } else if(asteroid == 2) { // If it was the boss!
  802. my.experience += 8;
  803. my.metal += 8;
  804. }
  805. my.fuel -= 2;
  806. if(my.fuel < 0) {
  807. my.fuel = 0;
  808. }
  809. md_printf("`bright green`Congrats on your victory!\r\n`white`");
  810. } else if(playerWon == 2) {
  811. // Target won
  812. my.experience -= (temp_hp + targ_dmg);
  813. my.metal -= temp_hp;
  814. my.fuel -= 3;
  815. if(my.experience < 0) {
  816. my.experience = 0;
  817. }
  818. if(my.metal < 0) {
  819. my.metal = 0;
  820. }
  821. if(my.fuel < 0) {
  822. my.fuel = 0;
  823. }
  824. md_printf("`bright red`Sorry for your loss! (We rebuild your ship)\r\n");
  825. my.hitpoints = 4;
  826. my.armorpoints = 8;
  827. my.shieldpoints = 0;
  828. my.shieldsup = 0;
  829. my.armors = 1;
  830. my.shields = 0;
  831. my.guns = 1;
  832. } else if(playerWon == 3) {
  833. // Chicken!
  834. my.fuel -= 1;
  835. }
  836. }
  837. }
  838. update_player(my);
  839. return my;
  840. }
  841. void about_game() {
  842. int done = 0;
  843. int page = 1;
  844. char p[12];
  845. char targ[256];
  846. while(!done) {
  847. switch(page) {
  848. case 1:
  849. md_clr_scr();
  850. md_sendfile("ansis/sc_doc_1.ans", FALSE);
  851. paws();
  852. break;
  853. case 2:
  854. md_clr_scr();
  855. md_sendfile("ansis/sc_doc_2.ans", FALSE);
  856. paws();
  857. break;
  858. case 3:
  859. md_clr_scr();
  860. md_sendfile("ansis/sc_doc_3.ans", FALSE);
  861. paws();
  862. break;
  863. case 4:
  864. md_clr_scr();
  865. md_sendfile("ansis/sc_doc_4.ans", FALSE);
  866. paws();
  867. break;
  868. case 5:
  869. done = 1;
  870. break;
  871. }
  872. md_clr_scr();
  873. page += 1;
  874. }
  875. }
  876. void play_game() {
  877. int done = 0;
  878. char ch;
  879. char ch1[256];
  880. user_inf myself;
  881. user_inf myself2;
  882. user_inf testing;
  883. int reset = 0;
  884. int doRefuel = 0;
  885. int pirate_encounter = 0;
  886. int pirate_hp = 0; // Reused for asteroid
  887. int pirate_dmg = 0; // Reused for asteroid
  888. int asteroid = 0; // 1 means yes we need to remove damage... completely.
  889. int me = locate_player(mdcontrol.user_firstname, mdcontrol.user_lastname);
  890. if(me != 0) {
  891. //myself = load_player(me);
  892. testing = load_player(me);
  893. if(testing.uid != 0) {
  894. myself = load_player(me);
  895. }
  896. dT age = formDate(compareDate(myself.laston));
  897. // Process player inactivity
  898. if(age.age >= 180) { // 6 Months or 1/2 a Year
  899. me = 0;
  900. reset = 1;
  901. }
  902. // Processing fuel regeneration
  903. if(age.age != 0) {
  904. doRefuel = 1;
  905. }
  906. }
  907. if(me != 0) {
  908. md_printf("`bright white`Welcome back `bright green`%s\r\n", myself.nick);
  909. dT age = formDate(compareDate(myself.laston));
  910. dolog("%s is now playing (G=%d A=%d S=%d F=%d M=%d AP=%d SP=%d (%d) HP=%d LO=%d or %d days)",
  911. myself.nick, myself.guns, myself.armors, myself.shields, myself.fuel, myself.metal, myself.armorpoints,
  912. myself.shieldpoints, myself.shieldsup, myself.hitpoints, myself.laston, age.age);
  913. myself.laston = dateStamp();
  914. if(age.day != 0 || age.month != 0 || age.year != 0) {
  915. md_printf("`bright yellow`Haven't seen you for");
  916. }
  917. if(age.day != 0) {
  918. if(age.day > 1) {
  919. md_printf(" %d days", age.day);
  920. } else {
  921. md_printf(" %d day", age.day);
  922. }
  923. }
  924. if(age.month != 0) {
  925. if(age.month > 1) {
  926. md_printf(" %d months", age.month);
  927. } else {
  928. md_printf(" %d month", age.month);
  929. }
  930. }
  931. if(age.year != 0) {
  932. if(age.year > 1) {
  933. md_printf(" %d years", age.year);
  934. } else {
  935. md_printf(" %d year", age.year);
  936. }
  937. }
  938. if(age.day != 0 || age.month != 0 || age.year != 0) {
  939. md_printf("\r\n");
  940. }
  941. if(doRefuel) {
  942. if(myself.fuel != 0) {
  943. myself.fuel += (age.age * 2);
  944. } else {
  945. myself.fuel += ((age.age * 2) + 10);
  946. md_printf("`bright green`For using all your fuel last time you get bonus fuel today!\r\n");
  947. }
  948. md_printf("`bright green`You now have %d fuel\r\n", myself.fuel);
  949. }
  950. update_player(myself);
  951. paws();
  952. } else {
  953. md_printf("`bright white`You look new here. (Hit Enter to use your alias)\r\n");
  954. while(done == 0) {
  955. md_printf("`bright yellow`What's your name: ");
  956. md_getstring(ch1, 26, 32, 126);
  957. md_printf("\r\n");
  958. if(strlen(ch1) > 0) {
  959. //md_getstring(ch1, 256, 32, 126);
  960. strcpy(myself.nick, ch1);
  961. } else {
  962. strcpy(myself.nick, mdcontrol.user_alias);
  963. }
  964. md_printf("`bright white`Are you sure you want to be called `bright green`%s\r\n");
  965. done = yesNo();
  966. if(done) {
  967. if(reset) { // So we are reseting the user, this wipes all values and uses update instead of create
  968. myself.experience = 0;
  969. myself.metal = 0;
  970. myself.fuel = 10;
  971. myself.guns = 1;
  972. myself.armors = 1;
  973. myself.shields = 0;
  974. myself.armorpoints = 8;
  975. myself.shieldpoints = 0;
  976. myself.hitpoints = 4;
  977. myself.shieldsup = 0;
  978. myself.laston = dateStamp();
  979. update_player(myself);
  980. } else { // Brand new user
  981. char name[256];
  982. strcpy(myself2.nick, myself.nick);
  983. strcpy(name, "");
  984. strcat(name, mdcontrol.user_firstname);
  985. strcat(name, " ");
  986. strcat(name, mdcontrol.user_lastname);
  987. strcpy(myself2.real, name);
  988. myself2.experience = 0;
  989. myself2.metal = 0;
  990. myself2.fuel = 10;
  991. myself2.guns = 1;
  992. myself2.armors = 1;
  993. myself2.shields = 0;
  994. myself2.armorpoints = 8;
  995. myself2.shieldpoints = 0;
  996. myself2.hitpoints = 4;
  997. myself2.shieldsup = 0;
  998. myself2.laston = dateStamp();
  999. create_player(myself2);
  1000. myself = load_player(locate_player(mdcontrol.user_firstname, mdcontrol.user_lastname));
  1001. }
  1002. }
  1003. }
  1004. done = 0; // Reset this for our next loop.
  1005. }
  1006. // Ok user is here now lets ask what they want to do... build/attack
  1007. md_clr_scr();
  1008. while(!done) {
  1009. md_printf("`bright yellow`You have %d fuel left for today,\r\n", myself.fuel);
  1010. if(myself.shieldsup == 0) {
  1011. md_printf("`bright yellow`Your ship has `bright blue`%d`bright yellow` shields, %d armor and %d hitpoints left:\r\n", myself.shieldpoints, myself.armorpoints, myself.hitpoints);
  1012. } else {
  1013. md_printf("`bright yellow`Your ship has %d armor, `blue`%d`bright yellow` shields and %d hitpoints left:\r\n", myself.armorpoints, myself.shieldpoints, myself.hitpoints);
  1014. }
  1015. if(myself.armorpoints < (myself.armors * 8)) {
  1016. md_printf("`bright black`(`bright white`R`bright black`)`white`epair Armor\r\n");
  1017. }
  1018. if(myself.shieldpoints < (myself.shields * 3)) {
  1019. md_printf("`bright black`(`bright white`G`bright black`)`white`enerate Shields\r\n");
  1020. }
  1021. if(myself.fuel >= 2) {
  1022. md_sendfile("ansis/sc_gameh.ans", FALSE);
  1023. ch = md_get_answer("SsHhBbQqRrGgLl\r");
  1024. } else {
  1025. md_sendfile("ansis/sc_gamel.ans", FALSE);
  1026. ch = md_get_answer("BbQqRrGgLl\r");
  1027. }
  1028. md_clr_scr();
  1029. switch(tolower(ch)) {
  1030. case '\r':
  1031. case 's':
  1032. if(myself.fuel >= 2) {
  1033. pirate_encounter = rand() % 100;
  1034. if(pirate_encounter >= 75) { // 25% chance to encounter a Pirate
  1035. if(myself.experience > 5000) {
  1036. if(pirate_encounter >= 95) { // 5% chance to encounter unknown one
  1037. md_printf("`bright yellow`You encounter the `bright red`Unknown One\r\n`white`");
  1038. asteroid = 2;
  1039. } else {
  1040. md_printf("`bright yellow`You encounter a `bright red`Pirate\r\n`white`");
  1041. asteroid = 0;
  1042. }
  1043. }
  1044. } else { // 75% chance to encounter a Asteroid
  1045. md_printf("`bright yellow`You encounter a `bright red`Asteroid\r\n`white`");
  1046. asteroid = 1;
  1047. }
  1048. if(asteroid == 0 || asteroid == 2) {
  1049. pirate_hp = ((myself.hitpoints + 1) + (rand() % 4));
  1050. if(asteroid == 2) {
  1051. pirate_hp += 10;
  1052. }
  1053. // Increase difficulty based on experience...
  1054. pirate_dmg = (myself.experience / 500);
  1055. if(pirate_dmg == 0) { // Always deal damage
  1056. pirate_dmg = 1;
  1057. }
  1058. if(asteroid == 2) { // Increase boss damage
  1059. pirate_dmg += 2;
  1060. }
  1061. // Send off to combat menu
  1062. myself = combat_menu(myself, pirate_hp, pirate_dmg, asteroid);
  1063. } else {
  1064. pirate_hp = (myself.hitpoints + 1) + (rand() % 10);
  1065. // Send off to combat menu
  1066. myself = combat_menu(myself, pirate_hp, 0, asteroid);
  1067. }
  1068. }
  1069. break;
  1070. case 'h':
  1071. md_printf("`bright white`Comming v0.4-dev\r\n");
  1072. break;
  1073. case 'b':
  1074. if(myself.metal != 0) {
  1075. // Send off to build menu
  1076. md_clr_scr();
  1077. //md_sendfile("ansis/sc_build.ans", FALSE);
  1078. myself = build_menu(myself);
  1079. } else {
  1080. md_printf("`bright white`I am sorry you have no metal go fight.`white`\r\n");
  1081. }
  1082. break;
  1083. case 'q':
  1084. done = 1;
  1085. break;
  1086. case 'r':
  1087. if(myself.armorpoints < (myself.armors * 8)) {
  1088. if(myself.metal > 0) {
  1089. myself.metal -= 1;
  1090. myself.armorpoints += 4;
  1091. if(myself.armorpoints > (myself.armors * 8)) {
  1092. myself.armorpoints = (myself.armors * 8);
  1093. }
  1094. md_printf("`bright green`Repaired 4 Armor Points!\r\n`white`");
  1095. } else {
  1096. md_printf("`bright red`You need metal inorder to repair!\r\n`white`");
  1097. }
  1098. } else {
  1099. md_printf("`bright yellow`You don't seem to need repairs!\r\n`white`");
  1100. }
  1101. break;
  1102. case 'g':
  1103. if(myself.shieldpoints < (myself.shields * 3)) {
  1104. myself.shieldpoints = (myself.shields * 3);
  1105. md_printf("`bright green`Regenerated Shields!\r\n`white`");
  1106. } else {
  1107. md_printf("`bright yellow`You don't seem to need to regenerate shields!\r\n`white`");
  1108. }
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. void main_menu() {
  1114. // Main menu for once things are done being initalized
  1115. int done = 0;
  1116. int done1 = 0; // For Reset Game and for New Day... also for Debug
  1117. int targ_ply = 1; // Target player to perform op!
  1118. int targ_max = 0; // Total number of players affected!
  1119. user_inf targ; // Player Struct
  1120. dT targ_age; // Date Struct
  1121. char targ_real[256];
  1122. char ch;
  1123. // Check Lock
  1124. inuse = check_lock();
  1125. // 0 means we got the lock first, 1 means someone else has it already!
  1126. // Lock game
  1127. if (inuse == 0) {
  1128. grab_lock();
  1129. }
  1130. while(!done) {
  1131. md_clr_scr();
  1132. if(mdcontrol.user_seclevel == 99 || mdcontrol.user_seclevel == 255) {
  1133. md_sendfile("ansis/sc_mainh.ans", TRUE);
  1134. } else {
  1135. md_sendfile("ansis/sc_mainl.ans", TRUE);
  1136. }
  1137. ch = md_get_answer("PpLlVvQqRrNnSsAa\r");
  1138. md_clr_scr();
  1139. switch(tolower(ch)){
  1140. case 'q':
  1141. // Quit
  1142. done = 1;
  1143. break;
  1144. case '\r':
  1145. case 'p':
  1146. // Play Game (Default if the player just hits enter)
  1147. if (inuse == 1) {
  1148. md_printf("`bright red`______________\r\n");
  1149. md_printf("`bright red` Game in use!\r\n");
  1150. md_printf("`bright red`______________\r\n");
  1151. paws();
  1152. } else {
  1153. //md_printf("`bright white`Play\r\n");
  1154. play_game();
  1155. }
  1156. break;
  1157. case 'a':
  1158. about_game();
  1159. break;
  1160. case 'l':
  1161. // List Players in the game
  1162. display_all_players();
  1163. paws();
  1164. break;
  1165. case 'v':
  1166. // Version
  1167. md_clr_scr();
  1168. md_printf("`bright yellow`_____________________________\r\n");
  1169. md_printf("`bright yellow` Space Construct v%d.%d-%s\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_TYPE);
  1170. md_printf("`bright yellow` By: Beanzilla\r\n");
  1171. md_printf("`bright yellow` FsxNet 21:4/110\r\n");
  1172. md_printf("`bright yellow` HappyNet 637:1/110\r\n");
  1173. md_printf("`bright yellow`_____________________________\r\n");
  1174. paws();
  1175. break;
  1176. case 'r':
  1177. if(mdcontrol.user_seclevel == 99 || mdcontrol.user_seclevel == 255) {
  1178. md_printf("`bright red`__ Reset ___\r\n");
  1179. md_printf("`bright red`DANGER THIS WILL DELETE ALL PLAYERS IN GAME\r\n");
  1180. if(yesNo() == 0) {
  1181. md_printf("`bright red`ABORTED!\r\n");
  1182. paws();
  1183. break;
  1184. }
  1185. dolog("%s issued reset", mdcontrol.user_alias);
  1186. delete_players(); // Delete all players in user table
  1187. } else {
  1188. md_printf("`bright red`Not Authorized, Contact System Operator if you feel this was in error!\r\n");
  1189. }
  1190. paws();
  1191. break;
  1192. case 's':
  1193. if(mdcontrol.user_seclevel == 99 || mdcontrol.user_seclevel == 255) {
  1194. md_printf("`bright yellow`__ Show Debug ___\r\n");
  1195. while(!done1) {
  1196. targ = load_player(targ_ply);
  1197. if(targ.uid != 0) {
  1198. targ_age = formDate(compareDate(targ.laston));
  1199. md_printf("* %s has %d guns, %d armor, %d shields\r\n", targ.nick, targ.guns, targ.armors, targ.shields);
  1200. md_printf(" %d armorpoints, %d shieldpoints, %d hitpoints\r\n", targ.armorpoints, targ.shieldpoints, targ.hitpoints);
  1201. md_printf(" %d laston (raw), %d laston (days), %d shieldsup\r\n", targ.laston, targ_age.age, targ.shieldsup);
  1202. md_printf(" %d fuel, %d metal, %d experience\r\n\r\n", targ.fuel, targ.metal, targ.experience);
  1203. } else {
  1204. done1 = 1;
  1205. }
  1206. targ_ply += 1;
  1207. targ_max += 1;
  1208. }
  1209. md_printf("`bright yellow`%d players\r\n", (targ_max - 1));
  1210. targ_ply = 1;
  1211. targ_max = 0;
  1212. done1 = 0;
  1213. } else {
  1214. md_printf("`bright yellow`Not Authorized, Contact System Operator if you feel this was in error!\r\n");
  1215. }
  1216. paws();
  1217. break;
  1218. case 'n':
  1219. if(mdcontrol.user_seclevel == 99 || mdcontrol.user_seclevel == 255) {
  1220. md_printf("`bright yellow`__ New Day ___\r\n");
  1221. dolog("%s issued new day", mdcontrol.user_alias);
  1222. while(!done1) {
  1223. targ = load_player(targ_ply);
  1224. if(targ.uid != 0) {
  1225. targ.laston -= 1; // Travel back 1 day
  1226. update_player(targ); // Save
  1227. } else {
  1228. done1 = 1;
  1229. }
  1230. targ_ply += 1;
  1231. targ_max += 1;
  1232. }
  1233. md_printf("`bright yellow`%d players\r\n", (targ_max - 1));
  1234. targ_ply = 1;
  1235. targ_max = 0;
  1236. done1 = 0;
  1237. } else {
  1238. md_printf("`bright yellow`Not Authorized, Contact System Operator if you feel this was in error!\r\n");
  1239. }
  1240. paws();
  1241. break;
  1242. }
  1243. }
  1244. // Unlock game
  1245. if (inuse == 0) {
  1246. rel_lock();
  1247. }
  1248. }
  1249. int main(int argc, char **argv) {
  1250. int socket;
  1251. if (argc < 2) { // Not enough arguments
  1252. fprintf(stderr, "usage:\n%s (DROPFILE [SOCKET])\r\n", argv[0]);
  1253. dolog("E %s (DROPFILE [SOCKET])", argv[0]);
  1254. return 0;
  1255. }
  1256. if (argc > 2) { // If socket is found use socket
  1257. if (argv[2][0] >= '0' && argv[2][0] <= '9') {
  1258. socket = strtol(argv[2], NULL, 10);
  1259. } else {
  1260. socket = -1;
  1261. }
  1262. } else {
  1263. socket = -1;
  1264. }
  1265. // Initiate MagiDoor
  1266. md_init(argv[1], socket);
  1267. md_clr_scr();
  1268. // Debug System
  1269. if (debug) {
  1270. dolog("=== Debug ===");
  1271. md_printf("`white`=== Debug ===\r\n");
  1272. md_printf("DropFile...\r\n");
  1273. log_drop();
  1274. int test = check_lock();
  1275. if (test == 0) {
  1276. md_printf("Lock is: Avalible\r\n");
  1277. md_printf("Database...\r\n");
  1278. db_test();
  1279. } else {
  1280. md_printf("Lock is: Taken\r\n");
  1281. md_printf("CANCELED Database Dump\r\n");
  1282. }
  1283. paws();
  1284. md_clr_scr();
  1285. } else {
  1286. dolog("--- Debug ---");
  1287. }
  1288. // Main Menu
  1289. main_menu();
  1290. // Goodbye
  1291. md_exit(0);
  1292. }