ID:170292
 
Ive been stuck on this question for a while now, so I hope someone can answer it. How can I code it so that, for instance, if a mob enters a turf such as a spike trap that occasionally shoots out spikes from a hole in the ground, that if the spikes shoot up while the mob is standing on it, the mob takes damage, rather than just having the spikes move up and do nothing.
Thanx in advance, Rein

if(mob/M in src)?
Reinhartstar wrote:
Ive been stuck on this question for a while now, so I hope someone can answer it. How can I code it so that, for instance, if a mob enters a turf such as a spike trap that occasionally shoots out spikes from a hole in the ground, that if the spikes shoot up while the mob is standing on it, the mob takes damage, rather than just having the spikes move up and do nothing.
Thanx in advance, Rein

You would do something like this. Note that in the following code, it is assumed that the icon, Spike Trap.dmi, has four icon_states: "spikes up", "spikes down", "spikes upward", "spikes downward". The latter two states would be animations, while the former two would be still: "spikes upward" would be an animation that transitions from "spikes down" to "spikes up", while "spikes downward" would be the opposite.

turf/Spike_Trap
icon='Spike Trap.dmi'
icon_state="spikes down"

//How much damage to hit the mob with
var/power=5

proc
Spikes_Up()
flick("spikes upward",src)
src.icon_state="spikes up"
for(var/mob/M in src.contents)
M.takeDamage(src.power)

Spikes_Down()
flick("spikes downward",src)
src.icon_state="spikes down"


Of course, you would have to define takeDamage() yourself.
In response to Wizkidd0123
Wizkidd0123 wrote:The Code
> turf/Spike_Trap
> icon='Spike Trap.dmi'
> icon_state="spikes down"
>
> //How much damage to hit the mob with
> var/power=5
>
> proc
> Spikes_Up()
if(!up)
> flick("spikes upward",src)
> src.icon_state="spikes up"
> for(var/mob/M in src.contents)
> M.takeDamage(src.power)
>
> Spikes_Down()
if(up)
> flick("spikes downward",src)
> src.icon_state="spikes down"
>

Building on what wiz wrote
now you can create the trap/thingy
turf/Spike_Trap
var/up=FALSE
Spikes_Up()
..()
Spikes_Down()
..()
proc
Activate()
while(src&&src.loc)//make sure its on an area
up!=up
up?Spikes_Down():Spikes_Up()
sleep 10//put the delay here
New()
.=..()
Activate()

Please tell me if you find any errors or if I can do it more efficiently.

AI-('Artificial Intelligence')Nothings real.lifs ust one good game. :)
In response to Angel_Inc
Angel_Inc wrote:
Please tell me if you find any errors or if I can do it more efficiently.

Well, frankly, you probably shouldn't be using a boolean variable to check if the spikes are up or down: you can just check the current icon_state. Also, instead of sleep(10) each time, which, by the way, is far too short: you should either have a global loop that opens and shuts each spike at the same time as needed, and if you don't want that, then let each spike have its own turf/Spike_Trap/var/speed, and then sleep(speed).

Of course, it would be alot more efficient and less processor-intensive, if you're using the latter method, to "stagger" the delay. That basically means that instead of having each Spike_Trap with the same speed toggle between open and closed at the same time, you would, perhaps instead do something like, sleep(speed+rand(((speed/4)*-1),(speed/4))). That way, each loop isn't being run at the exact same time. All in all, if you don't need each Spike_Trap to have its own speed, then it would be much better to just run a global loop through everything, toggling the Spike_Traps as needed.

An alternate method would be to have a global loop run through the world, and, using world.time, check if enough time has elapsed to open or close the spike. In other words, use a trap timer. This may be the best method, because that way, you maintain the speed of a global, as opposed to trap-specific, loop, while still allowing each Spike_Trap to have a speed:

turf/Spike_Trap
icon='Spike Trap.dmi'
icon_state="spikes down"

var
power=5 //How much damage to hit the mob with
speed=300 //By default, they'll toggle every 30 seconds.
toggle_timer=0

New()
..()
toggle_timer=speed+world.time

proc
toggleLevel()
if(src.icon_state=="spikes up")
src.spikesDown()
else
src.spikesUp()

spikesUp()
flick("spikes upward",src)
src.icon_state="spikes up"
for(var/mob/M in src.contents)
M.takeDamage(src.power)

spikesDown()
flick("spikes downward",src)
src.icon_state="spikes down"

proc/toggle_SpikeTrap()
for(var/turf/Spike_Trap/trap in world)
if(trap.toggle_timer < world.time)
trap.toggle_timer=trap.speed+world.time
trap.toggleLevel()

world/New()
..()
toggle_SpikeTrap()