1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780 |
- /*
- Space Construct v0.5-dev
- Beanzilla@21:4/110 on FsxNet
- Beanzilla@637:1/110 on HappyNet
- */
- #define VERSION_MAJOR 0
- #define VERSION_MINOR 5
- #ifndef VERSION_TYPE
- #define VERSION_TYPE "dev"
- #endif
- #define PATH_MAX 256
- #define PATH_SEP "/"
- #include <MagiDoor.h>
- #include <sqlite3.h>
- #if defined(_MSC_VER) || defined(WIN32)
- #define snprintf _snprintf
- #define strcasecmp _stricmp
- #include <winsock2.h>
- #ifndef _MSC_VER
- #define _MSC_VER 1
- #endif
- #else
- #include <arpa/inet.h>
- #endif
- // Standard C
- #include <time.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h> // stat
- char * log_path = "logs";
- int inuse = 0; // Are any other copies of us running? (We are a single user door!)
- int debug = 0; // Are we in debug mode?
- // User Structure (This includes guns, fuel, armor, shields, HP)
- typedef struct user_info {
- int uid; // Primary Key
- int experience; // Think about this as score
- // Was ship structure, now is part of user
- int guns; // Placed Guns, More means more damage (Deals from 1-2 damage per attack)
- int fuel; // How many "turns" do we have
- 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)
- int armors; // Placed Armors (Each Armor takes 4 points of damage then breaks off)
- 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)
- int hitpoints; // Total hitpoints till we die (Rules for taking damage, shields first then armor then hitpoints)
- int armorpoints; // Points we get from armors (Repairable but costs 1 metal per point, but can break if taking too much damage)
- int shieldpoints; // Points we get from shields (Self-Regenerates, and does not get destroyed)
- int shieldsup; // 0 means yes, anything else means shields are offline, deduct if not 0 for each attack.
- int laston; // 20200630 is 6-30-2020
- char nick[256]; // What they go by on score board, this allows duping since we really go by real
- char real[256]; // Their real name to match with drop file info, prevents someone from loging in as another
- } user_inf;
- // DateStamp Structure to provide date difference
- typedef struct dateTamp {
- int year; // YYYY, or number of years difference
- int month; // MM, or number of months difference
- int day; // DD, or number of days difference.
- int age; // Taking down to aproximate day count.
- } dT; // YYYYMMDD it in int form
- void dolog(char *fmt, ...) {
- // Low end Logging
- char buffer[PATH_MAX];
- struct tm *time_now;
- time_t timen;
- FILE *logfptr;
- timen = time(NULL);
- time_now = localtime(&timen);
- if (log_path != NULL) {
- snprintf(buffer, PATH_MAX, "%s%s%04d%02d%02d.log", log_path, PATH_SEP,
- time_now->tm_year + 1900, time_now->tm_mon + 1, time_now->tm_mday);
- } else {
- snprintf(buffer, PATH_MAX, "%04d%02d%02d.log", time_now->tm_year + 1900,
- time_now->tm_mon + 1, time_now->tm_mday);
- }
- logfptr = fopen(buffer, "a");
- if (!logfptr) {
- return;
- }
- va_list ap;
- va_start(ap, fmt);
- vsnprintf(buffer, 512, fmt, ap);
- va_end(ap);
- fprintf(logfptr, "%02d:%02d:%02d %s\n", time_now->tm_hour, time_now->tm_min,
- time_now->tm_sec, buffer);
- fclose(logfptr);
- }
- int dateStamp() {
- // YYYYMMDD \o/ In a INT so we can store it and compare against it!
- struct tm *time_now;
- time_t timen;
- int result = 0;
- timen = time(NULL);
- time_now = localtime(&timen);
- result += ((time_now->tm_year + 1900) * 10000);
- result += (time_now->tm_mon * 100) + 100;
- result += time_now->tm_mday;
- return result;
- }
- int compareDate(int dt) {
- // Returns integer of difference, from now.
- struct tm *time_now;
- time_t timen;
- int now = 0;
- timen = time(NULL);
- time_now = localtime(&timen);
- now += ((time_now->tm_year + 1900) * 10000);
- now += (time_now->tm_mon * 100) + 100;
- now += time_now->tm_mday;
- return (now - dt); // 10000 = 1 Year, 100 = 1 Month, 1 = 1 Day
- }
- dT formDate(int diff) {
- // Reverse process so we can compare exact days or get the structure back out too.
- dT result;
- result.year = 0;
- result.month = 0;
- result.day = 0;
- result.age = 0;
- int notDone = 1;
- while(notDone) {
- if(diff >= 10000) {
- diff -= 10000;
- result.year += 1;
- } else if(diff >= 100) {
- diff -= 100;
- result.month += 1;
- } else if(diff >= 1) {
- diff -= 1;
- result.day += 1;
- } else {
- notDone = 0;
- }
- } // Process Aprox day count
- if(result.year != 0) {
- result.age += (365 * result.year);
- } else if(result.month != 0) {
- result.age += (30 * result.month);
- } else if(result.day != 0) {
- result.age += result.day;
- }
- return result;
- }
- int db_test() {
- // Displays all users in the database
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[256];
- char strbuffer[256];
- int sizeofdb = 0;
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(1);
- }
- sqlite3_busy_timeout(db, 5000);
- // DB open
- strcpy(sqlbuffer, "SELECT * FROM user;");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer), &stmt, NULL);
- while(sqlite3_step(stmt) == SQLITE_ROW) {
- 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",
- sqlite3_column_name(stmt, 0), // int, uid
- sqlite3_column_int(stmt, 0),
- sqlite3_column_name(stmt, 1), // text, nick
- sqlite3_column_text(stmt, 1),
- sqlite3_column_name(stmt, 2), // text, real
- sqlite3_column_text(stmt, 2),
- sqlite3_column_name(stmt, 3), // int, experiece
- sqlite3_column_int(stmt, 3),
- sqlite3_column_name(stmt, 4), // int, metal
- sqlite3_column_int(stmt, 4),
- sqlite3_column_name(stmt, 5), // int, fuel
- sqlite3_column_int(stmt, 5),
- sqlite3_column_name(stmt, 6), // int, guns
- sqlite3_column_int(stmt, 6),
- sqlite3_column_name(stmt, 7), // int, armors
- sqlite3_column_int(stmt, 7),
- sqlite3_column_name(stmt, 8), // int, shields
- sqlite3_column_int(stmt, 8),
- sqlite3_column_name(stmt, 9), // int, armorpoints
- sqlite3_column_int(stmt, 9),
- sqlite3_column_name(stmt, 10), // int, shieldpoints
- sqlite3_column_int(stmt, 10),
- sqlite3_column_name(stmt, 11), // int, hitpoints
- sqlite3_column_int(stmt, 11),
- sqlite3_column_name(stmt, 12), // int, shieldsup
- sqlite3_column_int(stmt, 12),
- sqlite3_column_name(stmt, 13), // int, laston
- sqlite3_column_int(stmt, 13)
- );
- sizeofdb += 1;
- } // Clean up database
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- return sizeofdb;
- }
- int check_lock() {
- // Checks stats of lock, (1 = Lock is in effect, 0 = No Lock established)
- struct stat s;
- if (stat("lock.flg", &s) == 0) {
- return 1;
- } else {
- return 0;
- }
- }
- void grab_lock() {
- // Attempt to grab lock
- int valid = check_lock();
- FILE *fhandle;
- if (valid == 0) {
- fhandle = fopen("lock.flg", "w");
- if(!fhandle) {
- dolog("E: Unable to make lock.flg!");
- fprintf(stderr, "Unable to establish lock!\r\n");
- md_exit(-1);
- }
- fprintf(fhandle, "I am in use already!\n");
- fclose(fhandle);
- } else {
- dolog("W: Lock already established!");
- }
- }
- void rel_lock() {
- // Attempt to release lock
- int valid = check_lock();
- if (valid == 1) {
- if (unlink("lock.flg") != 0) {
- dolog("E: Unable to release lock.flg!");
- fprintf(stderr, "Unable to release lock!\r\n");
- md_exit(-1);
- }
- } else {
- dolog("W: Lock already released!");
- }
- }
- void log_drop() {
- // Spits out info from Drop File:
- md_printf("`white`First=%s Last=%s Alias=%s TimeLeft=%d SecLevel=%d Location=%s Node=%d Socket=%d Sysop=%s\r\n",
- mdcontrol.user_firstname,
- mdcontrol.user_lastname,
- mdcontrol.user_alias,
- mdcontrol.user_timeleft,
- mdcontrol.user_seclevel,
- mdcontrol.user_location,
- mdcontrol.node,
- mdcontrol.socket,
- mdcontrol.sysop_name
- );
- }
- void paws() {
- // Aaah, DRY
- md_printf("`white`Press any key to continue...");
- md_getc();
- md_printf("\r\n");
- }
- int yesNo() {
- char ch;
- int done = 0;
- md_printf("`bright black`(`bright white`Y`bright black`)`white`es `bright black`(`bright red`N`bright black`)`red`o`white`");
- ch = md_get_answer("YyNn\r");
- md_printf("\r\n");
- switch(tolower(ch)) {
- case 'y':
- done = 1;
- break;
- case '\r':
- case 'n':
- break;
- }
- return done;
- }
- int randrange(int min, int max){
- return min + rand() / (RAND_MAX / (max - min + 1) + 1);
- }
- int locate_player(char first[], char last[]) {
- // returns user id for given real name and 0 for no record found
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[256];
- int result = 0;
- char name[256];
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_busy_timeout(db, 5000);
- // Form Real Name
- strcpy(name, "");
- strcat(name, first);
- strcat(name, " ");
- strcat(name, last);
- //md_printf("realname = '%s'\r\n", name);
- // Locating user with given name
- strcpy(sqlbuffer, "SELECT * from user where real=? COLLATE NOCASE;");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- sqlite3_bind_text(stmt, 1, name, strlen(name), SQLITE_STATIC);
- rc = sqlite3_step(stmt);
- if (rc == SQLITE_ROW) {
- //dolog("User=%s uid=%d", name, sqlite3_column_int(stmt, 0));
- result = sqlite3_column_int(stmt, 0);
- } else {
- dolog("W: Unable to locate user=%s", name);
- result = 0;
- } // Clean Up, return results
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- return result;
- }
- user_inf load_player(int uuid) {
- // Returns a player Structure from database
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[256];
- user_inf result;
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_busy_timeout(db, 5000);
- strcpy(sqlbuffer, "SELECT * FROM user WHERE uid=?;");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- sqlite3_bind_int(stmt, 1, uuid);
- rc = sqlite3_step(stmt);
- if (rc == SQLITE_ROW) {
- result.uid = sqlite3_column_int(stmt, 0);
- strcpy(result.nick, sqlite3_column_text(stmt, 1));
- strcpy(result.real, sqlite3_column_text(stmt, 2));
- result.experience = sqlite3_column_int(stmt, 3);
- result.metal = sqlite3_column_int(stmt, 4);
- result.fuel = sqlite3_column_int(stmt, 5);
- result.guns = sqlite3_column_int(stmt, 6);
- result.armors = sqlite3_column_int(stmt, 7);
- result.shields = sqlite3_column_int(stmt, 8);
- result.armorpoints = sqlite3_column_int(stmt, 9);
- result.shieldpoints = sqlite3_column_int(stmt, 10);
- result.hitpoints = sqlite3_column_int(stmt, 11);
- result.shieldsup = sqlite3_column_int(stmt, 12);
- result.laston = sqlite3_column_int(stmt, 13);
- } else {
- //dolog("E: Unable to locate user with id=%d got %s (%d)", uuid, sqlite3_errmsg(db), rc);
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- result.uid = 0;
- return result;
- }
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- return result;
- }
- user_inf highest_player() {
- // Used to find if someone may have fought the Unknown One and won the game already!
- int done = 0;
- user_inf test;
- int high = 0;
- int high_exp = 0;
- int id = 1;
- while(!done) {
- test = load_player(id);
- if(test.uid != 0) {
- if(test.experience > high_exp) {
- // Update highest player id and experience
- high_exp = test.experience;
- high = id;
- }
- } else {
- done = 1;
- }
- id += 1;
- } // Ok highest experiencing player found!
- test = load_player(high);
- return test;
- }
- void update_player(user_inf data) {
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[1024];
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_busy_timeout(db, 5000);
- // Bad, don't do this... opens to SQL injection!
- //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;",
- // data.nick, data.experience, data.metal, data.fuel, data.guns, data.armors, data.shields, data.armorpoints, data.shieldpoints, data.hitpoints, data.uid);
- strcpy(sqlbuffer, "UPDATE user SET nick=?, experience=?, metal=?, fuel=?, guns=?, armors=?, shields=?, armorpoints=?, shieldpoints=?, hitpoints=?, shieldsup=?, laston=? WHERE uid=?;");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- // Bind All data values
- sqlite3_bind_text(stmt, 1, data.nick, strlen(data.nick), SQLITE_STATIC);
- sqlite3_bind_int(stmt, 2, data.experience);
- sqlite3_bind_int(stmt, 3, data.metal);
- sqlite3_bind_int(stmt, 4, data.fuel);
- sqlite3_bind_int(stmt, 5, data.guns);
- sqlite3_bind_int(stmt, 6, data.armors);
- sqlite3_bind_int(stmt, 7, data.shields);
- sqlite3_bind_int(stmt, 8, data.armorpoints);
- sqlite3_bind_int(stmt, 9, data.shieldpoints);
- sqlite3_bind_int(stmt, 10, data.hitpoints);
- sqlite3_bind_int(stmt, 11, data.shieldsup);
- sqlite3_bind_int(stmt, 12, data.laston);
- sqlite3_bind_int(stmt, 13, data.uid);
- // Execute
- rc = sqlite3_step(stmt);
- if(rc != SQLITE_DONE) {
- dolog("E: failed updating player=%d got error %s (%d)", data.uid, sqlite3_errmsg(db), rc);
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- }
- int create_player(user_inf data) {
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[1024];
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_busy_timeout(db, 5000);
- strcpy(sqlbuffer, "INSERT INTO user (nick, real, experience, metal, fuel, guns, armors, shields, armorpoints, shieldpoints, hitpoints, shieldsup, laston) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- sqlite3_bind_text(stmt, 1, data.nick, strlen(data.nick), SQLITE_STATIC);
- sqlite3_bind_text(stmt, 2, data.real, strlen(data.real), SQLITE_STATIC);
- sqlite3_bind_int(stmt, 3, data.experience);
- sqlite3_bind_int(stmt, 4, data.metal);
- sqlite3_bind_int(stmt, 5, data.fuel);
- sqlite3_bind_int(stmt, 6, data.guns);
- sqlite3_bind_int(stmt, 7, data.armors);
- sqlite3_bind_int(stmt, 8, data.shields);
- sqlite3_bind_int(stmt, 9, data.armorpoints);
- sqlite3_bind_int(stmt, 10, data.shieldpoints);
- sqlite3_bind_int(stmt, 11, data.hitpoints);
- sqlite3_bind_int(stmt, 12, data.shieldsup);
- sqlite3_bind_int(stmt, 13, data.laston);
- rc = sqlite3_step(stmt);
- if(rc != SQLITE_DONE) {
- dolog("E: failed inserting player=%d got error %s (%d)", data.nick, sqlite3_errmsg(db), rc);
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- return 1; // Good
- }
- // Removed due to fact it throws off the internal id system
- /*void delete_player(int uuid) {
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[256];
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_busy_timeout(db, 5000);
- strcpy(sqlbuffer, "DELETE FROM user WHERE uid=?;");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- sqlite3_bind_int(stmt, 1, uuid);
- rc = sqlite3_step(stmt);
- if (rc != SQLITE_DONE){
- dolog("E: Unable to locate user with id=%d got %s (%d)", uuid, sqlite3_errmsg(db), rc);
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- }*/
- void delete_players() {
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[256];
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_busy_timeout(db, 5000);
- strcpy(sqlbuffer, "DELETE FROM user;");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- rc = sqlite3_step(stmt);
- if (rc != SQLITE_DONE){
- dolog("E: Delete_players 1/2, Unexpected error got %s (%d)", sqlite3_errmsg(db), rc);
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- md_exit(-1);
- }
- strcpy(sqlbuffer, "UPDATE sqlite_sequence SET seq = 0 WHERE name='user';");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- rc = sqlite3_step(stmt);
- if (rc != SQLITE_DONE){
- dolog("E: Delete_players 2/2, Unexpected error got %s (%d)", sqlite3_errmsg(db), rc);
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- md_exit(-1);
- }
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- }
- int display_all_players() {
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[1024];
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(1);
- }
- sqlite3_busy_timeout(db, 5000);
- strcpy(sqlbuffer, "SELECT * from user ORDER BY experience DESC, nick DESC;");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- int total = 0;
- md_printf("`bright green`___ The Players ___\r\n");
- while(sqlite3_step(stmt) == SQLITE_ROW) {
- md_printf("`bright white` %s (%d)\r\n", sqlite3_column_text(stmt, 1), sqlite3_column_int(stmt, 3));
- total += 1;
- }
- if(total > 1) {
- md_printf("`green`There are a total of `bright green`%d`green` players\r\n", total);
- } else if(total == 0) {
- md_printf("`green`There are a total of `bright green`%d`green` players\r\n", total);
- } else {
- md_printf("`green`There are a total of `bright green`%d`green` player\r\n", total);
- }
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- return total;
- }
- int display_all_opponents(int us) {
- sqlite3 *db;
- sqlite3_stmt *stmt;
- char sqlbuffer[1024];
- int rc = sqlite3_open("spaceconstruct.db3", &db);
- if(rc) { // Did we do a successful open?
- dolog("E: failed opening database %s", sqlite3_errmsg(db));
- sqlite3_close(db);
- md_exit(1);
- }
- sqlite3_busy_timeout(db, 5000);
- strcpy(sqlbuffer, "SELECT * from user ORDER BY experience DESC, nick DESC;");
- sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
- int total = 1;
- md_printf("`bright green`___ Target List ___\r\n");
- while(sqlite3_step(stmt) == SQLITE_ROW) {
- if(total != us) {
- md_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));
- }
- total += 1;
- }
- total -= 1;
- sqlite3_finalize(stmt);
- sqlite3_close(db);
- return total;
- }
- user_inf build_menu(user_inf my) {
- int done = 0;
- char ch;
- if(my.metal != 0) {
- while(!done) {
- md_clr_scr();
- md_sendfile("ansis/sc_build.ans", FALSE);
- // Paint over sc_build.ans file
- md_set_cursor(3, 15);
- md_printf("%5d", (4 * (my.guns + 1)));
- md_set_cursor(4, 15);
- md_printf("%5d", (3 * (my.armors + 1)));
- md_set_cursor(5, 15);
- md_printf("%5d", (6 * (my.shields + 1)));
- md_set_cursor(9, 0);
- // Resuming normal operations
- md_printf("`bright yellow`You have %d metal\r\n", my.metal);
- ch = md_get_answer("GgAaSsCcRr\r");
- switch(tolower(ch)) {
- case 'g':
- if(my.metal >= (4 * (my.guns + 1))) {
- my.metal -= (4 * (my.guns + 1));
- my.guns += 1;
- my.hitpoints += 1;
- md_printf("`bright green`Added a new gun!\r\n`white`");
- paws();
- } else {
- md_printf("`bright red`You don't have the %d metal for this!\r\n`white`", (4 * (my.guns + 1)));
- paws();
- }
- break;
- case 'a':
- if(my.metal >= (3 * (my.armors + 1))) {
- my.metal -= (3 * (my.armors + 1));
- my.armors += 1;
- my.hitpoints += 1;
- my.armorpoints += 8;
- md_printf("`bright green`Added a new armor!\r\n`white`");
- paws();
- } else {
- md_printf("`bright red`You don't have the %d metal for this!\r\n`white`", (3 * (my.armors + 1)));
- paws();
- }
- break;
- case 's':
- if(my.metal >= (6 * (my.shields + 1))) {
- my.metal -= (6 * (my.shields + 1));
- my.shields += 1;
- my.hitpoints += 1;
- if(my.shieldsup == 0) {
- my.shieldpoints += 3;
- } else {
- my.shieldpoints += 1;
- }
- md_printf("`bright green`Added a new shield!\r\n`white`");
- paws();
- } else {
- md_printf("`bright red`You don't have the %d metal for this!\r\n`white`", (6 * (my.shields + 1)));
- paws();
- }
- break;
- case '\r':
- case 'c':
- done = 1;
- break;
- case 'r':
- if(my.experience > 25) {
- if(my.metal > 25) {
- my.experience -= 25;
- my.metal -= 25;
- my.fuel += 5;
- md_printf("`bright green`Made 5 fuel using 25 experience and 25 metal!\r\n");
- paws();
- } else {
- md_printf("`bright red`You need 25 experience and 25 metal to make fuel!\r\n");
- paws();
- }
- } else {
- md_printf("`bright red`You need 25 experience and 25 metal to make fuel!\r\n");
- paws();
- }
- break;
- }
- }
- update_player(my);
- return my;
- } else {
- return my;
- }
- }
- void pvp_menu(user_inf my, user_inf targ) {
- int done = 0;
- int playerWon = 0;
- char ch;
- // Extract temp values, assume offline player is at their best
- int temp_hp = (targ.shields + targ.armors + targ.guns + 2);
- int temp_ap = (targ.armors * 8);
- int temp_sp = (targ.shields * 3);
- int temp_su = 0;
- int temp_dmg = (targ.guns + 1); // Add 1 additional point of damage for offline player
- // Prep Combat values
- int fire = 0; // Fire Guns
- int flee = 0; // Run away
- int regen = 0; // Shields
- int temp = 0;
- int temp1 = 0;
- while(!done) {
- // Process Shields up
- if(temp_su != 0) {
- temp_su -= 1;
- }
- if(my.shieldsup != 0) {
- my.shieldsup -= 1;
- }
- // Process Crew repairing hitpoints
- if(temp_hp < (targ.shields + targ.armors + targ.guns + 2)) {
- regen = rand() % 100;
- if(regen >= 85) { // 15%, offline get a bonus 5% on repairing hull.
- temp_hp += 1;
- md_printf("`bright green`%s`green`'s crew repaired their hull for 1 point!\r\n", targ.nick);
- }
- }
- if(my.hitpoints < (my.shields + my.armors + my.guns + 2)) {
- regen = rand() % 100;
- if(regen >= 90) { // 10%
- my.hitpoints += 1;
- md_printf("`bright green`Your crew repaired your hull for 1 point!\r\n");
- }
- }
-
- // Process Shields regenerating and display player stats
- if(temp_su == 0) {
- if(temp_sp < (targ.shields * 3)) {
- regen = rand() % 100;
- if(regen >= 65) { // 35%, offline bonus
- temp_sp += targ.shields;
- if(temp_sp > (targ.shields * 3)) {
- temp_sp = (targ.shields * 3);
- }
- md_printf("`bright green`%s`green`'s shields regenerate %d points!\r\n", targ.nick, targ.shields);
- }
- }
- md_printf("`bright white`%s has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left\r\n", targ.nick, temp_sp, temp_ap, temp_hp);
- } else {
- if(temp_sp < (targ.shields * 3)) {
- regen = rand() % 100;
- if(regen >= 65) { // 35%, offline bonus
- temp_sp += 1;
- if(temp_sp > (targ.shields * 3)) {
- temp_sp = (targ.shields * 3);
- }
- md_printf("`bright green`%s`green`'s shields regenerate 1 points!\r\n", targ.nick);
- }
- }
- md_printf("`bright white`%s has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left\r\n", targ.nick, temp_sp, temp_ap, temp_hp);
- }
- // Online Players Shields
- if(my.shieldsup == 0) {
- if(my.shieldpoints < (my.shields * 3)) {
- regen = rand() % 100;
- if(regen >= 70) { // 30%
- my.shieldpoints += my.shields;
- if(my.shieldpoints > (my.shields * 3)) {
- my.shieldpoints = (my.shields * 3);
- }
- md_printf("`bright green`Your shields regenerate %d points!\r\n", my.shields);
- }
- }
- md_printf("`bright white`Your ship has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left:\r\n", my.shieldpoints, my.armorpoints, my.hitpoints);
- } else {
- if(my.shieldpoints < (my.shields * 3)) {
- regen = rand() % 100;
- if(regen >= 70) { // 30%
- my.shieldpoints += 1;
- if(my.shieldpoints > (my.shields * 3)) {
- my.shieldpoints = (my.shields * 3);
- }
- md_printf("`bright green`Your shields regenerate 1 points!\r\n");
- }
- }
- md_printf("`bright white`Your ship has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left:\r\n", my.shieldpoints, my.armorpoints, my.hitpoints);
- }
- // Form combat menu (Not allowing online to repair since offline can't really repair either)
- md_printf("`bright black`(`bright white`A`bright black`)`white`ttack\r\n");
- md_printf("`bright black`(`bright white`F`bright black`)`white`lee\r\n");
- // Online Players action
- ch = md_get_answer("AaFf\r");
- switch(tolower(ch)) {
- case '\r':
- case 'a':
- fire = rand() % 100;
- temp = my.guns;
- if(fire >= 50) {
- // Proccess online dealing damage to offline
- if(temp_su == 0 && temp_sp != 0) {
- if(temp >= temp_sp) {
- temp_su = (targ.shields + 2);
- temp -= temp_sp;
- temp_sp = 0;
- md_printf("`bright cyan`%s's shields absorbed %d damage and went offline!\r\n", targ.nick, temp_dmg);
- } else {
- temp1 = temp;
- temp -= temp_sp;
- temp_sp -= temp1;
- md_printf("`bright cyan`%s's shields absorbed %d damage!\r\n", targ.nick, temp1);
- temp1 = 0;
- }
- } // Armor
- if(temp > 0) {
- if(temp_ap != 0) {
- if(temp >= temp_ap) {
- temp1 = temp;
- temp -= temp_ap;
- temp_ap = 0;
- md_printf("`yellow`%s's armor took %d damage and broke!\r\n`white`", targ.nick, temp1);
- temp1 = 0;
- } else {
- temp1 = temp;
- temp -= temp_ap;
- temp_ap -= temp1;
- md_printf("`bright yellow`%s's armor took %d damage!\r\n`white`", targ.nick, temp1);
- temp1 = 0;
- }
- }
- } // Offline's hitpoints!
- if(temp > 0) {
- if(temp < temp_hp) {
- temp1 = temp;
- temp -= temp_hp; // should be 0
- temp_hp -= temp1;
- md_printf("`bright red`%s's hull took %d damage!\r\n", targ.nick, temp1);
- temp1 = 0;
- } else {
- playerWon = 1; // Online won over Offline!
- done = 1;
- md_printf("`red`%s's hull took %d damage and broke!\r\n", targ.nick, temp);
- }
- }
- } else { // Online missed
- md_printf("`white`You missed!\r\n");
- }
- break;
- case 'f':
- flee = rand() % 100;
- if(flee >= 80) { // 20% chance the online flees
- playerWon = 3; // Online ran away from Offline
- done = 1;
- md_printf("`bright green`You ran away from %s!\r\n", targ.nick);
- } else {
- md_printf("`bright red`%s found you!\r\n", targ.nick);
- }
- break;
- } // Now for Offline to attack
- if(!playerWon) {
- temp = temp_dmg;
- fire = rand() % 100;
- if(fire >= 45) { // 55%, 5% bonus for offline
- if(my.shieldsup == 0 && my.shieldpoints != 0) {
- if(temp >= my.shieldpoints) {
- // Damage will lower shields!
- my.shieldsup = (my.shields + 2);
- temp -= my.shieldpoints;
- my.shieldpoints = 0;
- md_printf("`bright cyan`Shields absorbed %d damage and went offline!\r\n`white`", temp_dmg);
- } else {
- // Damage to shields but not lower them
- temp1 = temp;
- temp -= my.shieldpoints;
- my.shieldpoints -= temp1;
- temp1 = 0;
- md_printf("`bright cyan`Shields absorbed %d damage!\r\n`white`", temp_dmg);
- }
- }
- // shields now armor
- if(temp > 0) {
- if(my.armorpoints != 0) {
- // We do have armor
- if(temp >= my.armorpoints) {
- // Damage will destroy armor!
- temp1 = temp;
- temp -= my.armorpoints;
- my.armorpoints = 0;
- md_printf("`yellow`Armor took %d damage and broke!\r\n`white`", temp1);
- temp1 = 0;
- } else {
- // Nope armor is good
- temp1 = temp;
- temp -= my.armorpoints;
- my.armorpoints -= temp1;
- md_printf("`bright yellow`Armor took %d damage!\r\n`white`", temp1);
- temp1 = 0;
- }
- }
- }
- // armor now hull
- if(temp > 0) {
- if(temp < my.hitpoints) {
- temp1 = temp;
- temp -= my.hitpoints; // should be 0
- my.hitpoints -= temp1;
- md_printf("`bright red`Hull took %d damage!\r\n", temp1);
- } else {
- playerWon = 2; // offline won over online!
- done = 1;
- my.hitpoints = 0;
- md_printf("`red`Hull took %d damage and broke!\r\n", temp);
- }
- }
- }
- }
- }
- // Winning Move?
- if(playerWon) {
- switch(playerWon) {
- case 1: // Online won over offline
- if(targ.experience) { // Just incase someone is dumb and attacked a player with no exp
- my.experience += (targ.experience / 2); // Gain half exp from offline
- targ.experience -= (targ.experience / 2); // Process offline giving online that exp
- }
- if(targ.metal) {
- my.metal += (targ.metal / 2);
- targ.metal -= (targ.metal / 2);
- }
- my.fuel -= 4;
- if(my.fuel < 0) {
- my.fuel = 0;
- }
- md_printf("`bright green`Congrats on defeating %s!\r\n`white`", targ.nick);
- break;
- case 2: // Offline won over online!
- if(my.experience) {
- targ.experience += (my.experience / 2);
- my.experience -= (my.experience / 2);
- }
- if(my.metal) {
- targ.metal += (my.metal / 2);
- my.metal -= (my.metal / 2);
- }
- my.fuel -= 4;
- if(my.fuel < 0) {
- my.fuel = 0;
- }
- md_printf("`bright red`Sorry for your loss! (We rebuild your ship)\r\n");
- my.hitpoints = 4;
- my.armorpoints = 8;
- my.shieldpoints = 0;
- my.shieldsup = 0;
- my.armors = 1;
- my.shields = 0;
- my.guns = 1;
- break;
- case 3:
- my.fuel -= 1;
- break;
- }
- }
- // DOne
- update_player(targ);
- update_player(my);
- }
- user_inf combat_menu(user_inf my, int targ_hp, int targ_dmg, int asteroid) {
- int done = 0;
- int playerWon = 0; // Did the player win? (1 = Yes, 2 = No, 3 = Flee)
- char ch;
- int armorRep = 0; // Can armor be repaired?
- int temp = 0;
- int temp1 = 0;
- int temp2 = 0;
- int temp_hp = targ_hp;
- int flee = 0;
- int fire = 0;
- int regen = 0;
- while(!done) {
- if(asteroid == 0) {
- md_printf("`bright white`Pirate has %d hitpoints left\r\n", targ_hp);
- } else if(asteroid == 1) {
- md_printf("`bright white`Asteroid has %d hitpoints left\r\n", targ_hp);
- } else if(asteroid == 2) {
- md_printf("`bright white`Unknown One has %d hitpoints left\r\n", targ_hp);
- }
- // Proccess Shields comming back online
- if(my.shieldsup != 0) {
- my.shieldsup -= 1;
- }
- // Proccess Crew repairing hull
- if(my.hitpoints < (my.guns + my.armors + my.shields + 2)) {
- regen = rand() % 100;
- if(regen >= 90) {
- my.hitpoints += 1;
- if(my.hitpoints > (my.guns + my.armors + my.shields + 2)) {
- my.hitpoints = (my.guns + my.armors + my.shields + 2);
- }
- md_printf("`bright green`Your crew repaired your hull for 1 point!\r\n");
- }
- }
- if(my.shieldsup == 0) {
- // Proccess Shield Regeneration
- if(my.shieldpoints < (my.shields * 3)) {
- regen = rand() % 100;
- if(regen >= 70) {
- my.shieldpoints += my.shields;
- if(my.shieldpoints > (my.shields * 3)) {
- my.shieldpoints = (my.shields * 3);
- }
- md_printf("`bright green`Your shields regenerate %d points!\r\n", my.shields);
- }
- }
- md_printf("`bright white`Your ship has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left:\r\n", my.shieldpoints, my.armorpoints, my.hitpoints);
- } else {
- // Shields should still regenerate some
- if(my.shieldpoints < (my.shields * 3)) {
- regen = rand() % 100;
- if(regen >= 70) {
- my.shieldpoints += 1;
- if(my.shieldpoints > (my.shields * 3)) {
- my.shieldpoints = (my.shields * 3);
- }
- md_printf("`bright green`Your shields regenerate 1 points!\r\n");
- }
- }
- md_printf("`bright white`Your ship has `bright cyan`%d`bright white` shields, %d armor and %d hitpoints left:\r\n", my.shieldpoints, my.armorpoints, my.hitpoints);
- }
- // Forming Combat Menu
- md_printf("`bright black`(`bright white`A`bright black`)`white`ttack\r\n");
- if(my.armorpoints < (my.armors * 8)) {
- md_printf("`bright black`(`bright white`R`bright black`)`white`epair Armor\r\n");
- armorRep = 1; // Yes we can repair armor
- }
- md_printf("`bright black`(`bright white`F`bright black`)`white`lee\r\n");
- // Proccess User action
- ch = md_get_answer("AaRrFf\r");
- switch(tolower(ch)) {
- case '\r':
- case 'a':
- fire = rand() % 100;
- if(asteroid == 1) { // Its a rock so allow player to hit it more frequent
- fire += 15;
- }
- if(fire >= 50) {
- targ_hp -= my.guns;
- if(targ_hp <= 0) {
- md_printf("`bright red`Target Destroyed!\r\n");
- done = 1;
- playerWon = 1;
- } else {
- md_printf("`bright red`Target took %d damage!\r\n", my.guns);
- }
- } else {
- md_printf("`white`You Missed!\r\n");
- }
- break;
- case 'r':
- if(armorRep) {
- if(my.metal > 0) {
- my.metal -= 1;
- my.armorpoints += 2;
- if(my.armorpoints < (my.armors * 8)) {
- my.armorpoints = (my.armors * 8);
- }
- md_printf("`bright green`Repaired 2 Armor points!\r\n");
- } else {
- md_printf("`bright red`You need metal inorder to repair!\r\n");
- }
- } else {
- md_printf("`bright yellow`You don't seem to need repairs!\r\n");
- }
- break;
- case 'f':
- flee = rand() % 100;
- if(asteroid == 1) {
- flee = 100; // Always allow fleeing from asteroid
- } else if(asteroid == 2) {
- flee = 0; // Never allow fleeing from Unknown One
- }
- if(flee >= 82) {
- playerWon = 3;
- done = 1;
- md_printf("`bright green`You ran away!\r\n");
- } else {
- md_printf("`bright red`Target found you!\r\n");
- }
- break;
- } // If Enemy is alive proccess incomming attack
- if(targ_hp > 0) {
- temp = targ_dmg;
- fire = rand() % 100;
- if(asteroid == 1) { // Yup it doesn't have gunz cuz it's rock.
- fire = 0;
- } else if(asteroid == 2) { // This is the Unknown One
- fire += 5;
- }
- if(fire >= 40) {
- // Shields
- if(my.shieldsup == 0 && my.shieldpoints != 0) {
- if(temp >= my.shieldpoints) {
- // Damage will lower shields!
- my.shieldsup = (my.shields + 2);
- temp -= my.shieldpoints;
- my.shieldpoints = 0;
- md_printf("`bright cyan`Shields absorbed %d damage and went offline!\r\n`white`", targ_dmg);
- } else {
- // Damage to shields but not lower them
- temp1 = temp;
- temp -= my.shieldpoints;
- my.shieldpoints -= temp1;
- temp1 = 0;
- md_printf("`bright cyan`Shields absorbed %d damage!\r\n`white`", targ_dmg);
- }
- }
- // shields now armor
- if(temp > 0) {
- if(my.armorpoints != 0) {
- // We do have armor
- if(temp >= my.armorpoints) {
- // Damage will destroy armor!
- temp2 = temp;
- temp -= my.armorpoints;
- my.armorpoints = 0;
- md_printf("`yellow`Armor took %d damage and broke!\r\n`white`", temp2);
- } else {
- // Nope armor is good
- temp1 = temp;
- temp2 = temp;
- temp -= my.armorpoints;
- my.armorpoints -= temp1;
- temp1 = 0;
- md_printf("`bright yellow`Armor took %d damage!\r\n`white`", temp2);
- }
- }
- }
- // armor now hull
- if(temp > 0) {
- if(temp < my.hitpoints) {
- temp1 = temp;
- temp -= my.hitpoints; // should be 0
- my.hitpoints -= temp1;
- md_printf("`bright red`Hull took %d damage!\r\n", temp1);
- } else {
- playerWon = 2; // Nope the target did us in
- done = 1;
- my.hitpoints = 0;
- md_printf("`red`Hull took %d damage and broke!\r\n", temp);
- }
- }
- }
- // firing
- }
- // Winning move?
- if(playerWon) {
- if(playerWon == 1) {
- // Player won
- my.experience += (temp_hp + targ_dmg);
- my.metal += temp_hp;
- if(asteroid == 1) { // If asteroid reward more experience and metal!
- my.experience += 2;
- my.metal += 2;
- } else if(asteroid == 2) { // If it was the boss!
- my.experience += 6000; // Ensure the game triggers a reset!
- my.metal = 0; // Stop the player!
- my.fuel = 2; // Prevent the player from doing anything else
- }
- my.fuel -= 2;
- if(my.fuel < 0) {
- my.fuel = 0;
- }
- md_printf("`bright green`Congrats on your victory!\r\n`white`");
- } else if(playerWon == 2) {
- // Target won
- my.experience -= (temp_hp + targ_dmg);
- my.metal -= temp_hp;
- my.fuel -= 3;
- if(my.experience < 0) {
- my.experience = 0;
- }
- if(my.metal < 0) {
- my.metal = 0;
- }
- if(my.fuel < 0) {
- my.fuel = 0;
- }
- md_printf("`bright red`Sorry for your loss! (We rebuild your ship)\r\n");
- my.hitpoints = 4;
- my.armorpoints = 8;
- my.shieldpoints = 0;
- my.shieldsup = 0;
- my.armors = 1;
- my.shields = 0;
- my.guns = 1;
- } else if(playerWon == 3) {
- // Chicken!
- my.fuel -= 1;
- }
- }
- }
- update_player(my);
- return my;
- }
- void about_game() {
- int done = 0;
- int page = 1;
- char p[12];
- char targ[256];
- while(!done) {
- switch(page) {
- case 1:
- md_clr_scr();
- md_sendfile("ansis/sc_doc_1.ans", FALSE);
- paws();
- break;
- case 2:
- md_clr_scr();
- md_sendfile("ansis/sc_doc_2.ans", FALSE);
- paws();
- break;
- case 3:
- md_clr_scr();
- md_sendfile("ansis/sc_doc_3.ans", FALSE);
- paws();
- break;
- case 4:
- md_clr_scr();
- md_sendfile("ansis/sc_doc_4.ans", FALSE);
- paws();
- break;
- case 5:
- md_clr_scr();
- md_sendfile("ansis/sc_doc_5.ans", FALSE);
- paws();
- break;
- case 6:
- done = 1;
- break;
- }
- md_clr_scr();
- page += 1;
- }
- }
- void play_game() {
- int done = 0;
- char ch;
- char ch1[256];
- user_inf myself;
- user_inf myself2;
- user_inf testing;
- int reset = 0;
- int doRefuel = 0;
- int pirate_encounter = 0;
- int pirate_hp = 0; // Reused for asteroid
- int pirate_dmg = 0; // Reused for asteroid
- int asteroid = 0; // 1 means yes we need to remove damage... completely.
- int me = locate_player(mdcontrol.user_firstname, mdcontrol.user_lastname);
- int abort = 0;
- if(me != 0) {
- //myself = load_player(me);
- testing = load_player(me);
- if(testing.uid != 0) {
- myself = load_player(me);
- }
- dT age = formDate(compareDate(myself.laston));
- // Process player inactivity
- if(age.age >= 30) { // 30 days
- md_printf("`bright green`Since you haven't played a while we have reset your account!\r\n");
- me = 0;
- reset = 1;
- }
- // Processing fuel regeneration
- if(age.age != 0) {
- doRefuel = 1;
- }
- }
- if(me != 0) {
- md_printf("`bright white`Welcome back `bright green`%s\r\n", myself.nick);
- dT age = formDate(compareDate(myself.laston));
- 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)",
- myself.nick, myself.guns, myself.armors, myself.shields, myself.fuel, myself.metal, myself.armorpoints,
- myself.shieldpoints, myself.shieldsup, myself.hitpoints, myself.laston, age.age);
- myself.laston = dateStamp();
- if(age.day != 0 || age.month != 0 || age.year != 0) {
- md_printf("`bright yellow`Haven't seen you for");
- }
- if(age.day != 0) {
- if(age.day > 1) {
- md_printf(" %d days", age.day);
- } else {
- md_printf(" %d day", age.day);
- }
- }
- if(age.month != 0) {
- if(age.month > 1) {
- md_printf(" %d months", age.month);
- } else {
- md_printf(" %d month", age.month);
- }
- }
- if(age.year != 0) {
- if(age.year > 1) {
- md_printf(" %d years", age.year);
- } else {
- md_printf(" %d year", age.year);
- }
- }
- if(age.day != 0 || age.month != 0 || age.year != 0) {
- md_printf("\r\n");
- }
- if(doRefuel) {
- if(myself.fuel != 0) {
- myself.fuel += (age.age * 2);
- } else {
- myself.fuel += ((age.age * 2) + 10);
- md_printf("`bright green`For using all your fuel last time you get bonus fuel today!\r\n");
- }
- md_printf("`bright green`You now have %d fuel\r\n", myself.fuel);
- }
- update_player(myself);
- paws();
- } else {
- md_printf("`bright white`You look new here. (Hit Enter to abort)\r\n");
- while(done == 0) {
- md_printf("`bright yellow`What's your name: ");
- md_getstring(ch1, 26, 32, 126);
- md_printf("\r\n");
- if(strlen(ch1) > 0) {
- //md_getstring(ch1, 256, 32, 126);
- strcpy(myself.nick, ch1);
- } else {
- md_clr_scr();
- abort = 1;
- done = 1;
- md_printf("`bright red`Ok, come back later then...`white`\r\n");
- paws();
- }
- if(abort == 0) {
- md_printf("`bright white`Are you sure you want to be called `bright green`%s\r\n");
- done = yesNo();
- if(done) {
- if(reset) { // So we are reseting the user, this wipes all values and uses update instead of create
- myself.experience = 0;
- myself.metal = 0;
- myself.fuel = 10;
- myself.guns = 1;
- myself.armors = 1;
- myself.shields = 0;
- myself.armorpoints = 8;
- myself.shieldpoints = 0;
- myself.hitpoints = 4;
- myself.shieldsup = 0;
- myself.laston = dateStamp();
- update_player(myself);
- } else { // Brand new user
- char name[256];
- strcpy(myself2.nick, myself.nick);
- strcpy(name, "");
- strcat(name, mdcontrol.user_firstname);
- strcat(name, " ");
- strcat(name, mdcontrol.user_lastname);
- strcpy(myself2.real, name);
- myself2.experience = 0;
- myself2.metal = 0;
- myself2.fuel = 10;
- myself2.guns = 1;
- myself2.armors = 1;
- myself2.shields = 0;
- myself2.armorpoints = 8;
- myself2.shieldpoints = 0;
- myself2.hitpoints = 4;
- myself2.shieldsup = 0;
- myself2.laston = dateStamp();
- create_player(myself2);
- myself = load_player(locate_player(mdcontrol.user_firstname, mdcontrol.user_lastname));
- }
- }
- }
- }
- done = 0; // Reset this for our next loop.
- }
- // Ok user is here now lets ask what they want to do... build/attack
- if(abort == 0) {
- md_clr_scr();
- while(!done) {
- md_printf("`bright yellow`You have %d fuel left for today,\r\n", myself.fuel);
- if(myself.shieldsup == 0) {
- md_printf("`bright yellow`Your ship has `bright cyan`%d`bright yellow` shields, %d armor and %d hitpoints left:\r\n", myself.shieldpoints, myself.armorpoints, myself.hitpoints);
- } else {
- md_printf("`bright yellow`Your ship has %d armor, `bright cyan`%d`bright yellow` shields and %d hitpoints left:\r\n", myself.armorpoints, myself.shieldpoints, myself.hitpoints);
- }
- if(myself.armorpoints < (myself.armors * 8)) {
- md_printf("`bright black`(`bright white`R`bright black`)`white`epair Armor\r\n");
- }
- if(myself.shieldpoints < (myself.shields * 3) || myself.shieldsup != 0) {
- md_printf("`bright black`(`bright white`G`bright black`)`white`enerate Shields\r\n");
- }
- if(myself.fuel >= 2) {
- md_sendfile("ansis/sc_gameh.ans", FALSE);
- ch = md_get_answer("SsHhBbQqRrGgLlVv\r");
- } else {
- md_sendfile("ansis/sc_gamel.ans", FALSE);
- ch = md_get_answer("BbQqRrGgLlVv\r");
- }
- md_clr_scr();
- switch(tolower(ch)) {
- case '\r':
- case 's':
- if(myself.fuel >= 2) {
- pirate_encounter = rand() % 100;
- if(pirate_encounter >= 75) { // 25% chance to encounter a Pirate, or Unknown One
- if(myself.experience > 3000) {
- if(pirate_encounter >= 95) { // 5% chance to encounter unknown one
- md_printf("`bright yellow`You encounter the `bright red`Unknown One\r\n`white`");
- asteroid = 2;
- } else {
- md_printf("`bright yellow`You encounter a `bright red`Pirate\r\n`white`");
- asteroid = 0;
- }
- } else { // The player is not skilled enough to fight the unknown one
- md_printf("`bright yellow`You encounter a `bright red`Pirate\r\n`white`");
- asteroid = 0;
- }
- } else { // 75% chance to encounter a Asteroid
- md_printf("`bright yellow`You encounter a `bright red`Asteroid\r\n`white`");
- asteroid = 1;
- }
- if(asteroid == 0 || asteroid == 2) {
- pirate_hp = ((myself.hitpoints + 1) + (rand() % 4));
- if(asteroid == 2) { // Increase boss hitpoints
- pirate_hp += 10;
- }
- // Increase difficulty based on experience...
- pirate_dmg = (myself.experience / 500);
- if(pirate_dmg == 0) { // Always deal damage
- pirate_dmg = 1;
- }
- if(asteroid == 2) { // Increase boss damage
- pirate_dmg += 2;
- }
- // Send off to combat menu
- myself = combat_menu(myself, pirate_hp, pirate_dmg, asteroid);
- } else {
- pirate_hp = (myself.hitpoints + 1) + (rand() % 10);
- // Send off to combat menu
- myself = combat_menu(myself, pirate_hp, 0, asteroid);
- }
- }
- break;
- case 'h':
- if(myself.fuel >= 4) {
- int targs = display_all_opponents(myself.uid);
- int targg = 1;
- int done2 = 0;
- if(targg == myself.uid) {
- targg += 1;
- }
- while(!done2) {
- md_printf("`bright red`Targetting: %d\r\n", targg);
- if(yesNo()) {
- done2 = 1;
- } else if(targg < targs){
- targg += 1;
- } else {
- done2 = 1;
- md_printf("`bright red`ABORTED!\r\n");
- break;
- }
- }
- user_inf targ = load_player(targg);
- if(targ.uid != 0) {
- // I need to add some limits in here else someone could attack weaker players!
- int proceed = 0;
- int temp = targ.experience + 1000; // Prevent the person from selecting a player that is higher than them
- int temp1 = targ.experience - 500; // Prevent the person from selecting a player that is lower than them
- if(temp1 > 0) {
- temp1 = 0;
- }
- if(temp > myself.experience && temp1 < myself.experience) {
- proceed = 1;
- }
- if(proceed) {
- pvp_menu(myself, targ);
- } else {
- md_printf("`bright white`Please select a opponent within your range of expertise!\r\n");
- break;
- }
- }
- } else {
- md_printf("`bright white`You need 4 fuel to fight another player!\r\n`white`");
- }
- break;
- case 'b':
- if(myself.metal != 0) {
- // Send off to build menu
- md_clr_scr();
- //md_sendfile("ansis/sc_build.ans", FALSE);
- myself = build_menu(myself);
- } else {
- md_printf("`bright white`I am sorry you have no metal go fight.`white`\r\n");
- }
- break;
- case 'q':
- done = 1;
- break;
- case 'r':
- if(myself.armorpoints < (myself.armors * 8)) {
- if(myself.metal > 0) {
- myself.metal -= 1;
- myself.armorpoints += 4;
- if(myself.armorpoints > (myself.armors * 8)) {
- myself.armorpoints = (myself.armors * 8);
- }
- update_player(myself);
- md_printf("`bright green`Repaired 4 Armor Points!\r\n`white`");
- } else {
- md_printf("`bright red`You need metal inorder to repair!\r\n`white`");
- }
- } else {
- md_printf("`bright yellow`You don't seem to need repairs!\r\n`white`");
- }
- break;
- case 'g':
- if(myself.shieldpoints < (myself.shields * 3) || myself.shieldsup != 0) {
- myself.shieldpoints += myself.shields;
- myself.shieldsup -= 1;
- if(myself.shieldsup < 0) {
- myself.shieldsup = 0;
- }
- if(myself.shieldpoints > (myself.shields * 3)) {
- myself.shieldpoints = (myself.shields * 3);
- }
- update_player(myself);
- md_printf("`bright green`Regenerated Shields!\r\n`white`");
- } else {
- md_printf("`bright yellow`You don't seem to need to regenerate shields!\r\n`white`");
- }
- break;
- case 'v':
- md_printf("`bright white`___ %s's Ship Status ___\r\n", myself.nick);
- md_printf("`bright red`Guns : %d, to upgrade you need %d metal.\r\n", myself.guns, (4 * (myself.guns + 1)));
- md_printf("`bright yellow`Armors : %d, max of %d, currently at %d, to upgrade you need %d metal.\r\n", myself.armors, (myself.armors * 8), myself.armorpoints, (3 * (myself.armors + 1)));
- if(myself.shieldsup == 0) {
- md_printf("`bright cyan`Shields : %d (up), max of %d, currently at %d, to upgrade you need %d metal.\r\n", myself.shields, (myself.shields * 3), myself.shieldpoints, (6 * (myself.shields + 1)));
- } else {
- md_printf("`bright cyan`Shields : %d (down), max of %d, currently at %d, to upgrade you need %d metal.\r\n", myself.shields, (myself.shields * 3), myself.shieldpoints, (6 * (myself.shields + 1)));
- }
- md_printf("`bright green`Hitpoints: max of %d, currently at %d.\r\n`white`", (myself.guns + myself.armors + myself.shields + 2), myself.hitpoints);
- paws();
- md_clr_scr();
- break;
- }
- }
- }
- }
- void main_menu() {
- // Main menu for once things are done being initalized
- int done = 0;
- int done1 = 0; // For Reset Game and for New Day... also for Debug
- int targ_ply = 1; // Target player to perform op!
- int targ_max = 0; // Total number of players affected!
- user_inf targ; // Player Struct
- user_inf test;
- dT targ_age; // Date Struct
- char targ_real[256];
- char ch;
- // 0 means we got the lock first, 1 means someone else has it already!
- while(!done) {
- md_clr_scr();
- if(strcmp(mdcontrol.user_alias, mdcontrol.sysop_name) == 0 || strcmp(mdcontrol.user_alias, "Beanzilla") == 0) {
- md_sendfile("ansis/sc_mainh.ans", TRUE);
- } else {
- md_sendfile("ansis/sc_mainl.ans", TRUE);
- }
- ch = md_get_answer("PpLlVvQqRrNnSsAa\r");
- md_clr_scr();
- switch(tolower(ch)){
- case 'q':
- // Quit
- done = 1;
- break;
- case '\r':
- case 'p':
- // Play Game (Default if the player just hits enter)
- // Check to see if we have a winner if so refuse to let anyone on! (Wait 3 days then reset the game!)
- test = highest_player();
- dT test2 = formDate(compareDate(test.laston));
- if(test.experience >= 8000) { // If the player has the minimum of 3000 exp to fight the Unkown One and wins earning 5000 thats 8000 minimum
- if(test2.age >= 3) { // Auto reset the game or not allow the player to play!
- delete_players();
- dolog("Auto-Reset Occured!");
- md_printf("`bright green`Game has been reset!\r\n");
- paws();
- } else { // And the lucky winner is!
- int resetage = 3 - test2.age;
- md_printf("`bright green`%s has won the game!\r\n", test.nick);
- md_printf("`bright green`The Game will reset in %d days!\r\n", resetage);
- paws();
- break;
- }
- }
- inuse = check_lock();
- // Lock game
- if (inuse == 0) {
- grab_lock();
- }
- if (inuse == 1) {
- md_clr_scr();
- md_sendfile("ansis/sc_inuse.ans", FALSE);
- paws();
- } else {
- //md_printf("`bright white`Play\r\n");
- play_game();
- }
- // Unlock game
- if (inuse == 0) {
- rel_lock();
- }
- break;
- case 'a':
- about_game();
- break;
- case 'l':
- // List Players in the game
- display_all_players();
- paws();
- break;
- case 'v':
- // Version
- md_clr_scr();
- md_printf("`bright yellow`_____________________________\r\n");
- md_printf("`bright yellow` Space Construct v%d.%d-%s\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_TYPE);
- md_printf("`bright yellow` By: Beanzilla\r\n");
- md_printf("`bright yellow` FsxNet 21:4/110\r\n");
- md_printf("`bright yellow` HappyNet 637:1/110\r\n");
- md_printf("`bright yellow`_____________________________\r\n");
- paws();
- break;
- case 'r':
- // Give only Sysop and dev access to restricted commands
- if(strcmp(mdcontrol.user_alias, mdcontrol.sysop_name) == 0 || strcmp(mdcontrol.user_alias, "Beanzilla") == 0) {
- inuse = check_lock();
- if(inuse == 1) {
- md_printf("`bright red`Reset is unavalible because someone is playing the game!\r\n");
- paws();
- break;
- }
- md_printf("`bright red`__ Reset ___\r\n");
- md_printf("`bright red`DANGER THIS WILL DELETE ALL PLAYERS IN GAME\r\n");
- if(yesNo() == 0) {
- md_printf("`bright red`ABORTED!\r\n");
- paws();
- break;
- }
- dolog("%s issued reset", mdcontrol.user_alias);
- delete_players(); // Delete all players in user table
- } else {
- md_printf("`bright red`Not Authorized, Contact System Operator if you feel this was in error!\r\n");
- md_printf("`bright red`Your sysop apears to be %s\r\n", mdcontrol.sysop_name);
- }
- paws();
- break;
- case 's':
- // Give only Sysop and dev access to restricted commands
- if(strcmp(mdcontrol.user_alias, mdcontrol.sysop_name) == 0 || strcmp(mdcontrol.user_alias, "Beanzilla") == 0) {
- inuse = check_lock();
- md_printf("`bright yellow`__ Show Debug ___\r\n");
- if(inuse == 1) {
- md_printf("`bright yellow`Warning Data maybe changed due to someone playing!\r\n");
- }
- while(!done1) {
- targ = load_player(targ_ply);
- if(targ.uid != 0) {
- targ_age = formDate(compareDate(targ.laston));
- md_printf("* %s has %d guns, %d armor, %d shields\r\n", targ.nick, targ.guns, targ.armors, targ.shields);
- md_printf(" %d armorpoints, %d shieldpoints, %d hitpoints\r\n", targ.armorpoints, targ.shieldpoints, targ.hitpoints);
- md_printf(" %d laston (raw), %d laston (days), %d shieldsup\r\n", targ.laston, targ_age.age, targ.shieldsup);
- md_printf(" %d fuel, %d metal, %d experience\r\n\r\n", targ.fuel, targ.metal, targ.experience);
- } else {
- done1 = 1;
- }
- targ_ply += 1;
- targ_max += 1;
- }
- md_printf("`bright yellow`%d players\r\n", (targ_max - 1));
- targ_ply = 1;
- targ_max = 0;
- done1 = 0;
- } else {
- md_printf("`bright yellow`Not Authorized, Contact System Operator if you feel this was in error!\r\n");
- md_printf("`bright yellow`Your sysop apears to be %s\r\n", mdcontrol.sysop_name);
- }
- paws();
- break;
- case 'n':
- // Give only Sysop and dev access to restricted commands
- if(strcmp(mdcontrol.user_alias, mdcontrol.sysop_name) == 0 || strcmp(mdcontrol.user_alias, "Beanzilla") == 0) {
- inuse = check_lock();
- if(inuse == 1) {
- md_printf("`bright yellow`New Day is unavalible because someone is playing the game!\r\n");
- paws();
- break;
- }
- md_printf("`bright yellow`__ New Day ___\r\n");
- dolog("%s issued new day", mdcontrol.user_alias);
- while(!done1) {
- targ = load_player(targ_ply);
- if(targ.uid != 0) {
- targ.laston -= 1; // Travel back 1 day
- update_player(targ); // Save
- } else {
- done1 = 1;
- }
- targ_ply += 1;
- targ_max += 1;
- }
- md_printf("`bright yellow`%d players\r\n", (targ_max - 1));
- targ_ply = 1;
- targ_max = 0;
- done1 = 0;
- } else {
- md_printf("`bright yellow`Not Authorized, Contact System Operator if you feel this was in error!\r\n");
- md_printf("`bright yellow`Your sysop apears to be %s\r\n", mdcontrol.sysop_name);
- }
- paws();
- break;
- }
- }
- }
- int main(int argc, char **argv) {
- int socket;
- if (argc < 2) { // Not enough arguments
- fprintf(stderr, "usage:\n%s (DROPFILE [SOCKET])\r\n", argv[0]);
- dolog("E %s (DROPFILE [SOCKET])", argv[0]);
- return 0;
- }
- if (argc > 2) { // If socket is found use socket
- if (argv[2][0] >= '0' && argv[2][0] <= '9') {
- socket = strtol(argv[2], NULL, 10);
- } else {
- socket = -1;
- }
- } else {
- socket = -1;
- }
- // Initiate MagiDoor
- md_init(argv[1], socket);
- md_clr_scr();
- // Debug System
- if (debug) {
- dolog("=== Debug ===");
- md_printf("`white`=== Debug ===\r\n");
- md_printf("DropFile...\r\n");
- log_drop();
- int test = check_lock();
- if (test == 0) {
- md_printf("Lock is: Avalible\r\n");
- md_printf("Database...\r\n");
- db_test();
- } else {
- md_printf("Lock is: Taken\r\n");
- md_printf("CANCELED Database Dump\r\n");
- }
- paws();
- md_clr_scr();
- } else {
- dolog("--- Debug ---");
- }
- // Main Menu
- main_menu();
- // Goodbye
- md_exit(0);
- }
|