ID:158991
 
I want to spawn my enemies by making the player step on an area, if the player hits a variable, then enemys will spawn around him.
this is a two part question..

ok first of all here is the first piece of unfinished coding..

mob/var
spawnchance=0
targeter=0
area/spawnisland
Entered(atom/movable/O)
..()
if(ismob(O))
var/mob/M = O
if(M.targeter==0)
M.spawnchance=rand(1,9)
if(M.spawnchance==3)
if(M.level<=30)
//Here I want to spawn monsters around the player


whats the code to use for making the enemys appear or what should I use rather?

next part:
when the enemy is spawned, I want it to focus on killing the player who spawned it.
so how would I do this,
I created a variable called "targeter"
so...

heres my current AI code:
mob
Heartless
name = "Shadow"
icon = 'HartlessMob.dmi'
class = "Heartless"
Mhealth=300
density = 1
HL=1
level=1
New()
src.move()
Bump(mob/M)
if(ismob(M))
if(M.client)
Fight(M)
else
return
else
..()
proc/Fight(mob/M)
var/damage = rand(5,10)
M.Mhealth -= damage
s_damage(M,damage,"#FFFFFF")
M.death()


proc
move()
for(var/mob/M in range(10))
if(M.client)
step_towards(src,M)
spawn(15)
src.move()


ok, so obviously I need to make the AI a bit smarter..
which I'll actually do.
but for now I just want to switch it up to where it only chases the player who spawned it.
and then if the player is out of range, I want it to delete itself..

anyone care to lend a hand?

what i think that would work best is when is says

-------if(M.level <= 30)---------------
you should put somthing like this....
     if(M.level <= 30)
var/mob/A = new/mob/Heartless
A.loc = M.loc
step_rand(A) // so it will move at any random place around the person
//an if you wanted to add in more than one Heartless that spawn'd on that area you would jsut do this.
var/mob/B = new/mob/Heartless
B.loc = M.loc
step_rand(B) // and so on and so forth


And just repeat the var/mob/(Change Letter Here!) part for howmany heartless you want to appear becide them but remember to change the A to some other letter or else you'll end up with a runtime error

By the way instead of this part of your spawning code

                  M.spawnchance=rand(1,9)
if(M.spawnchance==3)

it would be much easyier by duing the prob() in their such as this

                  if(prob(INSERT A PERCENTAGE OUT OF 100 HERE)) // this percentage represents how often the monster will spawn on this spot everytime it is steped on for example the prob(80), 80% of the time it is steped on, a monster will appear there
AND CARRY ON THE REST OF THE CODE HERE!!!



Edit: i just typed in a long explination on how you could do the second halve of your question, but i accidentally pressed Tab and Backspace so i'm gonna have to type it all again D:<


How you could do your second part of what i'm thinking what you have to do is to create a var into the Heartless which tells them who their Creator is ex. usr.key, which would look somthing like this

     Heartless
var/Creator = "None"

Now you have to add in a part where it tells them who the creator is and you should put this in the same spot where you made it create the random heartless,

     if(M.level <= 30)
var/mob/A = new/mob/Heartless
A.loc = M.loc
A.Creator = "[M.key]"
step_rand(A)

now what you have to do last is make the proc where the heartless checks the person before it attacks, for example (i have to get off the comp soon so i'm not gonna do the coding for this again...) their are two people playing your game, Example1, and Example2 (that is their byond keys) Example1 watches as Example2 Creates a series ammount of heartless around him, the heartless see both mobs, and they are confused on which one to attack. What you want is for the Heartless to attack their creator, so add a part in to the move() proc you already have in which the heartless have to check the var/Creator first before they pick which one to attack, do you understand so far? and if Example2 flee's from the area, they would go and attack Example1 because they would be just randomly walking around and doing nothing.


P.S. I haven't tested the code above out so dont complain if it doesn't work, i tried to specify what I mean the best i could.

Oh ya and I sent you an Email about aplication for your game as a pixel artist, I just forgot to add in that my byond key is Xxdragonslxx.
In response to Xxdragonslxx
Thank you Dragon,
I actually fixed the code myself when looking at some of my summoning code.
and I remembered I could multiply it for the same affect!

haha.

But you're code also works, and it seems to take much less space, I'll prob switch over to using it.

and I'll take a look at your application.
=]
Just some advice on efficiency. You can probably slim this:
      if(ismob(O))
var/mob/M = O
if(M.targeter==0)
M.spawnchance=rand(1,9)
if(M.spawnchance==3)
if(M.level<=30)

down to this:
var/mob/M = O
if(istype(M) && !M.targeter && rand(1,9)==3 && M.level <= 30)


We first typecast O as a mob, M. This doesn't actually change what O is, so when we use istype(M), it will evaluate FALSE if it's not a mob. If() will evaluate false when istype() evaluates false. !M.targeter is equivalent in this instance to M.targeter==0.

I did take the liberty of bypassing the mob's spawnchance variable as it seemed superfluous here. If it's needed for evaluation outside of this section, simply remove the && rand(1,9) from the if statement and then assign and check the variable as before, under the first if check.

Note that this is just advice, your code should work fine (i.e. as intended) as is :)


EDIT: Dragon had a good point about using prob, which fits in nicely:
var/mob/M = O
if(istype(M) && !M.targeter && prob(11) && M.level <= 30)


I used prob(11) as your check is essentially a 1 in 9 chance of something happpening (1/9 = .111~), or about 11%.
In response to CriticalBotch
Is prob() really that much more efficient than rand()?

EDIT
...or is it just the fact that he was using the rand() proc in a probability scenario, and therefore using it incorrectly?
In response to Spunky_Girl
Spunky_Girl wrote:
Is prob() really that much more efficient than rand()?
Eh, six on one, half-dozen the other unless I'm mistaken.
There's not much performance game, if any, when using prob over rand. The key is that prob is much more natural to use. For many developers, it's easier to look at an if statement that says if(prob(10)) and know that it'll trigger 10% of the time than it is to look at if(rand(1,10)).

Also, prob() has the benefit of being a procedure with a boolean return value. Either yes, or no.


So, performance-wise I doubt it's any more efficient. However, from a readability and "elegance" standpoint, prob() is much tidier.

Spunky_Girl wrote:
EDIT
...or is it just the fact that he was using the rand() proc in a probability scenario, and therefore using it incorrectly?

Not at all. rand() is definitely a probability function so and perfectly usable in his original and my first example. As stated above, prob() is just tidier.
In response to CriticalBotch
Not at all. rand() is definitely a probability function so and perfectly usable in his original and my first example. As stated above, prob() is just tidier.

Well I said what I said because I use rand() for it's actual return value, meaning nothing probability related in the end. Take damage for example. In most (if not all) games where you deal damage to an opponent, you have a range of damage you deal (eg. 4-6 damage). That's the type of thing that I use rand() for. Otherwise I use prob().
In response to Spunky_Girl
Spunky_Girl wrote:
Not at all. rand() is definitely a probability function so and perfectly usable in his original and my first example. As stated above, prob() is just tidier.

Well I said what I said because I use rand() for it's actual return value, meaning nothing probability related in the end. Take damage for example. In most (if not all) games where you deal damage to an opponent, you have a range of damage you deal (eg. 4-6 damage). That's the type of thing that I use rand() for. Otherwise I use prob().

rand() generates numbers to allow you to make your own probability assessments. prob() does it's own probability assessment and returns TRUE or FALSE. Both are probability functions. However, rand() has more general utility because it returns values within a range larger than (0,1).

In the end, there's nothing wrong with using rand() in place of prob() for pure chance determination. Are there more elegant, clean ways of doing so? Sure. But that doesn't invalidate it's use.
In response to CriticalBotch
I guess what I meant to say was that I use rand() for its range of number returns when given two arguments, where prob() only has one argument and returns either true or false. I use rand() for... randoms (go figure). And I use prob() for... probability (again go figure). I'm saying I don't use rand() for anything probability related that the prob() proc can take care of for me.

Jeez, this is hard for me to explain for some reason >.<

var/dmg = rand(1,10) //good use of rand() in my opinion

//bad use of rand() in my opinion
var/r = rand(1,10)
if(r == 3) src<<"You win the lottery!"

//better for prob() :D
if(prob(10))
src<<"You win the lattery!"
In response to Spunky_Girl
Honestly, does it really matter.

if(rand(1,10)==3) world<<"k"
if(prob(10)) world<<"k"


Use whichever springs to mind first, in this example it really does not matter. Argueing in this context is pointless.
In response to Spunky_Girl
Spunky_Girl wrote:
I guess what I meant to say [...]

I wouldn't think he didn't understand that. That's still a matter of probability all the same, and he's seen that. f.ex., there's a probability of 10% for the damage to be 3. In other words, using randomization is essentially just implementing probability. One isn't really different from the other. rand() and prob() are essentially the same thing in the basic sense.
In response to Mysame
Mysame wrote:
Honestly, does it really matter.

Yes, provided you care about your code's readability and form. As CriticalBotch already pointed out, using prob() for these cases tends to be much more readable and elegant.

Argueing in this context is pointless.

Then why are you? ;P (If you haven't noticed, you're essentially arguing that it doesn't matter.)
In response to Kaioken
I'm arguing that it doesn't matter, I'm not arguing on the matter that doesn't matter :>