diff --git a/dat/missions/cargo.lua b/dat/missions/cargo.lua index 4e80d47..19b39b9 100644 --- a/dat/missions/cargo.lua +++ b/dat/missions/cargo.lua @@ -16,8 +16,7 @@ function create() misn_reward = carg_mass * 1000 + rnd.int(0, 5000) misn.setReward(string.format("%d Scred", misn_reward)) - -- Set the hooks. - hook.land("land") + end function accept() @@ -25,6 +24,7 @@ function accept() toolkit.msg("Mission Accepted", string.format("The workers load the %d tons of %s onto your ship.", carg_mass, carg_type)) + end function land() @@ -33,6 +33,9 @@ function land() player.pay(misn_reward) toolkit.msg("Mission Accomplished", string.format("The workers unload the %s at the docks.", carg_type)) + + -- Set the hooks. + hook.land("land") end end diff --git a/src/land.c b/src/land.c index 8319c48..fd71162 100644 --- a/src/land.c +++ b/src/land.c @@ -103,6 +103,7 @@ static void news_close(char* str); static void misn(void); static void misn_close(char* str); static void misn_accept(char* str); +static void misn_genList(int first); static void misn_update(char* str); // The local market. @@ -810,8 +811,7 @@ static void misn(void) { window_addList(secondary_wid, 20, -40, 300, MISSION_HEIGHT-60, "lstMission", misn_names, mission_ncomputer, 0, misn_update); - - misn_update(NULL); + misn_genList(1); } static void misn_close(char* str) { @@ -820,7 +820,38 @@ static void misn_close(char* str) { } static void misn_accept(char* str) { + char* misn_name; (void)str; + + misn_name = toolkit_getList(secondary_wid, "lstMission"); +} + +static void misn_genList(int first) { + int i, j; + char** misn_names; + + if(!first) + window_destroyWidget(secondary_wid, "lstMission"); + + // List. + if(mission_ncomputer != 0) { + // there are missions. + misn_names = malloc(sizeof(char*) * mission_ncomputer); + j = 0; + for(i = 0; i < mission_ncomputer; i++) + if(mission_computer[i].title) + misn_names[j++] = strdup(mission_computer[i].title); + } else { + // No missions. + misn_names = malloc(sizeof(char*)); + misn_names[0] = strdup("No Missions"); + } + + window_addList(secondary_wid, 20, -40, + 300, MISSION_HEIGHT-60, + "lstMission", misn_names, j, 0, misn_update); + + misn_update(NULL); } static void misn_update(char* str) { @@ -837,7 +868,8 @@ static void misn_update(char* str) { return; } for(i = 0; i < mission_ncomputer; i++) - if(strcmp(active_misn, mission_computer[i].title)==0) { + if(mission_computer[i].title && + (strcmp(active_misn, mission_computer[i].title)==0)) { window_modifyText(secondary_wid, "txtReward", mission_computer[i].reward); window_modifyText(secondary_wid, "txtDesc", mission_computer[i].desc); return; @@ -938,7 +970,7 @@ void takeoff(void) { // Cleanup mission computer. for(i = 0; i < mission_ncomputer; i++) - mission_free(&mission_computer[i]); + mission_cleanup(&mission_computer[i]); free(mission_computer); mission_computer = NULL; mission_ncomputer = 0; diff --git a/src/mission.c b/src/mission.c index 6aac223..3a13781 100644 --- a/src/mission.c +++ b/src/mission.c @@ -36,7 +36,6 @@ extern int misn_run(Mission* misn, char* func); // Static. static int mission_init(Mission* mission, MissionData* misn); -static void mission_cleanup(Mission* misn); static void mission_freeData(MissionData* mission); static int mission_matchFaction(MissionData* misn, int faction); static int mission_location(char* loc); @@ -100,63 +99,23 @@ int mission_add(Mission* mission) { } // Clean up a mission. -static void mission_cleanup(Mission* misn) { +void mission_cleanup(Mission* misn) { hook_rmParent(misn->id); // Remove existing hooks. - misn->data = NULL; if(misn->title) free(misn->title); if(misn->desc) free(misn->desc); if(misn->reward) free(misn->reward); lua_close(misn->L); + memset(misn, 0, sizeof(Mission)); } // Free a mission. static void mission_freeData(MissionData* mission) { - if(mission->name) { - free(mission->name); - mission->name = NULL; - } - if(mission->lua) { - free(mission->lua); - mission->lua = NULL; - } - if(mission->avail.planet) { - free(mission->avail.planet); - mission->avail.planet = NULL; - } - if(mission->avail.system) { - free(mission->avail.system); - mission->avail.system = NULL; - } - if(mission->avail.factions) { - free(mission->avail.factions); - mission->avail.factions = NULL; - mission->avail.nfactions = 0; - } -} - -// Free an active mission. -void mission_free(Mission* mission) { - if(mission->id == 0) return; - - if(mission->title) { - free(mission->title); - mission->title = NULL; - } - - if(mission->desc) { - free(mission->desc); - mission->desc = NULL; - } - - if(mission->reward) { - free(mission->reward); - mission->reward = NULL; - } - - if(mission->L) { - lua_close(mission->L); - mission->L = NULL; - } + if(mission->name) free(mission->name); + if(mission->lua) free(mission->lua); + if(mission->avail.planet) free(mission->avail.planet); + if(mission->avail.system) free(mission->avail.system); + if(mission->avail.factions) free(mission->avail.factions); + memset(mission, 0, sizeof(MissionData)); } // Does mission match faction requirement? diff --git a/src/mission.h b/src/mission.h index 0d8dd45..0ccdaee 100644 --- a/src/mission.h +++ b/src/mission.h @@ -62,7 +62,7 @@ void mission_accept(Mission* misn); // Load/Quit. int missions_load(void); -void mission_free(Mission* mission); +void mission_cleanup(Mission* misn); void missions_free(void); void missions_cleanup(void); diff --git a/src/toolkit.c b/src/toolkit.c index 5fabd75..0a26f45 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -493,7 +493,8 @@ static void widget_cleanup(Widget* widget) { case WIDGET_LIST: // Must clear the list. if(widget->dat.lst.options) { for(i = 0; i < widget->dat.lst.noptions; i++) - free(widget->dat.lst.options[i]); + if(widget->dat.lst.options[i]) + free(widget->dat.lst.options[i]); free(widget->dat.lst.options); } break;