Problem WITH!!!!! Ai

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

User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Problem WITH!!!!! Ai

Post by Lord Aro »

the error message provides you with valuable debugging information:
Ar4er wrote:Image
The first red line gives you all the info you can get: the error occurred in RoadPathFind.nut, on line 5, character 9
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
Arie-
Director
Director
Posts: 593
Joined: 20 Jan 2009 16:07

Re: Problem WITH!!!!! Ai

Post by Arie- »

As it seems you are a really inexperienced programmer, three things you should know (there are more but these are really important for you at the moment):
1- Think about your code: what do I want the code to do, how can I achieve this (in a general way), then think about programming language specific approaches.
2- Write code: get familiar with a language and it's properties.
3- Debug: learn what debugging is and how to approach errors and their causes.

These three are very general but apply to all languages. An example for you concerning squirrel, you want to construct a route between two cities.
1) Think about your code. You need to find cities to construct routes in between. You need to construct the route (roads, depots, stations, cars). Now we know this, think about the cities in more detail, how do you find cities (based on which properties do you want te select cities etc.). If that problem is tackeled, you need to know how to construct a route, build depots and stations, where do you want to place those, what is your approach when connecting the two towns with roads etc.
2) Write code. You now know your general approach to the construction of the route, now you need to put your thoughts in squirrel language. An example is:

Code: Select all

- get a list of towns
- sort the list of towns descending
- get the first two towns
- construct a road between the two towns
- build a station in both towns
- build a depot in both towns
- build trucks
- give orders to the trucks
- start the trucks
3) Debug. Obviously you are going to get errors, now you need to know how to find and fix those errors. Find line numbers, find error descriptions and try to understand them.
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

But i can't find a error becouse non pop ups on me
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Problem WITH!!!!! Ai

Post by Lord Aro »

in that case there isn't an error


to see ai debug output go to far right button on menu bar and select ai debug
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
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

i know that
i get only this
Attachments
Capture.PNG
Capture.PNG (4.95 KiB) Viewed 2615 times
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Problem WITH!!!!! Ai

Post by Lord Aro »

Ok, now add:

Code: Select all

AILog.Info("*function-name*");
to the beginning of each function. Then post the debug screen
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
Arie-
Director
Director
Posts: 593
Joined: 20 Jan 2009 16:07

Re: Problem WITH!!!!! Ai

Post by Arie- »

Ar4er wrote:But i can't find a error becouse non pop ups on me
Have you read bullet three: "You are going to get errors.". Ergo: know what your code should do, and if something doesn't go as expected, learn how to find what goes wrong. An error is not only an error when you get notified. You really have to be more specific and have to put in more effort yourself because people are going to ignore you if you don't google yourself.
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

Nothing's there

Can you download this file and look for your self
Attachments
AcelaExpress.rar
(6.83 KiB) Downloaded 57 times
Last edited by Ar4er on 22 Aug 2010 16:16, edited 1 time in total.
Radicalimero
Engineer
Engineer
Posts: 23
Joined: 02 Dec 2009 17:06

Re: Problem WITH!!!!! Ai

Post by Radicalimero »

Ar4er wrote:But why my Ai isn't building anything i quess problem is in here

Code: Select all

function AcelaExpress::Start()
 {
   AILog.Info("AcelaExpress Started.");
   SetCompanyName();
  
   //Keep running. If Start() exits, the AI dies.
   while (true) <----------- here is the problem. while (true) stay here so he will always stay u forgot the brackets
{ <------ opening bracket
   			this.Sleep(1);
			Debugs.Warning("Main loop started"); <-------- this line will also give an error
			Manager.ManageLoan();
//			Debugs.Debug("manageOnly = " + RoadPathFind.manageOnly);
			if(RoadPathFind.manageOnly == false)
			{
				local vehList = AIVehicleList();
				vehList.Valuate(AIVehicle.GetVehicleType);
				vehList.KeepValue(AIVehicle.VT_ROAD);
				local numOfVehs = vehList.Count();
				if(AIGameSettings.GetValue("vehicle.max_roadveh") <=  numOfVehs)
				{
					Debugs.Info("Max amount of road vehicles reached");
					RoadPathFind.manageOnly = true;
				}
				else
				{
					RoadPathFind.Main();
				}
			}
			else
			{
				Debugs.Info("Sleeping because there is nothing to build");
				this.Sleep(25);
			}
		}
       } <------ close bracket
 
 function AcelaExpress::Stop()
Image
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

Radicalimero wrote:
Ar4er wrote:But why my Ai isn't building anything i quess problem is in here

Code: Select all

function AcelaExpress::Start()
 {
   AILog.Info("AcelaExpress Started.");
   SetCompanyName();
  
this is 19   //Keep running. If Start() exits, the AI dies.
this is 20   while (true) } <----------- here is the problem. while (true) stay here so he will always stay u forgot the brackets
{ <------ opening bracket
   			this.Sleep(1);
			Debugs.Warning("Main loop started"); <-------- this line will also give an error
			Manager.ManageLoan();
//			Debugs.Debug("manageOnly = " + RoadPathFind.manageOnly);
			if(RoadPathFind.manageOnly == false)
			{
				local vehList = AIVehicleList();
				vehList.Valuate(AIVehicle.GetVehicleType);
				vehList.KeepValue(AIVehicle.VT_ROAD);
				local numOfVehs = vehList.Count();
				if(AIGameSettings.GetValue("vehicle.max_roadveh") <=  numOfVehs)
				{
					Debugs.Info("Max amount of road vehicles reached");
					RoadPathFind.manageOnly = true;
				}
				else
				{
					RoadPathFind.Main();
				}
			}
			else
			{
				Debugs.Info("Sleeping because there is nothing to build");
				this.Sleep(25);
			}
		}
       } <------ close bracket
 
 function AcelaExpress::Stop()

error
Attachments
Capture.PNG
Capture.PNG (6.74 KiB) Viewed 2605 times
Radicalimero
Engineer
Engineer
Posts: 23
Joined: 02 Dec 2009 17:06

Re: Problem WITH!!!!! Ai

Post by Radicalimero »

those arrows <------
where to point out the problem the correct code should be

Code: Select all

 function AcelaExpress::Start()
 {
   AILog.Info("AcelaExpress Started.");
   SetCompanyName();
  
   //Keep running. If Start() exits, the AI dies.
   while (true)
   {
   			this.Sleep(1);
			AILog.Warning("Main loop started");
			Manager.ManageLoan();
//			Debugs.Debug("manageOnly = " + RoadPathFind.manageOnly);
			if(RoadPathFind.manageOnly == false)
			{
				local vehList = AIVehicleList();
				vehList.Valuate(AIVehicle.GetVehicleType);
				vehList.KeepValue(AIVehicle.VT_ROAD);
				local numOfVehs = vehList.Count();
				if(AIGameSettings.GetValue("vehicle.max_roadveh") <=  numOfVehs)
				{
					AILog.Info("Max amount of road vehicles reached");
					RoadPathFind.manageOnly = true;
				}
				else
				{
					RoadPathFind.Main();
				}
			}
			else
			{
				AILog.Info("Sleeping because there is nothing to build");
				this.Sleep(25);
			}
		}
	}
Image
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Problem WITH!!!!! Ai

Post by Lord Aro »

Radicalimero wrote:

Code: Select all

         Debugs.Warning("Main loop started"); <-------- this line will also give an error
this is because AroAI uses a custom Debug system. you will have to change it before anything will work

also, you can't expect us you write your code for you. Most of us have our own AIs to write. you MUST learn what the error messages mean and how to fix them.
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
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

it seems to work becouse that i see some working on ai debug
now i can finaly do something by my own


As for the error i think i fix those lines where is error by AILog.Info i quess that would work
Attachments
Capture.PNG
Capture.PNG (13.18 KiB) Viewed 2600 times
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

i done it i done thank you Lord Aro, Radicalimero, And Zuu
Thank you all i am so sorry if i was big pain thank you guys best forum ever now i am going to try add function so he makes 3 town route
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Problem WITH!!!!! Ai

Post by Lord Aro »

well done! :D

don't forget to credit me!
it took me months to get that code right... :roll:
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
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

don't worry i am not selfwish
and i would buy you a cake if you lived next to me
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

One more question how to make my ai so he do random events like one time he is building buses but another time trucks
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Problem WITH!!!!! Ai

Post by Lord Aro »

I would do something similar to the company name picker. You would have to write a lot of code so the AI can support trucks though.
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
Ar4er
Engineer
Engineer
Posts: 45
Joined: 11 Jul 2010 01:48

Re: Problem WITH!!!!! Ai

Post by Ar4er »

i was think about that too but i didn't find this way of coding in any AI
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: Problem WITH!!!!! Ai

Post by Brumi »

There is a bunch of random functions in AIBase: http://noai.openttd.org/docs/1.0.3/classAIBase.html
For example if you want to use trucks in 64% of the cases, do this:

Code: Select all

if (AIBase.Chance(64, 100)) {
	// build trucks
	...
} else {
	// build buses
	...
}
AIBase::RandItem() is useful if you want to arrange items in a list in a random order. I use this in the industry/town selection alghorithm in SimpleAI. (see cBuilder::FindService() in builder.nut)
Example of this:

Code: Select all

local townlist = AITownList();
townlist.Valuate(AIBase.RandItem); // after valuating the items are arranged in a random order

local town = townlist.Begin(); // pick a random town
SimpleAI uses a lot of random functions here and there, the only problem is that my code is not very well commented, so it is not the easiest to understand :P
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 9 guests