diff options
author | ai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2011-07-02 12:07:32 +0000 |
---|---|---|
committer | ai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2011-07-02 12:07:32 +0000 |
commit | 329d0adaf22491489d511982e57ae614dc1834c9 (patch) | |
tree | ba26b25451971071e38ea95a31ec646de76f83a8 /src/map | |
parent | 179b9771be97e87168b39799bdbf82767d46fc45 (diff) | |
download | hercules-329d0adaf22491489d511982e57ae614dc1834c9.tar.gz hercules-329d0adaf22491489d511982e57ae614dc1834c9.tar.bz2 hercules-329d0adaf22491489d511982e57ae614dc1834c9.tar.xz hercules-329d0adaf22491489d511982e57ae614dc1834c9.zip |
* More instancing updates.
- Improved instance_id validity checking.
- Fixed instance name being copied with memcpy rather than safestrncpy (since r13901).
- Removed unused instance data field 'progress_timeoutval' (since r13901).
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@14879 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/map')
-rw-r--r-- | src/map/instance.c | 51 | ||||
-rw-r--r-- | src/map/instance.h | 2 |
2 files changed, 34 insertions, 19 deletions
diff --git a/src/map/instance.c b/src/map/instance.c index fecbe41f7..d533a0188 100644 --- a/src/map/instance.c +++ b/src/map/instance.c @@ -28,6 +28,24 @@ int instance_start = 0; // To keep the last index + 1 of normal map inserted in the map[ARRAY] struct s_instance instance[MAX_INSTANCE]; + +/// Checks whether given instance id is valid or not. +static bool instance_is_valid(int instance_id) +{ + if( instance_id < 1 || instance_id >= ARRAYLENGTH(instance) ) + {// out of range + return false; + } + + if( instance[instance_id].state == INSTANCE_FREE ) + {// uninitialized/freed instance slot + return false; + } + + return true; +} + + /*-------------------------------------- * name : instance name * Return value could be @@ -62,13 +80,13 @@ int instance_create(int party_id, const char *name) 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].progress_timeout = 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) ); + safestrncpy( instance[i].name, name, sizeof(instance[i].name) ); memset( instance[i].map, 0x00, sizeof(instance[i].map) ); p->instance_id = i; @@ -88,7 +106,7 @@ int instance_add_map(const char *name, int instance_id, bool usebasename) if( m < 0 ) return -1; // source map not found - if( instance[instance_id].state == INSTANCE_FREE ) + if( !instance_is_valid(instance_id) ) { ShowError("instance_add_map: trying to attach '%s' map to non-existing instance %d.\n", name, instance_id); return -1; @@ -158,6 +176,12 @@ int instance_add_map(const char *name, int instance_id, bool usebasename) int instance_map2imap(int m, int instance_id) { int i; + + if( !instance_is_valid(instance_id) ) + { + return -1; + } + for( i = 0; i < instance[instance_id].num_map; i++ ) { if( instance[instance_id].map[i] && map[instance[instance_id].map[i]].instance_src_map == m ) @@ -173,7 +197,6 @@ int instance_map2imap(int m, int instance_id) *--------------------------------------*/ int instance_mapid2imapid(int m, int instance_id) { - int i, max; if( map[m].flag.src4instance == 0 ) return m; // not instances found for this map else if( map[m].instance_id ) @@ -182,16 +205,10 @@ int instance_mapid2imapid(int m, int instance_id) return -1; } - if( instance_id <= 0 ) + if( !instance_is_valid(instance_id) ) 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; + return instance_map2imap(m, instance_id); } /*-------------------------------------- @@ -214,7 +231,7 @@ void instance_init(int instance_id) { int i; - if( !instance_id ) + if( !instance_is_valid(instance_id) ) return; // nothing to do for( i = 0; i < instance[instance_id].num_map; i++ ) @@ -309,7 +326,7 @@ void instance_destroy(int instance_id) struct party_data *p; time_t now = time(NULL); - if( !instance_id || instance[instance_id].state == INSTANCE_FREE ) + if( !instance_is_valid(instance_id) ) return; // nothing to do if( instance[instance_id].progress_timeout && instance[instance_id].progress_timeout <= now ) @@ -361,7 +378,7 @@ void instance_check_idle(int instance_id) bool idle = true; time_t now = time(NULL); - if( !instance_id || instance[instance_id].idle_timeoutval == 0 ) + if( !instance_is_valid(instance_id) || instance[instance_id].idle_timeoutval == 0 ) return; if( instance[instance_id].users ) @@ -389,7 +406,7 @@ void instance_set_timeout(int instance_id, unsigned int progress_timeout, unsign { time_t now = time(0); - if( !instance_id ) + if( !instance_is_valid(instance_id) ) return; if( instance[instance_id].progress_timer != INVALID_TIMER ) @@ -399,13 +416,11 @@ void instance_set_timeout(int instance_id, unsigned int progress_timeout, unsign 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; } diff --git a/src/map/instance.h b/src/map/instance.h index a4735113a..e2b0354f9 100644 --- a/src/map/instance.h +++ b/src/map/instance.h @@ -24,7 +24,7 @@ struct s_instance { struct linkdb_node *ivar, *svar; // Instance Variable for scripts int progress_timer; - time_t progress_timeout, progress_timeoutval; + time_t progress_timeout; int idle_timer; time_t idle_timeout, idle_timeoutval; |