/** * @brief Runs missions matching location, all Lua side and one-shot. * * @param loc Location to match. * @param faction Faction of the planet. * @param planet Name of the current planet. * @param sysname Name of the current system. */ void missions_run( int loc, int faction, const char* planet, const char* sysname ) { MissionData* misn; Mission mission; int i; double chance; for (i=0; i<mission_nstack; i++) { misn = &mission_stack[i]; if (misn->avail.loc != loc) continue; if (!mission_meetReq(i, faction, planet, sysname)) continue; chance = (double)(misn->avail.chance % 100)/100.; if (chance == 0.) /* We want to consider 100 -> 100% not 0% */ chance = 1.; if (RNGF() < chance) { mission_init( &mission, misn, 1, 1, NULL ); mission_cleanup(&mission); /* it better clean up for itself or we do it */ } } }
/* Creates a new game */ void game_new() { static int first_time = 1; countries_init(); mission_init(); pactos_flush(); player_map( player_initplayer ); player_all_set_status( PLAYER_STATUS_HABILITADO ); g_game.playing = 0; g_game.turno = NULL; g_game.empieza_turno = NULL; g_game.estado = JUEGO_ESTADO_ESPERANDO; g_game.round_number = 1; if( first_time ) { first_time = 0; g_game.gamenumber = 0; g_game.seed = get_int_from_dev_random(); } else { g_game.gamenumber++; g_game.seed = rand(); srand( g_game.seed ); } player_kick_unparent_robots(); }
/** * @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 Starts a mission. * * Mission must still call misn.accept() to actually get added to the player's * active missions. * * @param name Name of the mission to start. * @param[out] id ID of the newly created mission. * @return 0 on success, >0 on forced exit (misn.finish), <0 on error. */ int mission_start( const char *name, unsigned int *id ) { Mission mission; MissionData *mdat; int ret; /* Try to get the mission. */ mdat = mission_get( mission_getID(name) ); if (mdat == NULL) return -1; /* Try to run the mission. */ ret = mission_init( &mission, mdat, 1, 1, id ); /* Add to mission giver if necessary. */ if (landed && (ret==0) && (mdat->avail.loc==MIS_AVAIL_BAR)) npc_patchMission( &mission ); else mission_cleanup( &mission ); /* Clean up in case not accepted. */ return ret; }
/** * @brief Parses the actual individual mission nodes. * * @param parent Parent node to parse. * @return 0 on success. */ static int missions_parseActive( xmlNodePtr parent ) { Mission *misn; MissionData *data; int m, i; char *buf; char *title; const char **items; int nitems; int id, sys, type; StarSystem *ssys; xmlNodePtr node, cur, nest; m = 0; /* start with mission 0 */ node = parent->xmlChildrenNode; do { if (xml_isNode(node,"mission")) { misn = player_missions[m]; /* process the attributes to create the mission */ xmlr_attr(node,"data",buf); data = mission_get(mission_getID(buf)); if (data == NULL) { WARN("Mission '%s' from savegame not found in game - ignoring.", buf); free(buf); continue; } else { if (mission_init( misn, data, 0, 0, NULL )) { WARN("Mission '%s' from savegame failed to load properly - ignoring.", buf); free(buf); continue; } misn->accepted = 1; } free(buf); /* this will orphan an identifier */ xmlr_attr(node,"id",buf); misn->id = atol(buf); free(buf); cur = node->xmlChildrenNode; do { xmlr_strd(cur,"title",misn->title); xmlr_strd(cur,"desc",misn->desc); xmlr_strd(cur,"reward",misn->reward); /* Get the markers. */ if (xml_isNode(cur,"markers")) { nest = cur->xmlChildrenNode; do { if (xml_isNode(nest,"marker")) { /* Get ID. */ xmlr_attr(nest,"id",buf); id = (buf != NULL) ? atoi(buf) : -1; if (buf != NULL) free(buf); /* Get type. */ xmlr_attr(nest,"type",buf); type = (buf != NULL) ? atoi(buf) : -1; if (buf != NULL) free(buf); /* Get system. */ ssys = system_get( xml_get( nest )); if (ssys == NULL) { WARN( "System Marker to '%s' does not exist", xml_get( nest ) ); continue; } sys = system_index( ssys ); mission_addMarker( misn, id, sys, type ); } } while (xml_nextNode(nest)); } /* Cargo. */ if (xml_isNode(cur,"cargos")) { nest = cur->xmlChildrenNode; do { if (xml_isNode(nest,"cargo")) mission_linkCargo( misn, xml_getLong(nest) ); } while (xml_nextNode(nest)); } /* OSD. */ if (xml_isNode(cur,"osd")) { xmlr_attr(cur,"nitems",buf); if (buf != NULL) { nitems = atoi(buf); free(buf); } else continue; xmlr_attr(cur,"title",title); items = malloc( nitems * sizeof(char*) ); i = 0; nest = cur->xmlChildrenNode; do { if (xml_isNode(nest,"msg")) { if (i > nitems) { WARN("Inconsistency with 'nitems' in savefile."); break; } items[i] = xml_get(nest); i++; } } while (xml_nextNode(nest)); /* Create the osd. */ misn->osd = osd_create( title, nitems, items, data->avail.priority ); free(items); free(title); /* Set active. */ xmlr_attr(cur,"active",buf); if (buf != NULL) { osd_active( misn->osd, atoi(buf) ); free(buf); } } /* Claims. */ if (xml_isNode(cur,"claims")) misn->claims = claim_xmlLoad( cur ); if (xml_isNode(cur,"lua")) /* start the unpersist routine */ nxml_unpersistLua( misn->L, cur ); } while (xml_nextNode(cur)); m++; /* next mission */ if (m >= MISSION_MAX) break; /* full of missions, must be an error */ } } while (xml_nextNode(node)); return 0; }