Jump to content

Modifying AI to change first house position


azayrahmad
 Share

Recommended Posts

Hi,

I'd like to modify Petra bot to make the first AI house to be built within Civic Center range. I think the code to manage it is in this part in queueplanBuilding, but it seems to have no impact on the game.

Here's the code:

	if (template.hasClass("House") && !alreadyHasHouses)
	{
		// try to get some space to place several houses first
		bestTile = placement.findBestTile(3*radius, obstructions);
		if (!bestTile.val)
			bestTile = undefined;
	}

I tried modifying the radius multiplier to 6, 2, even 0.5 but it seems that they always end up building it outside of CC's perimeter, sometimes even at the edge of territory. I tried to find findBestTile code but to no avail. I believe findBestTile prevents any building to be built inside CC's range. Does anyone might know about this findBestTile script?

Thank you.

Link to comment
Share on other sites

7 hours ago, Freagarach said:

Hi,

just a few lines earlier (~ line 200) you can see several "placement.addInfluence(...)"-functions that might be of interest to you. Especially the lines around 230 are interesting for your question.

I hope this helps! Otherwise just ask again :)

Hi @Freagarach! Thanks for answering. I believe you refer to this particular part of code:

				else if (template.hasClass("House"))
				{
					if (ent.hasClass("House"))
					{
						placement.addInfluence(x, z, 60/cellSize, 40);    // houses are close to other houses
						alreadyHasHouses = true;
					}
					else if (!ent.hasClass("StoneWall") || ent.hasClass("Gates"))
						placement.addInfluence(x, z, 60/cellSize, -40);   // and further away from other stuffs
				}

I assume this code means that when AI want to build a house, if it sees existing house it will move closer but if it sees walls and gates it will back off. This is my first suspicion too. I tried to change it to make AI build near Civ Center as well.

				else if (template.hasClass("House"))
				{
					if (ent.hasClass("House") || ent.hasClass("CivCentre"))
					{
						placement.addInfluence(x, z, 60/cellSize, 40);    // houses are close to other houses
						alreadyHasHouses = true;
					}
					else if (!ent.hasClass("StoneWall") || ent.hasClass("Gates"))
						placement.addInfluence(x, z, 60/cellSize, -40);   // and further away from other stuffs
				}

It presumably makes AI build a house near Civ Center, but during testing, the first house is always far away, sometimes in Acropolis  it will build house on the foot of the hill. I also modified addInfluence value from 40 to 200 to even 1000 and nothing's changed. I'm at my wits end on this particular code. Hopefully you can enlighten me.

Thank you.

Link to comment
Share on other sites

You can check with a

warn("Hello world");

if the code is called at all, I suspect it not to because of line 213. You can try and remove that condition (I am by no means an expert on the AI and do not know what side effects this could have!). If you try that it may be wise to increase the radius a bit with every house built, otherwise it either might get very full around the CC quickly or the AI builds the first houses too far away for your liking ;)

Link to comment
Share on other sites

Hi, sorry to bother you again. Do you mean to remove this line (213)?

if (!HQ.requireHouses || !template.hasClass("House"))

I tried that and still the code won't pass the code block I posted previously. And how do you mean by increasing radius can prevent AI to build first house too far away? I'm not sure I understand how addInfluence and findBestTile work.

On a different topic, how do you usually do scripting on 0 AD? Do you use text editor or an IDE? Thank you.

Link to comment
Share on other sites

No problem at all! Glad to be of service :)

Aye, I indeed meant that line. I meant the following: You want the AI to build its houses close to the CC, that means you want to add a addInfluence so that it actually tries to do that. But if the area is already full of houses, you need a bigger range to allow more houses. When you start with the bigger range however, the AI might try to build the houses already at the edge of that range, which is not exactly like you want, I guess.

placement.addInfluence(x, z, 60/cellSize, +XX)

Means the AI will try to place the building within XX meter(s) from "ent".

placement.addInfluence(x, z, 60/cellSize, -XX)

Means the AI will try to place the building at least XX meter(s) away from "ent".

Oh, you might be able to remove that previous block now since in your mod houses are less (or more) special.

Hehe, I make it myself unnecessary hard: I use a plain text editor, to force myself to write properly since I've just started coding. But I would advice an IDE.

  • Like 1
Link to comment
Share on other sites

10 hours ago, Freagarach said:

placement.addInfluence(x, z, 60/cellSize, +XX)

Means the AI will try to place the building within XX meter(s) from "ent".


placement.addInfluence(x, z, 60/cellSize, -XX)

Means the AI will try to place the building at least XX meter(s) away from "ent".

So that's what those mean. I somehow thought addInfluence will add some kind of influence score, so the higher XX is the closer they are, but turns out I was wrong.

Anyway, I found out why the first house is never placed near to civ center. It's placed just a couple of lines away (line 220-227):

let struct = m.getBuiltEntity(gameState, ent);
if (struct.resourceDropsiteTypes() && struct.resourceDropsiteTypes().indexOf("food") != -1)
{
	if (template.hasClass("Field") || template.hasClass("Corral"))
		placement.addInfluence(x, z, 80/cellSize, 50);
	else // If this is not a field add a negative influence because we want to leave this area for fields
		placement.addInfluence(x, z, 80/cellSize, -20);
}

Petra deliberately emptying the area around any food dropsite (including Civ Center) to make fields later. Excluding civ center for this condition will ensure that the area near Civ Center can be occupied by any building. Thanks for helping!

10 hours ago, Freagarach said:

Hehe, I make it myself unnecessary hard: I use a plain text editor, to force myself to write properly since I've just started coding. But I would advice an IDE.

Now that's a good work ethic. I myself rather focus on problem solving and let the computer to do spellcheck for me lol. I just found out in trac wiki that Eclipse IDE is recommended, so I'm going to try using it. But if you have any other recommendation let me know.

Thank you.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...