Transport Tycoon Forums

The place to talk about Transport Tycoon
It is currently Fri Feb 22, 2019 11:47 pm

All times are UTC




Post new topic  Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Feb 01, 2019 2:17 pm 
Offline
Engineer
Engineer

Joined: Mon Jan 14, 2019 1:30 pm
Posts: 3
Hi folks,

I'm currently trying to figure out the AIList.Valuate function but it does not really make much sense to me yet.
Somehow it seems it valuates the index instead of the value of my list.

What I'm trying to archive: I want to check all waypoint locations in the DistanceManhattan to a specific tile.
Before this I did this with a for loop through all elements but it's slow and a lot of code for something (I thought) so simple.

What I came up with so far:
Code:
intersections.Valuate(AIBaseStation.GetLocation); //asign tile ID to value
_info("manual calculated between: " + intersections.GetValue(intersections.Begin()) + " and " + tileS + " is: " + AIMap.DistanceManhattan(intersections.GetValue(intersections.Begin()),tileS),2);
_info("1---------------------------",1)
foreach (key,value in intersections)
   _info(key + " --> " + value)
_info("2---------------------------",1)
intersections.Valuate(distance, tileS)
foreach (key,value in intersections)
   _info(key + " --> " + value)


This produces for example following debug:
Quote:
manual calculated between: 248265 and 169443 is: 180
1--------------------
2 --> 248265
4 --> 211915
2--------------------
4 --> -1
2 --> -1


so clearly the distance is not calculated. My distance function (I used AIMap.DistanceManhattan before but want to debug more) looks like this:
Code:
function distance(tile1, tile2)
{
AILog.Error ("E---------------------------")
AILog.Info ("Tile 1: " + tile1 + " and Tile 2: " + tile2 + " are " + AIMap.DistanceManhattan(tile1,tile2)+ " tiles appart")
return AIMap.DistanceManhattan(tile1,tile2)
}


which returns:
Quote:
E--------------------
Tile 1: 2 and Tile2: 169443 are -1 tiles appart


conclusion: it seems instead of value the index is passed to my function. Everything I'm reading in other AI they used the exact same approach. Where do I got it wrong? How can I hand over the value of index 2 instead of 2?

for the sake of completeness: My _info function just prints debug messages and formats/add time stamp and similar. It works fine and does not appear to make any issues. Debug mode is a setting from the AIsettings
Code:
function _info(message, level = 0)
{
local date = AIDate.GetYear(AIDate.GetCurrentDate()) + "-" + AIDate.GetMonth(AIDate.GetCurrentDate()) + "-" + AIDate.GetDayOfMonth(AIDate.GetCurrentDate());
message = date + ":        " + message;
switch (level)
   {
   case 1: //warning, print always in yellow
      {
      AILog.Warning (message)
      }
      break;
   case 2: //Error, print always in red
      {
      AILog.Error (message)
      }
      break;
   case 3: //special case for welcome message
      {
      AILog.Info (message)
      }
      break;
   default: // Info but only if debug mode is set
      {
      if (debug_mode)
         AILog.Info (message)
      }
      break;
   }
return 1   
}


Is there any better documentation/examples about valuate then the NOAI documentation which could really explain this to me? I trust it's a very powerfull function and often used in other AIs. But I simply don't get the hang the moment it goes into more than one parameter.
--> http://noai.openttd.org/api/trunk/classAIList.html


Top
   
PostPosted: Sun Feb 03, 2019 2:23 am 
Offline
Traffic Manager
Traffic Manager

Joined: Thu Feb 26, 2015 12:52 am
Posts: 244
I think it's this:

Code:
local intersections = AIWaypointList(AIWaypoint.WAYPOINT_ANY);
intersections.Valuate(AIStation.GetDistanceManhattanToTile, tileS);

_________________
Formerly known as Samu


Top
   
PostPosted: Wed Feb 20, 2019 10:07 pm 
Offline
Engineer
Engineer
User avatar

Joined: Tue Jun 21, 2011 4:30 pm
Posts: 35
Location: In Da House!
Howdy,

Not an expert by any means... but here is my understanding.

in your first code block, intersections.Valuate(AIBaseStation.GetLocation) indicates that intersections is a list of stations, and the result would be a list of stations, with their location tiles as values.

AIBaseStation.Getlocation(station_id) is the syntax i would think for a regular statement. so your valuate is looking for the station_id to be the "item", and put the location in "value" for each item-value pair in your list.

If intersections is NOT a list of station_ids then the results could be undefined!


The next bit... intersections.Valuate(distance, tileS) indicates intersections is a list of tiles, and the result would be a list of tiles, with their distance to tileS as values.

if intersections is NOT a list of tiles, then the results could be undefined!


by the logging it can be seen that intersections is not a list of stations, nor a list of tiles, so it does not surprise the wonky results!


your code for distance function looks fine. It would work if you passed actual tiles.

I don't think you can print "tiles" with AILog . maybe AIMap.GetTileX(tile1) + ", " + AIMap.GetTileY(tile1) is what you are after?


xarick's code is a good example of something similar to what you are trying to do.


Good luck, and keep at it,
Kev!

P.S. The key thing to remember for Valuate is the ITEM will be the first variable passed to the valuator, so single input functions only have the function name... blah.Valuate(function_requiring_only_one_input) where blah is a list of things the function expects as input.

_________________
I DO like a nice caboose...


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000-2019 phpBB Limited

Copyright © Owen Rudge/The Transport Tycoon Forums 2001-2019.
Hosted by Zernebok Hosting.