Prev: 9C42 Up: Map Next: 9C8C
9C55: Add up the contents of an object
Recursive worker for 9C42 and 9C3D: for each object held by object A, adds its weight (or size, if B=1) to C, and for weights also the weight of its contents. The overflow test uses JP PE.
9C55 PUSH IX Save the caller's IX
9C57 LD IX,$BF53 IX = the start of the object index, ready to look at every object in the game in turn
9C5B CALL $9B04 Look at the next object: IY = its own record
9C5E JR Z,$9C84 Have we run out of objects (reached the index's terminator)?
9C60 CP (IY+$01) No: is THIS object held by the one whose contents we are adding up?
9C63 JR NZ,$9C5B It is not: move on and look at the next object
9C65 PUSH AF It is: save the object we are adding up for a moment (it is not disturbed by anything above)
9C66 SUB A Are we totalling weight (B=0) or size (B=1)?
9C67 CP B
9C68 LD A,C A = the running total so far
9C69 JR Z,$9C74 Weight wanted: add this object's own weight instead (skip down)
9C6B ADD A,(IY+$02) Size wanted: add this object's own size (byte 2 of its record)
9C6E JP PE,$9C87 Did that go past 255? Then the whole total is unknowable - give up
9C71 LD C,A Otherwise keep the new, larger running total
9C72 JR $9C81 ...and go on to look inside this object too, in case it holds anything itself
9C74 ADD A,(IY+$03) Weight wanted: add this object's own weight (byte 3 of its record)
9C77 JP PE,$9C87 Did that go past 255? Then the whole total is unknowable - give up
9C7A LD C,A Otherwise keep the new, larger running total
9C7B LD A,(IX+$00) A = the object we just added in, so we can look inside it as well
9C7E CALL $9C55 Recursively add up whatever THAT object holds, in case it is itself a container
9C81 POP AF Restore the object we are totalling up (saved earlier)
9C82 JR $9C5B Go back and look for more objects held by it
9C84 POP IX No more objects anywhere: restore the caller's IX
9C86 RET
9C87 POP AF OVERFLOW: throw away the object number we had saved
9C88 LD C,$FF The total becomes $FF, meaning "too big to say exactly"
9C8A JR $9C84 Finish as normal
Prev: 9C42 Up: Map Next: 9C8C