From 03149635727f16b45e99fd5f2f3647d6c4cb749f Mon Sep 17 00:00:00 2001 From: MadCamel Date: Wed, 16 Sep 2009 07:49:36 -0400 Subject: Found and replaced more calls to rand() --- src/map/itemdb.c | 4 ++-- src/map/magic-expr.c | 17 ++++++++--------- src/map/mob.c | 2 +- src/map/script.c | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/map/itemdb.c b/src/map/itemdb.c index d9cb429..3b49b50 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -111,8 +111,8 @@ int itemdb_searchrandomid(int flags) if(count > 0) { for(i=0;i<1000;i++) { - index = rand()%count; - if( rand()%1000000 < list[index].per) { + index = MRAND(count); + if( MRAND(1000000) < list[index].per) { nameid = list[index].nameid; break; } diff --git a/src/map/magic-expr.c b/src/map/magic-expr.c index c136a17..9d3c751 100644 --- a/src/map/magic-expr.c +++ b/src/map/magic-expr.c @@ -607,8 +607,6 @@ fun_location(env_t *env, int args_nr, val_t *result, val_t *args) return 0; } -/* Recall that glibc's rand() isnt' too bad in the lower bits */ - static int fun_random(env_t *env, int args_nr, val_t *result, val_t *args) { @@ -619,7 +617,8 @@ fun_random(env_t *env, int args_nr, val_t *result, val_t *args) RESULTINT = 0; return 0; } - RESULTINT = rand() % delta; + RESULTINT = MRAND(delta); + if (ARGINT(0) < 0) RESULTINT = -RESULTINT; return 0; @@ -629,9 +628,9 @@ static int fun_random_dir(env_t *env, int args_nr, val_t *result, val_t *args) { if (ARGINT(0)) - RESULTDIR = rand() & 0x7; + RESULTDIR = mt_random() & 0x7; else - RESULTDIR = (rand() & 0x3) * 2; + RESULTDIR = (mt_random() & 0x3) * 2; return 0; } @@ -850,7 +849,7 @@ magic_random_location(location_t *dest, area_t *area) { switch (area->ty) { case AREA_UNION: { - int rv = rand() % area->size; + int rv = MRAND(area->size); if (rv < area->a.a_union[0]->size) magic_random_location(dest, area->a.a_union[0]); else @@ -870,14 +869,14 @@ magic_random_location(location_t *dest, area_t *area) if (h <= 1) h = 1; - x += rand() % w; - y += rand() % h; + x += MRAND(w); + y += MRAND(h); if (!map_is_solid(m, x, y)) { int start_x = x; int start_y = y; int i; - int initial_dir = rand() & 0x7; + int initial_dir = mt_random() & 0x7; int dir = initial_dir; /* try all directions, up to a distance to 10, for a free slot */ diff --git a/src/map/mob.c b/src/map/mob.c index f04ff28..cd74caa 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -1651,7 +1651,7 @@ static int mob_randomwalk(struct mob_data *md,int tick) int i,x,y,c,d=12-md->move_fail_count; if(d<5) d=5; for(i=0;ibl.x+r%(d*2+1)-d; y=md->bl.y+r/(d*2+1)%(d*2+1)-d; if((c=map_getcell(md->bl.m,x,y))!=1 && c!=5 && mob_walktoxy(md,x,y,1)==0){ diff --git a/src/map/script.c b/src/map/script.c index 40fcd62..8d9abe8 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -4147,7 +4147,7 @@ int buildin_sc_start2(struct script_state *st) bl = map_id2bl(st->rid); if(bl->type == BL_PC && ((struct map_session_data *)bl)->state.potionpitcher_flag) bl = map_id2bl(((struct map_session_data *)bl)->skilltarget); - if(rand()%10000 < per) + if(MRAND(10000) < per) skill_status_change_start(bl,type,val1,0,0,0,tick,0); return 0; } -- cgit v1.2.3-60-g2f50 From b396c9ae5d7fab3e13cff70e2a8ebc080aeeb472 Mon Sep 17 00:00:00 2001 From: MadCamel Date: Wed, 16 Sep 2009 10:26:38 -0400 Subject: Modified AI so mob stop walking to their target's last location when the target is gone This should help stop pileups at doors and make battle a bit more interesting --- src/map/mob.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/map/mob.c b/src/map/mob.c index cd74caa..c2a2955 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -699,10 +699,10 @@ static int mob_walk(struct mob_data *md,unsigned int tick,int data) } /*========================================== - * Attack processing of mob + * Check if mob should be attempting to attack *------------------------------------------ */ -static int mob_attack(struct mob_data *md,unsigned int tick,int data) +static int mob_check_attack(struct mob_data *md) { struct block_list *tbl=NULL; struct map_session_data *tsd=NULL; @@ -716,7 +716,7 @@ static int mob_attack(struct mob_data *md,unsigned int tick,int data) md->state.state=MS_IDLE; md->state.skillstate=MSS_IDLE; - if( md->skilltimer!=-1 ) // スキル使用中 + if( md->skilltimer!=-1 ) return 0; if(md->opt1>0 || md->option&2) @@ -742,11 +742,11 @@ static int mob_attack(struct mob_data *md,unsigned int tick,int data) return 0; if(tsd){ - if( pc_isdead(tsd) || tsd->invincible_timer != -1 || pc_isinvisible(tsd) || md->bl.m != tbl->m || tbl->prev == NULL || distance(md->bl.x,md->bl.y,tbl->x,tbl->y)>=13 ){ - md->target_id=0; - md->state.targettype = NONE_ATTACKABLE; - return 0; - } + if( pc_isdead(tsd) || tsd->invincible_timer != -1 || pc_isinvisible(tsd) || md->bl.m != tbl->m || tbl->prev == NULL || distance(md->bl.x,md->bl.y,tbl->x,tbl->y)>=13 ) { + md->target_id=0; + md->state.targettype = NONE_ATTACKABLE; + return 0; + } } if(tmd){ if(md->bl.m != tbl->m || tbl->prev == NULL || distance(md->bl.x,md->bl.y,tbl->x,tbl->y)>=13){ @@ -758,7 +758,7 @@ static int mob_attack(struct mob_data *md,unsigned int tick,int data) if(!md->mode) - mode=mob_db[md->class].mode; + mode=mob_db[md->class].mode; else mode=md->mode; @@ -780,6 +780,26 @@ static int mob_attack(struct mob_data *md,unsigned int tick,int data) range++; if(distance(md->bl.x,md->bl.y,tbl->x,tbl->y) > range) return 0; + + return 1; +} + +/*========================================== + * Attack processing of mob + *------------------------------------------ + */ +static int mob_attack(struct mob_data *md,unsigned int tick,int data) +{ + struct block_list *tbl=NULL; + + nullpo_retr(0, md); + + if((tbl=map_id2bl(md->target_id))==NULL) + return 0; + + if (!mob_check_attack(md)) + return 0; + if(battle_config.monster_attack_direction_change) md->dir=map_calc_dir(&md->bl, tbl->x,tbl->y ); // 向き設定 @@ -918,6 +938,7 @@ static int mob_timer(int tid,unsigned int tick,int id,int data) map_freeblock_lock(); switch(md->state.state){ case MS_WALK: + mob_check_attack(md); mob_walk(md,tick,data); break; case MS_ATTACK: -- cgit v1.2.3-60-g2f50 From bf517943387f364ad7b11854d803a84612f8cf60 Mon Sep 17 00:00:00 2001 From: MadCamel Date: Wed, 30 Sep 2009 13:42:54 -0400 Subject: Added -fno-strict-aliasing to CFLAGS This will likely fix a few minor weirdnesses, and compiler warnings. --- Makefile | 4 ++-- src/common/mt_rand.c | 9 +++++++++ src/common/mt_rand.h | 1 + src/common/utils.h | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/Makefile b/Makefile index 08e54f4..40abc74 100644 --- a/Makefile +++ b/Makefile @@ -18,10 +18,10 @@ OPT = -g -O2 ${M32} ifeq ($(findstring CYGWIN,$(PLATFORM)), CYGWIN) OS_TYPE = -DCYGWIN -CFLAGS = $(OPT) -Wall -DFD_SETSIZE=4096 -I../common $(PACKETDEF) $(OS_TYPE) +CFLAGS = $(OPT) -Wall -fno-strict-aliasing -DFD_SETSIZE=4096 -I../common $(PACKETDEF) $(OS_TYPE) else OS_TYPE = -CFLAGS = $(OPT) -Wall -Wno-pointer-sign -I../common $(PACKETDEF) $(OS_TYPE) +CFLAGS = $(OPT) -Wall -fno-strict-aliasing -Wno-pointer-sign -I../common $(PACKETDEF) $(OS_TYPE) endif MKDEF = CC="$(CC)" CFLAGS="$(CFLAGS)" diff --git a/src/common/mt_rand.c b/src/common/mt_rand.c index ab733ae..fc9a9ec 100644 --- a/src/common/mt_rand.c +++ b/src/common/mt_rand.c @@ -108,3 +108,12 @@ unsigned long mt_random(void) y ^= (y << 15) & 0xEFC60000U; return(y ^ (y >> 18)); } + +int mt_rand(void) { + unsigned long r = mt_random(); + while (r >> 16) + r = (r & 0xFFFF) + (r >> 16); + + return(r); +} + diff --git a/src/common/mt_rand.h b/src/common/mt_rand.h index 07f6ef0..bda5861 100644 --- a/src/common/mt_rand.h +++ b/src/common/mt_rand.h @@ -4,5 +4,6 @@ void mt_seed(unsigned long seed); unsigned long mt_reload(void); unsigned long mt_random(void); +int mt_rand(void); #endif /* __mt_rand_h */ diff --git a/src/common/utils.h b/src/common/utils.h index 0b006c3..3493808 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -38,5 +38,5 @@ * MRAND(10), returns 0-9. * MPRAND(5,10) returns 5-14. */ -#define MRAND(mod) (int) (mt_random() % mod) +#define MRAND(mod) (mt_rand() % mod) #define MPRAND(add, mod) add + MRAND(mod) -- cgit v1.2.3-60-g2f50 From 765c84f370747e35902757b6771368009ae2a8f6 Mon Sep 17 00:00:00 2001 From: MadCamel Date: Wed, 30 Sep 2009 13:42:54 -0400 Subject: Added -fno-strict-aliasing to CFLAGS This will likely fix a few minor weirdnesses, and compiler warnings. --- Makefile | 4 ++-- src/common/mt_rand.c | 9 +++++++++ src/common/mt_rand.h | 1 + src/common/utils.h | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/Makefile b/Makefile index 08e54f4..40abc74 100644 --- a/Makefile +++ b/Makefile @@ -18,10 +18,10 @@ OPT = -g -O2 ${M32} ifeq ($(findstring CYGWIN,$(PLATFORM)), CYGWIN) OS_TYPE = -DCYGWIN -CFLAGS = $(OPT) -Wall -DFD_SETSIZE=4096 -I../common $(PACKETDEF) $(OS_TYPE) +CFLAGS = $(OPT) -Wall -fno-strict-aliasing -DFD_SETSIZE=4096 -I../common $(PACKETDEF) $(OS_TYPE) else OS_TYPE = -CFLAGS = $(OPT) -Wall -Wno-pointer-sign -I../common $(PACKETDEF) $(OS_TYPE) +CFLAGS = $(OPT) -Wall -fno-strict-aliasing -Wno-pointer-sign -I../common $(PACKETDEF) $(OS_TYPE) endif MKDEF = CC="$(CC)" CFLAGS="$(CFLAGS)" diff --git a/src/common/mt_rand.c b/src/common/mt_rand.c index ab733ae..fc9a9ec 100644 --- a/src/common/mt_rand.c +++ b/src/common/mt_rand.c @@ -108,3 +108,12 @@ unsigned long mt_random(void) y ^= (y << 15) & 0xEFC60000U; return(y ^ (y >> 18)); } + +int mt_rand(void) { + unsigned long r = mt_random(); + while (r >> 16) + r = (r & 0xFFFF) + (r >> 16); + + return(r); +} + diff --git a/src/common/mt_rand.h b/src/common/mt_rand.h index 07f6ef0..bda5861 100644 --- a/src/common/mt_rand.h +++ b/src/common/mt_rand.h @@ -4,5 +4,6 @@ void mt_seed(unsigned long seed); unsigned long mt_reload(void); unsigned long mt_random(void); +int mt_rand(void); #endif /* __mt_rand_h */ diff --git a/src/common/utils.h b/src/common/utils.h index 0b006c3..3493808 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -38,5 +38,5 @@ * MRAND(10), returns 0-9. * MPRAND(5,10) returns 5-14. */ -#define MRAND(mod) (int) (mt_random() % mod) +#define MRAND(mod) (mt_rand() % mod) #define MPRAND(add, mod) add + MRAND(mod) -- cgit v1.2.3-60-g2f50 From cf1920e296365c8323fd52b6d927766d296b59c6 Mon Sep 17 00:00:00 2001 From: MadCamel Date: Wed, 30 Sep 2009 13:50:50 -0400 Subject: Revert "Added -fno-strict-aliasing to CFLAGS This will likely fix a few minor weirdnesses, and compiler warnings." This reverts commit 765c84f370747e35902757b6771368009ae2a8f6. --- Makefile | 4 ++-- src/common/mt_rand.c | 9 --------- src/common/mt_rand.h | 1 - src/common/utils.h | 2 +- 4 files changed, 3 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/Makefile b/Makefile index 40abc74..08e54f4 100644 --- a/Makefile +++ b/Makefile @@ -18,10 +18,10 @@ OPT = -g -O2 ${M32} ifeq ($(findstring CYGWIN,$(PLATFORM)), CYGWIN) OS_TYPE = -DCYGWIN -CFLAGS = $(OPT) -Wall -fno-strict-aliasing -DFD_SETSIZE=4096 -I../common $(PACKETDEF) $(OS_TYPE) +CFLAGS = $(OPT) -Wall -DFD_SETSIZE=4096 -I../common $(PACKETDEF) $(OS_TYPE) else OS_TYPE = -CFLAGS = $(OPT) -Wall -fno-strict-aliasing -Wno-pointer-sign -I../common $(PACKETDEF) $(OS_TYPE) +CFLAGS = $(OPT) -Wall -Wno-pointer-sign -I../common $(PACKETDEF) $(OS_TYPE) endif MKDEF = CC="$(CC)" CFLAGS="$(CFLAGS)" diff --git a/src/common/mt_rand.c b/src/common/mt_rand.c index fc9a9ec..ab733ae 100644 --- a/src/common/mt_rand.c +++ b/src/common/mt_rand.c @@ -108,12 +108,3 @@ unsigned long mt_random(void) y ^= (y << 15) & 0xEFC60000U; return(y ^ (y >> 18)); } - -int mt_rand(void) { - unsigned long r = mt_random(); - while (r >> 16) - r = (r & 0xFFFF) + (r >> 16); - - return(r); -} - diff --git a/src/common/mt_rand.h b/src/common/mt_rand.h index bda5861..07f6ef0 100644 --- a/src/common/mt_rand.h +++ b/src/common/mt_rand.h @@ -4,6 +4,5 @@ void mt_seed(unsigned long seed); unsigned long mt_reload(void); unsigned long mt_random(void); -int mt_rand(void); #endif /* __mt_rand_h */ diff --git a/src/common/utils.h b/src/common/utils.h index 3493808..0b006c3 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -38,5 +38,5 @@ * MRAND(10), returns 0-9. * MPRAND(5,10) returns 5-14. */ -#define MRAND(mod) (mt_rand() % mod) +#define MRAND(mod) (int) (mt_random() % mod) #define MPRAND(add, mod) add + MRAND(mod) -- cgit v1.2.3-60-g2f50