[Patch] all_ais commands

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
User avatar
MagicBuzz
Tycoon
Tycoon
Posts: 1354
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

[Patch] all_ais commands

Post by MagicBuzz »

Hello,

According the FS#4216, I designed a small patch that introduce three new console commands in order to manage all AIs at the same time.

start_all_ais
stop_all_ais
reload_all_ais

Those three commands haven't arguments.

start_all_ais starts any unstarted AI (will start the same number of AI that the competitors parameter)
stop_all_ais stops any running AI.
reload_all_ais stops then starts any running AI. This doesn't impact non-running AIs.

-- Edit : Fixed a warning when compiling (uint and int comparision)
Attachments
cmd_all_ais_r21200.patch
(3.65 KiB) Downloaded 174 times
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: [Patch] all_ais commands

Post by Lord Aro »

Nice!
I haven't tested it yet, but i think the staet_all_ais should have an argument of whether to start as many as the player has specified (in difficulty settings) or total possible ais (14)

Another thought, perhaps they would be better named ###_all_ai as for me, ais doesn't look right in lower case
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
MagicBuzz
Tycoon
Tycoon
Posts: 1354
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

Re: [Patch] all_ais commands

Post by MagicBuzz »

Ok for the argument, i'll try to change it.

I agree with "xxx_all_ai" without the "s".
User avatar
MagicBuzz
Tycoon
Tycoon
Posts: 1354
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

Re: [Patch] all_ais commands

Post by MagicBuzz »

Here is the update.

But I have a problem with some "magic code" :

Code: Select all

	FOR_ALL_COMPANIES(c) {
		if (n >= max) break;
		if (c->index > n++) continue;
		/* Start a new AI company */
		DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
	}
If max = 4 and there is already 2 AI competitor, then the computer will start 4 new competitors instead of 2 new.

I can't figure out why it does this. In fact, I don't understand what c->index is.
I thought it was 0 if no AI was controlling the company (empty slot) and N if an AI control it (with N = the position in the companies array). But it looks like I'm wrong.

It produces another bug : when no parameter is set, the typing several times "start_all_ai" will start 14 AIs, as it doesn't take care of the currently running AIs.
Attachments
cmd_all_ai_r21211.patch
(4.36 KiB) Downloaded 166 times
User avatar
MagicBuzz
Tycoon
Tycoon
Posts: 1354
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

Re: [Patch] all_ais commands

Post by MagicBuzz »

Here is an update.
Change:
- Renamed start_all_ais by start_all_ai
- Renamed stop_all_ais by stop_all_ai
- Renamed reload_all_ais by reload_all_ai
=> AIs in lower case doesn't look natural

Added Feature:
- start_all_ai now supports an argument <AI count>. It's the desired total AI count.

Fix:
- start_all_ai now doesn't start more AIs than desired. IE when already running AIs, it will start <AI count> - <Already running AIs> new AIs.
In order to properly count the already running AIs, I replaced the previous FOR loop with magical code with some cleaner and humanproof code.

Code: Select all

	/* Total AI to start is total_ai_count - current running AIs count */
	int new_ai_count = total_ai_count;
	Company *c;
	FOR_ALL_COMPANIES(c) {
		if (Company::IsValidAiID(c->index)) {
			new_ai_count--;
		}
	}

[...]

	/* Start the desired AIs */
	for (int i = 0; i < new_ai_count; i++) {
		DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
	}
Attachments
cmd_all_ai_r21218.patch
(4.85 KiB) Downloaded 168 times
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: [Patch] all_ais commands

Post by Lord Aro »

Lookin good!
Two things occur to me:
1. Are the commands like others, in that you can also use 'startallai', without the underscores
2. In the for loop that starts the AIs, shouldn't it be 'i <= ####' (can't remember the variable name :lol: )
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
MagicBuzz
Tycoon
Tycoon
Posts: 1354
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

Re: [Patch] all_ais commands

Post by MagicBuzz »

1/ I didn't know that. Anyway, I tested and "startallai", "stopallai" and "reloadallai" are working
2/ the for loop may stop at <= ### if it was starting at 1, but it starts at 0, so the condition is well <
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 15 guests