/*-------------------------------------- * 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_is_valid(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 } }
/*-------------------------------------- * 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_is_valid(instance_id) ) return; // nothing to do 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; }
/*-------------------------------------- * name : instance name * Return value could be * -4 = already exists | -3 = no free instances | -2 = party not found | -1 = invalid type * On success return instance_id *--------------------------------------*/ int instance_create(int party_id, const char *name) { int i; struct party_data* p; if( party_id ) { if( ( p = party_search(party_id) ) == NULL ) { ShowError("instance_create: party %d not found for instance '%s'.\n", party_id, name); return -2; } if( 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("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 = 0; instance[i].users = 0; instance[i].party_id = (( party_id ) ? party_id : 0); instance[i].ivar = NULL; instance[i].svar = NULL; safestrncpy( instance[i].name, name, sizeof(instance[i].name) ); memset( instance[i].map, 0x00, sizeof(instance[i].map) ); if( party_id ) p->instance_id = i; clif_instance(i, 1, 0); // Start instancing window ShowInfo("[Instance] Created: %s.\n", name); return i; }
/*-------------------------------------- * 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; }
/*-------------------------------------- * 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); }