[Fix] Proper hook cleanup, tried to impove mission computer a little.

This commit is contained in:
Allanis 2013-04-02 16:56:52 +01:00
parent 4a504e0228
commit 853a07cf19
5 changed files with 52 additions and 57 deletions

View File

@ -16,8 +16,7 @@ function create()
misn_reward = carg_mass * 1000 + rnd.int(0, 5000) misn_reward = carg_mass * 1000 + rnd.int(0, 5000)
misn.setReward(string.format("%d Scred", misn_reward)) misn.setReward(string.format("%d Scred", misn_reward))
-- Set the hooks.
hook.land("land")
end end
function accept() function accept()
@ -25,6 +24,7 @@ function accept()
toolkit.msg("Mission Accepted", toolkit.msg("Mission Accepted",
string.format("The workers load the %d tons of %s onto your ship.", string.format("The workers load the %d tons of %s onto your ship.",
carg_mass, carg_type)) carg_mass, carg_type))
end end
function land() function land()
@ -33,6 +33,9 @@ function land()
player.pay(misn_reward) player.pay(misn_reward)
toolkit.msg("Mission Accomplished", toolkit.msg("Mission Accomplished",
string.format("The workers unload the %s at the docks.", carg_type)) string.format("The workers unload the %s at the docks.", carg_type))
-- Set the hooks.
hook.land("land")
end end
end end

View File

@ -103,6 +103,7 @@ static void news_close(char* str);
static void misn(void); static void misn(void);
static void misn_close(char* str); static void misn_close(char* str);
static void misn_accept(char* str); static void misn_accept(char* str);
static void misn_genList(int first);
static void misn_update(char* str); static void misn_update(char* str);
// The local market. // The local market.
@ -810,8 +811,7 @@ static void misn(void) {
window_addList(secondary_wid, 20, -40, window_addList(secondary_wid, 20, -40,
300, MISSION_HEIGHT-60, 300, MISSION_HEIGHT-60,
"lstMission", misn_names, mission_ncomputer, 0, misn_update); "lstMission", misn_names, mission_ncomputer, 0, misn_update);
misn_genList(1);
misn_update(NULL);
} }
static void misn_close(char* str) { static void misn_close(char* str) {
@ -820,7 +820,38 @@ static void misn_close(char* str) {
} }
static void misn_accept(char* str) { static void misn_accept(char* str) {
char* misn_name;
(void)str; (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) { static void misn_update(char* str) {
@ -837,7 +868,8 @@ static void misn_update(char* str) {
return; return;
} }
for(i = 0; i < mission_ncomputer; i++) 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, "txtReward", mission_computer[i].reward);
window_modifyText(secondary_wid, "txtDesc", mission_computer[i].desc); window_modifyText(secondary_wid, "txtDesc", mission_computer[i].desc);
return; return;
@ -938,7 +970,7 @@ void takeoff(void) {
// Cleanup mission computer. // Cleanup mission computer.
for(i = 0; i < mission_ncomputer; i++) for(i = 0; i < mission_ncomputer; i++)
mission_free(&mission_computer[i]); mission_cleanup(&mission_computer[i]);
free(mission_computer); free(mission_computer);
mission_computer = NULL; mission_computer = NULL;
mission_ncomputer = 0; mission_ncomputer = 0;

View File

@ -36,7 +36,6 @@ extern int misn_run(Mission* misn, char* func);
// Static. // Static.
static int mission_init(Mission* mission, MissionData* misn); static int mission_init(Mission* mission, MissionData* misn);
static void mission_cleanup(Mission* misn);
static void mission_freeData(MissionData* mission); static void mission_freeData(MissionData* mission);
static int mission_matchFaction(MissionData* misn, int faction); static int mission_matchFaction(MissionData* misn, int faction);
static int mission_location(char* loc); static int mission_location(char* loc);
@ -100,63 +99,23 @@ int mission_add(Mission* mission) {
} }
// Clean up a mission. // Clean up a mission.
static void mission_cleanup(Mission* misn) { void mission_cleanup(Mission* misn) {
hook_rmParent(misn->id); // Remove existing hooks. hook_rmParent(misn->id); // Remove existing hooks.
misn->data = NULL;
if(misn->title) free(misn->title); if(misn->title) free(misn->title);
if(misn->desc) free(misn->desc); if(misn->desc) free(misn->desc);
if(misn->reward) free(misn->reward); if(misn->reward) free(misn->reward);
lua_close(misn->L); lua_close(misn->L);
memset(misn, 0, sizeof(Mission));
} }
// Free a mission. // Free a mission.
static void mission_freeData(MissionData* mission) { static void mission_freeData(MissionData* mission) {
if(mission->name) { if(mission->name) free(mission->name);
free(mission->name); if(mission->lua) free(mission->lua);
mission->name = NULL; if(mission->avail.planet) free(mission->avail.planet);
} if(mission->avail.system) free(mission->avail.system);
if(mission->lua) { if(mission->avail.factions) free(mission->avail.factions);
free(mission->lua); memset(mission, 0, sizeof(MissionData));
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;
}
} }
// Does mission match faction requirement? // Does mission match faction requirement?

View File

@ -62,7 +62,7 @@ void mission_accept(Mission* misn);
// Load/Quit. // Load/Quit.
int missions_load(void); int missions_load(void);
void mission_free(Mission* mission); void mission_cleanup(Mission* misn);
void missions_free(void); void missions_free(void);
void missions_cleanup(void); void missions_cleanup(void);

View File

@ -493,7 +493,8 @@ static void widget_cleanup(Widget* widget) {
case WIDGET_LIST: // Must clear the list. case WIDGET_LIST: // Must clear the list.
if(widget->dat.lst.options) { if(widget->dat.lst.options) {
for(i = 0; i < widget->dat.lst.noptions; i++) 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); free(widget->dat.lst.options);
} }
break; break;