//===== Hercules Plugin ======================================
//= Disable homunculus mapflag
//===== By: ==================================================
//= Originally by Samuel[Hercules]
//= Modified by Dastgir[Hercules]
//===== Current Version: =====================================
//= 1.1
//===== Compatible With: ===================================== 
//= Hercules/RagEmu
//===== Description: =========================================
//= Disable Homunculus in a certain map
//===== Usage: ===============================================
//= alberta <tab> mapflag <tab> nohomunc
//============================================================


#include "common/hercules.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common/utils.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/random.h"

#include "map/pc.h"
#include "map/npc.h"
#include "map/intif.h"
#include "map/pet.h"

#include "plugins/HPMHooking.h"
#include "common/HPMDataCheck.h"

HPExport struct hplugin_info pinfo = {	
	"nopet mapflag",
	SERVER_TYPE_MAP,
	"1.1",
	HPM_VERSION,
};

struct mapflag_data {
	unsigned nopet : 1;
};

void npc_parse_unknown_mapflag_pre(const char **name, const char **w3, const char **w4, const char **start, const char **buffer, const char **filepath, int **retval)
{
	if (strcmpi(*w3,"nopet") == 0) {
		int16 m = map->mapname2mapid(*name);
		struct mapflag_data *mf;
		if (( mf = getFromMAPD(&map->list[m], 0)) == NULL) {
			CREATE(mf, struct mapflag_data, 1);
			addToMAPD(&map->list[m], mf, 0, true);
		}
		mf->nopet = 1;
		hookStop();
	}
	return;
}
	
void clif_parse_LoadEndAck_post(int fd, struct map_session_data *sd)
{
	nullpo_retv(sd);

	if( sd->pd ) {
		struct mapflag_data *mf;
		mf = getFromMAPD(&map->list[sd->bl.m], 0);
		if (mf != NULL && mf->nopet == 1) {
			clif->messagecolor_self(sd->fd, COLOR_RED, "You can't spawn pet here.");
			pet->menu(sd, 3); //Option 3 is return to egg.
			hookStop();
		}
	}
	return;
}

void map_flags_init_pre(void)
{
	int i;
	for (i = 0; i < map->count; i++) {
		struct mapflag_data *mf = getFromMAPD(&map->list[i], 0);
		if (mf != NULL)
			removeFromMAPD(&map->list[i], 0);
	}
	return;
}

HPExport void plugin_init (void) {
	addHookPre(npc, parse_unknown_mapflag, npc_parse_unknown_mapflag_pre);
	addHookPost(clif, pLoadEndAck, clif_parse_LoadEndAck_post);
	addHookPre(map, flags_init, map_flags_init_pre);
}

HPExport void server_online(void)
{
	ShowInfo("'%s' Plugin by Dastgir/Hercules. Version '%s'\n", pinfo.name, pinfo.version);
}
