How do you create a 2D Array?

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
User avatar
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

How do you create a 2D Array?

Post by MinchinWeb »

I've poured over the forum, the OpenTTD Wiki, and Squirrel documentation and can't find anything that works and the C++ method doesn't seem to work either. So how do you create a 2D array?? I expect this to work, but it doesn't...

Code: Select all

WmAtlas [][] = array[WmTownList.Count()][WmTownList.Count()+1];
Any suggestions?


What I'm attempting to do is create a 'table' with the distances between select towns. Here's the (non-working) code. Perhaps I just need another approach...

Code: Select all

	WmTownList = AITownList();
	WmTownList.Valuate(AITown.GetPopulation);
	local PopLimit = WmDOT.GetSetting("MinTownSize");
	WmTownList.KeepAboveValue(PopLimit);				// cuts under the pop limit
	WmTownList.Sort(AIAbstractList.SORT_BY_VALUE, false);
	AILog.Info("     Ignoring towns with population under " + PopLimit + ". " + WmTownList.Count() + " towns left.");

	// Generate Distance Matrix
	local iTown = WmTownList.Begin();
	local WmTownList2 = WmTownList;
	local jTown = WmTownList2.Begin();
//	WmAtlas [][] = array[WmTownList.Count()][WmTownList.Count()+1];
//	int WmAtlas [][];		
	for(local i=0; i < WmTownList.Count(); i++) {
		WmAtlas[i][0] = iTown;		// THIS IS WHERE IT BREAKS...
		jTown = WmTownList2.Begin();
		for (local j = 0; j < WmTownList.Count(); j++) {
			WmAtlas[i][j+1]=AIMap.DistanceManhattan(AITown.GetLocation(iTown,jTown));
			jTown = WmTownList2.Next();
		}
		iTown = WmTownList.Next();
	}
Many thanks in advance.
Last edited by MinchinWeb on 07 Feb 2011 17:47, edited 2 times in total.
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: How do you create a 2D Array?

Post by Yexo »

Please note that townids are not always consecutive. If there are 3 towns it can be that they have ids 2, 3 and 8.

A multidimensional array can be created like this:

Code: Select all

local my_array = [];
for (i = 0; i < 10; i++) {
  local tmp_array = [];
  for (j = 0; j < 10; j++) {
    tmp_array.append(get_distance(i, j));
  }
  my_array.append(tmp_array);
}
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: How do you create a 2D Array?

Post by Lord Aro »

There must be an easier way than that, as NoCAB uses multi-dimensional arrays (world.nut), and it doesn't do it like that...
AroAI - A really feeble attempt at an AI

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. --Edsger Dijkstra
User avatar
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

Re: How do you create a 2D Array?

Post by MinchinWeb »

Thanks! Unfortunately that brings up the next question - how do you find out what size your array is?

And is there somewhere it explains what operators are available for arrays?
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: How do you create a 2D Array?

Post by Zuu »

My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: How do you create a 2D Array?

Post by krinn »

MinchinWeb wrote:how do you find out what size your array is?
yourarray.len()

and if i may:

Code: Select all

local wmTownList2=AIList();
wmTownList2.AddList(wmTownList);
wmTownList2.Valuate(AIMap.DistanceManhattan,AITown.GetLocation(iTown));
if i understood well what you were trying to do (one list with town+pop and one list town+distance from the biggest town)

for your array problem you can solve it with

Code: Select all

local myarray=[];
foreach (town, population in wmTownList) {
foreach (town2, distance in wmTownList2) {
if (town == town2) {
myarray.push(town);
myarray.push(population);
myarray.push(distance);
}}}
lowindex to access them in the array will be myarray.len() * index
highindex will be lowindex+2;
User avatar
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

Re: How do you create a 2D Array?

Post by MinchinWeb »

Thanks for all your help. I ended up using the method Yexo suggested (generating one 'row' at a time and then stuffing it into the first array). My working code looks like this:

Code: Select all

function WmDOT::GenerateAtlas(WmTownArray)
{
	// Generate Distance Matrix
	local iTown;
	local WmAtlas=[];
	WmAtlas.resize(WmTownArray.len());
	
	for(local i=0; i < WmTownArray.len(); i++) {
		iTown = WmTownArray[i];
		local TempArray = [];		// Generate the Array one 'line' at a time
		TempArray.resize(WmTownArray.len()+1);
		TempArray[0]=iTown;
		local jTown = AITown();
		for (local j = 0; j < WmTownArray.len(); j++) {
			jTown = WmTownArray[j];
			TempArray[j+1] = AIMap.DistanceManhattan(AITown.GetLocation(iTown),AITown.GetLocation(jTown));
		}
		WmAtlas[i]=TempArray;
	}

	return WmAtlas;
}
Which generates a little matrix like the following:
  • 6 0 329 288 153
    17 329 0 45 176
    14 288 45 0 135
    15 153 176 135 0
Where the first column is TownID's and the rest is the distance between the pairs of towns.

Addressing seems to work as I expect it. eg WmAtlas[0][3] gives 288.
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: How do you create a 2D Array?

Post by Zuu »

If you want to save some computation time you can skip generating half of the matrix as the distance from town A to B is equal to the distance from town B to A. However, when accessing the matrix you will need to make sure that A index is < B or the reverse depending on which part of the matrix you keep. So for tiny matrices as the example matrix it is probably just a waste of code complexity to only generate half matrices but for larger ones the memory + computation time saved can be significant. All depending on the problem size.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 3 guests