main.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. /*
  2. Space Construct v0.6-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 6
  8. #ifndef VERSION_TYPE
  9. #define VERSION_TYPE "dev"
  10. #endif
  11. #define PATH_MAX 256
  12. #define PATH_SEP "/"
  13. //#include <MagiDoor.h> // Sorry Apam... as much as I would like to keep using your stuff it's just not fully functional
  14. #include <OpenDoor.h> // Now using odoors for c++
  15. #include <sqlite3.h>
  16. #if defined(_MSC_VER) || defined(WIN32)
  17. #define snprintf _snprintf
  18. #define strcasecmp _stricmp
  19. #include <winsock2.h>
  20. #ifndef _MSC_VER
  21. #define _MSC_VER 1
  22. #endif
  23. #else
  24. #include <arpa/inet.h>
  25. #endif
  26. // Standard C
  27. #include <time.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <unistd.h> // stat
  36. #include <string>
  37. // sc.log ODoors has logging so why not use it!
  38. int inuse = 0; // Are any other copies of us running? (We are a single user door!)
  39. int debug = 0; // Are we in debug mode?
  40. // Please change it for your system!
  41. int allowDev = 1; // Allow "Beanzilla" to not need the password.
  42. std::string sysop_pass = "spaceISbig"; // Case sensitive!
  43. // LOGGING with file output
  44. #include "zf_log.h"
  45. FILE *g_log_file;
  46. static void file_output_callback(const zf_log_message *msg, void *arg) {
  47. (void)arg;
  48. *msg->p = '\n';
  49. fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
  50. fflush(g_log_file);
  51. }
  52. static void file_output_close(void) { fclose(g_log_file); }
  53. static int file_output_open(const char *const log_path) {
  54. g_log_file = fopen(log_path, "a");
  55. if (!g_log_file) {
  56. ZF_LOGW("Failed to open log file %s", log_path);
  57. return 0;
  58. }
  59. atexit(file_output_close);
  60. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
  61. return 1;
  62. }
  63. void log_flush(void) { fflush(g_log_file); }
  64. // END LOGGING
  65. // Randrange
  66. int randrange(int min, int max){
  67. return min + rand() / (RAND_MAX / (max - min + 1) + 1);
  68. }
  69. // Moved to User class
  70. #include "user.h"
  71. // DateStamp Structure to provide date difference
  72. typedef struct dateTamp {
  73. int year; // YYYY, or number of years difference
  74. int month; // MM, or number of months difference
  75. int day; // DD, or number of days difference.
  76. int age; // Taking down to aproximate day count.
  77. } dT; // YYYYMMDD it in int form
  78. int dateStamp() {
  79. // YYYYMMDD \o/ In a INT so we can store it and compare against it!
  80. struct tm *time_now;
  81. time_t timen;
  82. int result = 0;
  83. timen = time(NULL);
  84. time_now = localtime(&timen);
  85. result += ((time_now->tm_year + 1900) * 10000);
  86. result += (time_now->tm_mon * 100) + 100;
  87. result += time_now->tm_mday;
  88. return result;
  89. }
  90. int compareDate(int dt) {
  91. // Returns integer of difference, from now.
  92. struct tm *time_now;
  93. time_t timen;
  94. int now = 0;
  95. timen = time(NULL);
  96. time_now = localtime(&timen);
  97. now += ((time_now->tm_year + 1900) * 10000);
  98. now += (time_now->tm_mon * 100) + 100;
  99. now += time_now->tm_mday;
  100. return (now - dt); // 10000 = 1 Year, 100 = 1 Month, 1 = 1 Day
  101. }
  102. dT fromDate(int diff) {
  103. // Reverse process so we can compare exact days or get the structure back out too.
  104. dT result;
  105. result.year = diff / 10000;
  106. result.month = (diff / 100) % 100;
  107. result.day = diff % 100;
  108. result.age = 0;
  109. // Process Aprox day count
  110. result.age += (365 * result.year);
  111. result.age += (30 * result.month);
  112. result.age += result.day;
  113. return result;
  114. }
  115. int db_test() {
  116. // Displays all users in the database
  117. sqlite3 *db;
  118. sqlite3_stmt *stmt;
  119. char sqlbuffer[256];
  120. char strbuffer[256];
  121. int sizeofdb = 0;
  122. int rc = sqlite3_open("spaceconstruct.db3", &db);
  123. if(rc) { // Did we do a successful open?
  124. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  125. sqlite3_close(db);
  126. od_exit(-1, FALSE);
  127. }
  128. sqlite3_busy_timeout(db, 5000);
  129. // DB open
  130. strcpy(sqlbuffer, "SELECT * FROM user;");
  131. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer), &stmt, NULL);
  132. while(sqlite3_step(stmt) == SQLITE_ROW) {
  133. ZF_LOGV("%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",
  134. sqlite3_column_name(stmt, 0), // int, uid
  135. sqlite3_column_int(stmt, 0),
  136. sqlite3_column_name(stmt, 1), // text, nick
  137. sqlite3_column_text(stmt, 1),
  138. sqlite3_column_name(stmt, 2), // text, real
  139. sqlite3_column_text(stmt, 2),
  140. sqlite3_column_name(stmt, 3), // int, experiece
  141. sqlite3_column_int(stmt, 3),
  142. sqlite3_column_name(stmt, 4), // int, metal
  143. sqlite3_column_int(stmt, 4),
  144. sqlite3_column_name(stmt, 5), // int, fuel
  145. sqlite3_column_int(stmt, 5),
  146. sqlite3_column_name(stmt, 6), // int, gun
  147. sqlite3_column_int(stmt, 6),
  148. sqlite3_column_name(stmt, 7), // int, armor
  149. sqlite3_column_int(stmt, 7),
  150. sqlite3_column_name(stmt, 8), // int, shield
  151. sqlite3_column_int(stmt, 8),
  152. sqlite3_column_name(stmt, 9), // int, armorpoints
  153. sqlite3_column_int(stmt, 9),
  154. sqlite3_column_name(stmt, 10), // int, shieldpoints
  155. sqlite3_column_int(stmt, 10),
  156. sqlite3_column_name(stmt, 11), // int, hitpoints
  157. sqlite3_column_int(stmt, 11),
  158. sqlite3_column_name(stmt, 12), // int, shieldsup
  159. sqlite3_column_int(stmt, 12),
  160. sqlite3_column_name(stmt, 13), // int, laston
  161. sqlite3_column_int(stmt, 13)
  162. );
  163. sizeofdb += 1;
  164. } // Clean up database
  165. ZF_LOGV("There are %d users", sizeofdb);
  166. sqlite3_finalize(stmt);
  167. sqlite3_close(db);
  168. return sizeofdb;
  169. }
  170. int check_lock() {
  171. // Checks stats of lock, (1 = Lock is in effect, 0 = No Lock established)
  172. struct stat s;
  173. if (stat("lock.flg", &s) == 0) {
  174. return 1;
  175. } else {
  176. return 0;
  177. }
  178. }
  179. int grab_lock() {
  180. // Attempt to grab lock, (0 = Already gotten, 1 = Success, -1 = Error)
  181. int valid = check_lock();
  182. FILE *fhandle;
  183. if (valid == 0) {
  184. fhandle = fopen("lock.flg", "w");
  185. if(!fhandle) {
  186. ZF_LOGF("Unable to make lock.flg!");
  187. ZF_LOGF("Something went wrong perhaps we don't have permissions?");
  188. return -1;
  189. }
  190. fprintf(fhandle, "I am in use already!\n");
  191. fclose(fhandle);
  192. ZF_LOGV("Lock Established");
  193. return 1;
  194. } else {
  195. ZF_LOGW("Lock already established!");
  196. return 0;
  197. }
  198. }
  199. void rel_lock() {
  200. // Attempt to release lock
  201. int valid = check_lock();
  202. if (valid == 1) {
  203. if (unlink("lock.flg") != 0) {
  204. ZF_LOGF("Unable to release lock.flg!");
  205. ZF_LOGF("Something went wrong! Players might not be able to play now!");
  206. }
  207. } else {
  208. ZF_LOGW("Lock already released!");
  209. }
  210. }
  211. void log_drop() {
  212. // Spits out info from Drop File:
  213. ZF_LOGV("Name=%s Alias=%s TimeLeft=%d SecLevel=%d Location=%s Node=%d Sysop=%s",
  214. od_control.user_name,
  215. od_control.user_handle,
  216. od_control.user_timelimit,
  217. od_control.user_security,
  218. od_control.user_location,
  219. od_control.od_node,
  220. od_control.sysop_name
  221. );
  222. }
  223. void paws() {
  224. // Aaah, DRY
  225. od_printf("`white`Press any key to continue...");
  226. od_get_key(true);
  227. od_printf("\r\n");
  228. }
  229. int yesNo() {
  230. char ch;
  231. int done = 0;
  232. od_printf("`bright black`(`bright white`Y`bright black`)`white`es `bright black`(`bright red`N`bright black`)`red`o`white`");
  233. ch = od_get_answer("YyNn\r");
  234. od_printf("\r\n");
  235. switch(tolower(ch)) {
  236. case 'y':
  237. done = 1;
  238. break;
  239. case '\r':
  240. case 'n':
  241. break;
  242. }
  243. return done;
  244. }
  245. int locate_player(char name[]) {
  246. // returns user id for given real name and 0 for no record found
  247. sqlite3 *db;
  248. sqlite3_stmt *stmt;
  249. char sqlbuffer[256];
  250. int result = 0;
  251. int rc = sqlite3_open("spaceconstruct.db3", &db);
  252. if(rc) { // Did we do a successful open?
  253. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  254. sqlite3_close(db);
  255. od_exit(-1, FALSE);
  256. }
  257. sqlite3_busy_timeout(db, 5000);
  258. //od_printf("realname = '%s'\r\n", name);
  259. // Locating user with given name
  260. strcpy(sqlbuffer, "SELECT uid from user where real=? COLLATE NOCASE;");
  261. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  262. sqlite3_bind_text(stmt, 1, name, strlen(name), SQLITE_STATIC);
  263. rc = sqlite3_step(stmt);
  264. if (rc == SQLITE_ROW) {
  265. // Return user id
  266. result = sqlite3_column_int(stmt, 0);
  267. } else {
  268. // User not found
  269. ZF_LOGW("Unable to locate user %s", name);
  270. result = 0;
  271. } // Clean Up, return results
  272. sqlite3_finalize(stmt);
  273. sqlite3_close(db);
  274. return result;
  275. }
  276. void check_database() {
  277. // If the table users does not exist make it.
  278. sqlite3 *db;
  279. sqlite3_stmt *stmt;
  280. char sqlbuffer[256];
  281. int rc = sqlite3_open("spaceconstruct.db3", &db);
  282. if(rc) {
  283. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  284. sqlite3_close(db);
  285. od_exit(-1, FALSE);
  286. }
  287. sqlite3_busy_timeout(db, 5000);
  288. strcpy(sqlbuffer, "SELECT COUNT(*) FROM user;");
  289. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  290. rc = sqlite3_step(stmt);
  291. if(rc == SQLITE_ROW) {
  292. // Good
  293. } else {
  294. // Bad, Create table
  295. char *errmsg;
  296. rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS \"user\" (`uid` INTEGER PRIMARY KEY AUTOINCREMENT,`nick` TEXT,`real` TEXT,`experience` INTEGER,`metal` INTEGER,`fuel` INTEGER,`gun` INTEGER,`armor` INTEGER,`shield` INTEGER,`armorpoints` INTEGER,`shieldpoints` INTEGER,`hitpoints` INTEGER,`shieldsup` INTEGER,`laston` INTEGER);", NULL, NULL, &errmsg);
  297. ZF_LOGW("Users table did not exist created");
  298. }
  299. sqlite3_finalize(stmt);
  300. sqlite3_close(db);
  301. }
  302. User load_player(int uuid) {
  303. // Returns a player Structure from database
  304. sqlite3 *db;
  305. sqlite3_stmt *stmt;
  306. char sqlbuffer[256];
  307. //User *result;
  308. int rc = sqlite3_open("spaceconstruct.db3", &db);
  309. if(rc) { // Did we do a successful open?
  310. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  311. sqlite3_close(db);
  312. od_exit(-1, FALSE);
  313. }
  314. sqlite3_busy_timeout(db, 5000);
  315. strcpy(sqlbuffer, "SELECT * FROM user WHERE uid=?;");
  316. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  317. sqlite3_bind_int(stmt, 1, uuid);
  318. rc = sqlite3_step(stmt);
  319. if (rc == SQLITE_ROW) {
  320. std::string val1( (const char *)sqlite3_column_text(stmt, 1) );
  321. std::string val2( (const char *)sqlite3_column_text(stmt, 2) );
  322. User result(sqlite3_column_int(stmt, 0), val1, val2, sqlite3_column_int(stmt, 3), sqlite3_column_int(stmt, 13), sqlite3_column_int(stmt, 7), sqlite3_column_int(stmt, 9), sqlite3_column_int(stmt, 8), sqlite3_column_int(stmt, 10), sqlite3_column_int(stmt, 12), sqlite3_column_int(stmt, 11), sqlite3_column_int(stmt, 6), sqlite3_column_int(stmt, 4), sqlite3_column_int(stmt, 5));
  323. sqlite3_finalize(stmt);
  324. sqlite3_close(db);
  325. return result;
  326. } else {
  327. sqlite3_finalize(stmt);
  328. sqlite3_close(db);
  329. User result;
  330. return result;
  331. }
  332. sqlite3_finalize(stmt);
  333. sqlite3_close(db);
  334. }
  335. User highest_player() {
  336. // Used to find if someone may have fought the Unknown One and won the game already!
  337. int done = 0;
  338. User test;
  339. int high = 0;
  340. int high_exp = 0;
  341. int id = 1;
  342. while(!done) {
  343. test = load_player(id);
  344. if(test.get_uid() != 0) {
  345. if(test.get_experience() > high_exp) {
  346. // Update highest player id and experience
  347. high_exp = test.get_experience();
  348. high = id;
  349. }
  350. } else {
  351. done = 1;
  352. }
  353. id += 1;
  354. } // Ok highest experiencing player found!
  355. test = load_player(high);
  356. return test;
  357. }
  358. void update_player(User data) {
  359. sqlite3 *db;
  360. sqlite3_stmt *stmt;
  361. char sqlbuffer[1024];
  362. int rc = sqlite3_open("spaceconstruct.db3", &db);
  363. if(rc) { // Did we do a successful open?
  364. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  365. sqlite3_close(db);
  366. od_exit(-1, FALSE);
  367. }
  368. sqlite3_busy_timeout(db, 5000);
  369. // Bad, don't do this... opens to SQL injection!
  370. //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;",
  371. // data.nick, data.experience, data.metal, data.fuel, data.guns, data.armors, data.shields, data.armorpoints, data.shieldpoints, data.hitpoints, data.uid);
  372. // strcpy(sqlbuffer, "UPDATE user SET nick=?, experience=?, metal=?, fuel=?, gun=?, armor=?, shield=?, armorpoints=?, shieldpoints=?, hitpoints=?, shieldsup=?, laston=? WHERE uid=?;");
  373. // Are they actually going to be changing their nicks??
  374. strcpy(sqlbuffer, "UPDATE user SET experience=?, metal=?, fuel=?, gun=?, armor=?, shield=?, armorpoints=?, shieldpoints=?, hitpoints=?, shieldsup=?, laston=? WHERE uid=?;");
  375. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  376. // Bind All data values
  377. // sqlite3_bind_text(stmt, 1, data.get_nick().c_str(), data.get_nick().length(), SQLITE_STATIC);
  378. sqlite3_bind_int(stmt, 1, data.get_experience());
  379. sqlite3_bind_int(stmt, 2, data.get_metal());
  380. sqlite3_bind_int(stmt, 3, data.get_fuel());
  381. sqlite3_bind_int(stmt, 4, data.get_gun());
  382. sqlite3_bind_int(stmt, 5, data.get_armor());
  383. sqlite3_bind_int(stmt, 6, data.get_shield());
  384. sqlite3_bind_int(stmt, 7, data.get_armorpoints());
  385. sqlite3_bind_int(stmt, 8, data.get_shieldpoints());
  386. sqlite3_bind_int(stmt, 9, data.get_hitpoints());
  387. sqlite3_bind_int(stmt, 10, data.get_shieldsup());
  388. sqlite3_bind_int(stmt, 11, data.get_laston());
  389. sqlite3_bind_int(stmt, 12, data.get_uid());
  390. // Execute
  391. rc = sqlite3_step(stmt);
  392. if(rc != SQLITE_DONE) {
  393. ZF_LOGF("Failed updating player %s (%d)", sqlite3_errmsg(db), rc);
  394. sqlite3_finalize(stmt);
  395. sqlite3_close(db);
  396. od_exit(-1, FALSE);
  397. }
  398. sqlite3_finalize(stmt);
  399. sqlite3_close(db);
  400. }
  401. int create_player(User data) {
  402. sqlite3 *db;
  403. sqlite3_stmt *stmt;
  404. char sqlbuffer[1024];
  405. int rc = sqlite3_open("spaceconstruct.db3", &db);
  406. if(rc) { // Did we do a successful open?
  407. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  408. sqlite3_close(db);
  409. od_exit(-1, FALSE);
  410. }
  411. sqlite3_busy_timeout(db, 5000);
  412. strcpy(sqlbuffer, "INSERT INTO user (nick, real, experience, metal, fuel, gun, armor, shield, armorpoints, shieldpoints, hitpoints, shieldsup, laston) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
  413. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  414. std::string temp = data.get_nick();
  415. sqlite3_bind_text(stmt, 1, temp.c_str(), temp.length(), SQLITE_STATIC);
  416. std::string temp2 = data.get_real();
  417. sqlite3_bind_text(stmt, 2, temp2.c_str(), temp2.length(), SQLITE_STATIC);
  418. //sqlite3_bind_text(stmt, 1, data.get_nick().c_str(), data.get_nick().length(), SQLITE_STATIC);
  419. //sqlite3_bind_text(stmt, 2, data.get_real().c_str(), data.get_real().length(), SQLITE_STATIC);
  420. sqlite3_bind_int(stmt, 3, data.get_experience());
  421. sqlite3_bind_int(stmt, 4, data.get_metal());
  422. sqlite3_bind_int(stmt, 5, data.get_fuel());
  423. sqlite3_bind_int(stmt, 6, data.get_gun());
  424. sqlite3_bind_int(stmt, 7, data.get_armor());
  425. sqlite3_bind_int(stmt, 8, data.get_shield());
  426. sqlite3_bind_int(stmt, 9, data.get_armorpoints());
  427. sqlite3_bind_int(stmt, 10, data.get_shieldpoints());
  428. sqlite3_bind_int(stmt, 11, data.get_hitpoints());
  429. sqlite3_bind_int(stmt, 12, data.get_shieldsup());
  430. sqlite3_bind_int(stmt, 13, data.get_laston());
  431. rc = sqlite3_step(stmt);
  432. if(rc != SQLITE_DONE) {
  433. ZF_LOGF("Failed inserting player %s (%d)", sqlite3_errmsg(db), rc);
  434. sqlite3_finalize(stmt);
  435. sqlite3_close(db);
  436. od_exit(-1, FALSE);
  437. }
  438. sqlite3_finalize(stmt);
  439. sqlite3_close(db);
  440. return 1; // Good
  441. }
  442. void delete_player(int uuid) {
  443. sqlite3 *db;
  444. sqlite3_stmt *stmt;
  445. char sqlbuffer[256];
  446. int rc = sqlite3_open("spaceconstruct.db3", &db);
  447. if(rc) { // Did we do a successful open?
  448. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  449. sqlite3_close(db);
  450. od_exit(-1, FALSE);
  451. }
  452. sqlite3_busy_timeout(db, 5000);
  453. strcpy(sqlbuffer, "DELETE FROM user WHERE uid=?;");
  454. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  455. sqlite3_bind_int(stmt, 1, uuid);
  456. rc = sqlite3_step(stmt);
  457. if (rc != SQLITE_DONE){
  458. ZF_LOGF("Unable to locate user with id=%d got %s (%d)", uuid, sqlite3_errmsg(db), rc);
  459. sqlite3_finalize(stmt);
  460. sqlite3_close(db);
  461. od_exit(-1, FALSE);
  462. }
  463. sqlite3_finalize(stmt);
  464. sqlite3_close(db);
  465. }
  466. void delete_players() {
  467. sqlite3 *db;
  468. sqlite3_stmt *stmt;
  469. char sqlbuffer[256];
  470. int rc = sqlite3_open("spaceconstruct.db3", &db);
  471. if(rc) { // Did we do a successful open?
  472. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  473. sqlite3_close(db);
  474. od_exit(-1, FALSE);
  475. }
  476. sqlite3_busy_timeout(db, 5000);
  477. strcpy(sqlbuffer, "DELETE FROM user;");
  478. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  479. rc = sqlite3_step(stmt);
  480. if (rc != SQLITE_DONE){
  481. ZF_LOGF("Delete_players 1/2, Unexpected error %s (%d)", sqlite3_errmsg(db), rc);
  482. sqlite3_finalize(stmt);
  483. sqlite3_close(db);
  484. od_exit(-1, FALSE);
  485. }
  486. strcpy(sqlbuffer, "UPDATE sqlite_sequence SET seq = 0 WHERE name='user';");
  487. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  488. rc = sqlite3_step(stmt);
  489. if (rc != SQLITE_DONE){
  490. ZF_LOGF("Delete_players 2/2, Unexpected error %s (%d)", sqlite3_errmsg(db), rc);
  491. sqlite3_finalize(stmt);
  492. sqlite3_close(db);
  493. od_exit(-1, FALSE);
  494. }
  495. sqlite3_finalize(stmt);
  496. sqlite3_close(db);
  497. }
  498. int display_all_players() {
  499. sqlite3 *db;
  500. sqlite3_stmt *stmt;
  501. char sqlbuffer[1024];
  502. int rc = sqlite3_open("spaceconstruct.db3", &db);
  503. if(rc) { // Did we do a successful open?
  504. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  505. sqlite3_close(db);
  506. od_exit(-1, FALSE);
  507. }
  508. sqlite3_busy_timeout(db, 5000);
  509. strcpy(sqlbuffer, "SELECT * from user ORDER BY experience DESC, nick DESC;");
  510. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  511. int total = 0;
  512. od_printf("`bright green`___ The Players ___\r\n");
  513. while(sqlite3_step(stmt) == SQLITE_ROW) {
  514. od_printf("`bright white` %s (%d)\r\n", sqlite3_column_text(stmt, 1), sqlite3_column_int(stmt, 3));
  515. total += 1;
  516. }
  517. if(total > 1) {
  518. od_printf("`green`There are a total of `bright green`%d`green` players\r\n", total);
  519. } else if(total == 0) {
  520. od_printf("`green`There are a total of `bright green`%d`green` players\r\n", total);
  521. } else {
  522. od_printf("`green`There are a total of `bright green`%d`green` player\r\n", total);
  523. }
  524. sqlite3_finalize(stmt);
  525. sqlite3_close(db);
  526. return total;
  527. }
  528. int display_all_opponents(int us) {
  529. sqlite3 *db;
  530. sqlite3_stmt *stmt;
  531. char sqlbuffer[1024];
  532. int rc = sqlite3_open("spaceconstruct.db3", &db);
  533. if(rc) { // Did we do a successful open?
  534. ZF_LOGF("Failed opening database %s (%d)", sqlite3_errmsg(db), rc);
  535. sqlite3_close(db);
  536. od_exit(-1, FALSE);
  537. }
  538. sqlite3_busy_timeout(db, 5000);
  539. strcpy(sqlbuffer, "SELECT * from user ORDER BY experience DESC, nick DESC;");
  540. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  541. int total = 1;
  542. od_printf("`bright green`___ Target List ___\r\n");
  543. while(sqlite3_step(stmt) == SQLITE_ROW) {
  544. if(total != us) {
  545. od_printf("`bright black`(`bright white`%d`bright black`) `bright white`%s (%d)\r\n", total, sqlite3_column_text(stmt, 1), sqlite3_column_int(stmt, 3));
  546. }
  547. total += 1;
  548. }
  549. total -= 1;
  550. sqlite3_finalize(stmt);
  551. sqlite3_close(db);
  552. return total;
  553. }
  554. User build_menu(User my) {
  555. int done = 0;
  556. char ch;
  557. if(my.get_metal() != 0) {
  558. while(!done) {
  559. od_clr_scr();
  560. od_send_file("ansis/sc_build.ans");
  561. // Paint over sc_build.ans file
  562. od_set_cursor(3, 15);
  563. od_printf("%5d", my.calcGun());
  564. od_set_cursor(4, 15);
  565. od_printf("%5d", my.calcArmor());
  566. od_set_cursor(5, 15);
  567. od_printf("%5d", my.calcShield());
  568. od_set_cursor(9, 1);
  569. // Resuming normal operations
  570. od_printf("`bright yellow`You have %8d metal\r\n", my.get_metal());
  571. ch = od_get_answer("GgAaSsCcRr\r");
  572. switch(tolower(ch)) {
  573. case 'g':
  574. if(my.get_metal() >= my.calcGun()) {
  575. my.rmMetal(my.calcGun());
  576. my.addGun(1);
  577. od_printf("`bright green`Added a new gun!\r\n`white`");
  578. paws();
  579. } else {
  580. od_printf("`bright red`You don't have the %d metal for this!\r\n`white`", my.calcGun());
  581. paws();
  582. }
  583. break;
  584. case 'a':
  585. if(my.get_metal() >= my.calcArmor()) {
  586. my.rmMetal(my.calcArmor());
  587. my.addArmor(1);
  588. od_printf("`bright green`Added a new armor!\r\n`white`");
  589. paws();
  590. } else {
  591. od_printf("`bright red`You don't have the %d metal for this!\r\n`white`", my.calcArmor());
  592. paws();
  593. }
  594. break;
  595. case 's':
  596. if(my.get_metal() >= my.calcShield()) {
  597. my.rmMetal(my.calcShield());
  598. my.addShield(1);
  599. od_printf("`bright green`Added a new shield!\r\n`white`");
  600. paws();
  601. } else {
  602. od_printf("`bright red`You don't have the %d metal for this!\r\n`white`", my.calcShield());
  603. paws();
  604. }
  605. break;
  606. case '\r':
  607. case 'c':
  608. done = 1;
  609. break;
  610. case 'r':
  611. if(my.get_experience() >= 25) {
  612. if(my.get_metal() >= 25) {
  613. my.rmEXP(25);
  614. my.rmMetal(25);
  615. my.addFuel(5);
  616. od_printf("`bright green`Made 5 fuel using 25 experience and 25 metal!\r\n");
  617. paws();
  618. } else {
  619. od_printf("`bright red`You need 25 experience and 25 metal to make fuel!\r\n");
  620. paws();
  621. }
  622. } else {
  623. od_printf("`bright red`You need 25 experience and 25 metal to make fuel!\r\n");
  624. paws();
  625. }
  626. break;
  627. }
  628. }
  629. update_player(my);
  630. return my;
  631. } else {
  632. return my;
  633. }
  634. }
  635. User pvp_menu(User my, User targ) {
  636. int done = 0;
  637. int playerWon = 0;
  638. char ch;
  639. // We need to set the targ's hit points and such to max and enable us to "restore" them
  640. int temp_hp = targ.get_hitpoints();
  641. int temp_ap = targ.get_armorpoints();
  642. int temp_sp = targ.get_shieldpoints();
  643. int temp_su = targ.get_shieldsup();
  644. // Set Offline to max values
  645. int targ_dmg = (targ.get_gun() + 1); // Add bonus damage for offline
  646. targ.set_hitpoints(targ.calcHP());
  647. targ.set_armorpoints(targ.calcAP());
  648. targ.set_shieldpoints(targ.calcSP());
  649. targ.set_shieldsup(0);
  650. // Prep Combat values
  651. int fire = 0; // Fire Guns
  652. int flee = 0; // Run away
  653. int regen = 0; // Shields
  654. int test = 0;
  655. int test2 = 0;
  656. while(!done) {
  657. // Process Shields up
  658. if(temp_su != 0) {
  659. temp_su -= 1;
  660. }
  661. if(my.get_shieldsup() != 0) {
  662. my.rmSU(1);
  663. }
  664. // Process Crew repairing hitpoints
  665. if(targ.get_hitpoints() < targ.calcHP()) {
  666. regen = randrange(0, 100);
  667. if(regen >= 85) { // 15%, offline get a bonus 5% on repairing hull.
  668. targ.addHP(1);
  669. od_printf("`bright green`%s`green`'s crew repaired their hull for 1 point!\r\n", targ.get_nick().c_str());
  670. }
  671. }
  672. if(my.get_hitpoints() < my.calcHP()) {
  673. regen = randrange(0, 100);
  674. if(regen >= 90) { // 10%
  675. my.addHP(1);
  676. od_printf("`bright green`Your crew repaired your hull for 1 point!\r\n");
  677. }
  678. }
  679. // Process Shields regenerating and display player stats
  680. regen = randrange(0, 100);
  681. if(regen >= 75) { // 25%
  682. targ.regen();
  683. }
  684. od_printf("`bright white`%s has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left\r\n", targ.get_nick().c_str(), temp_sp, temp_ap, temp_hp);
  685. // Online Players Shields
  686. regen = randrange(0, 100);
  687. if(regen >= 80) { // 20%
  688. my.regen();
  689. }
  690. od_printf("`bright white`Your ship has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left\r\n", my.get_shieldpoints(), my.get_armorpoints(), my.get_hitpoints());
  691. // Form combat menu (Not allowing online to repair since offline can't really repair either)
  692. od_printf("`bright black`(`bright white`A`bright black`)`white`ttack\r\n");
  693. od_printf("`bright black`(`bright white`F`bright black`)`white`lee\r\n");
  694. // Online Players action
  695. ch = od_get_answer("AaFf\r");
  696. switch(tolower(ch)) {
  697. case '\r':
  698. case 'a':
  699. fire = randrange(0, 100);
  700. if(fire >= 50) {
  701. // Proccess online dealing damage to offline
  702. test = targ.takeDamage(my.get_gun());
  703. if(test == 0) {
  704. od_printf("`bright red`%s took %d damage!\r\n", targ.get_nick().c_str(), my.get_gun());
  705. } else {
  706. od_printf("`bright red`%s took %d damage, and blew up!\r\n", targ.get_nick().c_str(), my.get_gun());
  707. playerWon = 1; // Online beat offline
  708. }
  709. } else { // Online missed
  710. od_printf("`white`You missed!\r\n");
  711. }
  712. break;
  713. case 'f':
  714. flee = randrange(0, 100);
  715. if(flee >= 80) { // 20% chance the online flees
  716. playerWon = 3; // Online ran away from Offline
  717. done = 1;
  718. od_printf("`bright green`You ran away from %s!\r\n", targ.get_nick().c_str());
  719. } else {
  720. od_printf("`bright red`%s found you!\r\n", targ.get_nick().c_str());
  721. }
  722. break;
  723. } // Now for Offline to attack
  724. if(!playerWon) {
  725. fire = randrange(0, 100);
  726. if(fire >= 45) { // 55%, 5% bonus for offline
  727. test2 = my.takeDamage(targ_dmg);
  728. if(test2 == 0) {
  729. od_printf("`bright red`You took %d damage!\r\n", targ_dmg);
  730. } else {
  731. od_printf("`bright red`You took %d damage, and blew up!\r\n", targ_dmg);
  732. playerWon = 2;
  733. }
  734. }
  735. }
  736. // Winning Move?
  737. if(playerWon) {
  738. switch(playerWon) {
  739. case 1: // Online won over offline
  740. ZF_LOGV("%s beat %s in pvp", my.get_nick().c_str(), targ.get_nick().c_str());
  741. if(targ.get_experience()) {
  742. my.addEXP((targ.get_experience() / 2));
  743. targ.rmEXP((targ.get_experience() / 2));
  744. }
  745. if(targ.get_metal()) {
  746. my.addMetal((targ.get_metal() / 2));
  747. targ.rmMetal((targ.get_metal() / 2));
  748. }
  749. my.rmFuel(4);
  750. if(my.get_fuel() < 0) {
  751. my.set_fuel(0);
  752. }
  753. od_printf("`bright green`Congrats on defeating %s!\r\n`white`", targ.get_nick().c_str());
  754. break;
  755. case 2: // Offline won over online!
  756. ZF_LOGV("%s beat %s in pvp", targ.get_nick().c_str(), my.get_nick().c_str());
  757. if(my.get_experience()) {
  758. targ.addEXP((my.get_experience() / 2));
  759. my.rmEXP((my.get_experience() / 2));
  760. }
  761. if(my.get_metal()) {
  762. targ.addMetal((my.get_metal() / 2));
  763. my.rmMetal((my.get_metal() / 2));
  764. }
  765. my.rmFuel(4);
  766. if(my.get_fuel() < 0) {
  767. my.set_fuel(0);
  768. }
  769. od_printf("`bright red`Sorry for your loss! (We rebuild your ship)\r\n");
  770. my.set_hitpoints(4);
  771. my.set_armorpoints(8);
  772. my.set_shieldpoints(0);
  773. my.set_shieldsup(0);
  774. my.set_armor(1);
  775. my.set_shield(0);
  776. my.set_gun(1);
  777. break;
  778. case 3:
  779. ZF_LOGV("%s ran away from %s in pvp", my.get_nick().c_str(), targ.get_nick().c_str());
  780. my.rmFuel(4);
  781. if(my.get_fuel() < 0) {
  782. my.set_fuel(0);
  783. }
  784. break;
  785. }
  786. }
  787. }
  788. // Restore targ values from temps
  789. targ.set_shieldsup(temp_su);
  790. targ.set_armorpoints(temp_ap);
  791. targ.set_hitpoints(temp_hp);
  792. targ.set_shieldpoints(temp_sp);
  793. // DOne, save
  794. update_player(targ);
  795. update_player(my);
  796. return my;
  797. }
  798. User combat_menu(User my, int targ_hp, int targ_dmg, int asteroid) {
  799. int done = 0;
  800. int playerWon = 0; // Did the player win? (1 = Yes, 2 = No, 3 = Flee)
  801. char ch;
  802. int armorRep = 0; // Can armor be repaired?
  803. int temp = 0;
  804. int temp1 = 0;
  805. int temp2 = 0;
  806. int temp_hp = targ_hp;
  807. int flee = 0;
  808. int fire = 0;
  809. int regen = 0;
  810. while(!done) {
  811. if(asteroid == 0) {
  812. od_printf("`bright white`Pirate has %d hitpoints left\r\n", targ_hp);
  813. } else if(asteroid == 1) {
  814. od_printf("`bright white`Asteroid has %d hitpoints left\r\n", targ_hp);
  815. } else if(asteroid == 2) {
  816. od_printf("`bright white`Unknown One has %d hitpoints left\r\n", targ_hp);
  817. }
  818. // Proccess Shields comming back online
  819. if(my.get_shieldsup() != 0) {
  820. my.rmSU(1);
  821. }
  822. // Proccess Crew repairing hull
  823. if(my.get_hitpoints() < my.calcHP()) {
  824. regen = randrange(0, 100);
  825. if(regen >= 90) {
  826. my.addHP(1);
  827. }
  828. }
  829. // Proccess Shields Regen
  830. regen = randrange(0, 100);
  831. if(regen >= 80) { // 20%
  832. my.regen();
  833. }
  834. od_printf("`bright white`Your ship has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left:\r\n", my.get_shieldpoints(), my.get_armorpoints(), my.get_hitpoints());
  835. // Forming Combat Menu
  836. od_printf("`bright black`(`bright white`A`bright black`)`white`ttack\r\n");
  837. if(my.get_armorpoints() < my.calcAP()) {
  838. od_printf("`bright black`(`bright white`R`bright black`)`white`epair Armor\r\n");
  839. armorRep = 1; // Yes we can repair armor
  840. }
  841. od_printf("`bright black`(`bright white`F`bright black`)`white`lee\r\n");
  842. // Proccess User action
  843. ch = od_get_answer("AaRrFf\r");
  844. switch(tolower(ch)) {
  845. case '\r':
  846. case 'a':
  847. fire = rand() % 100;
  848. if(asteroid == 1) { // Its a rock so allow player to hit it more frequent
  849. fire -= 15;
  850. }
  851. if(fire >= 50) {
  852. targ_hp -= my.get_gun();
  853. if(targ_hp <= 0) {
  854. od_printf("`bright red`Target Destroyed!\r\n");
  855. done = 1;
  856. playerWon = 1;
  857. } else {
  858. od_printf("`bright red`Target took %d damage!\r\n", my.get_gun());
  859. }
  860. } else {
  861. od_printf("`white`You Missed!\r\n");
  862. }
  863. break;
  864. case 'r':
  865. if(armorRep) {
  866. my.repair(false);
  867. } else {
  868. od_printf("`bright yellow`You don't seem to need repairs!\r\n");
  869. }
  870. break;
  871. case 'f':
  872. flee = rand() % 100;
  873. if(asteroid == 1) {
  874. flee = 100; // Always allow fleeing from asteroid
  875. } else if(asteroid == 2) {
  876. flee = 0; // Never allow fleeing from Unknown One
  877. }
  878. if(flee >= 82) {
  879. playerWon = 3;
  880. done = 1;
  881. od_printf("`bright green`You ran away!\r\n");
  882. } else {
  883. od_printf("`bright red`Target found you!\r\n");
  884. }
  885. break;
  886. } // If Enemy is alive proccess incomming attack
  887. if(targ_hp > 0) {
  888. targ_dmg;
  889. fire = rand() % 100;
  890. if(asteroid == 1) { // Yup it doesn't have gunz cuz it's rock.
  891. fire = 0;
  892. } else if(asteroid == 2) { // This is the Unknown One
  893. fire += 5;
  894. }
  895. if(fire >= 40) {
  896. temp = my.takeDamage(targ_dmg);
  897. if(temp == 0) {
  898. od_printf("`bright red`You took %d damage!\r\n", targ_dmg);
  899. } else {
  900. od_printf("`bright red`You took %d damage, and blew up!\r\n", targ_dmg);
  901. playerWon = 2;
  902. }
  903. }
  904. // firing
  905. }
  906. // Winning move?
  907. if(playerWon) {
  908. if(playerWon == 1) {
  909. std::string who;
  910. switch(asteroid) {
  911. case 0:
  912. //Pirate
  913. who = "Pirate";
  914. break;
  915. case 1:
  916. // Asteroid
  917. who = "Asteroid";
  918. break;
  919. case 2:
  920. // Unknown One
  921. who = "Unkown One";
  922. break;
  923. }
  924. ZF_LOGV("%s beat %s", my.get_nick().c_str(), who.c_str());
  925. // Player won
  926. my.addEXP((temp_hp + targ_dmg));
  927. my.addMetal(temp_hp);
  928. if(asteroid == 1) { // If asteroid reward more experience and metal!
  929. my.addEXP(2);
  930. my.addMetal(2);
  931. } else if(asteroid == 2) { // If it was the boss!
  932. my.addEXP(6000); // Ensure the game triggers a reset!
  933. my.set_metal(0); // Stop the player!
  934. my.set_fuel(2); // Prevent the player from doing anything else
  935. }
  936. my.rmFuel(2);
  937. if(my.get_fuel() < 0) {
  938. my.set_fuel(0);
  939. }
  940. od_printf("`bright green`Congrats on your victory!\r\n`white`");
  941. } else if(playerWon == 2) {
  942. std::string who;
  943. switch(asteroid) {
  944. case 0:
  945. //Pirate
  946. who = "Pirate";
  947. break;
  948. case 1:
  949. // Asteroid
  950. who = "Asteroid";
  951. break;
  952. case 2:
  953. // Unknown One
  954. who = "Unkown One";
  955. break;
  956. }
  957. ZF_LOGV("%s beat %s", who.c_str(), my.get_nick().c_str());
  958. // Target won
  959. my.rmEXP((temp_hp + targ_dmg));
  960. my.rmMetal(temp_hp);
  961. my.rmFuel(3);
  962. if(my.get_experience() < 0) {
  963. my.set_experience(0);
  964. }
  965. if(my.get_metal() < 0) {
  966. my.set_metal(0);
  967. }
  968. if(my.get_fuel() < 0) {
  969. my.set_fuel(0);
  970. }
  971. od_printf("`bright red`Sorry for your loss! (We rebuild your ship)\r\n");
  972. my.set_hitpoints(4);
  973. my.set_armorpoints(8);
  974. my.set_shieldpoints(0);
  975. my.set_shieldsup(0);
  976. my.set_armor(1);
  977. my.set_shield(0);
  978. my.set_gun(1);
  979. } else if(playerWon == 3) {
  980. std::string who;
  981. switch(asteroid) {
  982. case 0:
  983. //Pirate
  984. who = "Pirate";
  985. break;
  986. case 1:
  987. // Asteroid
  988. who = "Asteroid";
  989. break;
  990. case 2:
  991. // Unknown One
  992. who = "Unkown One";
  993. break;
  994. }
  995. ZF_LOGV("%s ran away from %s", my.get_nick().c_str(), who.c_str());
  996. // Chicken!
  997. my.rmFuel(1);
  998. }
  999. }
  1000. }
  1001. update_player(my);
  1002. return my;
  1003. }
  1004. void about_game() {
  1005. int done = 0;
  1006. int page = 1;
  1007. while(!done) {
  1008. switch(page) {
  1009. case 1:
  1010. od_clr_scr();
  1011. od_send_file("ansis/sc_doc_1.ans");
  1012. paws();
  1013. break;
  1014. case 2:
  1015. od_clr_scr();
  1016. od_send_file("ansis/sc_doc_2.ans");
  1017. paws();
  1018. break;
  1019. case 3:
  1020. od_clr_scr();
  1021. od_send_file("ansis/sc_doc_3.ans");
  1022. paws();
  1023. break;
  1024. case 4:
  1025. od_clr_scr();
  1026. od_send_file("ansis/sc_doc_4.ans");
  1027. paws();
  1028. break;
  1029. case 5:
  1030. od_clr_scr();
  1031. od_send_file("ansis/sc_doc_5.ans");
  1032. paws();
  1033. break;
  1034. case 6:
  1035. done = 1;
  1036. break;
  1037. }
  1038. od_clr_scr();
  1039. page += 1;
  1040. }
  1041. }
  1042. void play_game() {
  1043. int done = 0;
  1044. char ch;
  1045. char ch1[256];
  1046. User myself;
  1047. User myself2;
  1048. User testing;
  1049. int reset = 0;
  1050. int doRefuel = 0;
  1051. int pirate_encounter = 0;
  1052. int pirate_hp = 0; // Reused for asteroid
  1053. int pirate_dmg = 0; // Reused for asteroid
  1054. int asteroid = 0; // 1 means yes we need to remove damage... completely.
  1055. int me = locate_player(od_control.user_name);
  1056. int abort = 0;
  1057. if(me != 0) {
  1058. //myself = load_player(me);
  1059. testing = load_player(me);
  1060. if(testing.get_uid() != 0) {
  1061. myself = load_player(me);
  1062. }
  1063. dT age = fromDate(compareDate(myself.get_laston()));
  1064. // Process player inactivity
  1065. if(age.age >= 30) { // 30 days
  1066. od_printf("`bright green`Since you haven't played a while we have reset your account!\r\n");
  1067. ZF_LOGW("%s was reset for expiring", od_control.user_name);
  1068. me = 0;
  1069. reset = 1;
  1070. }
  1071. // Processing fuel regeneration
  1072. if(age.age != 0) {
  1073. doRefuel = 1;
  1074. }
  1075. }
  1076. if(me != 0) {
  1077. od_printf("`bright white`Welcome back `bright green`%s\r\n", myself.get_nick().c_str());
  1078. ZF_LOGV("%s is back again", od_control.user_name);
  1079. dT age = fromDate(compareDate(myself.get_laston()));
  1080. myself.set_laston(dateStamp());
  1081. if(age.day != 0 || age.month != 0 || age.year != 0) {
  1082. od_printf("`bright yellow`Haven't seen you for");
  1083. }
  1084. if(age.day != 0) {
  1085. if(age.day > 1) {
  1086. od_printf(" %d days", age.day);
  1087. } else {
  1088. od_printf(" %d day", age.day);
  1089. }
  1090. }
  1091. if(age.month != 0) {
  1092. if(age.month > 1) {
  1093. od_printf(" %d months", age.month);
  1094. } else {
  1095. od_printf(" %d month", age.month);
  1096. }
  1097. }
  1098. if(age.year != 0) {
  1099. if(age.year > 1) {
  1100. od_printf(" %d years", age.year);
  1101. } else {
  1102. od_printf(" %d year", age.year);
  1103. }
  1104. }
  1105. if(age.day != 0 || age.month != 0 || age.year != 0) {
  1106. od_printf("\r\n");
  1107. }
  1108. if(doRefuel) {
  1109. if(myself.get_fuel() != 0) {
  1110. myself.addFuel((age.age * 2));
  1111. } else {
  1112. myself.addFuel(((age.age * 2) + 10));
  1113. od_printf("`bright green`For using all your fuel last time you get bonus fuel today!\r\n");
  1114. }
  1115. od_printf("`bright green`You now have %d fuel\r\n", myself.get_fuel());
  1116. }
  1117. update_player(myself);
  1118. paws();
  1119. } else {
  1120. od_printf("`bright white`You look new here. (Hit Enter to abort)\r\n");
  1121. ZF_LOGV("%s is a new player", od_control.user_name);
  1122. while(done == 0) {
  1123. od_printf("`bright yellow`What's your name: ");
  1124. od_input_str(ch1, 26, 32, 126);
  1125. od_printf("\r\n");
  1126. if(strlen(ch1) > 0) {
  1127. //od_input_str(ch1, 256, 32, 126);
  1128. myself.set_nick(ch1);
  1129. } else {
  1130. od_clr_scr();
  1131. abort = 1;
  1132. done = 1;
  1133. od_printf("`bright red`Ok, come back later then...`white`\r\n");
  1134. paws();
  1135. }
  1136. if(abort == 0) {
  1137. od_printf("`bright white`Are you sure you want to be called `bright green`%s\r\n", myself.get_nick().c_str());
  1138. done = yesNo();
  1139. if(done) {
  1140. if(reset) { // So we are reseting the user, this wipes all values and uses update instead of create
  1141. User myself(myself.get_uid(), myself.get_nick().c_str(), myself.get_real(), 0, dateStamp(), 1, 8, 0, 0, 0, 4, 1, 0, 10);
  1142. update_player(myself);
  1143. } else { // Brand new user
  1144. ZF_LOGI("Creating user");
  1145. User myself2(myself.get_nick().c_str(), od_control.user_name);
  1146. create_player(myself2);
  1147. myself = load_player(locate_player(od_control.user_name));
  1148. }
  1149. }
  1150. }
  1151. }
  1152. done = 0; // Reset this for our next loop.
  1153. }
  1154. // Ok user is here now lets ask what they want to do... build/attack
  1155. if(abort == 0) {
  1156. od_clr_scr();
  1157. while(!done) {
  1158. od_printf("`bright yellow`You have %d fuel left for today,\r\n", myself.get_fuel());
  1159. if(myself.get_shieldsup() == 0) {
  1160. od_printf("`bright yellow`Your ship has `bright cyan`%d`bright yellow` shields, %d armor and %d hitpoints left:\r\n", myself.get_shieldpoints(), myself.get_armorpoints(), myself.get_hitpoints());
  1161. } else {
  1162. od_printf("`bright yellow`Your ship has %d armor, `bright cyan`%d`bright yellow` shields and %d hitpoints left:\r\n", myself.get_shieldpoints(), myself.get_armorpoints(), myself.get_hitpoints());
  1163. }
  1164. if(myself.get_armorpoints() < myself.calcAP()) {
  1165. od_printf("`bright black`(`bright white`R`bright black`)`white`epair Armor\r\n");
  1166. }
  1167. if(myself.get_shieldpoints() < myself.calcSP() || myself.get_shieldsup() != 0) {
  1168. od_printf("`bright black`(`bright white`G`bright black`)`white`enerate Shields\r\n");
  1169. }
  1170. if(myself.get_fuel() >= 2) {
  1171. od_send_file("ansis/sc_gameh.ans");
  1172. ch = od_get_answer("SsHhBbQqRrGgLlVv");
  1173. } else {
  1174. od_send_file("ansis/sc_gamel.ans");
  1175. ch = od_get_answer("BbQqRrGgLlVv");
  1176. }
  1177. od_clr_scr();
  1178. switch(tolower(ch)) {
  1179. case 's':
  1180. if(myself.get_fuel() >= 1) {
  1181. pirate_encounter = rand() % 100;
  1182. if(pirate_encounter >= 75) { // 25% chance to encounter a Pirate, or Unknown One
  1183. if(myself.get_experience() > 3000) {
  1184. if(pirate_encounter >= 95) { // 5% chance to encounter unknown one
  1185. od_printf("`bright yellow`You encounter the `bright red`Unknown One\r\n`white`");
  1186. asteroid = 2;
  1187. } else {
  1188. od_printf("`bright yellow`You encounter a `bright red`Pirate\r\n`white`");
  1189. asteroid = 0;
  1190. }
  1191. } else { // The player is not skilled enough to fight the unknown one
  1192. od_printf("`bright yellow`You encounter a `bright red`Pirate\r\n`white`");
  1193. asteroid = 0;
  1194. }
  1195. } else { // 75% chance to encounter a Asteroid
  1196. od_printf("`bright yellow`You encounter a `bright red`Asteroid\r\n`white`");
  1197. asteroid = 1;
  1198. }
  1199. if(asteroid == 0 || asteroid == 2) {
  1200. pirate_hp = ((myself.get_hitpoints() + 1) + (randrange(0, 4)));
  1201. if(asteroid == 2) { // Increase boss hitpoints
  1202. pirate_hp += 10;
  1203. }
  1204. // Increase difficulty based on experience...
  1205. pirate_dmg = (myself.get_experience() / 500);
  1206. if(pirate_dmg == 0) { // Always deal damage
  1207. pirate_dmg = 1;
  1208. }
  1209. if(asteroid == 2) { // Increase boss damage
  1210. pirate_dmg += 2;
  1211. }
  1212. // Send off to combat menu
  1213. myself = combat_menu(myself, pirate_hp, pirate_dmg, asteroid);
  1214. } else {
  1215. pirate_hp = (myself.get_hitpoints() + 1) + (rand() % 10);
  1216. // Send off to combat menu
  1217. myself = combat_menu(myself, pirate_hp, 0, asteroid);
  1218. }
  1219. }
  1220. break;
  1221. case 'h':
  1222. if(myself.get_fuel() >= 4) {
  1223. int targs = display_all_opponents(myself.get_uid());
  1224. int targg = 1;
  1225. int done2 = 0;
  1226. if(targg == myself.get_uid()) {
  1227. targg += 1;
  1228. }
  1229. while(!done2) {
  1230. od_printf("`bright red`Targetting: %d\r\n", targg);
  1231. if(yesNo()) {
  1232. done2 = 1;
  1233. } else if(targg < targs){
  1234. targg += 1;
  1235. } else {
  1236. done2 = 1;
  1237. od_printf("`bright red`ABORTED!\r\n");
  1238. break;
  1239. }
  1240. }
  1241. User targ = load_player(targg);
  1242. if(targ.get_uid() != 0) {
  1243. // I need to add some limits in here else someone could attack weaker players!
  1244. int proceed = 0;
  1245. int temp = targ.get_experience() + 1000; // Prevent the person from selecting a player that is higher than them
  1246. int temp1 = targ.get_experience() - 500; // Prevent the person from selecting a player that is lower than them
  1247. if(temp1 > 0) {
  1248. temp1 = 0;
  1249. }
  1250. if(temp > myself.get_experience() && temp1 < myself.get_experience()) {
  1251. proceed = 1;
  1252. }
  1253. if(proceed) {
  1254. myself = pvp_menu(myself, targ);
  1255. } else {
  1256. od_printf("`bright white`Please select a opponent within your range of expertise!\r\n");
  1257. break;
  1258. }
  1259. }
  1260. } else {
  1261. od_printf("`bright white`You need 4 fuel to fight another player!\r\n`white`");
  1262. }
  1263. break;
  1264. case 'b':
  1265. if(myself.get_metal() != 0) {
  1266. // Send off to build menu
  1267. od_clr_scr();
  1268. //od_send_file("ansis/sc_build.ans", FALSE);
  1269. myself = build_menu(myself);
  1270. } else {
  1271. od_printf("`bright white`I am sorry you have no metal go fight.`white`\r\n");
  1272. }
  1273. break;
  1274. case 'q':
  1275. done = 1;
  1276. break;
  1277. case 'r':
  1278. if(myself.get_armorpoints() < (myself.get_armor() * 8)) {
  1279. myself.repair(true);
  1280. update_player(myself);
  1281. } else {
  1282. od_printf("`bright yellow`You don't seem to need repairs!\r\n`white`");
  1283. }
  1284. break;
  1285. case 'g':
  1286. if(myself.get_shieldpoints() < myself.calcSP() || myself.get_shieldsup() != 0) {
  1287. myself.regen();
  1288. update_player(myself);
  1289. } else {
  1290. od_printf("`bright yellow`You don't seem to need to regenerate shields!\r\n`white`");
  1291. }
  1292. break;
  1293. case 'v':
  1294. od_printf("`bright white` ___ %s's Ship Status ___\r\n", myself.get_nick().c_str());
  1295. od_printf("`bright white`Items: # Installed: Metal to Upgrade:\r\n");
  1296. od_printf("`bright red`Guns: %8d %8d\r\n", myself.get_gun(), myself.calcGun());
  1297. od_printf("`bright yellow`Armor: %8d %8d\r\n", myself.get_armor(), myself.calcArmor());
  1298. od_printf("`bright cyan`Shields: %8d %8d\r\n", myself.get_shield(), myself.calcShield());
  1299. od_printf("`bright white`\r\n ___ Situation Report ___\r\n");
  1300. od_printf("`bright white`Items: Maximum: Current:\r\n");
  1301. od_printf("`bright green`Hitpoints: %8d %8d\r\n", myself.calcHP(), myself.get_hitpoints());
  1302. od_printf("`bright yellow`Armor: %8d %8d\r\n", myself.calcAP(), myself.get_armorpoints());
  1303. od_printf("`bright cyan`Shields: %8d %8d\r\n", myself.calcSP(), myself.get_shieldpoints());
  1304. od_printf("`bright magenta`Experience: %8d\r\n", myself.get_experience());
  1305. od_printf("`bright magenta`Fuel: %8d\r\n", myself.get_fuel());
  1306. od_printf("`white`Metal: %8d\r\n", myself.get_metal());
  1307. paws();
  1308. od_clr_scr();
  1309. break;
  1310. }
  1311. }
  1312. }
  1313. }
  1314. void sysop_menu() {
  1315. int done = 0;
  1316. char ch;
  1317. int targ_ply = 1;
  1318. int targ_max = 0;
  1319. int done1 = 0;
  1320. User targ;
  1321. while(!done) {
  1322. od_clr_scr();
  1323. od_send_file("ansis/sc_sysop.ans");
  1324. ch = od_get_answer("RrNnQq");
  1325. //od_printf("\r\n");
  1326. switch(tolower(ch)) {
  1327. case 'q':
  1328. done = 1;
  1329. break;
  1330. case 'r':
  1331. od_printf("`bright red`__ Reset ___\r\n");
  1332. od_printf("`bright red`DANGER THIS WILL DELETE ALL PLAYERS IN GAME\r\n");
  1333. if(yesNo() == 0) {
  1334. od_printf("`bright red`ABORTED!\r\n");
  1335. paws();
  1336. break;
  1337. }
  1338. ZF_LOGW("%s issued reset", od_control.user_name);
  1339. delete_players();
  1340. paws();
  1341. break;
  1342. case 'n':
  1343. od_printf("`bright red`__ New Day ___\r\n");
  1344. while(!done1) {
  1345. targ = load_player(targ_ply);
  1346. if(targ.get_uid() != 0) {
  1347. targ.rmLaston(1); // Travel back 1 day
  1348. update_player(targ); // Save
  1349. } else {
  1350. done1 = 1;
  1351. }
  1352. targ_ply += 1;
  1353. targ_max += 1;
  1354. }
  1355. od_printf("`bright red`%d players affected\r\n", (targ_max - 1));
  1356. targ_ply = 1;
  1357. targ_max = 0;
  1358. done1 = 0;
  1359. ZF_LOGW("%s issued new day", od_control.user_name);
  1360. paws();
  1361. break;
  1362. }
  1363. }
  1364. od_clr_scr();
  1365. }
  1366. void cleanMe() {
  1367. rel_lock();
  1368. od_exit(0, FALSE);
  1369. }
  1370. void main_menu() {
  1371. // Main menu for once things are done being initalized
  1372. int done = 0;
  1373. int done1 = 0; // For Reset Game and for New Day... also for Debug
  1374. int targ_ply = 1; // Target player to perform op!
  1375. int targ_max = 0; // Total number of players affected!
  1376. User targ; // Player Struct
  1377. User test;
  1378. dT targ_age; // Date Struct
  1379. dT test2;
  1380. char targ_real[256];
  1381. char ch;
  1382. int auth; // Security
  1383. char sys[48]; // Security
  1384. // 0 means we got the lock first, 1 means someone else has it already!
  1385. while(!done) {
  1386. od_clr_scr();
  1387. od_send_file("ansis/sc_main.ans");
  1388. ch = od_get_answer("PpLlVvQqSsAa");
  1389. od_clr_scr();
  1390. switch(tolower(ch)){
  1391. case 'q':
  1392. // Quit
  1393. done = 1;
  1394. break;
  1395. case 'p':
  1396. // Play Game (Default if the player just hits enter)
  1397. // Check to see if we have a winner if so refuse to let anyone on! (Wait 3 days then reset the game!)
  1398. test = highest_player();
  1399. test2 = fromDate(compareDate(test.get_laston()));
  1400. if(test.get_experience() >= 8000) { // If the player has the minimum of 3000 exp to fight the Unkown One and wins earning 5000 thats 8000 minimum
  1401. if(test2.age >= 3) { // Auto reset the game or not allow the player to play!
  1402. delete_players();
  1403. ZF_LOGW("Auto-Reset Occured");
  1404. od_printf("`bright green`Game has been reset!\r\n");
  1405. paws();
  1406. } else { // And the lucky winner is!
  1407. int resetage = 3 - test2.age;
  1408. od_printf("`bright green`%s has won the game!\r\n", test.get_nick().c_str());
  1409. od_printf("`bright green`The Game will reset in %d days!\r\n", resetage);
  1410. ZF_LOGV("Auto-Reset will occur in %d days", resetage);
  1411. paws();
  1412. break;
  1413. }
  1414. }
  1415. play_game();
  1416. break;
  1417. case 'a':
  1418. about_game();
  1419. break;
  1420. case 'l':
  1421. // List Players in the game
  1422. display_all_players();
  1423. paws();
  1424. break;
  1425. case 'v':
  1426. // Version
  1427. od_clr_scr();
  1428. od_printf("`bright yellow`_____________________________\r\n");
  1429. od_printf("`bright yellow` Space Construct v%d.%d-%s\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_TYPE);
  1430. od_printf("`bright yellow` By: Beanzilla\r\n");
  1431. od_printf("`bright yellow` FsxNet 21:4/110\r\n");
  1432. od_printf("`bright yellow` HappyNet 637:1/110\r\n");
  1433. od_printf("`bright yellow`_____________________________\r\n");
  1434. paws();
  1435. break;
  1436. case 's':
  1437. auth = 0;
  1438. if(allowDev && strcmp(od_control.user_handle, "Beanzilla") == 0) {
  1439. od_printf("`bright green`Access Granted\r\n");
  1440. ZF_LOGV("Dev accessed sysop menu");
  1441. auth = 1;
  1442. } else {
  1443. od_printf("`bright red`Sysop Password: ");
  1444. strcpy(sys, "");
  1445. od_input_str(sys, 32, 32, 126);
  1446. od_clr_scr(); // Add a bit of protecton
  1447. if(sys == sysop_pass) {
  1448. od_printf("`bright green`Access Granted\r\n");
  1449. ZF_LOGV("%s accessed sysop menu", od_control.user_name);
  1450. auth = 1;
  1451. }
  1452. }
  1453. // Give only Sysop and dev access to restricted commands
  1454. if(auth) {
  1455. paws();
  1456. sysop_menu();
  1457. } else {
  1458. od_printf("`bright red`Access Denied\r\n");
  1459. ZF_LOGW("%s tried to access sysop menu", od_control.user_name);
  1460. paws();
  1461. }
  1462. break;
  1463. }
  1464. }
  1465. }
  1466. int main(int argc, char *argv[]) {
  1467. od_parse_cmd_line(argc, argv);
  1468. // Initiate Door
  1469. od_init();
  1470. od_clr_scr();
  1471. // Establish Logging
  1472. if(file_output_open("logs/sc.log") == 0) {
  1473. od_printf("`bright red`Error Log file not open!\r\n");
  1474. od_exit(-1, false);
  1475. }
  1476. // Debug System
  1477. if (debug) {
  1478. ZF_LOGV("=== Debug ===");
  1479. ZF_LOGV("DropFile...");
  1480. log_drop();
  1481. int test = check_lock();
  1482. if (test == 0) {
  1483. ZF_LOGV("Lock is: Avalible");
  1484. ZF_LOGV("Database...");
  1485. db_test();
  1486. } else {
  1487. ZF_LOGV("Lock is: Taken");
  1488. ZF_LOGV("CANCELED Database Dump");
  1489. }
  1490. } else {
  1491. ZF_LOGV("--- Debug ---");
  1492. }
  1493. // Normal Operations
  1494. inuse = check_lock();
  1495. if(!inuse) {
  1496. int lock = grab_lock();
  1497. if(!lock) {
  1498. od_printf("`white`Something went wrong getting the lock.\r\n");
  1499. ZF_LOGF("Lock issue");
  1500. paws();
  1501. od_exit(0, FALSE);
  1502. } else {
  1503. atexit(cleanMe);
  1504. check_database(); // Ensure the users table exists.
  1505. main_menu();
  1506. }
  1507. } else {
  1508. od_send_file("ansis/sc_inuse.ans");
  1509. ZF_LOGI("In use");
  1510. paws();
  1511. od_exit(0, FALSE);
  1512. }
  1513. // Odoors testing
  1514. // Screensaving
  1515. /*char screen[4096]; // 4MB buffer
  1516. // Draw box
  1517. if(od_draw_box(5, 1, 20, 15) == true) {
  1518. od_set_cursor(3, 7);
  1519. od_printf("`white`Box is `bright green`GOOD");
  1520. } else {
  1521. od_set_cursor(3, 7);
  1522. od_printf("`white`Box is `bright red`BAD");
  1523. }
  1524. od_set_cursor(25, 1);
  1525. paws();
  1526. od_save_screen(screen);
  1527. od_clr_scr();
  1528. od_set_cursor(1, 1);
  1529. // Popup
  1530. int run = 1;
  1531. while(run)
  1532. {
  1533. switch(od_popup_menu("Main Menu",
  1534. "^Files|^Electronic Mail|^News|E^xit",
  1535. 20, 5, 0, MENU_NORMAL | MENU_KEEP))
  1536. {
  1537. case 1:
  1538. od_set_cursor(25, 1);
  1539. od_printf("Files");
  1540. od_popup_menu("Files Menu",
  1541. "^Search For Files|^Download|^Upload",
  1542. 30, 7, 1, MENU_NORMAL | MENU_ALLOW_CANCEL | MENU_KEEP);
  1543. break;
  1544. case 2:
  1545. od_set_cursor(25, 1);
  1546. od_printf("Email");
  1547. od_popup_menu("EMail Menu",
  1548. "Get ^New Mail|^Send Mail|Send ^Fax",
  1549. 30, 8, 2, MENU_NORMAL | MENU_ALLOW_CANCEL | MENU_KEEP);
  1550. break;
  1551. case 3:
  1552. od_set_cursor(25, 1);
  1553. od_printf("News");
  1554. od_popup_menu("News Menu",
  1555. "Choose News^Group|^Read News|^Post News",
  1556. 30, 9, 3, MENU_NORMAL | MENU_ALLOW_CANCEL | MENU_KEEP);
  1557. break;
  1558. case 4:
  1559. od_set_cursor(25, 1);
  1560. od_printf("Exit");
  1561. od_popup_menu(NULL, NULL, 0, 0, 0, MENU_DESTROY);
  1562. run = 0;
  1563. break;
  1564. case POPUP_ERROR:
  1565. od_set_cursor(25, 1);
  1566. od_printf("ERROR");
  1567. break;
  1568. case POPUP_ESCAPE:
  1569. od_set_cursor(25, 1);
  1570. od_printf("ESCAPE");
  1571. break;
  1572. case POPUP_LEFT:
  1573. od_set_cursor(25, 1);
  1574. od_printf("LEFT");
  1575. break;
  1576. case POPUP_RIGHT:
  1577. od_set_cursor(25, 1);
  1578. od_printf("RIGHT");
  1579. break;
  1580. default:
  1581. od_set_cursor(25, 1);
  1582. od_printf("DEFAULT");
  1583. break;
  1584. }
  1585. }
  1586. paws();
  1587. od_restore_screen(screen); */
  1588. od_exit(0, false);
  1589. }