How to cycle through all items, or locations, or etc.

Paragon

Member
I tried to use the code

foreach l in $location
print $location[l];

But it tells me that i need an aggregate expression. I was wondering if anyone knew how to a loop of that sort using the variables such as $item, $location, $element etc.
 

macman104

Member
the foreach statement needs a map. $location is not a map. It is a variable like...int or string. So your statement is like saying

Code:
foreach i in int
print int[i]
If you wanted to print all of the possible integers. So...yea, you can't do that.

EDIT: Added the code tags since I was getting italics...
 

degrassi

New member
This is the only (easy) way I can think to do it. It'd only really be decent if you wanted to loop through the items for multiple checks of one sort or another. It creates a map with all the items in it that you can then foreach through.

It's pretty quick, but still probably not the most efficient use of resources. It may help you, or some other clever person, come up with a better idea.

Code:
// setup
	item [int] item_list;
	for i from 1 upto 4000
	{
		item it = to_item(i);
		if (it != $item[none])
			item_list[i]=to_item(i);
	}
	
// then general usage...
	foreach thing in item_list
	{
		print (thing + " : " +item_list[thing]);
	}
 

Camber

Member
If you know the range of numbers you want to list, you can do it on-the-fly. Here is what i use to list out all the familiar's i have:

Code:
For x from 1 upto 80 by 1 {
	if(have_familiar(to_familiar(x))) print(to_familiar(x)) ;
}
 

macman104

Member
These all work, but for things like the locations, I'm not sure what to tell the OP. But things like skills, familiars, items, etc, all have numbers associated with them that ASH has access to.
 

degrassi

New member
This seems to work to list all locations. Again, I'm sure there's a better way, but this generated a list for me.

Code:
string [string,string] loc;
file_to_map("adventures.txt",loc);

	foreach x in loc
	{
		foreach y in loc[x]
		{
// loc[x,y] == name of location, so you could to_location( loc[x,y] ) if you wanted to.
			print (loc[x,y]);
		}
	}

(note that adventures.txt is an internal data file)

[edit: fix typo in code from cut/paste error]
 

MagiNinjA

New member
Hmm, I was wondering. Should location to_location ( int ) be a valid call? holatuwol? I don't recall any oddities with location numbers.
 

zarqon

Well-known member
Sorry to dredge up an old thread, but I would still love to see a solution to this. I just tried using to_location() with an int parameter and did not get the expected results. A quick check of ashref showed it only accepts a string. This makes generating a list of known locations rather cumbersome.

I would love it to death if this would be able to print a list of all known adventurable locations (where the url is adventure=XX):

PHP:
for i = 1 to 200 {
  if (to_location(i) == $location[none]) continue;
  print(i+": "+to_location(i));
}
 

jasonharper

Developer
Adventure locations are not necessarily associated with an integer; the sewer, Daily Dungeon, Basement, Barrels, Hidden City, the various gyms, and probably others I'm not thinking of at the moment, would be inaccessible by to_location(int). What exactly do you need this for?
 

zarqon

Well-known member
I recently wrote a script intended to quickly list stat-appropriate locations to adventure. But it's not so quick. I got the location list the long way (suggested by degrassi above), but it takes about 20 seconds to load all locations and parse the monsters for safemox. Totally different example: a friend of mine is writing a bot that learns, adventuring in random locations until it begins to gather feedback and then adventuring more intelligently. (He's kind of a nerd like that -- we'll see how far he gets, since he's used to programming in more powerful languages.)

For anything that wants to 1) batch process / filter locations, or 2) select a location at random, to_location(int) would come in very handy. I looked through adventures.txt and the only locations that don't have an adventure=XX url are noncombat locations (with the exception of a couple boss fights and the hedge maze, which have very limited availability, and which would probably be scripted specifically rather than in batch).

Presently, choosing a random combat location is involved and requires a disproportionately large amount of system resources. With to_location(int), it's ridiculously easy:

Code:
int i;
repeat 
  i = random(200);
until (to_location(i) != $location[none])

I think this added functionality and ease-of-use outweighs not having every location present, especially considering that

1) Every unlimited combat location would be included, and
2) The excluded zones would in common use be scripted specifically (by name), rather than included in batch processing.
 

jasonharper

Developer
In revision 6595
to_location(int) became alive
Pass it a random number, go adv() there
If your Moxie's low you might just swear

with apologies to Zager & Evans
 

zarqon

Well-known member
In AD 2008,
to_location(int) was beginning.
What happen!
Somebody coded; he the bomb.

further apologies to Toaplan
 
Top