ID:179417
 
I am having a conceptual problem with this verb, which should be drawing a straight line of pinetrees from the players' mob to the endpoint. The end point is set using a separate verb, which works just fine.

According to my copy of Bresenhams formula, the place to set the pixel is right where i create my tree with new. However, I dont get a tree. Everything compiles just fine, and nothing seems to puke when I run it.

If someone could give me some advice on modifications, I will do my best to help someone in return.

Here is my linedraw verb:
note that xEnd and yEnd are mob vars, and already defined.

LineDraw()
var
xStart
yStart
xDiff
yDiff
OldX
OldY
X_unit
Y_unit
ErrorTerm
Length
i
xStart = usr.x
yStart = usr.y
OldX = xStart
OldY = yStart
yDiff = abs((yEnd - yStart))
if (yDiff > 0)
Y_unit = -1
else
Y_unit = 1
xDiff = abs((xEnd - xStart))
if (xDiff > 0)
X_unit = -1
else
X_unit = 1

ErrorTerm = 0
if (xDiff > yDiff)
Length = xDiff+1
for (i=0, i<Length, i++)
var/obj/PineTree/PineTree = new(src)
PineTree.x = OldX
PineTree.y = OldY
PineTree.z = src.z
PineTree.x += X_unit
OldX = PineTree.x
ErrorTerm += yDiff
if (ErrorTerm > xDiff)
ErrorTerm -= xDiff
PineTree.y += Y_unit
OldY = PineTree.y
else
Length = yDiff+1
for (i=0, i<Length, i++)
var/obj/PineTree/PineTree = new(src)
PineTree.y = OldY
PineTree.x = OldX
PineTree.z = src.z
PineTree.y += Y_unit
OldY = PineTree.y
ErrorTerm += xDiff
if (ErrorTerm > yDiff)
ErrorTerm -= yDiff
PineTree.x += X_unit
OldX = PineTree.x
its like the motor runs fine, but the transmission is missing a gear!
ThreeFingerPete wrote:
[snip]
LineDraw()
var
[snip]
xDiff
yDiff
[snip]
yDiff = abs((yEnd - yStart))
if (yDiff > 0)
Y_unit = -1
else
Y_unit = 1
xDiff = abs((xEnd - xStart))
if (xDiff > 0)
X_unit = -1
else
X_unit = 1
.
.
.

I'm not sure I know what the problem is, but xDiff might not be initializing.

yDiff will always be >= 0 because it is initialized with an absolute value. xDiff might not be initialized because it is indented in such a way that it will only be initialized if yDiff <= 0.

Knowing this requires knowing what yEnd is so abs(yEnd - yStart) can be known. If yEnd != yStart, xDiff will be null.
Other than ACWraith's mention of XDiff (that line is indented too far), I have one other thought: You're trying to set PineTree.x and PineTree.y directly, but these are read-only variables. What you need to do is set PineTree.loc=locate(OldX,OldY,z). Or better yet, pass this locate() in the new() function when you create the pine tree, which otherwise will be created within src.contents, wherever src is.

Lummox JR
In response to Lummox JR
x, y, and z are not read-only variables for movable objects. However, they are messy and don't bother to call some triggers if not called from within something like locate().

If style is a concern, I agree with Lummox JR. I'd more likely get the coordinates, use them to get the proper location, and use the location in PineTree's new call.
In response to ACWraith
Thanks to both of you.
In response to ACWraith
If they aren't read only, something was changed, because they're supposed to be.
In response to Spuzzum
Spuzzum wrote:
If they aren't read only, something was changed, because they're supposed to be.

They were read only last time I checked.
In response to Ebonshadow
Ebonshadow wrote:
Spuzzum wrote:
If they aren't read only, something was changed, because they're supposed to be.

They were read only last time I checked.

I tested it with a mob and with an obj when I mentioned it. It's possible that I generalized too much and only assumed the parent class could do it. However, I believe editable coordinates are one of the defining properties of movable (at least in my little fantasy).

Non-movable atoms have the coordinates read-only. I've seen that much in posts (but I've been too lazy to test it).
In response to Spuzzum
Spuzzum wrote:
If they aren't read only, something was changed, because they're supposed to be.

I believe it was changed.
In response to ACWraith
ACWraith wrote:
I tested it with a mob and with an obj when I mentioned it. It's possible that I generalized too much and only assumed the parent class could do it. However, I believe editable coordinates are one of the defining properties of movable (at least in my little fantasy).

Non-movable atoms have the coordinates read-only. I've seen that much in posts (but I've been too lazy to test it).

I think the main difference is an editable loc, but the x, y, and z vars should still be read-only.

Lummox JR
In response to Deadron
Ah, so it was... I don't remember reading about that in any release notes. =P