summaryrefslogtreecommitdiff
path: root/src/map/HPMmap.c
blob: 8a69cba970fabbe2fc382aa86f3dd424cb4fd921 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file

#include "../common/cbasetypes.h"
#include "../common/malloc.h"
#include "../common/showmsg.h"
#include "../common/HPM.h"

#include "HPMmap.h"
#include "pc.h"
#include "map.h"

//
#include "atcommand.h"
#include "chat.h"
#include "chrif.h"
#include "duel.h"
#include "elemental.h"
#include "homunculus.h"
#include "instance.h"
#include "intif.h"
#include "irc-bot.h"
#include "mail.h"
#include "mapreg.h"
#include "mercenary.h"
#include "party.h"
#include "pet.h"
#include "quest.h"
#include "storage.h"
#include "trade.h"

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

struct HPM_atcommand_list {
	//tracking currently not enabled
	// - requires modifying how plugins calls atcommand creation
	// - needs load/unload during runtime support
	//unsigned int pID;/* plugin id */
	char name[ATCOMMAND_LENGTH];
	AtCommandFunc func;
};

struct HPM_atcommand_list *atcommand_list = NULL;
unsigned int atcommand_list_items = 0;

void HPM_map_addToMSD(struct map_session_data *sd, void *data, unsigned int id, unsigned int type, bool autofree) {
	struct HPluginData *HPData;
	unsigned int i;
	
	for(i = 0; i < sd->hdatac; i++) {
		if( sd->hdata[i]->pluginID == id && sd->hdata[i]->type == type ) {
			ShowError("HPMi->addToMSD:%s: error! attempting to insert duplicate struct of type %u on '%s'\n",HPM->pid2name(id),type,sd->status.name);
			return;
		}
	}
	
	CREATE(HPData, struct HPluginData, 1);

	HPData->pluginID = id;
	HPData->type = type;
	HPData->flag.free = autofree ? 1 : 0;
	HPData->data = data;
	
	RECREATE(sd->hdata,struct HPluginData *,++sd->hdatac);
	sd->hdata[sd->hdatac - 1] = HPData;
}
void *HPM_map_getFromMSD(struct map_session_data *sd, unsigned int id, unsigned int type) {
	unsigned int i;
	
	for(i = 0; i < sd->hdatac; i++) {
		if( sd->hdata[i]->pluginID == id && sd->hdata[i]->type == type ) {
			break;
		}
	}

	if( i != sd->hdatac )
		return sd->hdata[i]->data;
	
	return NULL;
}
void HPM_map_removeFromMSD(struct map_session_data *sd, unsigned int id, unsigned int type) {
	unsigned int i;
	
	for(i = 0; i < sd->hdatac; i++) {
		if( sd->hdata[i]->pluginID == id && sd->hdata[i]->type == type ) {
			break;
		}
	}
	
	if( i != sd->hdatac ) {
		unsigned int cursor;
		
		aFree(sd->hdata[i]->data);
		aFree(sd->hdata[i]);
		sd->hdata[i] = NULL;
		
		for(i = 0, cursor = 0; i < sd->hdatac; i++) {
			if( sd->hdata[i] == NULL )
				continue;
			if( i != cursor )
				sd->hdata[cursor] = sd->hdata[i];
			cursor++;
		}
		
		sd->hdatac = cursor;
	}
	
}
void HPM_map_plugin_load_sub(struct hplugin *plugin) {
	plugin->hpi->addCommand		= HPM->import_symbol("addCommand",plugin->idx);
	plugin->hpi->addScript		= HPM->import_symbol("addScript",plugin->idx);
	/* */
	plugin->hpi->addToMSD		= HPM->import_symbol("addToMSD",plugin->idx);
	plugin->hpi->getFromMSD		= HPM->import_symbol("getFromMSD",plugin->idx);
	plugin->hpi->removeFromMSD	= HPM->import_symbol("removeFromMSD",plugin->idx);
}

bool HPM_map_add_atcommand(char *name, AtCommandFunc func) {
	unsigned int i = 0;
	
	for(i = 0; i < atcommand_list_items; i++) {
		if( !strcmpi(atcommand_list[i].name,name) ) {
			ShowDebug("HPM_map_add_atcommand: duplicate command '%s', skipping...\n", name);
			return false;
		}
	}
	
	i = atcommand_list_items;
	
	RECREATE(atcommand_list, struct HPM_atcommand_list , ++atcommand_list_items);
	
	safestrncpy(atcommand_list[i].name, name, sizeof(atcommand_list[i].name));
	atcommand_list[i].func = func;
	
	return true;
}

void HPM_map_atcommands(void) {
	unsigned int i;
	
	for(i = 0; i < atcommand_list_items; i++) {
		atcommand->add(atcommand_list[i].name,atcommand_list[i].func,true);
	}
}

void HPM_map_do_final(void) {
	if( atcommand_list )
		aFree(atcommand_list);
}