Converting location to ID number

Fluxxdog

Active member
Code:
int street_num(location which_house){ //Find the ID number for the location...
	for x from 1 to 1000{ //...since to_int($location[]) always returns 0
		if(to_location(x)==which_house) return x;}}
This is a function I created to return a location's ID number. What started out as a simple to_int($location[]) turned up a research venture into to_int() which reveals that monsters and locations always return 0.

Monsters I get.

What I don't get is why the ability to convert a location to its ID number wasn't implemented. Anyone have any insight on that?
 
Not all values of $location[] have sensible ids. Only adventure.php locations really have any sort of id to them.
 
Indeed. In addition, does that code pass verification? I was under the impression that ASH (like most languages) had to have a return value. In the event of a location that has no numeric association, that function won't return anything.
 
Indeed it won't. It would need to be more like:
Code:
int street_num(location which_house)
{
	for x from 1 to 1000
	{
		if(to_location(x)==which_house) return x;
	}
	return -1;
}
(The formatting change was just so my bad eyes could read it >.>)
 
While that is correct, the script this is a part of will never look for the number if one doesn't exist. For example, $location[King's Chamber] has no number, so it doesn't try to get a number. In the event an ID number is asked for a location that doesn't have one, I've added an error flag that throws and alerts me to a script asking for an invalid ID and which location that was. This is the new version I pulled together after Alhifar pointed it out in post 2:
Code:
int street_num(location which_house){      //Find the ID number for the location...

	for x from 1 to 1000{      //...since to_int($location[]) always returns 0

		if(to_location(x)==which_house) return x;}

	vprint(which_house+" has no ID number!","red",0); return 0;}
 
Isn't there to_url()?

(Basically, check if it's of the form "adventure.php?snarfblat=xxx", then extract the numeric part of that)

Note that some locations aren't keyed to a number (i.e. they don't hit adventure.php), so they'll have a different value. As such, you can't just directly do a substring match before checking to see that it matches this format.

But to be honest, a regex would probably work. Something like
Code:
int loc_to_int(location loc)
{
    matcher m = create_matcher("adventure\\.php\\?snarfblat=([0-9]+)", to_url(loc));
    int loc_id = -1;
    if (m.find()) loc_id = m.group(1).to_int();
    return loc_id;
}
 
Last edited:
That can work and there wouldn't be a cap of 1000. The only thing I have to watch for is locations without numbers. The number is used for a property so rather than return an invalid number, I'll have it throw a flag right away.
Code:
int street_num(location which_house){ //Find the ID number for the location...

	matcher m = create_matcher("adventure\\.php\\?snarfblat=([0-9]+)", to_url(which_house));

	if (m.find()) return m.group(1).to_int();

	vprint(which_house+" has no ID number!","red",0);

	return 0;}
Big thanks 3hee :) Gave it a test run and it works just fine. now it can always keep up with mafia updates, such as the brand new Tavern they rolled out today!
 
Trust me, I know how you feel. I have two characters I'm babysitting and they just finished the tavern. You can still go back though.
 
Back
Top