/** * @brief Player death menu, appears when player got creamed. */ void menu_death (void) { unsigned int wid; char path[PATH_MAX]; wid = window_create( "Death", -1, -1, DEATH_WIDTH, DEATH_HEIGHT ); window_onClose( wid, menu_death_close ); /* Allow the player to continue if the savegame exists, if not, propose to restart */ nsnprintf(path, PATH_MAX, "%ssaves/%s.ns", nfile_dataPath(), player.name); if (!player_isTut() && nfile_fileExists(path)) window_addButtonKey( wid, 20, 20 + BUTTON_HEIGHT*2 + 20*2, BUTTON_WIDTH, BUTTON_HEIGHT, "btnContinue", _("Continue"), menu_death_continue, SDLK_c ); else window_addButtonKey( wid, 20, 20 + BUTTON_HEIGHT*2 + 20*2, BUTTON_WIDTH, BUTTON_HEIGHT, "btnRestart", _("Restart"), menu_death_restart, SDLK_r ); window_addButtonKey( wid, 20, 20 + (BUTTON_HEIGHT+20), BUTTON_WIDTH, BUTTON_HEIGHT, "btnMain", _("Main Menu"), menu_death_main, SDLK_m ); window_addButtonKey( wid, 20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnExit", _("Exit Game"), menu_exit, SDLK_x ); menu_Open(MENU_DEATH); /* Makes it all look cooler since everything still goes on. */ unpause_game(); }
/** * @brief Generates a mission list. This runs create() so won't work with all * missions. * * @param[out] n Missions created. * @param faction Faction of the planet. * @param planet Name of the planet. * @param sysname Name of the current system. * @param loc Location * @return The stack of Missions created with n members. */ Mission* missions_genList( int *n, int faction, const char* planet, const char* sysname, int loc ) { int i,j, m, alloced; double chance; int rep; Mission* tmp; MissionData* misn; /* Missions can't be generated by tutorial. */ if (player_isTut()) { *n = 0; return NULL; } /* Find available missions. */ tmp = NULL; m = 0; alloced = 0; for (i=0; i<mission_nstack; i++) { misn = &mission_stack[i]; if (misn->avail.loc == loc) { /* Must meet requirements. */ if (!mission_meetReq(i, faction, planet, sysname)) continue; /* Must hit chance. */ chance = (double)(misn->avail.chance % 100)/100.; if (chance == 0.) /* We want to consider 100 -> 100% not 0% */ chance = 1.; rep = MAX(1, misn->avail.chance / 100); for (j=0; j<rep; j++) /* random chance of rep appearances */ if (RNGF() < chance) { m++; /* Extra allocation. */ if (m > alloced) { alloced += 32; tmp = realloc( tmp, sizeof(Mission) * alloced ); } /* Initialize the mission. */ if (mission_init( &tmp[m-1], misn, 1, 1, NULL )) m--; } } } /* Sort. */ if (tmp != NULL) { qsort( tmp, m, sizeof(Mission), mission_compare ); (*n) = m; } else { (*n) = 0; } return tmp; }
/** * @brief Restart the game, when player want to continue after death but without a savegame */ static void menu_death_restart( unsigned int wid, char* str ) { (void) str; window_destroy( wid ); menu_Close(MENU_DEATH); if (player_isTut()) player_newTutorial(); else player_new(); }
/** * @brief Registers all the mission libraries. * * @param L Lua state. * @return 0 on success. */ int misn_loadLibs( lua_State *L ) { nlua_loadStandard(L,0); nlua_loadMisn(L); nlua_loadTk(L); nlua_loadHook(L); nlua_loadMusic(L,0); nlua_loadTex(L,0); nlua_loadBackground(L,0); nlua_loadCamera(L,0); if (player_isTut()) nlua_loadTut(L); return 0; }
/** * @brief Runs all the events matching a trigger. * * @param trigger Trigger to match. */ void events_trigger( EventTrigger_t trigger ) { int i, c; int created; /* Events can't be triggered by tutorial. */ if (player_isTut()) return; created = 0; for (i=0; i<event_ndata; i++) { /* Make sure trigger matches. */ if (event_data[i].trigger != trigger) continue; /* Make sure chance is succeeded. */ if (RNGF() > event_data[i].chance) continue; /* Test uniqueness. */ if ((event_data[i].flags & EVENT_FLAG_UNIQUE) && (player_eventAlreadyDone( i ) || event_alreadyRunning(i))) continue; /* Test conditional. */ if (event_data[i].cond != NULL) { c = cond_check(event_data[i].cond); if (c<0) { WARN("Conditional for event '%s' failed to run.", event_data[i].name); continue; } else if (!c) continue; } /* Create the event. */ event_create( i, NULL ); created++; } /* Run claims if necessary. */ if (created) claim_activateAll(); }
/** * @brief Saves the current game. * * @return 0 on success. */ int save_all (void) { char file[PATH_MAX]; xmlDocPtr doc; xmlTextWriterPtr writer; /* Do not save during tutorial. Or if saving is off. */ if (player_isTut() || player_isFlag(PLAYER_NOSAVE)) return 0; /* Create the writer. */ writer = xmlNewTextWriterDoc(&doc, conf.save_compress); if (writer == NULL) { ERR("testXmlwriterDoc: Error creating the xml writer"); return -1; } /* Set the writer parameters. */ xmlw_setParams( writer ); /* Start element. */ xmlw_start(writer); xmlw_startElem(writer,"naev_save"); /* Save the version and such. */ xmlw_startElem(writer,"version"); xmlw_elem( writer, "naev", "%d.%d.%d", VMAJOR, VMINOR, VREV ); xmlw_elem( writer, "data", "%s", ndata_name() ); xmlw_endElem(writer); /* "version" */ /* Save the data. */ if (save_data(writer) < 0) { ERR("Trying to save game data"); goto err_writer; } /* Finish element. */ xmlw_endElem(writer); /* "naev_save" */ xmlw_done(writer); /* Write to file. */ if ((nfile_dirMakeExist("%s", nfile_dataPath()) < 0) || (nfile_dirMakeExist("%ssaves", nfile_dataPath()) < 0)) { WARN("Failed to create save directory '%ssaves'.", nfile_dataPath()); goto err_writer; } nsnprintf(file, PATH_MAX, "%ssaves/%s.ns", nfile_dataPath(), player.name); /* Back up old savegame. */ if (!save_loaded) { if (nfile_backupIfExists(file) < 0) { WARN("Aborting save..."); goto err_writer; } } save_loaded = 0; /* Critical section, if crashes here player's game gets corrupted. * Luckily we have a copy just in case... */ xmlFreeTextWriter(writer); if (xmlSaveFileEnc(file, doc, "UTF-8") < 0) { WARN("Failed to write savegame! You'll most likely have to restore it by copying your backup savegame over your current savegame."); goto err; } xmlFreeDoc(doc); return 0; err_writer: xmlFreeTextWriter(writer); err: xmlFreeDoc(doc); return -1; }
/** * @brief Creates an event. * * @param data Data to base event off of. * @param id ID to use (0 to generate). * @return 0 on success. */ static int event_create( int dataid, unsigned int *id ) { lua_State *L; uint32_t bufsize; char *buf; Event_t *ev; EventData_t *data; /* Create the event. */ event_nactive++; if (event_nactive > event_mactive) { event_mactive += EVENT_CHUNK; event_active = realloc( event_active, sizeof(Event_t) * event_mactive ); } ev = &event_active[ event_nactive-1 ]; memset( ev, 0, sizeof(Event_t) ); if ((id != NULL) && (*id != 0)) ev->id = *id; else ev->id = event_genID(); /* Add the data. */ ev->data = dataid; data = &event_data[dataid]; /* Open the new state. */ ev->L = nlua_newState(); L = ev->L; nlua_loadStandard(L,0); nlua_loadEvt(L); nlua_loadHook(L); nlua_loadTk(L); nlua_loadBackground(L,1); nlua_loadCamera(L,0); nlua_loadTex(L,0); nlua_loadMusic(L,0); if (player_isTut()) nlua_loadTut(L); /* Load file. */ buf = ndata_read( data->lua, &bufsize ); if (buf == NULL) { WARN("Event '%s' Lua script not found.", data->lua ); return -1; } if (luaL_dobuffer(L, buf, bufsize, data->lua) != 0) { WARN("Error loading event file: %s\n" "%s\n" "Most likely Lua file has improper syntax, please check", data->lua, lua_tostring(L,-1)); free(buf); return -1; } free(buf); /* Run Lua. */ if ((id==NULL) || (*id==0)) event_runLua( ev, "create" ); if (id != NULL) *id = ev->id; return 0; }