/src/map/map.c
C | 6503 lines | 4694 code | 822 blank | 987 comment | 1832 complexity | 91234b6f3bf689f90174f9cee4300f7f MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0
Large files files are truncated, but you can click here to view the full file
- /**
- * This file is part of Hercules.
- * http://herc.ws - http://github.com/HerculesWS/Hercules
- *
- * Copyright (C) 2012-2015 Hercules Dev Team
- * Copyright (C) Athena Dev Teams
- *
- * Hercules is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #define HERCULES_CORE
- #include "config/core.h" // CELL_NOSTACK, CIRCULAR_AREA, CONSOLE_INPUT, DBPATH, RENEWAL
- #include "map.h"
- #include "map/HPMmap.h"
- #include "map/atcommand.h"
- #include "map/battle.h"
- #include "map/battleground.h"
- #include "map/channel.h"
- #include "map/chat.h"
- #include "map/chrif.h"
- #include "map/clif.h"
- #include "map/duel.h"
- #include "map/elemental.h"
- #include "map/guild.h"
- #include "map/homunculus.h"
- #include "map/instance.h"
- #include "map/intif.h"
- #include "map/irc-bot.h"
- #include "map/itemdb.h"
- #include "map/log.h"
- #include "map/mail.h"
- #include "map/mapreg.h"
- #include "map/mercenary.h"
- #include "map/mob.h"
- #include "map/npc.h"
- #include "map/npc.h" // npc_setcells(), npc_unsetcells()
- #include "map/party.h"
- #include "map/path.h"
- #include "map/pc.h"
- #include "map/pet.h"
- #include "map/quest.h"
- #include "map/script.h"
- #include "map/skill.h"
- #include "map/status.h"
- #include "map/storage.h"
- #include "map/trade.h"
- #include "map/unit.h"
- #include "common/HPM.h"
- #include "common/cbasetypes.h"
- #include "common/conf.h"
- #include "common/console.h"
- #include "common/core.h"
- #include "common/ers.h"
- #include "common/grfio.h"
- #include "common/memmgr.h"
- #include "common/nullpo.h"
- #include "common/random.h"
- #include "common/showmsg.h"
- #include "common/socket.h" // WFIFO*()
- #include "common/strlib.h"
- #include "common/timer.h"
- #include "common/utils.h"
- #include <math.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifndef _WIN32
- #include <unistd.h>
- #endif
- struct map_interface map_s;
- struct mapit_interface mapit_s;
- struct map_interface *map;
- struct mapit_interface *mapit;
- /*==========================================
- * server player count (of all mapservers)
- *------------------------------------------*/
- void map_setusers(int users) {
- map->users = users;
- }
- int map_getusers(void) {
- return map->users;
- }
- /**
- * Expands map->bl_list on demand
- **/
- static inline void map_bl_list_expand(void) {
- map->bl_list_size += 250;
- RECREATE(map->bl_list, struct block_list *, map->bl_list_size);
- }
- /**
- * Expands map->block_free on demand
- **/
- static inline void map_block_free_expand(void) {
- map->block_free_list_size += 100;
- RECREATE(map->block_free, struct block_list *, map->block_free_list_size);
- }
- /*==========================================
- * server player count (this mapserver only)
- *------------------------------------------*/
- int map_usercount(void) {
- return db_size(map->pc_db);
- }
- /*==========================================
- * Attempt to free a map blocklist
- *------------------------------------------*/
- int map_freeblock (struct block_list *bl) {
- nullpo_retr(map->block_free_lock, bl);
- if (map->block_free_lock == 0) {
- if( bl->type == BL_ITEM )
- ers_free(map->flooritem_ers, bl);
- else
- aFree(bl);
- bl = NULL;
- } else {
- if( map->block_free_count >= map->block_free_list_size )
- map_block_free_expand();
- map->block_free[map->block_free_count++] = bl;
- }
- return map->block_free_lock;
- }
- /*==========================================
- * Lock blocklist, (prevent map->freeblock usage)
- *------------------------------------------*/
- int map_freeblock_lock (void) {
- return ++map->block_free_lock;
- }
- /*==========================================
- * Remove the lock on map_bl
- *------------------------------------------*/
- int map_freeblock_unlock (void) {
- if ((--map->block_free_lock) == 0) {
- int i;
- for (i = 0; i < map->block_free_count; i++) {
- if( map->block_free[i]->type == BL_ITEM )
- ers_free(map->flooritem_ers, map->block_free[i]);
- else
- aFree(map->block_free[i]);
- map->block_free[i] = NULL;
- }
- map->block_free_count = 0;
- } else if (map->block_free_lock < 0) {
- ShowError("map_freeblock_unlock: lock count < 0 !\n");
- map->block_free_lock = 0;
- }
- return map->block_free_lock;
- }
- // Timer function to check if there some remaining lock and remove them if so.
- // Called each 1s
- int map_freeblock_timer(int tid, int64 tick, int id, intptr_t data) {
- if (map->block_free_lock > 0) {
- ShowError("map_freeblock_timer: block_free_lock(%d) is invalid.\n", map->block_free_lock);
- map->block_free_lock = 1;
- map->freeblock_unlock();
- }
- return 0;
- }
- /**
- * Updates the counter (cell.cell_bl) of how many objects are on a tile.
- * @param add Whether the counter should be increased or decreased
- **/
- void map_update_cell_bl( struct block_list *bl, bool increase ) {
- #ifdef CELL_NOSTACK
- int pos;
- nullpo_retv(bl);
- if( bl->m < 0 || bl->x < 0 || bl->x >= map->list[bl->m].xs
- || bl->y < 0 || bl->y >= map->list[bl->m].ys
- || !(bl->type&BL_CHAR) )
- return;
- // When reading from mapcache the cell isn't initialized
- // TODO: Maybe start initializing cells when they're loaded instead of
- // having to get them here? [Panikon]
- if( map->list[bl->m].cell == (struct mapcell *)0xdeadbeaf )
- map->cellfromcache(&map->list[bl->m]);
- pos = bl->x + bl->y*map->list[bl->m].xs;
- if( increase )
- map->list[bl->m].cell[pos].cell_bl++;
- else
- map->list[bl->m].cell[pos].cell_bl--;
- #endif
- return;
- }
- /*==========================================
- * Adds a block to the map.
- * Returns 0 on success, 1 on failure (illegal coordinates).
- *------------------------------------------*/
- int map_addblock(struct block_list* bl)
- {
- int16 m, x, y;
- int pos;
- nullpo_ret(bl);
- if (bl->prev != NULL) {
- ShowError("map_addblock: bl->prev != NULL\n");
- return 1;
- }
- m = bl->m;
- x = bl->x;
- y = bl->y;
- if( m < 0 || m >= map->count ) {
- ShowError("map_addblock: invalid map id (%d), only %d are loaded.\n", m, map->count);
- return 1;
- }
- if( x < 0 || x >= map->list[m].xs || y < 0 || y >= map->list[m].ys ) {
- ShowError("map_addblock: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d\n", map->list[m].name, x, y, map->list[m].xs, map->list[m].ys);
- return 1;
- }
- pos = x/BLOCK_SIZE+(y/BLOCK_SIZE)*map->list[m].bxs;
- if (bl->type == BL_MOB) {
- bl->next = map->list[m].block_mob[pos];
- bl->prev = &map->bl_head;
- if (bl->next) bl->next->prev = bl;
- map->list[m].block_mob[pos] = bl;
- } else {
- bl->next = map->list[m].block[pos];
- bl->prev = &map->bl_head;
- if (bl->next) bl->next->prev = bl;
- map->list[m].block[pos] = bl;
- }
- #ifdef CELL_NOSTACK
- map->update_cell_bl(bl, true);
- #endif
- return 0;
- }
- /*==========================================
- * Removes a block from the map.
- *------------------------------------------*/
- int map_delblock(struct block_list* bl)
- {
- int pos;
- nullpo_ret(bl);
- // blocklist (2ways chainlist)
- if (bl->prev == NULL) {
- if (bl->next != NULL) {
- // can't delete block (already at the beginning of the chain)
- ShowError("map_delblock error : bl->next!=NULL\n");
- }
- return 0;
- }
- #ifdef CELL_NOSTACK
- map->update_cell_bl(bl, false);
- #endif
- pos = bl->x/BLOCK_SIZE+(bl->y/BLOCK_SIZE)*map->list[bl->m].bxs;
- if (bl->next)
- bl->next->prev = bl->prev;
- if (bl->prev == &map->bl_head) {
- //Since the head of the list, update the block_list map of []
- if (bl->type == BL_MOB) {
- map->list[bl->m].block_mob[pos] = bl->next;
- } else {
- map->list[bl->m].block[pos] = bl->next;
- }
- } else {
- bl->prev->next = bl->next;
- }
- bl->next = NULL;
- bl->prev = NULL;
- return 0;
- }
- /*==========================================
- * Moves a block a x/y target position. [Skotlex]
- * Pass flag as 1 to prevent doing skill->unit_move checks
- * (which are executed by default on BL_CHAR types)
- *------------------------------------------*/
- int map_moveblock(struct block_list *bl, int x1, int y1, int64 tick) {
- struct status_change *sc = NULL;
- int x0, y0;
- int moveblock;
- nullpo_ret(bl);
- x0 = bl->x;
- y0 = bl->y;
- moveblock = ( x0/BLOCK_SIZE != x1/BLOCK_SIZE || y0/BLOCK_SIZE != y1/BLOCK_SIZE);
- if (!bl->prev) {
- //Block not in map, just update coordinates, but do naught else.
- bl->x = x1;
- bl->y = y1;
- return 0;
- }
- //TODO: Perhaps some outs of bounds checking should be placed here?
- if (bl->type&BL_CHAR) {
- sc = status->get_sc(bl);
- skill->unit_move(bl,tick,2);
- status_change_end(bl, SC_RG_CCONFINE_M, INVALID_TIMER);
- status_change_end(bl, SC_RG_CCONFINE_S, INVALID_TIMER);
- //status_change_end(bl, SC_BLADESTOP, INVALID_TIMER); //Won't stop when you are knocked away, go figure...
- status_change_end(bl, SC_NJ_TATAMIGAESHI, INVALID_TIMER);
- status_change_end(bl, SC_MAGICROD, INVALID_TIMER);
- if (sc && sc->data[SC_PROPERTYWALK] &&
- sc->data[SC_PROPERTYWALK]->val3 >= skill->get_maxcount(sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2) )
- status_change_end(bl,SC_PROPERTYWALK,INVALID_TIMER);
- } else if (bl->type == BL_NPC) {
- npc->unsetcells(BL_UCAST(BL_NPC, bl));
- }
- if (moveblock) map->delblock(bl);
- #ifdef CELL_NOSTACK
- else map->update_cell_bl(bl, false);
- #endif
- bl->x = x1;
- bl->y = y1;
- if (moveblock) map->addblock(bl);
- #ifdef CELL_NOSTACK
- else map->update_cell_bl(bl, true);
- #endif
- if (bl->type&BL_CHAR) {
- struct map_session_data *sd = BL_CAST(BL_PC, bl);
- skill->unit_move(bl,tick,3);
- if (sd != NULL && sd->shadowform_id != 0) {
- //Shadow Form Target Moving
- struct block_list *d_bl;
- if ((d_bl = map->id2bl(sd->shadowform_id)) == NULL || !check_distance_bl(bl,d_bl,10)) {
- if( d_bl )
- status_change_end(d_bl,SC__SHADOWFORM,INVALID_TIMER);
- sd->shadowform_id = 0;
- }
- }
- if (sc && sc->count) {
- if (sc->data[SC_DANCING])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_DANCING]->val2), bl->m, x1-x0, y1-y0);
- else {
- if (sc->data[SC_CLOAKING])
- skill->check_cloaking(bl, sc->data[SC_CLOAKING]);
- if (sc->data[SC_WARM])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_WARM]->val4), bl->m, x1-x0, y1-y0);
- if (sc->data[SC_BANDING])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_BANDING]->val4), bl->m, x1-x0, y1-y0);
- if (sc->data[SC_NEUTRALBARRIER_MASTER])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_NEUTRALBARRIER_MASTER]->val2), bl->m, x1-x0, y1-y0);
- else if (sc->data[SC_STEALTHFIELD_MASTER])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_STEALTHFIELD_MASTER]->val2), bl->m, x1-x0, y1-y0);
- if( sc->data[SC__SHADOWFORM] ) {//Shadow Form Caster Moving
- struct block_list *d_bl;
- if( (d_bl = map->id2bl(sc->data[SC__SHADOWFORM]->val2)) == NULL || !check_distance_bl(bl,d_bl,10) )
- status_change_end(bl,SC__SHADOWFORM,INVALID_TIMER);
- }
- if (sc->data[SC_PROPERTYWALK]
- && sc->data[SC_PROPERTYWALK]->val3 < skill->get_maxcount(sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2)
- && map->find_skill_unit_oncell(bl,bl->x,bl->y,SO_ELECTRICWALK,NULL,0) == NULL
- && map->find_skill_unit_oncell(bl,bl->x,bl->y,SO_FIREWALK,NULL,0) == NULL
- && skill->unitsetting(bl,sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2,x0, y0,0)
- ) {
- sc->data[SC_PROPERTYWALK]->val3++;
- }
- }
- /* Guild Aura Moving */
- if (sd != NULL && sd->state.gmaster_flag) {
- if (sc->data[SC_LEADERSHIP])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_LEADERSHIP]->val4), bl->m, x1-x0, y1-y0);
- if (sc->data[SC_GLORYWOUNDS])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_GLORYWOUNDS]->val4), bl->m, x1-x0, y1-y0);
- if (sc->data[SC_SOULCOLD])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_SOULCOLD]->val4), bl->m, x1-x0, y1-y0);
- if (sc->data[SC_HAWKEYES])
- skill->unit_move_unit_group(skill->id2group(sc->data[SC_HAWKEYES]->val4), bl->m, x1-x0, y1-y0);
- }
- }
- } else if (bl->type == BL_NPC) {
- npc->setcells(BL_UCAST(BL_NPC, bl));
- }
- return 0;
- }
- /*==========================================
- * Counts specified number of objects on given cell.
- * flag:
- * 0x1 - only count standing units
- * 0x2 - don't count invinsible units
- * TODO: merge with bl_getall_area
- *------------------------------------------*/
- int map_count_oncell(int16 m, int16 x, int16 y, int type, int flag) {
- int bx,by;
- struct block_list *bl;
- int count = 0;
- if (x < 0 || y < 0 || (x >= map->list[m].xs) || (y >= map->list[m].ys))
- return 0;
- bx = x/BLOCK_SIZE;
- by = y/BLOCK_SIZE;
- if (type&~BL_MOB) {
- for (bl = map->list[m].block[bx+by*map->list[m].bxs]; bl != NULL; bl = bl->next) {
- if (bl->x == x && bl->y == y && bl->type&type) {
- if (flag&0x2) {
- struct status_change *sc = status->get_sc(bl);
- if (sc && (sc->option&OPTION_INVISIBLE))
- continue;
- }
- if (flag&0x1) {
- struct unit_data *ud = unit->bl2ud(bl);
- if (ud && ud->walktimer != INVALID_TIMER)
- continue;
- }
- count++;
- }
- }
- }
- if (type&BL_MOB) {
- for (bl = map->list[m].block_mob[bx+by*map->list[m].bxs]; bl != NULL; bl = bl->next) {
- if (bl->x == x && bl->y == y) {
- if (flag&0x2) {
- struct status_change *sc = status->get_sc(bl);
- if (sc && (sc->option&OPTION_INVISIBLE))
- continue;
- }
- if (flag&0x1) {
- struct unit_data *ud = unit->bl2ud(bl);
- if (ud && ud->walktimer != INVALID_TIMER)
- continue;
- }
- count++;
- }
- }
- }
- return count;
- }
- /*
- * Looks for a skill unit on a given cell
- * flag&1: runs battle_check_target check based on unit->group->target_flag
- */
- struct skill_unit* map_find_skill_unit_oncell(struct block_list* target,int16 x,int16 y,uint16 skill_id,struct skill_unit* out_unit, int flag) {
- int16 m,bx,by;
- struct block_list *bl;
- struct skill_unit *su;
- nullpo_retr(NULL, target);
- m = target->m;
- if (x < 0 || y < 0 || (x >= map->list[m].xs) || (y >= map->list[m].ys))
- return NULL;
- bx = x/BLOCK_SIZE;
- by = y/BLOCK_SIZE;
- for( bl = map->list[m].block[bx+by*map->list[m].bxs] ; bl != NULL ; bl = bl->next ) {
- if (bl->x != x || bl->y != y || bl->type != BL_SKILL)
- continue;
- su = BL_UCAST(BL_SKILL, bl);
- if( su == out_unit || !su->alive || !su->group || su->group->skill_id != skill_id )
- continue;
- if( !(flag&1) || battle->check_target(&su->bl,target,su->group->target_flag) > 0 )
- return su;
- }
- return NULL;
- }
- /** @name Functions for block_list search and manipulation
- */
- /* @{ */
- /**
- * Applies func to every block_list in bl_list starting with bl_list[blockcount].
- * Sets bl_list_count back to blockcount.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param blockcount Index of first relevant entry in bl_list
- * @param max Maximum sum of values returned by func (usually max number of func calls)
- * @param args Extra arguments for func
- * @return Sum of the values returned by func
- */
- static int bl_vforeach(int (*func)(struct block_list*, va_list), int blockcount, int max, va_list args) {
- int i;
- int returnCount = 0;
- map->freeblock_lock();
- for (i = blockcount; i < map->bl_list_count && returnCount < max; i++) {
- if (map->bl_list[i]->prev) { //func() may delete this bl_list[] slot, checking for prev ensures it wasn't queued for deletion.
- va_list argscopy;
- va_copy(argscopy, args);
- returnCount += func(map->bl_list[i], argscopy);
- va_end(argscopy);
- }
- }
- map->freeblock_unlock();
- map->bl_list_count = blockcount;
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type on map m.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param m Map id
- * @param type enum bl_type
- * @param args Extra arguments for func
- * @return Sum of the values returned by func
- */
- static int map_vforeachinmap(int (*func)(struct block_list*, va_list), int16 m, int type, va_list args) {
- int i;
- int returnCount = 0;
- int bsize;
- va_list argscopy;
- struct block_list *bl;
- int blockcount = map->bl_list_count;
- if (m < 0)
- return 0;
- bsize = map->list[m].bxs * map->list[m].bys;
- for (i = 0; i < bsize; i++) {
- if (type&~BL_MOB) {
- for (bl = map->list[m].block[i]; bl != NULL; bl = bl->next) {
- if (bl->type&type) {
- if( map->bl_list_count >= map->bl_list_size )
- map_bl_list_expand();
- map->bl_list[map->bl_list_count++] = bl;
- }
- }
- }
- if (type&BL_MOB) {
- for (bl = map->list[m].block_mob[i]; bl != NULL; bl = bl->next) {
- if( map->bl_list_count >= map->bl_list_size )
- map_bl_list_expand();
- map->bl_list[map->bl_list_count++] = bl;
- }
- }
- }
- va_copy(argscopy, args);
- returnCount = bl_vforeach(func, blockcount, INT_MAX, argscopy);
- va_end(argscopy);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type on map m.
- * Returns the sum of values returned by func.
- * @see map_vforeachinmap
- * @param func Function to be applied
- * @param m Map id
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_foreachinmap(int (*func)(struct block_list*, va_list), int16 m, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforeachinmap(func, m, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type on all maps
- * of instance instance_id.
- * Returns the sum of values returned by func.
- * @see map_vforeachinmap.
- * @param func Function to be applied
- * @param m Map id
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforeachininstance(int (*func)(struct block_list*, va_list), int16 instance_id, int type, va_list ap) {
- int i;
- int returnCount = 0;
- for (i = 0; i < instance->list[instance_id].num_map; i++) {
- int m = instance->list[instance_id].map[i];
- va_list apcopy;
- va_copy(apcopy, ap);
- returnCount += map->vforeachinmap(func, m, type, apcopy);
- va_end(apcopy);
- }
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type on all maps
- * of instance instance_id.
- * Returns the sum of values returned by func.
- * @see map_vforeachininstance.
- * @param func Function to be applied
- * @param m Map id
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_foreachininstance(int (*func)(struct block_list*, va_list), int16 instance_id, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforeachininstance(func, instance_id, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Retrieves all map objects in area that are matched by the type
- * and func. Appends them at the end of global bl_list array.
- * @param type Matching enum bl_type
- * @param m Map
- * @param func Matching function
- * @param ... Extra arguments for func
- * @return Number of found objects
- */
- static int bl_getall_area(int type, int m, int x0, int y0, int x1, int y1, int (*func)(struct block_list*, va_list), ...) {
- va_list args;
- int bx, by;
- struct block_list *bl;
- int found = 0;
- if (m < 0)
- return 0;
- if (x1 < x0) swap(x0, x1);
- if (y1 < y0) swap(y0, y1);
- // Limit search area to map size
- x0 = max(x0, 0);
- y0 = max(y0, 0);
- x1 = min(x1, map->list[m].xs - 1);
- y1 = min(y1, map->list[m].ys - 1);
- for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) {
- for (bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++) {
- if (type&~BL_MOB) {
- for (bl = map->list[m].block[bx + by * map->list[m].bxs]; bl != NULL; bl = bl->next) {
- if (bl->type&type && bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1) {
- if( map->bl_list_count >= map->bl_list_size )
- map_bl_list_expand();
- if (func) {
- va_start(args, func);
- if (func(bl, args)) {
- map->bl_list[map->bl_list_count++] = bl;
- found++;
- }
- va_end(args);
- } else {
- map->bl_list[map->bl_list_count++] = bl;
- found++;
- }
- }
- }
- }
- if (type&BL_MOB) { // TODO: fix this code duplication
- for (bl = map->list[m].block_mob[bx + by * map->list[m].bxs]; bl != NULL; bl = bl->next) {
- if (bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1) {
- if( map->bl_list_count >= map->bl_list_size )
- map_bl_list_expand();
- if (func) {
- va_start(args, func);
- if (func(bl, args)) {
- map->bl_list[map->bl_list_count++] = bl;
- found++;
- }
- va_end(args);
- } else {
- map->bl_list[map->bl_list_count++] = bl;
- found++;
- }
- }
- }
- }
- }
- }
- return found;
- }
- /**
- * Checks if bl is within range cells from center.
- * If CIRCULAR AREA is not used always returns 1, since
- * preliminary range selection is already done in bl_getall_area.
- * @return 1 if matches, 0 otherwise
- */
- static int bl_vgetall_inrange(struct block_list *bl, va_list args)
- {
- #ifdef CIRCULAR_AREA
- struct block_list *center = va_arg(args, struct block_list*);
- int range = va_arg(args, int);
- if (!check_distance_bl(center, bl, range))
- return 0;
- #endif
- return 1;
- }
- /**
- * Applies func to every block_list object of bl_type type within range cells from center.
- * Area is rectangular, unless CIRCULAR_AREA is defined.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param center Center of the selection area
- * @param range Range in cells from center
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforeachinrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int type, va_list ap) {
- int returnCount = 0;
- int blockcount = map->bl_list_count;
- va_list apcopy;
- if (range < 0) range *= -1;
- bl_getall_area(type, center->m, center->x - range, center->y - range, center->x + range, center->y + range, bl_vgetall_inrange, center, range);
- va_copy(apcopy, ap);
- returnCount = bl_vforeach(func, blockcount, INT_MAX, apcopy);
- va_end(apcopy);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type within range cells from center.
- * Area is rectangular, unless CIRCULAR_AREA is defined.
- * Returns the sum of values returned by func.
- * @see map_vforeachinrange
- * @param func Function to be applied
- * @param center Center of the selection area
- * @param range Range in cells from center
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_foreachinrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforeachinrange(func, center, range, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Applies func to some block_list objects of bl_type type within range cells from center.
- * Limit is set by count parameter.
- * Area is rectangular, unless CIRCULAR_AREA is defined.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param center Center of the selection area
- * @param range Range in cells from center
- * @param count Maximum sum of values returned by func (usually max number of func calls)
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforcountinrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int count, int type, va_list ap) {
- int returnCount = 0;
- int blockcount = map->bl_list_count;
- va_list apcopy;
- if (range < 0) range *= -1;
- bl_getall_area(type, center->m, center->x - range, center->y - range, center->x + range, center->y + range, bl_vgetall_inrange, center, range);
- va_copy(apcopy, ap);
- returnCount = bl_vforeach(func, blockcount, count, apcopy);
- va_end(apcopy);
- return returnCount;
- }
- /**
- * Applies func to some block_list objects of bl_type type within range cells from center.
- * Limit is set by count parameter.
- * Area is rectangular, unless CIRCULAR_AREA is defined.
- * Returns the sum of values returned by func.
- * @see map_vforcountinrange
- * @param func Function to be applied
- * @param center Center of the selection area
- * @param range Range in cells from center
- * @param count Maximum sum of values returned by func (usually max number of func calls)
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_forcountinrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int count, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforcountinrange(func, center, range, count, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Checks if bl is within shooting range from center.
- * There must be a shootable path between bl and center.
- * Does not check for range if CIRCULAR AREA is not defined, since
- * preliminary range selection is already done in bl_getall_area.
- * @return 1 if matches, 0 otherwise
- */
- static int bl_vgetall_inshootrange(struct block_list *bl, va_list args)
- {
- struct block_list *center = va_arg(args, struct block_list*);
- #ifdef CIRCULAR_AREA
- int range = va_arg(args, int);
- nullpo_ret(center);
- nullpo_ret(bl);
- if (!check_distance_bl(center, bl, range))
- return 0;
- #endif
- if (!path->search_long(NULL, center, center->m, center->x, center->y, bl->x, bl->y, CELL_CHKWALL))
- return 0;
- return 1;
- }
- /**
- * Applies func to every block_list object of bl_type type within shootable range from center.
- * There must be a shootable path between bl and center.
- * Area is rectangular, unless CIRCULAR_AREA is defined.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param center Center of the selection area
- * @param range Range in cells from center
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforeachinshootrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int type, va_list ap) {
- int returnCount = 0;
- int blockcount = map->bl_list_count;
- va_list apcopy;
- if (range < 0) range *= -1;
- bl_getall_area(type, center->m, center->x - range, center->y - range, center->x + range, center->y + range, bl_vgetall_inshootrange, center, range);
- va_copy(apcopy, ap);
- returnCount = bl_vforeach(func, blockcount, INT_MAX, ap);
- va_end(apcopy);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type within shootable range from center.
- * There must be a shootable path between bl and center.
- * Area is rectangular, unless CIRCULAR_AREA is defined.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param center Center of the selection area
- * @param range Range in cells from center
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_foreachinshootrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforeachinshootrange(func, center, range, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type in
- * rectangular area (x0,y0)~(x1,y1) on map m.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param m Map id
- * @param x0 Starting X-coordinate
- * @param y0 Starting Y-coordinate
- * @param x1 Ending X-coordinate
- * @param y1 Ending Y-coordinate
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforeachinarea(int (*func)(struct block_list*, va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int type, va_list ap) {
- int returnCount = 0;
- int blockcount = map->bl_list_count;
- va_list apcopy;
- bl_getall_area(type, m, x0, y0, x1, y1, NULL);
- va_copy(apcopy, ap);
- returnCount = bl_vforeach(func, blockcount, INT_MAX, apcopy);
- va_end(apcopy);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type in
- * rectangular area (x0,y0)~(x1,y1) on map m.
- * Returns the sum of values returned by func.
- * @see map_vforeachinarea
- * @param func Function to be applied
- * @param m Map id
- * @param x0 Starting X-coordinate
- * @param y0 Starting Y-coordinate
- * @param x1 Ending X-coordinate
- * @param y1 Ending Y-coordinate
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_foreachinarea(int (*func)(struct block_list*, va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforeachinarea(func, m, x0, y0, x1, y1, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Applies func to some block_list objects of bl_type type in
- * rectangular area (x0,y0)~(x1,y1) on map m.
- * Limit is set by @count parameter.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param m Map id
- * @param x0 Starting X-coordinate
- * @param y0 Starting Y-coordinate
- * @param x1 Ending X-coordinate
- * @param y1 Ending Y-coordinate
- * @param count Maximum sum of values returned by func (usually max number of func calls)
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforcountinarea(int (*func)(struct block_list*,va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int count, int type, va_list ap) {
- int returnCount = 0;
- int blockcount = map->bl_list_count;
- va_list apcopy;
- bl_getall_area(type, m, x0, y0, x1, y1, NULL);
- va_copy(apcopy, ap);
- returnCount = bl_vforeach(func, blockcount, count, apcopy);
- va_end(apcopy);
- return returnCount;
- }
- /**
- * Applies func to some block_list objects of bl_type type in
- * rectangular area (x0,y0)~(x1,y1) on map m.
- * Limit is set by @count parameter.
- * Returns the sum of values returned by func.
- * @see map_vforcountinarea
- * @param func Function to be applied
- * @param m Map id
- * @param x0 Starting X-coordinate
- * @param y0 Starting Y-coordinate
- * @param x1 Ending X-coordinate
- * @param y1 Ending Y-coordinate
- * @param count Maximum sum of values returned by func (usually max number of func calls)
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_forcountinarea(int (*func)(struct block_list*,va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int count, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforcountinarea(func, m, x0, y0, x1, y1, count, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Checks if bl is inside area that was in range cells from the center
- * before it was moved by (dx,dy) cells, but it is not in range cells
- * from center after movement is completed.
- * In other words, checks if bl is inside area that is no longer covered
- * by center's range.
- * Preliminary range selection is already done in bl_getall_area.
- * @return 1 if matches, 0 otherwise
- */
- static int bl_vgetall_inmovearea(struct block_list *bl, va_list args)
- {
- int dx = va_arg(args, int);
- int dy = va_arg(args, int);
- struct block_list *center = va_arg(args, struct block_list*);
- int range = va_arg(args, int);
- nullpo_ret(bl);
- nullpo_ret(center);
- if ((dx > 0 && bl->x < center->x - range + dx) ||
- (dx < 0 && bl->x > center->x + range + dx) ||
- (dy > 0 && bl->y < center->y - range + dy) ||
- (dy < 0 && bl->y > center->y + range + dy))
- return 1;
- return 0;
- }
- /**
- * Applies func to every block_list object of bl_type type in
- * area that was covered by range cells from center, but is no
- * longer after center is moved by (dx,dy) cells (i.e. area that
- * center has lost sight of).
- * If used after center has reached its destination and with
- * opposed movement vector (-dx,-dy), selection corresponds
- * to new area in center's view).
- * Uses rectangular area.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param center Center of the selection area
- * @param range Range in cells from center
- * @param dx Center's movement on X-axis
- * @param dy Center's movement on Y-axis
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforeachinmovearea(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int16 dx, int16 dy, int type, va_list ap) {
- int returnCount = 0;
- int blockcount = map->bl_list_count;
- int m, x0, x1, y0, y1;
- va_list apcopy;
- if (!range) return 0;
- if (!dx && !dy) return 0; // No movement.
- if (range < 0) range *= -1;
- m = center->m;
- x0 = center->x - range;
- x1 = center->x + range;
- y0 = center->y - range;
- y1 = center->y + range;
- if (dx == 0 || dy == 0) { // Movement along one axis only.
- if (dx == 0) {
- if (dy < 0) { y0 = y1 + dy + 1; } // Moving south
- else { y1 = y0 + dy - 1; } // North
- } else { //dy == 0
- if (dx < 0) { x0 = x1 + dx + 1; } // West
- else { x1 = x0 + dx - 1; } // East
- }
- bl_getall_area(type, m, x0, y0, x1, y1, NULL);
- }
- else { // Diagonal movement
- bl_getall_area(type, m, x0, y0, x1, y1, bl_vgetall_inmovearea, dx, dy, center, range);
- }
- va_copy(apcopy, ap);
- returnCount = bl_vforeach(func, blockcount, INT_MAX, apcopy);
- va_end(apcopy);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type in
- * area that was covered by range cells from center, but is no
- * longer after center is moved by (dx,dy) cells (i.e. area that
- * center has lost sight of).
- * If used after center has reached its destination and with
- * opposed movement vector (-dx,-dy), selection corresponds
- * to new area in center's view).
- * Uses rectangular area.
- * Returns the sum of values returned by func.
- * @see map_vforeachinmovearea
- * @param func Function to be applied
- * @param center Center of the selection area
- * @param range Range in cells from center
- * @param dx Center's movement on X-axis
- * @param dy Center's movement on Y-axis
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_foreachinmovearea(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int16 dx, int16 dy, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforeachinmovearea(func, center, range, dx, dy, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type in
- * cell (x,y) on map m.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param m Map id
- * @param x Target cell X-coordinate
- * @param y Target cell Y-coordinate
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforeachincell(int (*func)(struct block_list*, va_list), int16 m, int16 x, int16 y, int type, va_list ap) {
- int returnCount = 0;
- int blockcount = map->bl_list_count;
- va_list apcopy;
- bl_getall_area(type, m, x, y, x, y, NULL);
- va_copy(apcopy, ap);
- returnCount = bl_vforeach(func, blockcount, INT_MAX, apcopy);
- va_end(apcopy);
- return returnCount;
- }
- /**
- * Applies func to every block_list object of bl_type type in
- * cell (x,y) on map m.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param m Map id
- * @param x Target cell X-coordinate
- * @param y Target cell Y-coordinate
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_foreachincell(int (*func)(struct block_list*, va_list), int16 m, int16 x, int16 y, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforeachincell(func, m, x, y, type, ap);
- va_end(ap);
- return returnCount;
- }
- /**
- * Helper function for map_foreachinpath()
- * Checks if shortest distance from bl to path
- * between (x0,y0) and (x1,y1) is shorter than range.
- * @see map_foreachinpath
- */
- static int bl_vgetall_inpath(struct block_list *bl, va_list args)
- {
- int m = va_arg(args, int);
- int x0 = va_arg(args, int);
- int y0 = va_arg(args, int);
- int x1 = va_arg(args, int);
- int y1 = va_arg(args, int);
- int range = va_arg(args, int);
- int len_limit = va_arg(args, int);
- int magnitude2 = va_arg(args, int);
- int xi;
- int yi;
- int xu, yu;
- int k;
- nullpo_ret(bl);
- xi = bl->x;
- yi = bl->y;
- k = ( xi - x0 ) * ( x1 - x0 ) + ( yi - y0 ) * ( y1 - y0 );
- if ( k < 0 || k > len_limit ) //Since more skills use this, check for ending point as well.
- return 0;
- if ( k > magnitude2 && !path->search_long(NULL, NULL, m, x0, y0, xi, yi, CELL_CHKWALL) )
- return 0; //Targets beyond the initial ending point need the wall check.
- //All these shifts are to increase the precision of the intersection point and distance considering how it's
- //int math.
- k = ( k << 4 ) / magnitude2; //k will be between 1~16 instead of 0~1
- xi <<= 4;
- yi <<= 4;
- xu = ( x0 << 4 ) + k * ( x1 - x0 );
- yu = ( y0 << 4 ) + k * ( y1 - y0 );
- //Avoid needless calculations by not getting the sqrt right away.
- #define MAGNITUDE2(x0, y0, x1, y1) ( ( ( x1 ) - ( x0 ) ) * ( ( x1 ) - ( x0 ) ) + ( ( y1 ) - ( y0 ) ) * ( ( y1 ) - ( y0 ) ) )
- k = MAGNITUDE2(xi, yi, xu, yu);
- //If all dot coordinates were <<4 the square of the magnitude is <<8
- if ( k > range )
- return 0;
- return 1;
- }
- /**
- * Applies func to every block_list object of bl_type type in
- * path on a line between (x0,y0) and (x1,y1) on map m.
- * Path starts at (x0,y0) and is \a length cells long and \a range cells wide.
- * Objects beyond the initial (x1,y1) ending point are checked
- * for walls in the path.
- * Returns the sum of values returned by func.
- * @param func Function to be applied
- * @param m Map id
- * @param x Target cell X-coordinate
- * @param y Target cell Y-coordinate
- * @param type enum bl_type
- * @param ap Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_vforeachinpath(int (*func)(struct block_list*, va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int16 range, int length, int type, va_list ap) {
- // [Skotlex]
- // check for all targets in the square that
- // contains the initial and final positions (area range increased to match the
- // radius given), then for each object to test, calculate the distance to the
- // path and include it if the range fits and the target is in the line (0<k<1,
- // as they call it).
- // The implementation I took as reference is found at
- // http://web.archive.org/web/20050720125314/http://astronomy.swin.edu.au/~pbourke/geometry/pointline/
- // http://paulbourke.net/geometry/pointlineplane/
- // I won't use doubles/floats, but pure int math for
- // speed purposes. The range considered is always the same no matter how
- // close/far the target is because that's how SharpShooting works currently in
- // kRO
- int returnCount = 0;
- int blockcount = map->bl_list_count;
- va_list apcopy;
- //method specific variables
- int magnitude2, len_limit; //The square of the magnitude
- int mx0 = x0, mx1 = x1, my0 = y0, my1 = y1;
- len_limit = magnitude2 = MAGNITUDE2(x0, y0, x1, y1);
- if (magnitude2 < 1) //Same begin and ending point, can't trace path.
- return 0;
- if (length) { //Adjust final position to fit in the given area.
- //TODO: Find an alternate method which does not requires a square root calculation.
- int k = (int)sqrt((float)magnitude2);
- mx1 = x0 + (x1 - x0) * length / k;
- my1 = y0 + (y1 - y0) * length / k;
- len_limit = MAGNITUDE2(x0, y0, mx1, my1);
- }
- //Expand target area to cover range.
- if (mx0 > mx1) {
- mx0 += range;
- mx1 -= range;
- } else {
- mx0 -= range;
- mx1 += range;
- }
- if (my0 > my1) {
- my0 += range;
- my1 -= range;
- } else {
- my0 -= range;
- my1 += range;
- }
- range *= range << 8; //Values are shifted later on for higher precision using int math.
- bl_getall_area(type, m, mx0, my0, mx1, my1, bl_vgetall_inpath, m, x0, y0, x1, y1, range, len_limit, magnitude2);
- va_copy(apcopy, ap);
- returnCount = bl_vforeach(func, blockcount, INT_MAX, apcopy);
- va_end(apcopy);
- return returnCount;
- }
- #undef MAGNITUDE2
- /**
- * Applies func to every block_list object of bl_type type in
- * path on a line between (x0,y0) and (x1,y1) on map m.
- * Path starts at (x0,y0) and is \a length cells long and \a range cells wide.
- * Objects beyond the initial (x1,y1) ending point are checked
- * for walls in the path.
- * Returns the sum of values returned by func.
- * @see map_vforeachinpath
- * @param func Function to be applied
- * @param m Map id
- * @param x Target cell X-coordinate
- * @param y Target cell Y-coordinate
- * @param type enum bl_type
- * @param ... Extra arguments for func
- * @return Sum of the values returned by func
- */
- int map_foreachinpath(int (*func)(struct block_list*, va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int16 range, int length, int type, ...) {
- int returnCount;
- va_list ap;
- va_start(ap, type);
- returnCount = map->vforeachinpath(func, m, x0, y0, x1, y1, range, length, type, ap);
- va_end(ap);
- return returnCount;
- }
- /** @} */
- /// Generates a new flooritem object id from the interval [MIN_FLOORITEM, MAX_FLOORITEM).
- /// Used for floor items, skill units and chatroom objects.
- /// @return The new object id
- int map_get_new_object_id(void)
- {
- static int last_object_id = MIN_FLOORITEM - 1;
- int i;
- // find a free id
- i = last_object_id + 1;
- while( i != last_object_id ) {
- if( i == MAX_FLOORITEM )
- i = MIN_FLOORITEM;
- if( !idb_exists(map->id_db, i) )
- break;
- ++i;
- }
- if( i == last_object_id ) {
- ShowError("map_addobject: no free object id!\n");
- return 0;
- }
- // update cursor
- last_object_id = i;
- return i;
- }
- /*==========================================
- * Timered function to clear the floor (remove remaining item)
- * Called each flooritem_lifetime ms
- *------------------------------------------*/
- int map_clearflooritem_timer(int tid, int64 tick, int id, intptr_t data)
- {
- struct block_list *bl = idb_get(map->id_db, id);
- struct flooritem_data *fitem = BL_CAST(BL_ITEM, bl);
- if (fitem == NULL || fitem->cleartimer != tid) {
- ShowError("map_clearflooritem_timer : error\n");
- return 1;
- }
- if (pet->search_petDB_index(fitem->item_data.nameid, PET_EGG) >= 0)
- intif->delete_petdata(MakeDWord(fitem->item_data.card[1], fitem->item_data.card[2]));
- clif->clearflooritem(fitem, 0);
- map->deliddb(&fitem->bl);
- map->delblock(&fitem->bl);
- map->freeblock(&fitem->bl);
- return 0;
- }
- /*
- * clears a single bl item out of the bazooonga.
- */
- void map_clearflooritem(struct block_list *bl)
- {
- struct flooritem_data *fitem = BL_CAST(BL_ITEM, bl);
- nullpo_retv(fitem);
- nullpo_retv(fitem);
- if( fitem->cleartimer != INVALID_TIMER )
- timer->delete(fitem->cleartimer,map->clearflooritem_timer);
- clif->clearflooritem(fitem, 0);
- map->deliddb(&fitem->bl);
- map->delblock(&fitem->bl);
- map->freeblock(&fitem->bl);
- }
- /*==========================================
- * (m,x,y) locates a random available free cell around the given coordinates
- * to place an BL_ITEM object. Scan area is 9x9, returns 1 on success.
- * x and y are modified with the target cell when successful.
- *------------------------------------------*/
- int map_searchrandfreecell(int16 m, const struct block_list *bl, int16 *x, int16 *y, int stack) {
- int free_cell,i,j;
- int free_cells[9][2];
- nullpo_ret(x);
- nullpo_ret(y);
- for(free_cell=0,i=-1;i<=1;i++){
- if(i+*y<0 || i+*y>=map->list[m].ys)
- continue;
- for(j=-1;j<=1;j++){
- if(j+*x<0 || j+*x>=map->list[m].xs)
- continue;
- if (map->getcell(m, bl, j + *x, i + *y, CELL_CHKNOPASS) && !map->getcell(m, bl, j + *x, i + *y, CELL_CHKICEWALL))
- continue;
- //Avoid item stacking to prevent against exploits. [Skotlex]
- if(stack && map->count_oncell(m,j+*x,i+*y, BL_ITEM, 0) > stack)
- continue;
- free_cells[free_cell][0] = j+*x;
- free_cells[free_cell++][1] = i+*y;
- }
- }
- if(free_cell==0)
- return 0;
- free_cell = rnd()%free_cell;
- *x = free_cells[free_cell][0];
- *y = free_cells[free_cell][1];
- return 1;
- }
- int map_count_sub(struct block_list *bl,va_list ap) {
- return 1;
- }
- /*==========================================
- * Locates a random spare cell around the object given, using range as max
- * distance from that spot. Used for warping functions. Use range < 0 for
- * whole map range.
- * Returns 1 on success. when it fails and src is available, x/y are set to src's
- * src can be null as long as flag&1
- * when ~flag&1, m is not needed.
- * Flag values:
- * &1 = random cell must be around given m,x,y, not around src
- * &2 = the target should be able to walk to the target tile.
- * &4 = there shouldn't be any players around the target tile (use the no_spawn_on_player setting)
- *------------------------------------------*/
- int map_search_freecell(struct block_list *src, int16 m, int16 *x,int16 *y, int16 rx, int16 ry, int flag)
- {
- int tries, spawn=0;
- int bx, by;
- int rx2 = 2*rx+1;
- int ry2 = 2*ry+1;
- nullpo_ret(src);
- nullpo_ret(x);
- nullpo_ret(y);
- if( !src && (!(flag&1) || flag&2) )
- {
- ShowDebug("map_search_freecell: Incorrect usage! When src is NULL, flag has to be &1 and can't have &2\n");
- return 0;
- }
- if (flag&1) {
- bx = *x;
- by = *y;
- } else {
- bx = src->x;
- by = src->y;
- m = src->m;
- }
- if (!rx && !ry) {
- //No range? Return the target cell then....
- *x = bx;
- *y = by;
- return map->getcell(m, src, *x, *y, CELL_CHKREACH);
- }
- if (rx >= 0 && ry >= 0) {
- tries = rx2*ry2;
- if (tries > 100) tries = 100;
- } else {
- tries = map->list[m].xs*map->list[m].ys;
- if (tries > 500) tries = 500;
- }
- while(tries--) {
- *x = (rx >= 0)?(rnd()%rx2-rx+bx):(rnd()%(map->list[m].xs-2)+1);
- *y = (ry >= 0)?(rnd()%ry2-ry+by):(rnd()%(map->list[m].ys-2)+1);
- if (*x == bx && *y == by)
- continue; //Avoid picking the same target tile.
- if (map->getcell(m, src, *x, *y, CELL_CHKREACH)) {
- if(flag&2 && !unit->can_reach_pos(src, *x, *y, 1))
- continue;
- if(flag&4) {
- if (spawn >= 100) return 0; //Limit of retries reached.
- if (spawn++ < battle_config.no_spawn_on_player
- && map->foreachinarea(map->count_sub, m, *x-AREA_SIZE, *y-AREA_SIZE,
- *x+AREA_SIZE, *y+AREA_SIZE, BL_PC)
- )
- continue;
- }
- return 1;
- }
- }
- *x = bx;
- *y = by;
- return 0;
- }
- /*==========================================
- * Locates the closest, walkable cell with no blocks of a certain type on it
- * Returns true on success and sets x and y to cell found.
- * Otherwise returns false and x and y are not changed.
- * type: Types of block to count
- * flag:
- * 0x1 - only count standing units
- *------------------------------------------*/
- bool map_closest_freecell(int16 m, const struct block_list *bl, int16 *x, int16 *y, int type, int flag)
- {
- uint8 dir = 6;
- int16 tx;
- int16 ty;
- int costrange = 10;
- nullpo_ret(x);
- nullpo_ret(y);
- tx = *x;
- ty = *y;
- if(!map->count_oncell(m, tx, ty, type, flag))
- return true; //Current cell is free
- //Algorithm only works up to costrange of 34
- while(costrange <= 34) {
- short dx = dirx[dir];
- short dy = diry[dir];
- //Linear search
- if(dir%2 == 0 && costrange%MOVE_COST == 0) {
- tx = *x+dx*(costrange/MOVE_COST);
- ty = *y+dy*(costrange/MOVE_COST);
- if (!map->count_oncell(m, tx, ty, type, flag) && map->getcell(m, bl, tx, ty, CELL_CHKPASS)) {
- *x = tx;
- *y = ty;
- return true;
- }
- }
- //Full diagonal search
- else if(dir%2 == 1 && costrange%MOVE_DIAGONAL_COST == 0) {
- tx = *x+dx*(costrange/MOVE_DIAGONAL_COST);
- ty = *y+dy*(costrange/MOVE_DIAGONAL_COST);
- if (!map->count_oncell(m, tx, ty, type, flag) && map->getcell(m, bl, tx, ty, CELL_CHKPASS)) {
- *x = tx;
- *y = ty;
- return true;
- }
- }
- //One cell diagonal, rest linear (TODO: Find a better algorithm for this)
- else if(dir%2 == 1 && costrange%MOVE_COST == 4) {
- tx = *x+dx*((dir%4==3)?(costrange/MOVE_COST):1);
- ty = *y+dy*((dir%4==1)?(costrange/MOVE_COST):1);
- if (!map->count_oncell(m, tx, ty, type, flag) && map->getcell(m, bl, tx, ty, CELL_CHKPASS)) {
- *x = tx;
- *y = ty;
- return true;
- }
- tx = *x+dx*((dir%4==1)?(costrange/MOVE_COST):1);
- ty = *y+dy*((dir%4==3)?(costrange/MOVE_COST):1);
- if (!map->count_oncell(m, tx, ty, type, flag) && map->getcell(m, bl, tx, ty, CELL_CHKPASS)) {
- *x = tx;
- *y = ty;
- return true;
- }
- }
- //Get next direction
- if (dir == 5) {
- //Diagonal search complete, repeat with higher cost range
- if(costrange == 14) costrange += 6;
- else if(costrange == 28 || costrange >= 38) costrange += 2;
- else costrange += 4;
- dir = 6;
- } else if (dir == 4) {
- //Linear search complete, switch to diagonal directions
- dir = 7;
- } else {
- dir = (dir+2)%8;
- }
- }
- return false;
- }
- /*==========================================
- * Add an item to location (m,x,y)
- * Parameters
- * @item_data item attributes
- * @amount quantity
- * @m, @x, @y mapid,x,y
- * @first_charid, @second_charid, @third_charid, looting priority
- * @flag: &1 MVP item. &2 do stacking check.
- *------------------------------------------*/
- int map_addflooritem(const struct block_list *bl, struct item *item_data, int amount, int16 m, int16 x, int16 y, int first_charid, int second_charid, int third_charid, int flags)
- {
- int r;
- struct flooritem_data *fitem=NULL;
- nullpo_ret(item_data);
- if (!map->searchrandfreecell(m, bl, &x, &y, (flags&2)?1:0))
- return 0;
- r=rnd();
- fitem = ers_a…
Large files files are truncated, but you can click here to view the full file