Page 1 of 1

How to Make a Basic Pathfinding AI

Posted: Thu Dec 13, 2018 5:07
by KINGTUT10101
What do I mean by pathfinding AI? Well, in this context I'm talking about something that is able to detect a target and move towards it, eventually reaching said target.

Using fun and rect functionality, we can make a basic, but versatile pathfinding system.

First of all, here is the json of the target:

Code: Select all

[
	{
		"drawground":true,
		"frames":[
			{
				"steal":"$igloo00"
			}
		],
		"height":1,
		"id":"target1",
		"text":"ThiswillbewhatthePFAIwillbetracking.",
		"title":"T1(Stationary)",
		"type":"decoration",
		"width":1
	}
]
Pretty simple. This doesn't need to be anything special. Just remember the id (target1 in this case).

Here's the json for the tile that will be using the pathfinding AI (aka the PFAI):

Code: Select all

[
	{
		"frames":[
			{
				"steal":"$res00"
			}
		],
		"fun":[
			{
				"actions":[
					{
						"id":"PFAI",
						"type":"build",
						"x":1
					},
					{
						"type":"remove"
					}
				],
				"condition":{
					"h":7,
					"inner":[
						{
							"id":"target1",
							"type":"building"
						}
					],
					"max":9999,
					"min":1,
					"type":"rect",
					"w":7,
					"x":0,
					"y":-3
				}
			},
			{
				"actions":[
					{
						"id":"PFAI",
						"type":"build",
						"x":-1
					},
					{
						"type":"remove"
					}
				],
				"condition":{
					"h":7,
					"inner":[
						{
							"id":"target1",
							"type":"building"
						}
					],
					"max":9999,
					"min":1,
					"type":"rect",
					"w":7,
					"x":-7,
					"y":-3
				}
			},
			{
				"actions":[
					{
						"id":"PFAI",
						"type":"build",
						"y":1
					},
					{
						"type":"remove"
					}
				],
				"condition":{
					"h":7,
					"inner":[
						{
							"id":"target1",
							"type":"building"
						}
					],
					"max":9999,
					"min":1,
					"type":"rect",
					"w":7,
					"x":-3,
					"y":0
				}
			},
			{
				"actions":[
					{
						"id":"PFAI",
						"type":"build",
						"y":-1
					},
					{
						"type":"remove"
					}
				],
				"condition":{
					"h":7,
					"inner":[
						{
							"id":"target1",
							"type":"building"
						}
					],
					"max":9999,
					"min":1,
					"type":"rect",
					"w":7,
					"x":-3,
					"y":-7
				}
			}
		],
		"height":1,
		"id":"PFAI",
		"text":"PFAI(PathFinderAI)isanexampleofhowpathfindingcanbeaccomplished.Justplacethetargetinitssightrangeanditwilltrackit.",
		"title":"PFAI(TracksT1)",
		"type":"decoration",
		"width":1
	}
]
Here's an edited version with just the fun code:

Code: Select all

[
	[
		{
			"actions":[
				{
					"id":"PFAI",
					"type":"build",
					"x":1
				},
				{
					"type":"remove"
				}
			],
			"condition":{
				"h":7,
				"inner":[
					{
						"id":"target1",
						"type":"building"
					}
				],
				"max":9999,
				"min":1,
				"type":"rect",
				"w":7,
				"x":0,
				"y":-3
			}
		},
		{
			"actions":[
				{
					"id":"PFAI",
					"type":"build",
					"x":-1
				},
				{
					"type":"remove"
				}
			],
			"condition":{
				"h":7,
				"inner":[
					{
						"id":"target1",
						"type":"building"
					}
				],
				"max":9999,
				"min":1,
				"type":"rect",
				"w":7,
				"x":-7,
				"y":-3
			}
		},
		{
			"actions":[
				{
					"id":"PFAI",
					"type":"build",
					"y":1
				},
				{
					"type":"remove"
				}
			],
			"condition":{
				"h":7,
				"inner":[
					{
						"id":"target1",
						"type":"building"
					}
				],
				"max":9999,
				"min":1,
				"type":"rect",
				"w":7,
				"x":-3,
				"y":0
			}
		},
		{
			"actions":[
				{
					"id":"PFAI",
					"type":"build",
					"y":-1
				},
				{
					"type":"remove"
				}
			],
			"condition":{
				"h":7,
				"inner":[
					{
						"id":"target1",
						"type":"building"
					}
				],
				"max":9999,
				"min":1,
				"type":"rect",
				"w":7,
				"x":-3,
				"y":-7
			}
		}
	]
]
(It is a bit out of order, but you should get the general idea)

As you can see, we use the rect functionality to check a wide area of land for target1. When target1 is detected, the PFAI moves towards it. Not complicated at all.

However, while using this method, your ability to check and track buildings will be limited by how large of an area you check. Because we are using rect functionality to check for target1, the PFAI's range is limited. The amount of land you check can be increased though, but this is just something to keep in mind.

Think of this limitation like a sightline. Once the target leaves the sightline, our PFAI can no longer "see" it.
Screenshot_20181212-204211.png
Here is a visual example of the PFAI's sightlines. As you can see, they are limited and they do overlap a bit. Do not worry about this overlap, it is supposed to be like that.


As you can see, this is a fairly simple concept and hopefully it will help you make some cool plug-ins down the road. Although the concept is simple, it can be used for some very complex things, such as a monster that seeks out certain buildings and destroys them or a character from The Theos seeking out a TV for entertainment in order to increase its happiness. I hope to see some cool plug-ins that use this concept.

Useful things:
Rect Tutorial-
https://theotown.com/forum/viewtopic.ph ... 25#p118725
pathfinding_AI.json
(2.69 KiB) Downloaded 26 times
I use Json Genie, so the json here may look very weird. I would go to JsonLint to convert it to the regular format if you have problems reading it.
https://jsonlint.com/

Re: How to Make a Basic Pathfinding AI

Posted: Thu Dec 13, 2018 15:50
by Kartofun
Cool! Very useful. :bc :json