SmallTownAI

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
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

SmallTownAI

Post by ethaninfinity » 05 Apr 2018 18:35

Hello,
I have been playing OpenTTD for years, but this is my first attempt at a useful AI.
It is still a WIP, and occasionaly fails to make money.
Please help me to find and remove bugs, and any feedback is appreciated.

SmallTownAI currently:

builds extensive 12-station bus networks across the map

Known Issues:

Can't replace old vehicles

Can't remove failed routes






Some credit to WrightAI, as several parts of SmallTownAI are based upon it.
Attachments
SmallTownAI-7.tar
the newest version of this ai
(26.5 KiB) Downloaded 38 times
Nennpool Transport, Feb 27th, 1972.png
This ai in action
(1.06 MiB) Not downloaded yet
Last edited by ethaninfinity on 30 May 2019 10:41, edited 14 times in total.
Ethaninfinity

User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5391
Joined: 13 Sep 2004 13:21
Location: The Moon
Contact:

Re: SmallTownAI

Post by PikkaBird » 05 Apr 2018 23:31

Hey, that's a good start! Welcome to the forums and the fun of coding AI. :)

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 06 Apr 2018 16:02

Hey, that's a good start! Welcome to the forums and the fun of coding AI. :)
Thanks for the encouragement! It doesn't do much now, but I hope to work on it until it is at least competitive with some of the other AIs out there.

Also, an update! This version actually places stations that connect to roads and builds a bus to connect them.
Attachments
SmallTownAi_R1.tar
The first money-making(sometimes) version of this AI!
(13.5 KiB) Downloaded 62 times
Ethaninfinity

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 25 Apr 2018 00:27

Update!

R2: Now builds an inter-city network!

Now on BaNaNaS! :D
Attachments
Inter-city connection.png
(931.51 KiB) Not downloaded yet
SmallTownAi_R2.tar
(24.5 KiB) Downloaded 54 times
Ethaninfinity

User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5391
Joined: 13 Sep 2004 13:21
Location: The Moon
Contact:

Re: SmallTownAI

Post by PikkaBird » 25 Apr 2018 06:45

Nice update. :)

I've noticed it frequently builds the first two bus stops right next to each other. Also, with newgrfs loaded (eg Road Hog), the AI thinks it's building buses but nothing is built.
Attachments
Image1.png
(37.43 KiB) Not downloaded yet

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 26 Apr 2018 21:05

Nice update. :)

I've noticed it frequently builds the first two bus stops right next to each other. Also, with newgrfs loaded (eg Road Hog), the AI thinks it's building buses but nothing is built.
Thanks for the info! The bus stops being built close to each other is a known issue that should be fixed soon, and I will look into the bus issue and try to fix it.
Ethaninfinity

User avatar
acs121
Tycoon
Tycoon
Posts: 1944
Joined: 03 Nov 2017 18:57
Location: Courbevoie, near Paris, France

Re: SmallTownAI

Post by acs121 » 26 Apr 2018 22:22

PikkaBird wrote:Nice update. :)

I've noticed it frequently builds the first two bus stops right next to each other. Also, with newgrfs loaded (eg Road Hog), the AI thinks it's building buses but nothing is built.
Isn't that because the ai_engine_rank is set at 0 ?
AFAIK there are a few sets that do allow their vehicles being used by AI other than Road Hog. Bob's British Buses/Random Vehicles work perfectly with AI - even though, in the later years, it tends to use a lot the UKB Quad ;)

User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5391
Joined: 13 Sep 2004 13:21
Location: The Moon
Contact:

Re: SmallTownAI

Post by PikkaBird » 26 Apr 2018 23:05

acs121 wrote:Isn't that because the ai_engine_rank is set at 0 ?
No, it isn't.

User avatar
acs121
Tycoon
Tycoon
Posts: 1944
Joined: 03 Nov 2017 18:57
Location: Courbevoie, near Paris, France

Re: SmallTownAI

Post by acs121 » 26 Apr 2018 23:18

PikkaBird wrote:
acs121 wrote:Isn't that because the ai_engine_rank is set at 0 ?
No, it isn't.
Why then ?

User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5391
Joined: 13 Sep 2004 13:21
Location: The Moon
Contact:

Re: SmallTownAI

Post by PikkaBird » 27 Apr 2018 00:27

acs121 wrote:Why then ?
At a quick glance: Because something in ethan's vehicle evaluation code isn't working as intended. I'd draw his attention, particularly, to the fact that AICargo.CC_PASSENGERS is a cargo class (and == 1), not the cargo ID for passengers. It's probably safe to assume passengers are cargo 0, at least for now.

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 27 Apr 2018 03:07

Because something in ethan's vehicle evaluation code isn't working as intended. I'd draw his attention, particularly, to the fact that AICargo.CC_PASSENGERS is a cargo class (and == 1), not the cargo ID for passengers.
Yes, that was the issue, or at least one of the issues. My code also wasn't removing invalid engines from the list, so it was trying to build one of the default buses though it couldn't with Road Hogs in use. It was mostly luck that it somewhat worked in the first place, but all of that is (hopefully) fixed now. It also now chooses the newest vehicle available, which is not the best way to choose but not as bad as it was, and is simple to do.
Attachments
SmallTownAI-3.tar
(24.5 KiB) Downloaded 58 times
Ethaninfinity

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 04 May 2018 19:22

Hello, I have gotten stuck while trying to fix the issue of stations being built too close together. I don't see anything wrong with the code, but it seems to do absolutely nothing :

Code: Select all


//station1_location is the list of potential tiles for station construction

//go through every potential station location to check for nearby stations
for(local test_tile = station1_location.Begin(); station1_location.IsEnd() == 0; station1_location = station1_location.Next()) {

	//create a list of tiles near the tile being tested
	local neighbors = AITileList()
	neighbors.AddRectangle(test_tile - (2, 2), test_tile + (2, 2));
	
	
	//remove non-station tiles
	neighbors.Valuate(AITile.IsStationTile) 
	neighbors.RemoveValue(0);

	// if there are still tiles, remove the tile being tested from the list of potential station locations
	if(neighbors.Count != 0) {
		station1_location.RemoveTile(test_tile);
	}
}
After this is fixed, the next version of the ai should be nearly ready
Ethaninfinity

User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5391
Joined: 13 Sep 2004 13:21
Location: The Moon
Contact:

Re: SmallTownAI

Post by PikkaBird » 04 May 2018 19:53

neighbors.Count() ? Gets me every time too.

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 22 May 2018 23:31

neighbors.Count() ? Gets me every time too.
Unfortunately, that did not solve the issue. I have added some debug messages, and it appears that what is inside the loop never occurs. I am still stumped. The other thing I am working on for the next update is vehicle renewal, but that also needs a similar loop to work, and I haven't had any luck with it either.
Unfortunately, I haven't worked on the AI much recently, but should return to it after the end of the school year.

P.S.
How do you make a quote show who was quoted?
Ethaninfinity

Wormnest
Engineer
Engineer
Posts: 95
Joined: 14 Jul 2013 12:33
Location: Netherlands

Re: SmallTownAI

Post by Wormnest » 23 May 2018 21:47

I think you need to replace

Code: Select all

station1_location.IsEnd() == 0
with

Code: Select all

!station1_location.IsEnd()

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 27 May 2018 23:28

I think you need to replace
Code:
station1_location.IsEnd() == 0

with
Code:
!station1_location.IsEnd()
Thank you! This solved the problem, which means...

Update!
R4: Sensible Stations

The two initial stations now will not be closer than 2 tiles from another station.
Vehicles older than their lifespan will be replaced.
Attachments
SmallTownAI-R4.tar
(27.5 KiB) Downloaded 52 times
Ethaninfinity

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 30 Jul 2018 01:23

Update!
R5

This is a bugfix update that fixes the occasional infinite loop when a new network is created, the building of buses even when no bus station is built, and removes the broken vehicle replacement.
Attachments
SmallTownAI-5.tar
SmallTownAI R5
(26 KiB) Downloaded 50 times
Ethaninfinity

HGus
Engineer
Engineer
Posts: 95
Joined: 12 May 2013 22:28
Location: Argentina

Re: SmallTownAI

Post by HGus » 18 Apr 2019 21:06

When it starts creating a new network, it creates a sign on map, but it forgots to delete it, in one of my games it ended with more than 3 thousand signs before I realized the bug.

Line 172 at road.nut

Code: Select all

AISign.BuildSign(town_center, "Building Bus Network");

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 19 Apr 2019 02:08

Thanks for the report! I am currently working on rewriting of a lot of this AI, but here is a quick fix that should send the info to the debug window instead of signs. It may still repeat itself, but hopefully not in a way that could break a game.

R6: Less Signs!
SmallTownAI-6.tar
Less signs!
(26 KiB) Downloaded 28 times
Ethaninfinity

User avatar
ethaninfinity
Engineer
Engineer
Posts: 14
Joined: 05 Apr 2018 16:50
Location: due west of somewhere...

Re: SmallTownAI

Post by ethaninfinity » 23 Apr 2019 03:03

Update!
R7: Roadway to Success

Much of the code has been reworked, leading to better performance in nearly all areas. It also now reliably builds multiple networks, and considers town size when expanding networks.
Attachments
SmallTownAI-7.tar
(26.5 KiB) Downloaded 30 times
Ethaninfinity

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 3 guests