[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Swarm-Support] working with (indexed) lists: beginner's Q
From: |
Jurgen van der Pol |
Subject: |
[Swarm-Support] working with (indexed) lists: beginner's Q |
Date: |
Sun, 2 May 2004 22:16:24 +0200 |
Hi Swarmists,
A beginners question on working with lists. The user/refbooks only get
me this far and I'm not quite succeeding so would appreciate some
hints.
Building on simpleobserverbug2 (BIG thanks, whomever...) , I have a 2d
world (GUI, display, EZGraph etc, all is working, I so proud!) in which
bugs represent trucks: if empty go to a depot, if at depot, pick up
cargo and deliver it. What I want the model to do is for a bug, when
arriving at the depot, look on a (for all bugs to 'see') list of cargo
and get one (later: bid on), based on a number of conditions. The bug
needs to 'look' at the cargo objects to get one that matches certain
creiteria (e.g. value, weigth etc) and, once chosen, get the cargo's
destination from the cargo object & get paid a value (also instilled in
the cargo object when delivering it. Hope intentions are somewhat
clear.
I have observerswarm.h/m, modelswarm.h/m, bug.h/m, cargo.h/m etc.
My modelswarm.m - buildObjects builds me a number of cargo objects, and
the bug objects. Both are put on a list:
//--8<-----
cargoList = [List create: self];
for (y = 0; y < 1; y++) // just one to begin with, this will of
course be different once it 'works'
{
aCargo = [Cargo createBegin: self];
[aCargo setWorld: world];
aCargo = [aCargo createEnd];
[aCargo setX: 20 Y: 20 V: 100]; // again for test purposes hard
data here, later different. V = value
cargoID = cargoID + 1;
[aCargo setID: cargoID];
[cargoList addLast: aCargo]; // the cargo list
}
//--8<-----
I also make an index of cargoList here:
//--8<-----
cargoIndex = [cargoList begin: [self getZone]];
//--8<-----
This all seems to go OK.
The bugs are also familiar:
//--8<-----
bugList = [List create: self];
for (y = 0; y < numberOfTrucks; y++)
{
aBug = [Bug createBegin: self];
[aBug setWorld: world Food: food];
aBug = [aBug createEnd];
[aBug setX: [uniformIntRand getIntegerWithMin: 0 withMax:
worldXSize]
Y: [uniformIntRand getIntegerWithMin: 0 withMax:
worldYSize]];
bugID = bugID + 1;
[aBug setID: bugID];
[aBug getMyCargoList: cargoIndex]; // *** here I (think I)'m
passing the cargoIndex to an (id) variable in the bug
[bugList addLast: aBug];
}
//--8<-----
Also seems to work. Stuff then goes on to display the bug on-screen,
etc. The cargo is not displayed in any way.
Then - buildActions does (a.o)
//--8<-----
modelActions = [ActionGroup create: self];
[modelActions createActionForEach: bugList message: M(goAhead)];
//--8<-----
The goAhead method sits in bug.m and is basically an or/or:
//--8<-----
- goAhead
{
if (haveEaten == 0) // while not loaded - switch
[self getCargo]; // get to the depot
else
[self deliverCargo]; // deliver the cargo at the destination
return self;
}
//--8<-----
- getCargo sets the destination to the coordinates of the depot and
makes it go there
- deliverCargo is where the delivery action is.
I'm not quite sure how to go on next. I thing I know how to pass either
the cargoList or the cargoIndex to the individual bugs.
What I have trouble with is getting access to the cargo lists (and
particularly to and individual cargo object's variables) from within
the bug. In bug.m:
//--8<-----
- deliverCargo
{
id temp;
temp = [myCargoList next]; //*** can this work? Is this correct?
What have I just done?
if (deliveryPointSet == 0)
{
[self setDestinationX: [temp getXDest] Y: [temp getYDest]]; //***
ditto? is temp a cargo instance or not??? deliveryPointSet = 1;
}
//--8<-----
where - getXDest and - getYDest live in cargo.h/m and simply return the
X, Y values of the cargo instance:
//--8<-----
- getXDest
{
return xPos;
}
- getYDest
{
return yPos;
}
//--8<-----
Now, can this work in any way? I know that the end result code will be
different, but I need to get the basics going first. Am I on the right
track? Should I pass the cargoList or the cargoIndex to the bug? In
both cases, how then to access the elements contained therein, and the
variables they hold? Apparently my ObjC/Swarm knowledge is still not
advanced enough to get this down quickly. I do know I'm close, though.
Any (off-list if this is too trivial) pointers are greatly appreciated!
Cheers, TIA,
Jurgen.
- [Swarm-Support] working with (indexed) lists: beginner's Q,
Jurgen van der Pol <=