ID:145294
 
Code:
mob
Player
icon = 'Player.dmi'
var
range
damage
speed
fired = 0
Carbine
icon_state = "Carbine"
range = 5
damage = 3
speed = 5
Rifle
icon_state = "Rifle"
range = 6
damage = 8
speed = 9
Sniper
icon_state = "Sniper"
range = 8
damage = 15
speed = 25
Bazooka
icon_state = "Bazooka"
range = 3
damage = 6
speed = 50
New()
src.AI()
proc
AI()
if(src.fired == 1)
spawn(5)
src.AI()
world << "1"
return
if(src.fired == 0)
world << "0"
for(var/mob/Enemy/X in range(src,src.range))
src.fired = 1
if(istype(src,/mob/Player/Carbine))
missile(/obj/Bullet/Carbine,src,X)
if(istype(src,/mob/Player/Rifle))
missile(/obj/Bullet/Rifle,src,X)
if(istype(src,/mob/Player/Sniper))
missile(/obj/Bullet/Sniper,src,X)
if(istype(src,/mob/Player/Bazooka))
missile(/obj/Bullet/Bazooka,src,X)
X.health -= src.damage
if(X.health<=0)
del X
spawn(src.speed)
src.fired = 0
spawn(5)
src.AI()


Problem description:
This might look a little newbie, but... a /mob/Player that has the AI() activated, will shoot ALL of the mobs in range at once. I want it so that the /mob/Player can pick only ONE target in the range and shoot at it, instead of shooting ALL enemies in the range. Why does this happen??Anyone has an idea of how to do it? I would appreciate any help. :)
I think this post should be moved to developer how to. I'm sorry about that... I posted on the wrong subject.
I think to make it work a bit more effectivly, you should only allow it to fire in the direction you are facing. Think about it. How many snipers shoot people with their backs to the target
In response to Crzylme
The /mob/Player is like Abra's players in Castle.Thew /mob/Player are just some NPCs, that use çAI Brains to shot at the enemies.

I asked help for it because I'm making a Castle Game.
Gooseheaded wrote:
>                   for(var/mob/Enemy/X in range(src,src.range))
> src.fired = 1
> if(istype(src,/mob/Player/Carbine))
> missile(/obj/Bullet/Carbine,src,X)
> if(istype(src,/mob/Player/Rifle))
> missile(/obj/Bullet/Rifle,src,X)
> if(istype(src,/mob/Player/Sniper))
> missile(/obj/Bullet/Sniper,src,X)
> if(istype(src,/mob/Player/Bazooka))
> missile(/obj/Bullet/Bazooka,src,X)
> X.health -= src.damage
> if(X.health<=0)
> del X
> spawn(src.speed)
> src.fired = 0
> spawn(5)
> src.AI()
>


It is happening because you are looping through -all- the mobs in range(src,src.range). for() loops through an entire list. You probably want to use pick(), which will randomly select an item from a list.
Like this:
var/mob/Enemy/X=pick(range(src,src.range))
In response to Artekia
Well, the /mob/Player won't shoot an enemie, if it's in the range... that happened after I coded it like this:
        proc
AI()
if(src.fired == 1)
spawn(5)
src.AI()
world << "1"
return
if(src.fired == 0)
world << "0"
var/mob/Enemy/X = pick(range(src,src.range))
src.fired = 1
if(istype(src,/mob/Player/Carbine))
missile(/obj/Bullet/Carbine,src,X)
if(istype(src,/mob/Player/Rifle))
missile(/obj/Bullet/Rifle,src,X)
if(istype(src,/mob/Player/Sniper))
missile(/obj/Bullet/Sniper,src,X)
if(istype(src,/mob/Player/Bazooka))
missile(/obj/Bullet/Bazooka,src,X)
X.health -= src.damage
if(X.health<=0)
del X
spawn(src.speed)
src.fired = 0
spawn(5)
src.AI()


What should I do? =|
In response to Gooseheaded
You should probably not constantly call the proc, but keep it running with the background setting turned on. while() would be great here. Also, you should check for true and false values by if(var) and if(!var) because it is more robust.

And for your question about the pick(range()) thing not working, you could just use locate. It always works.
var/mob/X=locate(/mob/Enemy/)in range(src,src.range)


[My beleifs and experiences, could be wrong, please correct me]
I don't thing you can pick() from a list like that, but a buttload of arguments, rather. And, pick() may not even return a mob in the way it's being utilized. I don't even know if you can utilize it in such a way to filter it to only give you a mob.
I found a way of doing it by myself,m since noone helped me. I added a "break" udner the "for".

        proc
AI()
if(src.fired == 1)
spawn(5)
src.AI()
world << "1"
return
if(src.fired == 0)
world << "0"
for(var/mob/Enemy/X in range(src,src.range))
src.fired = 1
if(istype(src,/mob/Player/Carbine))
missile(/obj/Bullet/Carbine,src,X)
if(istype(src,/mob/Player/Rifle))
missile(/obj/Bullet/Rifle,src,X)
if(istype(src,/mob/Player/Sniper))
missile(/obj/Bullet/Sniper,src,X)
if(istype(src,/mob/Player/Bazooka))
missile(/obj/Bullet/Bazooka,src,X)
X.health -= src.damage
if(X.health<=0)
del X
break // HAHA!!
spawn(src.speed)
src.fired = 0
spawn(5)
src.AI()


Thanks for anyone that tried to help me.. :)

#Gooseheaded#
In response to Gooseheaded
Why not use
var/mob/Enemy/X = locate(/mob/Enemy/) in range(src,src.range)


rather than a loop that you're always stopping on the first time?

[hmm, it appears someone said this already]