flib_team *flib_team_copy(const flib_team *team) { flib_team *result = NULL; if(team) { flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team)); if(tmpTeam) { bool error = false; for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { tmpTeam->hogs[i].name = strdupWithError(team->hogs[i].name, &error); tmpTeam->hogs[i].hat = strdupWithError(team->hogs[i].hat, &error); tmpTeam->hogs[i].rounds = team->hogs[i].rounds; tmpTeam->hogs[i].kills = team->hogs[i].kills; tmpTeam->hogs[i].deaths = team->hogs[i].deaths; tmpTeam->hogs[i].suicides = team->hogs[i].suicides; tmpTeam->hogs[i].difficulty = team->hogs[i].difficulty; tmpTeam->hogs[i].initialHealth = team->hogs[i].initialHealth; tmpTeam->hogs[i].weaponset = flib_weaponset_copy(team->hogs[i].weaponset); if(team->hogs[i].weaponset && !tmpTeam->hogs[i].weaponset) { error = true; } } tmpTeam->name = strdupWithError(team->name, &error); tmpTeam->grave = strdupWithError(team->grave, &error); tmpTeam->fort = strdupWithError(team->fort, &error); tmpTeam->voicepack = strdupWithError(team->voicepack, &error); tmpTeam->flag = strdupWithError(team->flag, &error); tmpTeam->ownerName = strdupWithError(team->ownerName, &error); tmpTeam->bindingCount = team->bindingCount; if(team->bindings) { tmpTeam->bindings = flib_calloc(team->bindingCount, sizeof(flib_binding)); if(tmpTeam->bindings) { for(int i=0; i<tmpTeam->bindingCount; i++) { tmpTeam->bindings[i].action = strdupWithError(team->bindings[i].action, &error); tmpTeam->bindings[i].binding = strdupWithError(team->bindings[i].binding, &error); } } else { error = true; } } tmpTeam->rounds = team->rounds; tmpTeam->wins = team->wins; tmpTeam->campaignProgress = team->campaignProgress; tmpTeam->colorIndex = team->colorIndex; tmpTeam->hogsInGame = team->hogsInGame; tmpTeam->remoteDriven = team->remoteDriven; if(!error) { result = tmpTeam; tmpTeam = 0; } } flib_team_destroy(tmpTeam); } return result; }
int flib_ipc_append_fullconfig(flib_vector *vec, const flib_gamesetup *setup, bool netgame) { int result = -1; flib_vector *tempvector = flib_vector_create(); if(!log_badargs_if2(vec==NULL, setup==NULL) && tempvector) { bool error = false; bool perHogAmmo = false; bool sharedAmmo = false; error |= flib_ipc_append_message(vec, netgame ? "TN" : "TL"); if(setup->map) { error |= flib_ipc_append_mapconf(tempvector, setup->map, false); } if(setup->style) { error |= flib_ipc_append_style(tempvector, setup->style); } if(setup->gamescheme) { error |= flib_ipc_append_gamescheme(tempvector, setup->gamescheme); sharedAmmo = flib_scheme_get_mod(setup->gamescheme, "sharedammo"); // Shared ammo has priority over per-hog ammo perHogAmmo = !sharedAmmo && flib_scheme_get_mod(setup->gamescheme, "perhogammo"); } if(setup->teamlist->teams && setup->teamlist->teamCount>0) { int *clanColors = flib_calloc(setup->teamlist->teamCount, sizeof(int)); if(!clanColors) { error = true; } else { int clanCount = 0; for(int i=0; !error && i<setup->teamlist->teamCount; i++) { flib_team *team = setup->teamlist->teams[i]; // Find the clan index of this team (clans are identified by color). bool newClan = false; int clan = 0; while(clan<clanCount && clanColors[clan] != team->colorIndex) { clan++; } if(clan==clanCount) { newClan = true; clanCount++; clanColors[clan] = team->colorIndex; } // If shared ammo is active, only add an ammo store for the first team in each clan. bool noAmmoStore = sharedAmmo&&!newClan; error |= flib_ipc_append_addteam(tempvector, setup->teamlist->teams[i], perHogAmmo, noAmmoStore); } } free(clanColors); } error |= flib_ipc_append_message(tempvector, "!"); if(!error) { // Message created, now we can copy everything. flib_constbuffer constbuf = flib_vector_as_constbuffer(tempvector); if(!flib_vector_append(vec, constbuf.data, constbuf.size)) { result = 0; } } } return result; }
flib_scheme *flib_scheme_create(const char *schemeName) { flib_scheme *result = flib_calloc(1, sizeof(flib_scheme)); if(log_badargs_if(schemeName==NULL) || result==NULL) { return NULL; } result->name = flib_strdupnull(schemeName); result->mods = flib_calloc(flib_meta.modCount, sizeof(*result->mods)); result->settings = flib_calloc(flib_meta.settingCount, sizeof(*result->settings)); if(!result->mods || !result->settings || !result->name) { flib_scheme_destroy(result); return NULL; } for(int i=0; i<flib_meta.settingCount; i++) { result->settings[i] = flib_meta.settings[i].def; } return result; }
flib_team *flib_team_from_netmsg(char **parts) { flib_team *result = NULL; flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team)); if(tmpTeam) { if(!fillTeamFromMsg(tmpTeam, parts)) { result = tmpTeam; tmpTeam = NULL; } else { flib_log_e("Error parsing team from net."); } } flib_team_destroy(tmpTeam); return result; }
flib_ini *flib_ini_load(const char *filename) { flib_ini *result = NULL; if(!log_badargs_if(filename==NULL)) { flib_ini *tmpIni = flib_calloc(1, sizeof(flib_ini)); if(tmpIni) { tmpIni->inidict = iniparser_load(filename); if(tmpIni->inidict) { result = tmpIni; tmpIni = NULL; } } flib_ini_destroy(tmpIni); } return result; }
flib_ini *flib_ini_create(const char *filename) { flib_ini *result = NULL; flib_ini *tmpIni = flib_calloc(1, sizeof(flib_ini)); if(tmpIni) { if(filename) { tmpIni->inidict = iniparser_load(filename); } if(!tmpIni->inidict) { tmpIni->inidict = dictionary_new(0); } if(tmpIni->inidict) { result = tmpIni; tmpIni = NULL; } } flib_ini_destroy(tmpIni); return result; }
flib_ipcbase *flib_ipcbase_create() { flib_ipcbase *result = flib_calloc(1, sizeof(flib_ipcbase)); flib_acceptor *acceptor = flib_acceptor_create(0); if(!result || !acceptor) { free(result); flib_acceptor_close(acceptor); return NULL; } result->acceptor = acceptor; result->sock = NULL; result->readBufferSize = 0; result->port = flib_acceptor_listenport(acceptor); flib_log_i("Started listening for IPC connections on port %u", (unsigned)result->port); return result; }
flib_weaponset *flib_weaponset_create(const char *name) { flib_weaponset *result = NULL; if(!log_badargs_if(name==NULL)) { flib_weaponset *newSet = flib_calloc(1, sizeof(flib_weaponset)); if(newSet) { newSet->name = flib_strdupnull(name); if(newSet->name) { setField(newSet->loadout, "", 0, false); setField(newSet->crateprob, "", 0, false); setField(newSet->crateammo, "", 0, false); setField(newSet->delay, "", 0, false); result = newSet; newSet = NULL; } } flib_weaponset_destroy(newSet); } return result; }
flib_room *flib_room_from_netmsg(char **params) { flib_room *result = NULL; flib_room *tmpRoom = flib_calloc(1, sizeof(flib_room)); if(tmpRoom) { tmpRoom->inProgress = !strcmp(params[0], "True"); tmpRoom->name = flib_strdupnull(params[1]); tmpRoom->playerCount = atoi(params[2]); tmpRoom->teamCount = atoi(params[3]); tmpRoom->owner = flib_strdupnull(params[4]); tmpRoom->map = flib_strdupnull(params[5]); tmpRoom->scheme = flib_strdupnull(params[6]); tmpRoom->weapons = flib_strdupnull(params[7]); if(tmpRoom->name && tmpRoom->owner && tmpRoom->map && tmpRoom->scheme && tmpRoom->weapons) { result = tmpRoom; tmpRoom = NULL; } } flib_room_destroy(tmpRoom); return result; }
flib_map *flib_map_copy(const flib_map *map) { flib_map *result = NULL; if(map) { flib_map *newmap = flib_calloc(1, sizeof(flib_map)); if(newmap) { newmap->mapgen = map->mapgen; newmap->drawDataSize = map->drawDataSize; newmap->drawData = flib_bufdupnull(map->drawData, map->drawDataSize); newmap->mazeSize = map->mazeSize; newmap->name = flib_strdupnull(map->name); newmap->seed = flib_strdupnull(map->seed); newmap->templateFilter = map->templateFilter; newmap->theme = flib_strdupnull(map->theme); if((newmap->drawData || !map->drawData) && (newmap->name || !map->name) && (newmap->seed || !map->seed) && (newmap->theme || !map->theme)) { result = newmap; newmap = NULL; } } flib_map_destroy(newmap); } return result; }
flib_teamlist *flib_teamlist_create() { return flib_calloc(1, sizeof(flib_teamlist)); }
flib_weaponsetlist *flib_weaponsetlist_create() { return flib_calloc(1, sizeof(flib_weaponsetlist)); }
flib_team *flib_team_from_ini(const char *filename) { if(log_badargs_if(filename==NULL)) { return NULL; } flib_team *result = flib_calloc(1, sizeof(flib_team)); flib_ini *ini = NULL; if(!result) { return from_ini_handleError(result, ini); } ini = flib_ini_load(filename); if(!ini) { flib_log_e("Error loading team file %s", filename); return from_ini_handleError(result, ini); } if(flib_ini_enter_section(ini, "team")) { flib_log_e("Missing section \"Team\" in team file %s", filename); return from_ini_handleError(result, ini); } bool error = false; error |= flib_ini_get_str(ini, &result->name, "name"); error |= flib_ini_get_str(ini, &result->grave, "grave"); error |= flib_ini_get_str(ini, &result->fort, "fort"); error |= flib_ini_get_str(ini, &result->voicepack, "voicepack"); error |= flib_ini_get_str(ini, &result->flag, "flag"); error |= flib_ini_get_int(ini, &result->rounds, "rounds"); error |= flib_ini_get_int(ini, &result->wins, "wins"); error |= flib_ini_get_int(ini, &result->campaignProgress, "campaignprogress"); int difficulty = 0; error |= flib_ini_get_int(ini, &difficulty, "difficulty"); if(error) { flib_log_e("Missing or malformed entry in section \"Team\" in file %s", filename); return from_ini_handleError(result, ini); } for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { char sectionName[32]; if(snprintf(sectionName, sizeof(sectionName), "hedgehog%i", i) <= 0) { return from_ini_handleError(result, ini); } if(flib_ini_enter_section(ini, sectionName)) { flib_log_e("Missing section \"%s\" in team file %s", sectionName, filename); return from_ini_handleError(result, ini); } flib_hog *hog = &result->hogs[i]; error |= flib_ini_get_str(ini, &hog->name, "name"); error |= flib_ini_get_str(ini, &hog->hat, "hat"); error |= flib_ini_get_int(ini, &hog->rounds, "rounds"); error |= flib_ini_get_int(ini, &hog->kills, "kills"); error |= flib_ini_get_int(ini, &hog->deaths, "deaths"); error |= flib_ini_get_int(ini, &hog->suicides, "suicides"); result->hogs[i].difficulty = difficulty; result->hogs[i].initialHealth = TEAM_DEFAULT_HEALTH; if(error) { flib_log_e("Missing or malformed entry in section \"%s\" in file %s", sectionName, filename); return from_ini_handleError(result, ini); } } if(!flib_ini_enter_section(ini, "binds")) { result->bindingCount = flib_ini_get_keycount(ini); if(result->bindingCount<0) { flib_log_e("Error reading bindings from file %s", filename); result->bindingCount = 0; } result->bindings = flib_calloc(result->bindingCount, sizeof(flib_binding)); if(!result->bindings) { return from_ini_handleError(result, ini); } for(int i=0; i<result->bindingCount; i++) { char *keyname = flib_ini_get_keyname(ini, i); if(!keyname) { error = true; } else { result->bindings[i].action = flib_urldecode(keyname); error |= !result->bindings[i].action; error |= flib_ini_get_str(ini, &result->bindings[i].binding, keyname); } free(keyname); } } if(error) { flib_log_e("Error reading team file %s", filename); return from_ini_handleError(result, ini); } flib_ini_destroy(ini); return result; }