ID:177880
 
I'm still looking fo a code to keep monster on one kind of turf or keep them within certain boundaries. oh yeah and how do I make it so when you kill a monster you get money for it?
Codesterz wrote:
I'm still looking fo a code to keep monster on one kind of turf or keep them within certain boundaries.

Just FYI, "a code" doesn't make any sense. When you're referring to programming, "code" is plural.

There are a few ways to do what you want. One is to assign the turf to a particular area, and use Exit() to check if the monster can leave:
area/monsterzone
Exit(atom/movable/A)
if(ismob(A))
var/mob/M=A
if(!M.client) // assume all non-client mobs are monsters
return 0 // stuck here, sorry
return 1 // go ahead and exit

Another way is to alter the Move() proc for monsters:
mob/monster
Move(atom/newloc)
if(!newloc) return ..() // Move(null)
if(!loc) return ..() // Move from null
if(newloc.type!=loc.type)
return 0 // don't move to any other kind of turf
return ..()

You can also get fancier than that. Say you have a monster constricted to a certain area, say within 5 spaces of var/atom/home:
mob/monster
var/atom/home
var/mrange=5 // stay within 5 tiles of home atom

Move(atom/newloc)
if(!newloc) return ..()
if(!loc || !home || home.z!=newloc.z || get_dist(loc,home)>mrange)
.=..()
if(.) home=newloc
return
if(get_dist(newloc,home)<=mrange) return ..()
return 0 // can't move there

Notice the ifs in there. The first one allows the monster to move to null without a problem. The next check will reset the home atom (i.e., "leash" the monster to a new spot) to wherever they step next, IF:

  • the monster is being moved from null (or just created)
  • the monster doesn't already have a home turf
  • home turf is on another level
  • home turf is out of range (so movement would be impossible)

    The home turf is only set if the movement is completed, which is the reason for calling ..() first and checking its return value.
    If home turf doesn't move, then the monster checks to see if its new location is in range. If it is, then movement proceeds without a hitch. If it isn't, it returns 0 to indicate movement failed.

    Lummox JR
In response to Lummox JR
thanks I tested it and it works!
In response to Codesterz
Codesterz wrote:
thanks I tested it and it works!

Cool. If you elaborate on that code further in the future, I'd love to see what else you come up with. The options for limiting or directing monster movement are quite varied, and you might find that some of the choices you make may even add to the apparent intelligence of the monsters. Once you begin setting down rules and patterns for how a creature can move around, they begin to seem a lot more alive.

Lummox JR