Ejemplo n.º 1
0
// execute options from the recipe context menu
static int context_recipe_handler(window_info *win, int widget_id, int mx, int my, int option)
{
	switch (option)
	{
		case CMRIC_ADD:
		{
			// add additional row and select it
			if (wanted_num_recipe_entries < max_num_recipe_entries-1)
			{
				change_num_recipe_entries(&wanted_num_recipe_entries, wanted_num_recipe_entries+1);
				set_var_OPT_INT("wanted_num_recipe_entries", wanted_num_recipe_entries);
				cur_recipe = num_recipe_entries -1;
			}
			break;
		}
		case CMRIC_CLEAR:
		{
			// clear the current recipe
			size_t i;
			for(i=0; i<NUM_MIX_SLOTS; i++)
				recipes_store[cur_recipe].items[i].quantity = 0;
			clear_recipe_name(cur_recipe);
			break;
		}
		case CMRIC_SORT:
		{
			qsort(recipes_store, num_recipe_entries, sizeof(recipe_entry), recipe_cmp);
			break;
		}
		default:
			return 0;
	}
	return 1;
}
Ejemplo n.º 2
0
// execute options from the recipe context menu
static int context_recipe_handler(window_info *win, int widget_id, int mx, int my, int option)
{
	switch (option)
	{
		case CMRIC_ADD:
		{
			// add additional row and select it
			if (wanted_num_recipe_entries < max_num_recipe_entries-1)
			{
				change_num_recipe_entries(&wanted_num_recipe_entries, wanted_num_recipe_entries+1);
				set_var_OPT_INT("wanted_num_recipe_entries", wanted_num_recipe_entries);
				cur_recipe = num_recipe_entries -1;
			}
			break;
		}
		case CMRIC_CLEAR:
		{
			// clear the current recipe
			clear_recipe_name(cur_recipe);
			init_recipe_slot(cur_recipe);
			break;
		}
		case CMRIC_DELETE:
		{
			// delete the current recipe and move the rest down to fill the gap
			clear_recipe_name(cur_recipe);
			if (cur_recipe < (num_recipe_entries - 1))
				memmove(&recipes_store[cur_recipe], &recipes_store[cur_recipe+1],
					(num_recipe_entries - cur_recipe -1) * sizeof(recipe_entry));
			init_recipe_slot(num_recipe_entries - 1);
			break;
		}
		case CMRIC_SORT:
		{
			// alphabetically sort the recipes by name, unnamed then empty slots at the end
			qsort(recipes_store, num_recipe_entries, sizeof(recipe_entry), recipe_cmp);
			break;
		}
		default:
			return 0;
	}
	return 1;
}
Ejemplo n.º 3
0
/* load recipes, done just after login when we have the player name */
void load_recipes (){
	char fname[128];
	FILE *fp;
	size_t i;
	int logged = 0;
	off_t file_size;
	const size_t recipe_size = sizeof(item)*NUM_MIX_SLOTS;

	if (recipes_loaded) {
		/*
		 * save existing recipes instead of loading them if we are already logged in
		 * this will take place when relogging after disconnection
		 */
		save_recipes();
		save_recipe_names();
		return;
	}

	safe_snprintf(fname, sizeof(fname), "recipes_%s.dat",get_lowercase_username());

	/* get file length, if a valid length adjust the number of recipe slots if required */
	file_size = get_file_size_config(fname);
	if ((file_size > 0) && (file_size % recipe_size == 0))
	{
		int num_recipes_in_file = file_size / recipe_size - 1; // -1 as last is current in pipline
		if ((num_recipes_in_file > wanted_num_recipe_entries) && (num_recipes_in_file < max_num_recipe_entries))
		{
			wanted_num_recipe_entries = num_recipes_in_file;
			set_var_OPT_INT("wanted_num_recipe_entries", wanted_num_recipe_entries);
		}
	}

	/* allocate and initialise the recipe store */
	num_recipe_entries = max_prev_num_recipe_entries = wanted_num_recipe_entries;
	recipes_store = (recipe_entry *)calloc(num_recipe_entries, sizeof(recipe_entry));
	if (recipes_store == NULL)
	{
		max_prev_num_recipe_entries = num_recipe_entries = 0;
		return;
	}
	recipes_loaded=1;
	init_recipe_names();

	/* if the file exists but is not a valid size, don't use it */
	if ((file_size > 0) && (file_size % recipe_size != 0))
	{
		LOG_ERROR("%s: Invalid format (size mismatch) \"%s\"\n", reg_error_str, fname);
		return;
	}

	/* sliently ignore non existing file */
	if (file_exists_config(fname)!=1)
		return;

	fp = open_file_config(fname,"rb");
	if(fp == NULL){
		LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno));
		return;
	}

	/* attempt to read all the recipies we're expecting */
	for (i=0; !feof(fp) && i<num_recipe_entries; i++)
	{
		if (fread (recipes_store[i].items,recipe_size,1, fp) != 1)
		{
			if (!logged)
			{
				LOG_ERROR("%s() fail during read of file [%s] : %s\n", __FUNCTION__, fname, strerror(errno));
				logged = 1;
			}
			memset(recipes_store[i].items, 0, recipe_size);
			break;
		}
		fix_recipe_uids(recipes_store[i].items);
	}

	/* if there is another, use it as the current recipe in the manufacturing pipeline */
	if (!feof(fp))
	{
		if (fread (manu_recipe.items,recipe_size,1, fp) != 1)
			memset(manu_recipe.items, 0, recipe_size);
		else
			fix_recipe_uids(manu_recipe.items);
	}
	fclose (fp);

	load_recipe_names();
}