diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/map/Makefile.in | 4 | ||||
-rw-r--r-- | src/map/chrif.c | 5 | ||||
-rw-r--r-- | src/map/clif.c | 5 | ||||
-rw-r--r-- | src/map/instance.c | 458 | ||||
-rw-r--r-- | src/map/instance.h | 48 | ||||
-rw-r--r-- | src/map/intif.c | 3 | ||||
-rw-r--r-- | src/map/map.c | 568 | ||||
-rw-r--r-- | src/map/map.h | 40 | ||||
-rw-r--r-- | src/map/npc.c | 75 | ||||
-rw-r--r-- | src/map/npc.h | 4 | ||||
-rw-r--r-- | src/map/party.c | 12 | ||||
-rw-r--r-- | src/map/pc.c | 25 | ||||
-rw-r--r-- | src/map/script.c | 266 | ||||
-rw-r--r-- | src/map/script.h | 2 | ||||
-rw-r--r-- | src/map/unit.c | 3 |
15 files changed, 772 insertions, 746 deletions
diff --git a/src/map/Makefile.in b/src/map/Makefile.in index 10cf1ed39..cf6a29d6c 100644 --- a/src/map/Makefile.in +++ b/src/map/Makefile.in @@ -17,7 +17,7 @@ MAP_OBJ = map.o chrif.o clif.o pc.o status.o npc.o \ npc_chat.o chat.o path.o itemdb.o mob.o script.o \ storage.o skill.o atcommand.o battle.o battleground.o \ intif.o trade.o party.o vending.o guild.o pet.o \ - log.o mail.o date.o unit.o homunculus.o mercenary.o quest.o + log.o mail.o date.o unit.o homunculus.o mercenary.o quest.o instance.o MAP_TXT_OBJ = $(MAP_OBJ:%=obj_txt/%) \ obj_txt/mapreg_txt.o MAP_SQL_OBJ = $(MAP_OBJ:%=obj_sql/%) \ @@ -26,7 +26,7 @@ MAP_H = map.h chrif.h clif.h pc.h status.h npc.h \ chat.h itemdb.h mob.h script.h path.h \ storage.h skill.h atcommand.h battle.h battleground.h \ intif.h trade.h party.h vending.h guild.h pet.h \ - log.h mail.h date.h unit.h homunculus.h mercenary.h quest.h mapreg.h + log.h mail.h date.h unit.h homunculus.h mercenary.h quest.h instance.h mapreg.h HAVE_MYSQL=@HAVE_MYSQL@ ifeq ($(HAVE_MYSQL),yes) diff --git a/src/map/chrif.c b/src/map/chrif.c index c725ff30a..0ab4aec51 100644 --- a/src/map/chrif.c +++ b/src/map/chrif.c @@ -20,6 +20,7 @@ #include "skill.h" #include "status.h" #include "homunculus.h" +#include "instance.h" #include "mercenary.h" #include "chrif.h" #include "quest.h" @@ -316,9 +317,9 @@ int chrif_sendmap(int fd) int i; ShowStatus("Sending maps to char server...\n"); // Sending normal maps, not instances - WFIFOHEAD(fd, 4 + map_instance_start * 4); + WFIFOHEAD(fd, 4 + instance_start * 4); WFIFOW(fd,0) = 0x2afa; - for(i = 0; i < map_instance_start; i++) + for(i = 0; i < instance_start; i++) WFIFOW(fd,4+i*4) = map[i].index; WFIFOW(fd,2) = 4 + i * 4; WFIFOSET(fd,WFIFOW(fd,2)); diff --git a/src/map/clif.c b/src/map/clif.c index 3182ca6b7..2c5bf1234 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -33,6 +33,7 @@ #include "vending.h" #include "pet.h" #include "homunculus.h" +#include "instance.h" #include "mercenary.h" #include "log.h" #include "clif.h" @@ -7988,12 +7989,12 @@ void clif_parse_LoadEndAck(int fd,struct map_session_data *sd) pc_setinvincibletimer(sd,battle_config.pc_invincible_time); } - if (map[sd->bl.m].users++ == 0 && battle_config.dynamic_mobs) //Skotlex + if( map[sd->bl.m].users++ == 0 && battle_config.dynamic_mobs ) map_spawnmobs(sd->bl.m); if( map[sd->bl.m].instance_id ) { instance[map[sd->bl.m].instance_id].users++; - map_instance_check_idle(map[sd->bl.m].instance_id); + instance_check_idle(map[sd->bl.m].instance_id); } sd->state.debug_remove_map = 0; // temporary state to track double remove_map's [FlavioJS] diff --git a/src/map/instance.c b/src/map/instance.c new file mode 100644 index 000000000..45e610b8d --- /dev/null +++ b/src/map/instance.c @@ -0,0 +1,458 @@ +// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
+// For more information, see LICENCE in the main folder
+
+#include "../common/cbasetypes.h"
+#include "../common/socket.h"
+#include "../common/timer.h"
+#include "../common/malloc.h"
+#include "../common/version.h"
+#include "../common/nullpo.h"
+#include "../common/showmsg.h"
+#include "../common/strlib.h"
+#include "../common/utils.h"
+#include "../common/db.h"
+
+#include "clif.h"
+#include "instance.h"
+#include "map.h"
+#include "npc.h"
+#include "party.h"
+#include "pc.h"
+
+int instance_start = 0; // To keep the last index + 1 of normal map inserted in the map[ARRAY]
+struct s_instance instance[MAX_INSTANCE];
+
+/*--------------------------------------
+ * name : instance name
+ * Return value could be
+ * -4 = already exists | -3 = no free instances | -2 = missing parameter | -1 = invalid type
+ * On success return instance_id
+ *--------------------------------------*/
+int instance_create(int party_id, const char *name)
+{
+ int i;
+ struct party_data *p = NULL;
+
+ if( !party_id || !name )
+ {
+ ShowError("map_instance_create: missing parameter.\n");
+ return -2;
+ }
+
+ p = party_search(party_id);
+ if( !p || p->instance_id )
+ return -4; // Party already instancing
+
+ // Searching a Free Instance
+ // 0 is ignored as this mean "no instance" on maps
+ ARR_FIND(1, MAX_INSTANCE, i, instance[i].state == INSTANCE_FREE);
+ if( i == MAX_INSTANCE )
+ {
+ ShowError("map_instance_create: no free instances, consider increasing MAX_INSTANCE.\n");
+ return -3;
+ }
+
+ instance[i].state = INSTANCE_IDLE;
+ instance[i].instance_id = i;
+ instance[i].idle_timer = INVALID_TIMER;
+ instance[i].idle_timeout = instance[i].idle_timeoutval = 0;
+ instance[i].progress_timer = INVALID_TIMER;
+ instance[i].progress_timeout = instance[i].progress_timeoutval = 0;
+ instance[i].users = 0;
+ instance[i].party_id = party_id;
+ instance[i].ivar = NULL;
+ instance[i].svar = NULL;
+
+ memcpy( instance[i].name, name, sizeof(instance[i].name) );
+ memset( instance[i].map, 0x00, sizeof(instance[i].map) );
+ p->instance_id = i;
+
+ clif_instance(i, 1, 0); // Start instancing window
+ ShowInfo("[Instance] Created: %s.\n", name);
+ return i;
+}
+
+/*--------------------------------------
+ * Add a map to the instance using src map "name"
+ *--------------------------------------*/
+int instance_add_map(const char *name, int instance_id)
+{
+ int m = map_mapname2mapid(name), i, im = -1;
+ size_t num_cell, size;
+
+ if( m < 0 )
+ return -1; // source map not found
+
+ if( instance[instance_id].state == INSTANCE_FREE )
+ {
+ ShowError("instance_add_map: trying to attach '%s' map to non-existing instance %d.\n", name, instance_id);
+ return -1;
+ }
+ if( instance[instance_id].num_map >= MAX_MAP_PER_INSTANCE )
+ {
+ ShowError("instance_add_map: trying to add '%s' map to instance %d (%s) failed. Please increase MAX_MAP_PER_INSTANCE.\n", name, instance_id, instance[instance_id].name);
+ return -2;
+ }
+ if( map[m].instance_id != 0 )
+ { // Source map already belong to a Instance.
+ ShowError("instance_add_map: trying to instance already instanced map %s.\n", name);
+ return -4;
+ }
+
+ ARR_FIND( instance_start, map_num, i, !map[i].name[0] ); // Searching for a Free Map
+ if( i < map_num ) im = i; // Unused map found (old instance)
+ else if( map_num - 1 >= MAX_MAP_PER_SERVER )
+ { // No more free maps
+ ShowError("instance_add_map: no more free space to create maps on this server.\n");
+ return -5;
+ }
+ else im = map_num++; // Using next map index
+
+ memcpy( &map[im], &map[m], sizeof(struct map_data) ); // Copy source map
+ snprintf(map[im].name, MAP_NAME_LENGTH, "%.3d%s", instance_id, name); // Generate Name for Instance Map
+ map[im].index = mapindex_addmap(-1, map[im].name); // Add map index
+
+ if( !map[im].index )
+ {
+ map[im].name[0] = '\0';
+ ShowError("instance_add_map: no more free map indexes.\n");
+ return -3; // No free map index
+ }
+
+ // Reallocate cells
+ num_cell = map[im].xs * map[im].ys;
+ CREATE( map[im].cell, struct mapcell, num_cell );
+ memcpy( map[im].cell, map[m].cell, num_cell * sizeof(struct mapcell) );
+
+ size = map[im].bxs * map[im].bys * sizeof(struct block_list*);
+ map[im].block = (struct block_list**)aCalloc(size, 1);
+ map[im].block_mob = (struct block_list**)aCalloc(size, 1);
+
+ memset(map[im].npc, 0x00, sizeof(map[i].npc));
+ map[im].npc_num = 0;
+
+ memset(map[im].moblist, 0x00, sizeof(map[im].moblist));
+ map[im].mob_delete_timer = INVALID_TIMER;
+
+ map[im].m = im;
+ map[im].instance_id = instance_id;
+ map[im].instance_src_map = m;
+ map[m].flag.src4instance = 1; // Flag this map as a src map for instances
+
+ instance[instance_id].map[instance[instance_id].num_map++] = im; // Attach to actual instance
+ map_addmap2db(&map[im]);
+
+ return im;
+}
+
+/*--------------------------------------
+ * m : source map of this instance
+ * party_id : source party of this instance
+ * type : result (0 = map id | 1 = instance id)
+ *--------------------------------------*/
+int instance_map2imap(int m, int party_id, int type)
+{
+ int i;
+ struct party_data *p;
+ if( (p = party_search(party_id)) == NULL || !p->instance_id )
+ return -1;
+
+ for( i = 0; i < instance[p->instance_id].num_map; i++ )
+ {
+ if( instance[p->instance_id].map[i] && map[instance[p->instance_id].map[i]].instance_src_map == m )
+ {
+ if( type == 0 )
+ return instance[p->instance_id].map[i];
+ else
+ return p->instance_id;
+ }
+ }
+ return -1;
+}
+
+/*--------------------------------------
+ * m : source map
+ * instance_id : where to search
+ * result : mapid of map "m" in this instance
+ *--------------------------------------*/
+int instance_mapid2imapid(int m, int instance_id)
+{
+ int i, max;
+ if( map[m].instance_src_map == 0 )
+ return m; // not instances found for this map
+ else if( map[m].instance_id )
+ { // This map is a instance, not a src map instance
+ ShowError("map_instance_mapid2imapid: already instanced (%d / %d)\n", m, instance_id);
+ return -1;
+ }
+
+ if( instance_id <= 0 )
+ return -1;
+
+ max = instance[instance_id].num_map;
+
+ for( i = 0; i < max; i++ )
+ if( map[instance[instance_id].map[i]].instance_src_map == m )
+ return instance[instance_id].map[i];
+
+ return -1;
+}
+
+/*--------------------------------------
+ * map_instance_map_npcsub
+ * Used on Init instance. Duplicates each script on source map
+ *--------------------------------------*/
+int instance_map_npcsub(struct block_list* bl, va_list args)
+{
+ struct npc_data* nd = (struct npc_data*)bl;
+ int m = va_arg(args, int); // Destination Map
+
+ npc_duplicate4instance(nd, m);
+ return 1;
+}
+
+/*--------------------------------------
+ * Init all map on the instance. Npcs are created here
+ *--------------------------------------*/
+void instance_init(int instance_id)
+{
+ int i;
+ if( !instance_id ) return;
+ if( instance[instance_id].state != INSTANCE_IDLE )
+ {
+ ShowError("instance_init: instance already initialited.\n");
+ return;
+ }
+
+ for( i = 0; i < instance[instance_id].num_map; i++ )
+ map_foreachinmap(instance_map_npcsub, map[instance[instance_id].map[i]].instance_src_map, BL_NPC, instance[instance_id].map[i]);
+
+ instance[instance_id].state = INSTANCE_BUSSY;
+ ShowInfo("[Instance] Initialized %s.\n", instance[instance_id].name);
+}
+
+/*--------------------------------------
+ * Used on instance deleting process.
+ * Warps all players on each instance map to its save points.
+ *--------------------------------------*/
+int instance_del_load(struct map_session_data* sd, va_list args)
+{
+ int m = va_arg(args,int);
+ if( !sd || sd->bl.m != m )
+ return 0;
+
+ pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 0);
+ return 1;
+}
+
+/*--------------------------------------
+ * Removes a simple instance map
+ *--------------------------------------*/
+void instance_del_map(int m)
+{
+ int sm, i;
+ if( m <= 0 || !map[m].instance_id )
+ {
+ ShowError("Tried to remove non-existing instance map (%d)\n", m);
+ return;
+ }
+
+ sm = map[m].instance_src_map;
+ map_foreachpc(instance_del_load, m);
+ map_foreachinmap(cleanup_sub, m, BL_ALL);
+
+ if( map[m].mob_delete_timer != INVALID_TIMER )
+ delete_timer(map[m].mob_delete_timer, map_removemobs_timer);
+
+ mapindex_removemap( map[m].index );
+
+ // Free memory
+ aFree(map[m].cell);
+ aFree(map[m].block);
+ aFree(map[m].block_mob);
+
+ // Remove from instance
+ for( i = 0; i < instance[map[m].instance_id].num_map; i++ )
+ {
+ if( instance[map[m].instance_id].map[i] == m )
+ {
+ instance[map[m].instance_id].num_map--;
+ for( ; i < instance[map[m].instance_id].num_map; i++ )
+ instance[map[m].instance_id].map[i] = instance[map[m].instance_id].map[i+1];
+ i = -1;
+ break;
+ }
+ }
+ if( i == instance[map[m].instance_id].num_map )
+ ShowError("map_instance_del: failed to remove %s from instance list (%s): %d\n", map[m].name, instance[map[m].instance_id].name, m);
+
+ map_removemapdb(&map[m]);
+ memset(&map[m], 0x00, sizeof(map[0]));
+}
+
+/*--------------------------------------
+ * Used for instance variables. Clean each variable from memory.
+ *--------------------------------------*/
+void instance_destroy_freesvar(void *key, void *data, va_list args)
+{
+ if( data ) aFree(data);
+}
+
+/*--------------------------------------
+ * Timer to destroy instance by process or idle
+ *--------------------------------------*/
+int instance_destroy_timer(int tid, unsigned int tick, int id, intptr data)
+{
+ instance_destroy(id);
+ return 0;
+}
+
+/*--------------------------------------
+ * Removes a instance, all its maps and npcs.
+ *--------------------------------------*/
+void instance_destroy(int instance_id)
+{
+ int last = 0, type;
+ struct party_data *p;
+ time_t now = time(NULL);
+
+ if( !instance_id || instance[instance_id].state == INSTANCE_FREE )
+ return;
+
+ if( instance[instance_id].progress_timeout && instance[instance_id].progress_timeout <= now )
+ type = 1;
+ else if( instance[instance_id].idle_timeout && instance[instance_id].idle_timeout <= now )
+ type = 2;
+ else
+ type = 3;
+
+ clif_instance(instance_id, 5, type); // Report users this instance has been destroyed
+
+ while( instance[instance_id].num_map && last != instance[instance_id].map[0] )
+ { // Remove all maps from instance
+ last = instance[instance_id].map[0];
+ instance_del_map( instance[instance_id].map[0] );
+ }
+
+ if( instance[instance_id].ivar )
+ linkdb_final( &instance[instance_id].ivar ); // Remove numeric vars
+
+ if( instance[instance_id].svar )
+ { // Remove string vars
+ linkdb_foreach( &instance[instance_id].svar, instance_destroy_freesvar );
+ linkdb_final( &instance[instance_id].svar );
+ }
+
+ if( instance[instance_id].progress_timer != INVALID_TIMER )
+ delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);
+ if( instance[instance_id].idle_timer != INVALID_TIMER )
+ delete_timer( instance[instance_id].idle_timer, instance_destroy_timer);
+
+ instance[instance_id].ivar = NULL;
+ instance[instance_id].svar = NULL;
+
+ if( instance[instance_id].party_id && (p = party_search(instance[instance_id].party_id)) != NULL )
+ p->instance_id = 0; // Update Party information
+
+ ShowInfo("[Instance] Destroyed %s.\n", instance[instance_id].name);
+ memset( &instance[instance_id], 0x00, sizeof(instance[0]) );
+
+ instance[instance_id].state = INSTANCE_FREE;
+}
+
+/*--------------------------------------
+ * Checks if there are users in the instance or not to start idle timer
+ *--------------------------------------*/
+void instance_check_idle(int instance_id)
+{
+ bool idle = true;
+ time_t now = time(NULL);
+
+ if( !instance_id || instance[instance_id].idle_timeoutval == 0 )
+ return;
+
+ if( instance[instance_id].users )
+ idle = false;
+
+ if( instance[instance_id].idle_timer != INVALID_TIMER && !idle )
+ {
+ delete_timer(instance[instance_id].idle_timer, instance_destroy_timer);
+ instance[instance_id].idle_timer = INVALID_TIMER;
+ instance[instance_id].idle_timeout = 0;
+ clif_instance(instance_id, 3, 0); // Notify instance users normal instance expiration
+ }
+ else if( instance[instance_id].idle_timer == INVALID_TIMER && idle )
+ {
+ instance[instance_id].idle_timeout = now + instance[instance_id].idle_timeoutval;
+ instance[instance_id].idle_timer = add_timer( gettick() + (unsigned int)instance[instance_id].idle_timeoutval * 1000, instance_destroy_timer, instance_id, 0);
+ clif_instance(instance_id, 4, 0); // Notify instance users it will be destroyed of no user join it again in "X" time
+ }
+}
+
+/*--------------------------------------
+ * Set instance Timers
+ *--------------------------------------*/
+void instance_set_timeout(int instance_id, unsigned int progress_timeout, unsigned int idle_timeout)
+{
+ time_t now = time(0);
+
+ if( !instance_id )
+ return;
+
+ if( instance[instance_id].progress_timer != INVALID_TIMER )
+ delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);
+ if( instance[instance_id].idle_timer != INVALID_TIMER )
+ delete_timer( instance[instance_id].idle_timer, instance_destroy_timer);
+
+ if( progress_timeout )
+ {
+ instance[instance_id].progress_timeoutval = progress_timeout;
+ instance[instance_id].progress_timeout = now + progress_timeout;
+ instance[instance_id].progress_timer = add_timer( gettick() + progress_timeout * 1000, instance_destroy_timer, instance_id, 0);
+ }
+ else
+ {
+ instance[instance_id].progress_timeoutval = 0;
+ instance[instance_id].progress_timeout = 0;
+ instance[instance_id].progress_timer = INVALID_TIMER;
+ }
+
+ if( idle_timeout )
+ {
+ instance[instance_id].idle_timeoutval = idle_timeout;
+ instance[instance_id].idle_timer = INVALID_TIMER;
+ instance_check_idle(instance_id);
+ }
+ else
+ {
+ instance[instance_id].idle_timeoutval = 0;
+ instance[instance_id].idle_timeout = 0;
+ instance[instance_id].idle_timer = INVALID_TIMER;
+ }
+
+ if( instance[instance_id].idle_timer == INVALID_TIMER && instance[instance_id].progress_timer != INVALID_TIMER )
+ clif_instance(instance_id, 3, 0);
+}
+
+/*--------------------------------------
+ * Checks if sd in on a instance and should be kicked from it
+ *--------------------------------------*/
+void instance_check_kick(struct map_session_data *sd)
+{
+ int m = sd->bl.m;
+
+ clif_instance_leave(sd->fd);
+ if( map[m].instance_id )
+ { // User was on the instance map
+ if( map[m].save.map )
+ pc_setpos(sd, map[m].save.map, map[m].save.x, map[m].save.y, 3);
+ else
+ pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
+ }
+}
+
+void do_init_instance(void)
+{
+ memset(instance, 0x00, sizeof(instance));
+ add_timer_func_list(instance_destroy_timer, "instance_destroy_timer");
+}
diff --git a/src/map/instance.h b/src/map/instance.h new file mode 100644 index 000000000..a98117c57 --- /dev/null +++ b/src/map/instance.h @@ -0,0 +1,48 @@ +// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
+// For more information, see LICENCE in the main folder
+
+#ifndef _INSTANCE_H_
+#define _INSTANCE_H_
+
+#define MAX_MAP_PER_INSTANCE 10
+#define MAX_INSTANCE 500
+
+typedef enum instance_state { INSTANCE_FREE, INSTANCE_IDLE, INSTANCE_BUSSY } instance_state;
+
+struct s_instance {
+ char name[61]; // Instance Name - required for clif functions.
+ instance_state state;
+ short instance_id;
+ int party_id;
+
+ int map[MAX_MAP_PER_INSTANCE];
+ int num_map;
+ int users;
+
+ struct linkdb_node *ivar, *svar; // Instance Variable for scripts
+
+ int progress_timer;
+ time_t progress_timeout, progress_timeoutval;
+
+ int idle_timer;
+ time_t idle_timeout, idle_timeoutval;
+};
+
+extern int instance_start;
+extern struct s_instance instance[MAX_INSTANCE];
+
+int instance_create(int party_id, const char *name);
+int instance_add_map(const char *name, int instance_id);
+void instance_del_map(int m);
+int instance_map2imap(int m, int party_id, int type);
+int instance_mapid2imapid(int m, int instance_id);
+void instance_destroy(int instance_id);
+void instance_init(int instance_id);
+
+void instance_check_idle(int instance_id);
+void instance_check_kick(struct map_session_data *sd);
+void instance_set_timeout(int instance_id, unsigned int progress_timeout, unsigned int idle_timeout);
+
+void do_init_instance();
+
+#endif
diff --git a/src/map/intif.c b/src/map/intif.c index d3a46001f..9b60d40c0 100644 --- a/src/map/intif.c +++ b/src/map/intif.c @@ -431,10 +431,9 @@ int intif_party_changemap(struct map_session_data *sd,int online) return 0; if( (m=map_mapindex2mapid(sd->mapindex)) >= 0 && map[m].instance_id ) - mapindex = map[map[m].instance_map[0]].index; + mapindex = map[map[m].instance_src_map].index; else mapindex = sd->mapindex; - WFIFOHEAD(inter_fd,19); WFIFOW(inter_fd,0)=0x3025; diff --git a/src/map/map.c b/src/map/map.c index 8a74bcca1..ea938ab8f 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -38,6 +38,7 @@ #include "guild.h" #include "pet.h" #include "homunculus.h" +#include "instance.h" #include "mercenary.h" #include "atcommand.h" #include "log.h" @@ -115,9 +116,6 @@ static int bl_list_count = 0; struct map_data map[MAX_MAP_PER_SERVER]; int map_num = 0; -int map_instance_start = 0; // To keep the last index + 1 of normal map inserted in the map[ARRAY] -struct map_instance instance[MAX_INSTANCE]; - int map_port=0; int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; @@ -2670,7 +2668,7 @@ int map_addmap(char* mapname) if( strcmpi(mapname,"clear")==0 ) { map_num = 0; - map_instance_start = 0; + instance_start = 0; return 0; } @@ -2787,6 +2785,20 @@ int map_readgat (struct map_data* m) } /*====================================== +/*====================================== + * Add/Remove map to the map_db + *--------------------------------------*/ +void map_addmap2db(struct map_data *m) +{ + uidb_put(map_db, (unsigned int)m->index, m); +} + +void map_removemapdb(struct map_data *m) +{ + uidb_remove(map_db, (unsigned int)m->index); +} + +/*====================================== * Initiate maps loading stage *--------------------------------------*/ int map_readallmaps (void) @@ -2841,7 +2853,7 @@ int map_readallmaps (void) continue; } - uidb_put(map_db, (unsigned int)map[i].index, &map[i]); + map_addmap2db(&map[i]); map[i].m = i; memset(map[i].moblist, 0, sizeof(map[i].moblist)); //Initialize moblist [Skotlex] @@ -2866,7 +2878,7 @@ int map_readallmaps (void) // finished map loading ShowInfo("Successfully loaded '"CL_WHITE"%d"CL_RESET"' maps."CL_CLL"\n",map_num); - map_instance_start = map_num; // Next Map Index will be instances + instance_start = map_num; // Next Map Index will be instances if (maps_removed) ShowNotice("Maps removed: '"CL_WHITE"%d"CL_RESET"'\n",maps_removed); @@ -2874,544 +2886,6 @@ int map_readallmaps (void) return 0; } -/*====================================== - * Instancing [by Sirius White] - *--------------------------------------*/ - -/*-------------------------------------- - * map_instance_map2imap - * m : source map of this instance - * sd : source party/guild of this instance - * type : result (0 = map id | 1 = instance id) - *--------------------------------------*/ -int map_instance_map2imap(int m, struct map_session_data *sd, int type) -{ - int i; - struct party_data *p; - if( !sd->status.party_id || (p = party_search(sd->status.party_id)) == NULL || !p->instance_id ) - return -1; - - for( i = 0; i < instance[p->instance_id].num_map; i++ ) - { - if( instance[p->instance_id].map[i] && map[instance[p->instance_id].map[i]].instance_map[0] == m ) - { - if( type == 0 ) - return instance[p->instance_id].map[i]; - else - return p->instance_id; - } - } - return -1; -} - -/*-------------------------------------- - * map_instance_mapid2imapid - * m : source map - * instance_id : where to search - * result : mapid of map "m" in this instance - *--------------------------------------*/ -int map_instance_mapid2imapid(int m, int instance_id) -{ - int i, max; - if( map[m].instance_map[0] == 0 ) - return m; // not instances found for this map - else if( map[m].instance_id ) - { // This map is a instance - ShowError("map_instance_mapid2imapid: already instanced (%d / %d)\n", m, instance_id); - return -1; - } - - if( instance_id <= 0 ) - return -1; - - max = instance[instance_id].num_map; - - for( i = 0; i < max; i++ ) - if( map[instance[instance_id].map[i]].instance_map[0] == m ) - return instance[instance_id].map[i]; - - return -1; -} - -/*-------------------------------------- - * map_instance_create - * type : 0 = Party | 1 = Guild - * group_id : party_id / guild_id - * name_id : ... - * name : instance name - * Return value could be - * -4 = already exists | -3 = no free instances | -2 = missing parameter | -1 = invalid type - * On success return instance_id - *--------------------------------------*/ -int map_instance_create(int party_id, int name_id, const char *name) -{ - int i; - struct party_data *p = NULL; - - if( !party_id || !name_id || !name ) - { - ShowError("map_instance_create: missing parameter.\n"); - return -2; - } - - p = party_search(party_id); - if( !p || p->instance_id ) - return -4; // Party already instancing - - // Searching a Free Instance - // 0 is ignored as this mean "no instance" on maps - ARR_FIND(1, MAX_INSTANCE, i, instance[i].party_id == 0); - if( i == MAX_INSTANCE ) - { - ShowError("map_instance_create: no free instances, consider increasing MAX_INSTANCE.\n"); - return -3; - } - - instance[i].instance_id = i; - instance[i].name_id = name_id; - instance[i].idle_timer = INVALID_TIMER; - instance[i].idle_timeout = instance[i].idle_timeoutval = 0; - instance[i].progress_timer = INVALID_TIMER; - instance[i].progress_timeout = instance[i].progress_timeoutval = 0; - instance[i].users = 0; - instance[i].party_id = party_id; - instance[i].ivar = NULL; - instance[i].svar = NULL; - - memcpy( instance[i].name, name, sizeof(instance[i].name) ); - memset( instance[i].map, 0x00, sizeof(instance[i].map) ); - p->instance_id = i; - - clif_instance(i, 1, 0); // Start instancing window - ShowInfo("[Instance] Created: %s.\n", name); - return i; -} - -/*-------------------------------------- - * map_instance_npcname - * Conver "name" to "name_MEM000" format - *--------------------------------------*/ -char *map_instance_npcname(char *name, int instance_id) -{ - static char npc_name[NAME_LENGTH+1]; - uint32 crc = crc32(name, strlen(name)); - - snprintf(npc_name, sizeof(npc_name), "MEM_%.8x_%u", crc, instance_id); - return &npc_name[0]; -} - -/*-------------------------------------- - * map_instance_map - * Add a map to the instance using src map "name" - *--------------------------------------*/ -int map_instance_map(const char *name, int instance_id) -{ - int m = map_mapname2mapid(name), i, ii = -1, im = -1; - size_t num_cell, size; - - if( m < 0 ) - return -1; // source map not found - - if( !instance[instance_id].name_id ) - { - ShowError("map_instance_map: trying to attach '%s' map to non-existing instance %d.\n", name, instance_id); - return -1; - } - if( instance[instance_id].num_map >= MAX_MAP_PER_INSTANCE ) - { - ShowError("map_instance_map: trying to add '%s' map to instance %d (%s) failed. Please increase MAX_MAP_PER_INSTANCE.\n", name, instance_id, instance[instance_id].name); - return -2; - } - if( map[m].instance_id != 0 ) - { // Source map is a Instance. - ShowError("map_instance_map: trying to instance already instanced map %s.\n", name); - return -4; - } - - ARR_FIND(map_instance_start, map_num, i, !map[i].name[0]); // Searching for a Free Map - if( i < map_num ) im = i; // Unused map found (old instance) - else if( map_num - 1 >= MAX_MAP_PER_SERVER ) - { // No more free maps - ShowError("map_instance_map: no more free space to create maps on this server.\n"); - return -5; - } - else im = map_num++; // Using next map index - - // Grab instance map id - ARR_FIND(0, ARRAYLENGTH(map[m].instance_map), i, !map[m].instance_map[i]); - if( i >= ARRAYLENGTH(map[m].instance_map) ) - { - ShowError("map_instance_map: limit of instances per map reach.\n"); - return -2; - } - - ii = i; - - // Copy source map - memcpy( &map[im], &map[m], sizeof(struct map_data)); - - // Add map index - snprintf(map[im].name, MAP_NAME_LENGTH, "%.3d%s", ii, name); - map[im].index = mapindex_addmap(-1, map[im].name); - if( !map[im].index ) - { - map[im].name[0] = '\0'; - ShowError("map_instance_map: no more free map indexes.\n"); - return -3; // No free map index - } - - // Reallocate cells - num_cell = map[im].xs * map[im].ys; - CREATE( map[im].cell, struct mapcell, num_cell); - memcpy( map[im].cell, map[m].cell, num_cell * sizeof(struct mapcell)); - - size = map[im].bxs * map[im].bys * sizeof(struct block_list*); - map[im].block = (struct block_list**)aCalloc(size, 1); - map[im].block_mob = (struct block_list**)aCalloc(size, 1); - - memset(map[im].npc, 0x00, sizeof(map[i].npc)); - map[im].npc_num = 0; - - memset(map[im].moblist, 0x00, sizeof(map[im].moblist)); - map[im].mob_delete_timer = INVALID_TIMER; - - map[im].m = im; - map[im].instance_id = instance_id; - map[m].instance_map[ii] = im; // add the mapid of this instance to the source map - map[im].instance_map[0] = m; // uses index [0] to store source map for this instance - - instance[instance_id].map[instance[instance_id].num_map++] = im; // Attach to actual instance - uidb_put(map_db, (unsigned int)map[im].index, &map[im]); // Put to map list - - return im; -} - -/*-------------------------------------- - * map_instance_map_npcsub - * Used on Init instance. Duplicates each script on source map - *--------------------------------------*/ -int map_instance_map_npcsub(struct block_list* bl, va_list args) -{ - char *inst_name; - static char w1[50], w2[50], w3[50], w4[50]; - const char* stat_buf = "- call from instancing subsystem -\n"; - struct npc_data* nd = (struct npc_data*)bl; - int m = va_arg(args, int); - - inst_name = map_instance_npcname(nd->exname, map[m].instance_id); - if( inst_name == NULL ) - return 1; - - if( nd->subtype == WARP ) - { // Adjust destination, if instanced - struct npc_data *wnd; - int dm = map_mapindex2mapid(nd->u.warp.mapindex), im; - - if( dm < 0 ) return 1; - - im = map_instance_mapid2imapid(dm, map[m].instance_id); - if( im == -1 ) - { - ShowError("map_instance_map_npcsub: warp (%s) leading to instanced map (%s), but instance map is not attached to current instance.\n", map[dm].name, nd->exname); - return 1; - } - - CREATE(wnd, struct npc_data, 1); - wnd->bl.id = npc_get_new_npc_id(); - map_addnpc(m, wnd); - wnd->bl.prev = wnd->bl.next = NULL; - wnd->bl.m = m; - wnd->bl.x = nd->bl.x; - wnd->bl.y = nd->bl.y; - safestrncpy(wnd->name, "", ARRAYLENGTH(wnd->name)); - safestrncpy(wnd->exname, inst_name, ARRAYLENGTH(wnd->exname)); - wnd->class_ = WARP_CLASS; - wnd->speed = 200; - wnd->u.warp.mapindex = map_id2index(im); - wnd->u.warp.x = nd->u.warp.x; - wnd->u.warp.y = nd->u.warp.y; - wnd->u.warp.xs = nd->u.warp.xs; - wnd->u.warp.ys = nd->u.warp.ys; - wnd->bl.type = BL_NPC; - wnd->subtype = WARP; - npc_setcells(wnd); - map_addblock(&wnd->bl); - status_set_viewdata(&wnd->bl, wnd->class_); - status_change_init(&wnd->bl); - unit_dataset(&wnd->bl); - clif_spawn(&wnd->bl); - strdb_put(npcname_db, wnd->exname, wnd); - - return 1; - } - - snprintf(w1, sizeof(w1), "%s,%d,%d,%d", map[m].name, nd->bl.x, nd->bl.y, nd->ud.dir); - snprintf(w2, sizeof(w2), "duplicate(%s)", nd->exname); - snprintf(w3, sizeof(w3), "%s::%s", nd->name, inst_name); - if( nd->u.scr.xs >= 0 && nd->u.scr.ys >= 0 ) - snprintf(w4, sizeof(w4), "%d,%d,%d", nd->class_, nd->u.scr.xs, nd->u.scr.ys); - else - snprintf(w4, sizeof(w4), "%d", nd->class_); - - npc_parse_duplicate(w1, w2, w3, w4, stat_buf, stat_buf, "INSTANCING"); - return 1; -} - -/*-------------------------------------- - * map_instance_init - * Init all map on the instance. Npcs are created here - *--------------------------------------*/ -void map_instance_init(int instance_id) -{ - int i; - if( !instance_id ) - return; - - for( i = 0; i < instance[instance_id].num_map; i++ ) - map_foreachinmap(map_instance_map_npcsub, map[instance[instance_id].map[i]].instance_map[0], BL_NPC, instance[instance_id].map[i]); - - ShowInfo("[Instance] Initialized %s.\n", instance[instance_id].name); -} - -/*-------------------------------------- - * map_instance_del_load - * Used on instance deleting process. - * Warps all players on each instance map to its save points. - *--------------------------------------*/ -int map_instance_del_load(struct map_session_data* sd, va_list args) -{ - int m = va_arg(args,int); - if( !sd || sd->bl.m != m ) - return 0; - - pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 0); - return 1; -} - -/*-------------------------------------- - * map_instance_del - * Removes a simple instance map - *--------------------------------------*/ -void map_instance_del(int m) -{ - int sm, i; - if( m <= 0 || !map[m].instance_id ) - { - ShowError("Tried to remove non-existing instance map (%d)\n", m); - return; - } - - sm = map[m].instance_map[0]; - map_foreachpc(map_instance_del_load, m); - map_foreachinmap(cleanup_sub, m, BL_ALL); - - if( map[m].mob_delete_timer != INVALID_TIMER ) - delete_timer(map[m].mob_delete_timer, map_removemobs_timer); - - mapindex_removemap( map[m].index ); - - // Free memory - aFree(map[m].cell); - aFree(map[m].block); - aFree(map[m].block_mob); - - // Remove from instance - for( i = 0; i < instance[map[m].instance_id].num_map; i++ ) - { - if( instance[map[m].instance_id].map[i] == m ) - { - instance[map[m].instance_id].num_map--; - for( ; i < instance[map[m].instance_id].num_map; i++ ) - instance[map[m].instance_id].map[i] = instance[map[m].instance_id].map[i+1]; - i = -1; - break; - } - } - if( i == instance[map[m].instance_id].num_map ) - ShowError("map_instance_del: failed to remove %s from instance list (%s): %d\n", map[m].name, instance[map[m].instance_id].name, m); - - uidb_remove(map_db, map[m].index); // Remove from mapindex db - - map[m].m = map[m].index = 0; - map[m].name[0] = '\0'; - - ARR_FIND(0, ARRAYLENGTH(map[sm].instance_map), i, map[sm].instance_map[i] == m); - if( i < ARRAYLENGTH(map[sm].instance_map) ) - { - memmove(&map[sm].instance_map[i], &map[sm].instance_map[i+1], sizeof(map[sm].instance_map) - (i+1)*sizeof(map[sm].instance_map[0])); - map[sm].instance_map[ARRAYLENGTH(map[sm].instance_map) - 1] = 0; // Because i am not sure if the final value is set to 0 by "memmove" - } - else - ShowError("Failed to remove instance from instance db.\n"); - - memset(&map[m], 0x00, sizeof(map[0])); -} - -/*-------------------------------------- - * map_instance_destroy_freesvar - * Used for instance variables. Clean each variable from memory. - *--------------------------------------*/ -void map_instance_destroy_freesvar(void *key, void *data, va_list args) -{ - if( data ) aFree(data); -} - -/*-------------------------------------- - * map_instance_destroy_timer - * Timer to destroy instance by process or idle - *--------------------------------------*/ -int map_instance_destroy_timer(int tid, unsigned int tick, int id, intptr data) -{ - map_instance_destroy(id); - return 0; -} - -/*-------------------------------------- - * map_instance_destroy - * Removes a instance, all its maps and npcs. - *--------------------------------------*/ -void map_instance_destroy(int instance_id) -{ - int last = 0, type; - struct party_data *p; - time_t now = time(NULL); - - if( !instance_id || !instance[instance_id].name_id ) - return; - - if( instance[instance_id].progress_timeout && instance[instance_id].progress_timeout <= now ) - type = 1; - else if( instance[instance_id].idle_timeout && instance[instance_id].idle_timeout <= now ) - type = 2; - else - type = 3; - - clif_instance(instance_id, 5, type); // Report users this instance has been destroyed - - while( instance[instance_id].num_map && last != instance[instance_id].map[0] ) - { - last = instance[instance_id].map[0]; - map_instance_del( instance[instance_id].map[0] ); - } - - if( instance[instance_id].ivar ) - linkdb_final( &instance[instance_id].ivar ); - - if( instance[instance_id].svar ) - { - linkdb_foreach( &instance[instance_id].svar, map_instance_destroy_freesvar ); - linkdb_final( &instance[instance_id].svar ); - } - - if( instance[instance_id].progress_timer != INVALID_TIMER ) - delete_timer( instance[instance_id].progress_timer, map_instance_destroy_timer); - if( instance[instance_id].idle_timer != INVALID_TIMER ) - delete_timer( instance[instance_id].idle_timer, map_instance_destroy_timer); - - instance[instance_id].ivar = NULL; - instance[instance_id].svar = NULL; - - if( instance[instance_id].party_id && (p = party_search(instance[instance_id].party_id)) != NULL ) - p->instance_id = 0; - - ShowInfo("[Instance] Destroyed %s.\n", instance[instance_id].name); - memset( &instance[instance_id], 0x00, sizeof(instance[0]) ); -} - -/*-------------------------------------- - * map_instance_check_idle - * Checks if there are users in the instance or not to start idle timer - *--------------------------------------*/ -void map_instance_check_idle(int instance_id) -{ - bool idle = true; - time_t now = time(NULL); - - if( !instance_id || instance[instance_id].idle_timeoutval == 0 ) - return; - - if( instance[instance_id].users ) - idle = false; - - if( instance[instance_id].idle_timer != INVALID_TIMER && !idle ) - { - delete_timer(instance[instance_id].idle_timer, map_instance_destroy_timer); - instance[instance_id].idle_timer = INVALID_TIMER; - instance[instance_id].idle_timeout = 0; - clif_instance(instance_id, 3, 0); // Notify instance users normal instance expiration - } - else if( instance[instance_id].idle_timer == INVALID_TIMER && idle ) - { - instance[instance_id].idle_timeout = now + instance[instance_id].idle_timeoutval; - instance[instance_id].idle_timer = add_timer( gettick() + (unsigned int)instance[instance_id].idle_timeoutval * 1000, map_instance_destroy_timer, instance_id, 0); - clif_instance(instance_id, 4, 0); // Notify instance users it will be destroyed of no user join it again in "X" time - } -} - -/*-------------------------------------- - * map_instance_set_timeout - * Set instance Timers - *--------------------------------------*/ -void map_instance_set_timeout(int instance_id, unsigned int progress_timeout, unsigned int idle_timeout) -{ - time_t now = time(0); - - if( !instance_id ) - return; - - if( instance[instance_id].progress_timer != INVALID_TIMER ) - delete_timer( instance[instance_id].progress_timer, map_instance_destroy_timer); - if( instance[instance_id].idle_timer != INVALID_TIMER ) - delete_timer( instance[instance_id].idle_timer, map_instance_destroy_timer); - - if( progress_timeout ) - { - instance[instance_id].progress_timeoutval = progress_timeout; - instance[instance_id].progress_timeout = now + progress_timeout; - instance[instance_id].progress_timer = add_timer( gettick() + progress_timeout * 1000, map_instance_destroy_timer, instance_id, 0); - } - else - { - instance[instance_id].progress_timeoutval = 0; - instance[instance_id].progress_timeout = 0; - instance[instance_id].progress_timer = INVALID_TIMER; - } - - if( idle_timeout ) - { - instance[instance_id].idle_timeoutval = idle_timeout; - instance[instance_id].idle_timer = INVALID_TIMER; - map_instance_check_idle(instance_id); - } - else - { - instance[instance_id].idle_timeoutval = 0; - instance[instance_id].idle_timeout = 0; - instance[instance_id].idle_timer = INVALID_TIMER; - } - - if( instance[instance_id].idle_timer == INVALID_TIMER && instance[instance_id].progress_timer != INVALID_TIMER ) - clif_instance(instance_id, 3, 0); -} - -/*-------------------------------------- - * map_instance_check_mapkick - * Checks if sd in on a instance and should be kicked from it - *--------------------------------------*/ -void map_instance_check_kick(struct map_session_data *sd) -{ - int m = sd->bl.m; - - clif_instance_leave(sd->fd); - if( map[m].instance_id ) - { // User was on the instance map - if( map[m].save.map ) - pc_setpos(sd, map[m].save.map, map[m].save.x, map[m].save.y, 3); - else - pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3); - } -} - //////////////////////////////////////////////////////////////////////// static int map_ip_set = 0; static int char_ip_set = 0; @@ -3830,7 +3304,7 @@ void do_final(void) mapit_free(iter); for( i = 0; i < MAX_INSTANCE; i++ ) - if( instance[i].party_id ) map_instance_destroy(i); + if( instance[i].state != INSTANCE_FREE ) instance_destroy(i); id_db->foreach(id_db,cleanup_db_sub); chrif_char_reset_offline(); @@ -4056,8 +3530,6 @@ int do_init(int argc, char *argv[]) map_sql_init(); #endif /* not TXT_ONLY */ - memset(instance, 0x00, sizeof(instance)); - mapindex_init(); if(enable_grf) grfio_init(GRF_PATH_FILENAME); @@ -4067,11 +3539,11 @@ int do_init(int argc, char *argv[]) add_timer_func_list(map_freeblock_timer, "map_freeblock_timer"); add_timer_func_list(map_clearflooritem_timer, "map_clearflooritem_timer"); add_timer_func_list(map_removemobs_timer, "map_removemobs_timer"); - add_timer_func_list(map_instance_destroy_timer, "map_instance_destroy_timer"); add_timer_interval(gettick()+1000, map_freeblock_timer, 0, 0, 60*1000); do_init_atcommand(); do_init_battle(); + do_init_instance(); do_init_chrif(); do_init_clif(); do_init_script(); diff --git a/src/map/map.h b/src/map/map.h index 8e02becbd..a5a9ac03d 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -45,8 +45,6 @@ struct item_data; #define MAX_IGNORE_LIST 20 // official is 14 #define MAX_VENDING 12 #define MOBID_EMPERIUM 1288 -#define MAX_MAP_PER_INSTANCE 10 -#define MAX_INSTANCE 500 //The following system marks a different job ID system used by the map server, //which makes a lot more sense than the normal one. [Skotlex] @@ -463,6 +461,7 @@ struct map_data { unsigned nochat :1; unsigned partylock :1; unsigned guildlock :1; + unsigned src4instance : 1; // To flag this map when it's used as a src map for instances } flag; struct point save; struct npc_data *npc[MAX_NPC_PER_MAP]; @@ -478,8 +477,9 @@ struct map_data { int jexp; // map experience multiplicator int bexp; // map experience multiplicator int nocommand; //Blocks @/# commands for non-gms. [Skotlex] + // Instance Variables int instance_id; - int instance_map[1000]; + int instance_src_map; }; /// Stores information about a remote map (for multi-mapserver setups). @@ -492,22 +492,6 @@ struct map_data_other_server { uint16 port; }; -// Instancing -struct map_instance { - char name[61]; - unsigned int name_id; -// int m; - unsigned int instance_id; - int party_id; - int map[MAX_MAP_PER_INSTANCE]; - int num_map; - struct linkdb_node *ivar, *svar; - time_t progress_timeout, idle_timeout; - time_t progress_timeoutval, idle_timeoutval; - int progress_timer, idle_timer; - int users; -}; - int map_getcell(int,int,int,cell_chk); int map_getcellp(struct map_data*,int,int,cell_chk); void map_setcell(int m, int x, int y, cell_t cell, bool flag); @@ -515,7 +499,6 @@ void map_setgatcell(int m, int x, int y, int gat); extern struct map_data map[]; extern int map_num; -extern int map_instance_start; extern int autosave_interval; extern int minsave_interval; @@ -637,21 +620,8 @@ int map_addmobtolist(unsigned short m, struct spawn_data *spawn); // [Wizputer] void map_spawnmobs(int); // [Wizputer] void map_removemobs(int); // [Wizputer] void do_reconnect_map(void); //Invoked on map-char reconnection [Skotlex] - -// Instancing -int map_instance_map(const char *name, int instance_id); -void map_instance_del(int m); -int map_instance_create(int party_id, int name_id, const char *name); -void map_instance_init(int instance_id); -int map_instance_mapid2imapid(int m, int instance_id); -int map_instance_map2imap(int m, struct map_session_data *sd, int type); -void map_instance_destroy(int instance_id); -void map_instance_check_idle(int instance_id); -void map_instance_check_kick(struct map_session_data *sd); -char *map_instance_npcname(char *name, int instance_id); -void map_instance_set_timeout(int instance_id, unsigned int progress_timeout, unsigned int idle_timeout); - -extern struct map_instance instance[MAX_INSTANCE]; +void map_addmap2db(struct map_data *m); +void map_removemapdb(struct map_data *m); extern char *INTER_CONF_NAME; extern char *LOG_CONF_NAME; diff --git a/src/map/npc.c b/src/map/npc.c index 05a58f92f..3614968c0 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -21,6 +21,7 @@ #include "script.h" #include "mob.h" #include "pet.h" +#include "instance.h" #include "battle.h" #include "skill.h" #include "unit.h" @@ -2303,6 +2304,80 @@ const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const ch return end; } +int npc_duplicate4instance(struct npc_data *snd, int m) +{ + char newname[NAME_LENGTH]; + int i = 0; + + if( map[m].instance_id == 0 ) + return 1; + + snprintf(newname, ARRAYLENGTH(newname), "dup_%d_%d", map[m].instance_id, snd->bl.id); + if( npc_name2id(newname) != NULL ) + { // Name already in use + ShowError("npc_duplicate4instance: the npcname (%s) is already in use while trying to duplicate npc %s in instance %d.\n", newname, snd->exname, map[m].instance_id); + return 1; + } + + if( snd->subtype == WARP ) + { // Adjust destination, if instanced + struct npc_data *wnd = NULL; // New NPC + int dm = map_mapindex2mapid(snd->u.warp.mapindex), im; + if( dm < 0 ) return 1; + + im = instance_mapid2imapid(dm, map[m].instance_id); + if( im == -1 ) + { + ShowError("npc_duplicate4instance: warp (%s) leading to instanced map (%s), but instance map is not attached to current instance.\n", map[dm].name, snd->exname); + return 1; + } + + CREATE(wnd, struct npc_data, 1); + wnd->bl.id = npc_get_new_npc_id(); + map_addnpc(m, wnd); + wnd->bl.prev = wnd->bl.next = NULL; + wnd->bl.m = m; + wnd->bl.x = snd->bl.x; + wnd->bl.y = snd->bl.y; + safestrncpy(wnd->name, "", ARRAYLENGTH(wnd->name)); + safestrncpy(wnd->exname, newname, ARRAYLENGTH(wnd->exname)); + wnd->class_ = WARP_CLASS; + wnd->speed = 200; + wnd->u.warp.mapindex = map_id2index(im); + wnd->u.warp.x = snd->u.warp.x; + wnd->u.warp.y = snd->u.warp.y; + wnd->u.warp.xs = snd->u.warp.xs; + wnd->u.warp.ys = snd->u.warp.ys; + wnd->bl.type = BL_NPC; + wnd->subtype = WARP; + npc_setcells(wnd); + map_addblock(&wnd->bl); + status_set_viewdata(&wnd->bl, wnd->class_); + status_change_init(&wnd->bl); + unit_dataset(&wnd->bl); + clif_spawn(&wnd->bl); + strdb_put(npcname_db, wnd->exname, wnd); + } + else + { + static char w1[50], w2[50], w3[50], w4[50]; + const char* stat_buf = "- call from instancing subsystem -\n"; + + snprintf(w1, sizeof(w1), "%s,%d,%d,%d", map[m].name, snd->bl.x, snd->bl.y, snd->ud.dir); + snprintf(w2, sizeof(w2), "duplicate(%s)", snd->exname); + snprintf(w3, sizeof(w3), "%s::%s", snd->name, newname); + + if( snd->u.scr.xs >= 0 && snd->u.scr.ys >= 0 ) + snprintf(w4, sizeof(w4), "%d,%d,%d", snd->class_, snd->u.scr.xs, snd->u.scr.ys); // Touch Area + else + snprintf(w4, sizeof(w4), "%d", snd->class_); + + npc_parse_duplicate(w1, w2, w3, w4, stat_buf, stat_buf, "INSTANCING"); + } + + return 0; +} + void npc_setcells(struct npc_data* nd) { int m = nd->bl.m, x = nd->bl.x, y = nd->bl.y, xs, ys; diff --git a/src/map/npc.h b/src/map/npc.h index 56d2bad5c..dcb112966 100644 --- a/src/map/npc.h +++ b/src/map/npc.h @@ -145,11 +145,9 @@ int npc_reload(void); void npc_read_event_script(void); int npc_script_event(struct map_session_data* sd, enum npce_event type); -const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath); - +int npc_duplicate4instance(struct npc_data *snd, int m); int npc_cashshop_buy(struct map_session_data *sd, int nameid, int amount, int points); extern struct npc_data* fake_nd; -extern DBMap* npcname_db; #endif /* _NPC_H_ */ diff --git a/src/map/party.c b/src/map/party.c index a7e6758e4..4a10036d3 100644 --- a/src/map/party.c +++ b/src/map/party.c @@ -14,6 +14,7 @@ #include "atcommand.h" //msg_txt() #include "pc.h" #include "map.h" +#include "instance.h" #include "battle.h" #include "intif.h" #include "clif.h" @@ -540,7 +541,7 @@ int party_member_leaved(int party_id, int account_id, int char_id) clif_charnameupdate(sd); //Update name display [Skotlex] //TODO: hp bars should be cleared too if( p->instance_id ) - map_instance_check_kick(sd); + instance_check_kick(sd); } return 0; @@ -559,15 +560,18 @@ int party_broken(int party_id) if( p->instance_id ) { instance[p->instance_id].party_id = 0; - map_instance_destroy( p->instance_id ); + instance_destroy( p->instance_id ); } - for(i=0;i<MAX_PARTY;i++){ - if(p->data[i].sd!=NULL){ + for( i = 0; i < MAX_PARTY; i++ ) + { + if( p->data[i].sd!=NULL ) + { clif_party_leaved(p,p->data[i].sd,p->party.member[i].account_id,p->party.member[i].name,0x10); p->data[i].sd->status.party_id=0; } } + idb_remove(party_db,party_id); return 0; } diff --git a/src/map/pc.c b/src/map/pc.c index 25bfaa9e9..2ded5e38b 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -25,6 +25,7 @@ #include "map.h" #include "path.h" #include "homunculus.h" +#include "instance.h" #include "mercenary.h" #include "mob.h" // MAX_MOB_RACE_DB #include "npc.h" // fake_nd @@ -3868,11 +3869,13 @@ int pc_steal_coin(struct map_session_data *sd,struct block_list *target) *------------------------------------------*/ int pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, uint8 clrtype) { + struct party_data *p; int m; nullpo_retr(0, sd); - if (!mapindex || !mapindex_id2name(mapindex)) { + if( !mapindex || !mapindex_id2name(mapindex) ) + { ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex); return 1; } @@ -3884,17 +3887,17 @@ int pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y } m = map_mapindex2mapid(mapindex); - if( map[m].instance_map[0] && map[m].instance_id == 0 ) - { // Source Instance Map - int im = map_instance_map2imap(m, sd, 0); - if( im <= 0 ) - { - ShowError("pc_setpos: player %s trying to enter instance map '%s' without instanced copy.\n", sd->status.name, map[m].name); - return 2; // map not found + if( map[m].flag.src4instance && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id ) + { + // Request the mapid of this src map into the instance of the party + int im = instance_map2imap(m, sd->status.party_id, 0); + if( im < 0 ) + ; // Player will enter the src map for instances + else + { // Changes destiny to the instance map, not the source map + m = im; + mapindex = map_id2index(m); } - - m = im; - mapindex = map_id2index(m); } sd->state.changemap = (sd->mapindex != mapindex); diff --git a/src/map/script.c b/src/map/script.c index 3a74c5d00..ac4c34860 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -30,6 +30,7 @@ #include "pet.h" #include "mapreg.h" #include "homunculus.h" +#include "instance.h" #include "mercenary.h" #include "intif.h" #include "skill.h" @@ -71,10 +72,6 @@ // - remove GETVALUE / SETVALUE // - clean up the set_reg / set_val / setd_sub mess -void crc32_init(); - - - // // struct script_state* st; // @@ -3447,7 +3444,6 @@ int do_final_script() *------------------------------------------*/ int do_init_script() { - crc32_init(); userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0); scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50); @@ -7474,10 +7470,9 @@ BUILDIN_FUNC(monster) else { m = map_mapname2mapid(mapn); - if( map[m].instance_map[0] && map[m].instance_id == 0 && st->instance_id ) - { - m = map_instance_mapid2imapid(m, st->instance_id); - if( m < 0 ) + if( map[m].flag.src4instance && st->instance_id ) + { // Try to redirect to the instance map, not the src map + if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 ) { ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn); return 1; @@ -7555,10 +7550,9 @@ BUILDIN_FUNC(areamonster) else { m = map_mapname2mapid(mapn); - if( map[m].instance_map[0] && map[m].instance_id == 0 && st->instance_id ) - { - m = map_instance_mapid2imapid(m, st->instance_id); - if( m < 0 ) + if( map[m].flag.src4instance && st->instance_id ) + { // Try to redirect to the instance map, not the src map + if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 ) { ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn); return 1; @@ -7619,9 +7613,9 @@ BUILDIN_FUNC(killmonster) if( (m=map_mapname2mapid(mapname))<0 ) return 0; - if( map[m].instance_map[0] && map[m].instance_id == 0 && st->instance_id && (m=map_instance_mapid2imapid(m, st->instance_id)) < 0 ) + if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 ) return 0; - + if( script_hasdata(st,4) ) { if ( script_getnum(st,4) == 1 ) { map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag); @@ -7657,10 +7651,10 @@ BUILDIN_FUNC(killmonsterall) int m; mapname=script_getstr(st,2); - if( (m=map_mapname2mapid(mapname))<0 ) + if( (m = map_mapname2mapid(mapname))<0 ) return 0; - - if( map[m].instance_map[0] && map[m].instance_id == 0 && st->instance_id && (m=map_instance_mapid2imapid(m, st->instance_id)) < 0 ) + + if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 ) return 0; if( script_hasdata(st,3) ) { @@ -9915,12 +9909,12 @@ BUILDIN_FUNC(mobcount) // Added by RoVeRT event=script_getstr(st,3); check_event(st, event); - if( (m=map_mapname2mapid(mapname))<0 ) { + if( (m = map_mapname2mapid(mapname)) < 0 ) { script_pushint(st,-1); return 0; } - if( map[m].instance_map[0] && map[m].instance_id == 0 && st->instance_id && (m=map_instance_mapid2imapid(m, st->instance_id)) < 0 ) + if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 ) { script_pushint(st,-1); return 0; @@ -13727,15 +13721,12 @@ BUILDIN_FUNC(bg_get_data) BUILDIN_FUNC(instance_create) { const char *name; - int party_id, name_id; - int res; + int party_id, res; name = script_getstr(st, 2); party_id = script_getnum(st, 3); - name_id = script_getnum(st, 4); - - res = map_instance_create(party_id, name_id, name); + res = instance_create(party_id, name); if( res == -4 ) // Already exists { script_pushint(st, -1); @@ -13763,19 +13754,24 @@ BUILDIN_FUNC(instance_create) BUILDIN_FUNC(instance_destroy) { int instance_id; - + struct map_session_data *sd; + struct party_data *p; + if( script_hasdata(st, 2) ) instance_id = script_getnum(st, 2); - else + else if( st->instance_id ) instance_id = st->instance_id; - + else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id ) + instance_id = p->instance_id; + else return 0; + if( instance_id <= 0 || instance_id >= MAX_INSTANCE ) { ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id); return 0; } - - map_instance_destroy(instance_id); + + instance_destroy(instance_id); return 0; } @@ -13785,18 +13781,14 @@ BUILDIN_FUNC(instance_attachmap) int m; int instance_id; - instance_id = script_getnum(st, 2); - name = script_getstr(st, 3); - - m = map_instance_map(name, instance_id); - - if( m < 0 ) + name = script_getstr(st, 2); + instance_id = script_getnum(st, 3); + if( (m = instance_add_map(name, instance_id)) < 0 ) { ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m); script_pushconststr(st, ""); return 0; } - script_pushconststr(st, map[m].name); return 0; @@ -13804,19 +13796,27 @@ BUILDIN_FUNC(instance_attachmap) BUILDIN_FUNC(instance_detachmap) { - const char *name; - int m; - - name = script_getstr(st, 2); - - m = map_mapname2mapid(name); - if( m < 0 ) - { - ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", name); - return 0; - } - map_instance_del(m); - + struct map_session_data *sd; + struct party_data *p; + const char *str; + int m, instance_id; + + str = script_getstr(st, 2); + if( script_hasdata(st, 3) ) + instance_id = script_getnum(st, 3); + else if( st->instance_id ) + instance_id = st->instance_id; + else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id ) + instance_id = p->instance_id; + else return 0; + + if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id,0)) < 0 ) + { + ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str); + return 0; + } + + instance_del_map(m); return 0; } @@ -13835,7 +13835,7 @@ BUILDIN_FUNC(instance_attach) BUILDIN_FUNC(instance_id) { int type, instance_id; - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd; struct party_data *p; if( script_hasdata(st, 2) ) @@ -13843,13 +13843,14 @@ BUILDIN_FUNC(instance_id) type = script_getnum(st, 2); if( type == 0 ) instance_id = st->instance_id; - else if( type == 1 && sd && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL ) + else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL ) instance_id = p->instance_id; else instance_id = 0; } else instance_id = st->instance_id; + script_pushint(st, instance_id); return 0; } @@ -13858,29 +13859,30 @@ BUILDIN_FUNC(instance_set_timeout) { int progress_timeout, idle_timeout; int instance_id; + struct map_session_data *sd; + struct party_data *p; progress_timeout = script_getnum(st, 2); idle_timeout = script_getnum(st, 3); - + if( script_hasdata(st, 4) ) instance_id = script_getnum(st, 4); - else + else if( st->instance_id ) instance_id = st->instance_id; - + else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id ) + instance_id = p->instance_id; + else return 0; + if( instance_id > 0 ) - map_instance_set_timeout(instance_id, progress_timeout, idle_timeout); + instance_set_timeout(instance_id, progress_timeout, idle_timeout); return 0; } BUILDIN_FUNC(instance_init) { - int instance_id; - - instance_id = script_getnum(st, 2); - - map_instance_init(instance_id); - + int instance_id = script_getnum(st, 2); + instance_init(instance_id); return 0; } @@ -13888,78 +13890,105 @@ BUILDIN_FUNC(instance_announce) { const char *str, *color=NULL; int flag,instance_id,i; + struct map_session_data *sd; + struct party_data *p; - instance_id=script_getnum(st,2); - str=script_getstr(st,3); - flag=script_getnum(st,4); - if (script_hasdata(st,5)) - color=script_getstr(st,5); - + instance_id = script_getnum(st,2); + str = script_getstr(st,3); + flag = script_getnum(st,4); + if( script_hasdata(st,5) ) + color = script_getstr(st,5); + if( instance_id == 0 ) - instance_id = st->instance_id; - + { + if( st->instance_id ) + instance_id = st->instance_id; + else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id ) + instance_id = p->instance_id; + else return 0; + } + if( instance_id <= 0 || instance_id >= MAX_INSTANCE ) return 0; - for(i=0; i<instance[instance_id].num_map; i++) - map_foreachinmap(buildin_mapannounce_sub, instance[instance_id].map[i], BL_PC, str,strlen(str)+1,flag&0x10, color); + for( i = 0; i < instance[instance_id].num_map; i++ ) + map_foreachinmap(buildin_mapannounce_sub, instance[instance_id].map[i], BL_PC, str, strlen(str) + 1, flag&0x10, color); + return 0; } BUILDIN_FUNC(instance_npcname) { const char *str; - struct npc_data *nd = map_id2nd(st->oid); int instance_id; + + struct map_session_data *sd; + struct party_data *p; + struct npc_data *nd; str = script_getstr(st, 2); if( script_hasdata(st, 3) ) instance_id = script_getnum(st, 3); - else + else if( st->instance_id ) instance_id = st->instance_id; - - script_pushconststr(st, map_instance_npcname((char*)str, instance_id)); + else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id ) + instance_id = p->instance_id; + else return 0; + + if( (nd = npc_name2id(str)) != NULL ) + { + char npcname[NAME_LENGTH]; + snprintf(npcname, ARRAYLENGTH(npcname), "dup_%d_%d", instance_id, nd->bl.id); + script_pushconststr(st,npcname); + } + else + script_pushconststr(st,""); + return 0; } BUILDIN_FUNC(has_instance) { - TBL_PC* sd = script_rid2sd(st); + struct map_session_data *sd = script_rid2sd(st); const char *str; int m; - + str = script_getstr(st, 2); - if( (m = map_mapname2mapid(str)) < 0 || (m = map_instance_map2imap(m,sd,0)) < 0 ) + if( !sd || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, sd->status.party_id, 0)) < 0 ) { script_pushconststr(st, ""); return 0; } - + script_pushconststr(st, map[m].name); return 0; } BUILDIN_FUNC(instance_warpall) { - TBL_PC *pl_sd; - int m, i; + struct map_session_data *pl_sd; + int m, i, instance_id; const char *mapn; int x, y; unsigned short mapindex; struct party_data *p = NULL; - - if( !st->instance_id ) - return 0; - - mapn = script_getstr(st, 2); - x = script_getnum(st, 3); - y = script_getnum(st, 4); - - if( (m=map_mapname2mapid(mapn)) < 0 || (map[m].instance_map[0] && (m=map_instance_mapid2imapid(m,st->instance_id)) < 0) ) + + mapn = script_getstr(st,2); + x = script_getnum(st,3); + y = script_getnum(st,4); + if( script_hasdata(st,5) ) + instance_id = script_getnum(st,5); + else if( st->instance_id ) + instance_id = st->instance_id; + else if( (pl_sd = script_rid2sd(st)) != NULL && pl_sd->status.party_id && (p = party_search(pl_sd->status.party_id)) != NULL && p->instance_id ) + instance_id = p->instance_id; + else return 0; + + if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) ) return 0; - - if( !(p = party_search(instance[st->instance_id].party_id)) ) + + if( !(p = party_search(instance[instance_id].party_id)) ) return 0; mapindex = map_id2index(m); @@ -13969,39 +13998,6 @@ BUILDIN_FUNC(instance_warpall) return 0; } -void crc32_init() -{ - uint32 crc, poly; - uint32 i, c; - - poly = 0xEDB88320; - - for( i = 0; i < 256; i++ ) - { - crc = i; - for( c = 8; c; c-- ) - { - if( crc & 1 ) - crc = (crc >> 1) ^ poly; - else - crc >>= 1; - } - - crctab[i] = crc; - } -} - -uint32 crc32(char *dat, int len) -{ - uint32 crc, i; - - crc = 0xFFFFFFFF; - for( i=0; i<len; i++ ) - crc = ((crc >> 8 ) & 0x00FFFFFF) ^ crctab[(crc ^ *dat++) & 0xFF]; - - return (crc ^ 0xFFFFFFFF); -} - /*========================================== * Custom Fonts *------------------------------------------*/ @@ -14074,7 +14070,7 @@ BUILDIN_FUNC(areamobuseskill) return 0; } - if( map[m].instance_map[0] && map[m].instance_id == 0 && st->instance_id && (m=map_instance_mapid2imapid(m, st->instance_id)) < 0 ) + if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 ) return 0; center.m = m; @@ -14467,19 +14463,21 @@ struct script_function buildin_func[] = { BUILDIN_DEF(bg_get_data,"ii"), BUILDIN_DEF(bg_getareausers,"isiiii"), BUILDIN_DEF(bg_updatescore,"sii"), + // Instancing - BUILDIN_DEF(instance_create,"sii?"), + BUILDIN_DEF(instance_create,"si"), BUILDIN_DEF(instance_destroy,"?"), - BUILDIN_DEF(instance_attachmap,"is"), - BUILDIN_DEF(instance_detachmap,"is"), + BUILDIN_DEF(instance_attachmap,"si"), + BUILDIN_DEF(instance_detachmap,"s?"), + BUILDIN_DEF(instance_attach,"i"), + BUILDIN_DEF(instance_id,"?"), + BUILDIN_DEF(instance_set_timeout,"ii?"), BUILDIN_DEF(instance_init,"i"), BUILDIN_DEF(instance_announce,"isi*"), - BUILDIN_DEF(instance_attach,"i"), BUILDIN_DEF(instance_npcname,"s?"), BUILDIN_DEF(has_instance,"s"), - BUILDIN_DEF(instance_id,"?"), - BUILDIN_DEF(instance_warpall,"sii"), - BUILDIN_DEF(instance_set_timeout,"ii?"), + BUILDIN_DEF(instance_warpall,"sii?"), + //Quest Log System [Inkfish] BUILDIN_DEF(setquest, "i"), BUILDIN_DEF(erasequest, "i"), diff --git a/src/map/script.h b/src/map/script.h index 904ddf740..79822031c 100644 --- a/src/map/script.h +++ b/src/map/script.h @@ -166,6 +166,4 @@ int add_str(const char* p); const char* get_str(int id); int script_reload(void); -uint32 crc32(char *dat, int len); - #endif /* _SCRIPT_H_ */ diff --git a/src/map/unit.c b/src/map/unit.c index f19eb4d4a..878c1c934 100644 --- a/src/map/unit.c +++ b/src/map/unit.c @@ -13,6 +13,7 @@ #include "mob.h" #include "pet.h" #include "homunculus.h" +#include "instance.h" #include "mercenary.h" #include "skill.h" #include "clif.h" @@ -1843,7 +1844,7 @@ int unit_remove_map_(struct block_list *bl, int clrtype, const char* file, int l if( map[bl->m].instance_id ) { instance[map[bl->m].instance_id].users--; - map_instance_check_idle(map[bl->m].instance_id); + instance_check_idle(map[bl->m].instance_id); } sd->state.debug_remove_map = 1; // temporary state to track double remove_map's [FlavioJS] sd->debug_file = file; |