ID:273054
 
I'm a noob developer and I'm making my first game. In this game there's a verb for cutting down tree's that will change the tree's icon state to that of a fallen tree move that fallen tree one space away from the player/mob and will create a stump icon where the tree used to be. I've got a variable fell to keep track if the tree has fallen so the player can't cut the tree again. However, I have encountered a few problems with my code. Here's my code see if you can spot the problem(s).
 
obj/tree/verb
cut()
set src in view(1)
var/obj/tree/T = locate() in get_step(usr,usr.dir)
if(!Fell)
if(T)
var/turf/G = get_step(src,usr.dir)
if (!G.density)
Move(G)
T.icon = 'fallen.dmi'
new/obj/stump(get_step(usr,usr.dir))
Fell = 1
else
return

There are two problems. The easier one to solve is that this code doesn't detect if there is an object with density in the next space. It detects if the turf has density but not if any objects in the turf has density(i.e. if there is a tree in the next space)
The other problem will be harder to solve (I think). You see I want the player to cut the tree that he is facing (no diagonals). However if there are two trees right next to eachother(look at the "picture" below") and a player is facing one of the trees and the other tree is diagonal to the player, the code will behave funny. It will usually treat the tree that is diagonal to the player as the src. So in the game the tree diagonal to the player will move but the tree next to the player will change into the fallen icon. I tried to avoid this by putting "if(src == T)" into the code but instead of solving the problem it will just do nothing! Please help!

(Picture)
t
pt (p is player location and the two t's are the tree locations)
Donut_eater wrote:
It detects if the turf has density but not if any objects in the turf has density(i.e. if there is a tree in the next space)

Loop through the atom/movables on the turf and see if they are dense, and if they are, FAIL.
for(var/atom/movable/A in G)
if(A.density) return



As for the latter problem, you have a few options.

One is to change the verb to your mob instead of having it on your tree. Then you can do /obj/tree in get_step(usr,usr.dir)

Another is to check in the verb to see if the player is in a diagonal.
It seems like you are making things much more complicated than they need to be.

obj/tree/verb/cut()
set src in view(1)
//first, find if the player is facing a tree
var/obj/tree/T = locate() in get_step(usr,usr.dir)
//if there is no tree, don't do anything
if(!T) return
//if the tree is at a different location, just have the player cut that one
if(T.loc != src.loc)
T.cut()
return

//if we're here, then we know that the player is facing src
if(!Fell)
//try to move into the next turf, and if the move succeeds, cut src down
if(step(src, usr.dir))
src.icon = 'fallen.dmi'
//stump blah blah blah
In response to Garthor
Wow, I feel kinda dumb. Well I guess that's a part of learning. THanks for the help.