ID:138451
 
I really am afraid of getting back into coding... I hate having to learn new things. See what happens when you take a vacation? I do have an odd question. Look at the following... what I'm trying to do here is calculate the weight of a bag and its contents, no matter how many items are nested inside of other items in the bag.

proc/Weigh(item)
var/totalweight = item.weight
var/obj/O
for (O as obj in item) totalweight += Weigh(O)
return totalweight

Would that work? Besides any errors I might have, I was curious to know... if a proc calls itself, and then returns, does it return to itself, or to the original proc that called it the first time?

Z
On 8/6/00 11:41 am Zilal wrote:
I really am afraid of getting back into coding... I hate having to learn new things. See what happens when you take a vacation?

I know the feeling! But you need only plod onward as long as it is at least somewhat enjoyable. When there are thousands of inhabitants occupying the land of Cerule, I think you'll be happy!

[Off topic, but just recently Dan and I were bantering about the mud market and some things that could be added to make the language more mud-friendly. Your text parsing system came up and we started discussing some related items that could be automated by the program, such as case-insensitivity, the ability to give adjectives to objects to be used in the text expansion, including context-sensitive ones (like "get the top sword"), and a few other things. Anyway, since you and Gabriel have basically been the inspiration for this model, we may need to use you as consultants in the future!]

I do have an odd question. Look at the following... what I'm trying to do here is calculate the weight of a bag and its contents, no matter how many items are nested inside of other items in the bag.

proc/Weigh(item)
var/totalweight = item.weight
var/obj/O
for (O as obj in item) totalweight += Weigh(O)
return totalweight

Would that work?

It looks perfect to me. And this is about the only way I can think of to do it compactly, so I would definitely go with it.

Besides any errors I might have, I was curious to know... if a proc calls itself, and then returns, does it return to itself, or to the original proc that called it the first time?

It always returns to its last immediate caller. Incidentally, a proc that calls itself is known as a "recursive" proc. Recursion was one of the most difficult things for me to figure out, but now I use it all the time. I've found that the easiest way to undertand it is to think of the action that the interpreter is doing at runtime. In your example, we might have:

weigh box
box's base weight = 5 lb
now let's add the contents of the box
weigh bag
bag's base weight = 1 lb
now let's add the contents of the bag
weigh orange
orange's base weight = 0.2 lb
no contents to add
total weight of bag now = 1 + 0.2 = 1.2 lb
weigh apple
apple's base weight = 0.3 lb
no contents to add
total weight of bag now = 1.2 + 0.3 = 1.5 lb
no more contents in bag
total weight of bag = 1.5 lb
total weight of box now = 5 + 1.5 = 6.5 lb
no more contents in box
total weight of box = 6.5 lb

The point here is that each "weigh" call is a separate entity, returning to whomever called it. The server basically processes each instruction line by line, so it's not even aware about how the different calls may be inter-related through recursion.

I hope this helps. Based on your proc though, it sounds like you understand this pretty well already. Good luck!
In response to Tom H.
On 8/6/00 1:38 pm Tom H. wrote:
[Off topic, but just recently Dan and I were bantering
about the mud market and some things that could be added
to make the language more mud-friendly. Your text
parsing system came up and we started discussing some
related items that could be automated by the program,
such as case-insensitivity, the ability to give
adjectives to objects to be used in the text expansion,
including context-sensitive ones (like "get the top
sword"), and a few other things. Anyway, since you and
Gabriel have basically been the inspiration for this
model, we may need to use you as consultants in the
future!]

Whenever you're ready, I'm game!

Basically what I do for weight is adjust the total weight for the container whenever an item is added or removed. Thus if I add an orange to a bag, the weight of the orange gets added to the total weight of the bag. If I put the bag in a box, the total weight of the box increased by the total weight of the bag, etc.

As a safty control, some checks will run if something specific happens (like storing a backpack full of stuff in a vault). The check will run through all the items and verify that the correct weight is being calculated.

The one thing I'm not sure of though is if I have:
obj/box/sack/orange
obj/box/sack/pouch
obj/box/sack/pouch/gem

For the weight of obj/box do I just need to run a check for(obj in box) or do I need to break it down further (that is, does it automatically recognize that the gem inside the pouch inside the box is also contained in the box?)

- -
In response to Tom H.
On 8/6/00 1:38 pm Tom H. wrote:
Anyway, since you and Gabriel have basically been the inspiration for this model, we may need to use you as consultants in the future!

Ooh, I love to feel important.

Z