Heh, while trying to figure out the
other way the CalculateHolding conditional could be expressed, I realized that, ok, the original suggested like
was correct. I just had copied it wrong to Roodylib, so Roodylib had this:
Code: Select all
if i is not worn or i is not clothing and obj ~= player
when it should have been:
Code: Select all
if i is not worn or i is not clothing or obj ~= player
Having you lay the groundwork first, I took my own swing at the problem. In doing so, I especially like how your method provides an easy way to provide special messages for unstowable objects.
Code: Select all
global hold_object
! just a constant to make it easier to have unlimited-space
! holdall objects
constant INFINITE -1
!\ I'm using after routines to set my global variable, but it's
a really unrefined way to do so.\!
class holdall
{
is container
after
{
object DoGet, DoTakeOff
{
hold_object = self
return false
}
object DoDrop, DoPutIn
{
hold_object = 0
return false
}
}
holding 0
capacity INFINITE
}
replace Acquire(newparent, newchild)
{
local p
CalculateHolding(newparent)
if (newparent.holding + newchild.size > newparent.capacity) and
newparent.capacity ~= INFINITE
{
if hold_object and newparent = player and parent(hold_object) = player
{
if not CheckHoldAll(newchild)
return false
}
else
return false
}
p = parent(newchild)
move newchild to newparent
CalculateHolding(p)
newchild is moved
newchild is not hidden
newparent.holding = newparent.holding + newchild.size
return true
}
routine CheckHoldAll(newchild)
{
local obj
if (hold_object is openable and hold_object is not open) or
FindObject(hold_object, location) ~= 1
return false
while (player.capacity < (player.holding + newchild.size))
{
if obj = youngest(player)
{
return false
}
obj = PickObject
if obj ~= hold_object
{
if Acquire(hold_object, obj)
print "(moving "; The(obj);" to "; The(hold_object);" to make room)"
}
}
return true
}
routine PickObject
{
local obj
obj = child(player)
while true
{
if (obj ~= hold_object and not PickRules(obj)) or
obj = youngest(player)
return obj
else
obj = younger(obj)
}
}
!\ This is a routine to have it ignore certain items that you
don't want being stowed away. \!
routine PickRules
{
! example: key objects are ignored
! if obj.type = key
! return true
}
So, my version takes the oldest child of the player that isn't the holdall object (or something disallowed by PickRules) and puts it in the holdall object.
It'd be cooler if I set it up so the method-of-picking could be more interchangeable, and even though I put infinite-space support in there, it'd be nice if with limited-space holdalls, I added everything the player is holding first just to make sure there
will be room for the new item before I start moving stuff.
Looking at yours again, that's cool that it goes by biggest-items-first. Also, your version can technically handle multiple bags of holding, which might be very appealing to some others. I figured most games have one holdall, and I'd try to capitalize on that fact but the resulting code wasn't quite as simplified as I was hoping for.
Heh, while trying to figure out the [i]other[/i] way the CalculateHolding conditional could be expressed, I realized that, ok, the original suggested like [i]was[/i] correct. I just had copied it wrong to Roodylib, so Roodylib had this:
[code]if i is not worn or i is not clothing and obj ~= player[/code]
when it should have been:
[code]if i is not worn or i is not clothing or obj ~= player[/code]
Having you lay the groundwork first, I took my own swing at the problem. In doing so, I especially like how your method provides an easy way to provide special messages for unstowable objects.
[code]global hold_object
! just a constant to make it easier to have unlimited-space
! holdall objects
constant INFINITE -1
!\ I'm using after routines to set my global variable, but it's
a really unrefined way to do so.\!
class holdall
{
is container
after
{
object DoGet, DoTakeOff
{
hold_object = self
return false
}
object DoDrop, DoPutIn
{
hold_object = 0
return false
}
}
holding 0
capacity INFINITE
}
replace Acquire(newparent, newchild)
{
local p
CalculateHolding(newparent)
if (newparent.holding + newchild.size > newparent.capacity) and
newparent.capacity ~= INFINITE
{
if hold_object and newparent = player and parent(hold_object) = player
{
if not CheckHoldAll(newchild)
return false
}
else
return false
}
p = parent(newchild)
move newchild to newparent
CalculateHolding(p)
newchild is moved
newchild is not hidden
newparent.holding = newparent.holding + newchild.size
return true
}
routine CheckHoldAll(newchild)
{
local obj
if (hold_object is openable and hold_object is not open) or
FindObject(hold_object, location) ~= 1
return false
while (player.capacity < (player.holding + newchild.size))
{
if obj = youngest(player)
{
return false
}
obj = PickObject
if obj ~= hold_object
{
if Acquire(hold_object, obj)
print "(moving "; The(obj);" to "; The(hold_object);" to make room)"
}
}
return true
}
routine PickObject
{
local obj
obj = child(player)
while true
{
if (obj ~= hold_object and not PickRules(obj)) or
obj = youngest(player)
return obj
else
obj = younger(obj)
}
}
!\ This is a routine to have it ignore certain items that you
don't want being stowed away. \!
routine PickRules
{
! example: key objects are ignored
! if obj.type = key
! return true
}[/code]
So, my version takes the oldest child of the player that isn't the holdall object (or something disallowed by PickRules) and puts it in the holdall object.
It'd be cooler if I set it up so the method-of-picking could be more interchangeable, and even though I put infinite-space support in there, it'd be nice if with limited-space holdalls, I added everything the player is holding first just to make sure there [i]will[/i] be room for the new item before I start moving stuff.
Looking at yours again, that's cool that it goes by biggest-items-first. Also, your version can technically handle multiple bags of holding, which might be very appealing to some others. I figured most games have one holdall, and I'd try to capitalize on that fact but the resulting code wasn't quite as simplified as I was hoping for.