I have a deathCheck proc that relocates the enemy mob at random coordinates from 1 to 15 for both x and y position. For some reason, when the deathCheck goes off, it tends to give the /exact/ same coordinates, twice, three times, even four times in a row. I don't see any issues with my code and I think this needs to be looked into. I am using BYOND version 511.1380
Code Snippet (if applicable) to Reproduce Problem:
mob
var/hp
var/maxhp
var/strength
proc
deathCheck(var/mob/M)
world << "Enemy HP: [M.hp]"
if(M.hp <= 0)
M.hp = M.maxhp
M.Move(locate(rand(1,15), rand(1,15), 1))
world << "Enemy respawned at: [M.x], [M.y], [M.z]"
Player
icon = 'Mob.dmi'
icon_state = "Player"
hp = 10
maxhp = 10
strength = 2
verb
Attack()
for(var/mob/Enemy/e in enemyList)
if(src.dir == NORTH)
if(e.loc == locate(src.x, src.y + 1, src.z))
e.hp -= src.strength
src.deathCheck(e)
else if(src.dir == SOUTH)
if(e.loc == locate(src.x, src.y - 1, src.z))
e.hp -= src.strength
src.deathCheck(e)
else if(src.dir == WEST)
if(e.loc == locate(src.x - 1,src.y, src.z))
e.hp -= src.strength
src.deathCheck(e)
else if(src.dir == EAST)
if(e.loc == locate(src.x + 1, src.y, src.z))
e.hp -= src.strength
src.deathCheck(e)
Expected Results:
Different x and y coordinates from M.Move()
Actual Results:
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 10, 6, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 3, 9, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 3, 9, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 15, 3, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 15, 3, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 2, 9, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 5, 3, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 13, 1, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 15, 1, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 15, 1, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 7, 10, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 7, 10, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 7, 10, 1
Enemy HP: 3
Enemy HP: 1
Enemy HP: -1
Enemy respawned at: 7, 3, 1
Does the problem occur:
Occurs during any non-hosted game session.
Workarounds:
None so far
You can alter this behavior by using rand_seed() to initialize the generator with your own seed to add a bit more randomness (but you'll need to change the seed each time, or the results will always be the same)
Your issue is that you're not accounting for failures in Move(), which is going to result in you seeing inconsistancies when movement is unsuccessful. The only time rand() tends to fail this way is when it's being called within the span of a couple of seconds, which I doubt you're doing here.
[Edit]
It should also be noted that you should probably be using world.maxx and world.maxy in rand() and not a static number, or you're gonna have to adjust the code anytime you change the size of your map.