[CLOSED] Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

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

Post Reply
Jaume
Engineer
Engineer
Posts: 60
Joined: 24 Aug 2007 15:43

[CLOSED] Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

Post by Jaume »

Hi!

Since OpenTTD v1.13 the function AIGroup.CreateGroup() requires two parameters. Some AIs can crash because older versions of AIGroup.CreateGroup() function requires only one parameter.

In SuperSimpleAI v39 I fixed this with this class:

Code: Select all

/**
 * This file is part of SuperSimpleAI: An OpenTTD AI.
 *
 * Author: Jaume Sabater
 *
 * It's free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * any later version.
 *
 * You should have received a copy of the GNU General Public License
 * with it.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Define the MyAIGroup class which extends the AIGroup functions.
 */
class MyAIGroup /* extends AIGroup */
{
        /**
         * Create a new group.
         * @param vehicle_type The type of vehicle to create a group for.
         * @param parent_group_id The parent group id to create this group under,
         *         GROUP_INVALID for top-level (default and optional).
         * @return The GroupID of the new greup, or an invalid GroupID when it failed.
         *         Check the return value using AIGroup.IsValidGroup(). In test-mode 0
         *         is returned if it was successful; any other value indicates failure.
         */
        static function CreateGroup(vehicle_type, parent_group_id = AIGroup.GROUP_INVALID);
}

local ottd_v = AIController.GetVersion();
local ottd_major = (ottd_v & 0xF0000000) >> 28;
local ottd_minor = (ottd_v & 0x0F000000) >> 24;

if (ottd_major > 0 && ottd_minor > 12) {
        // Since OpenTTD 1.13 AIGroup.CreateGroup() requires two parameters.
        function MyAIGroup::CreateGroup(vehicle_type, parent_group_id = AIGroup.GROUP_INVALID)
        {
                return AIGroup.CreateGroup(vehicle_type, parent_group_id);
        }
} else {
        function MyAIGroup::CreateGroup(vehicle_type, parent_group_id = AIGroup.GROUP_INVALID)
        {
                return AIGroup.CreateGroup(vehicle_type);
        }
}
You can use it to fix your AI!
Last edited by Jaume on 19 Feb 2023 19:51, edited 1 time in total.
User avatar
glx
OpenTTD Developer
OpenTTD Developer
Posts: 622
Joined: 02 Dec 2005 15:43
Location: Drancy(93) - France
Contact:

Re: Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

Post by glx »

AIGroup.CreateGroup() requires two parameters since API version 1.9.
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 988
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

Post by frosch »

This is not how it is supposed to work.

* AIGroup::CreateGroup was changed in 1.9. See the changelog: https://docs.openttd.org/ai-api/ai__changelog_8hpp.html
* OpenTTD provides an API compatibility layer. It provides the API matching the version requested in your info.nut file: https://github.com/OpenTTD/OpenTTD/blob ... at_1.2.nut
* SuperSimpleAI specifies API version 1.2 in its info.nut file.
* You can see the compatibility script getting loaded in the first line "1.2 API compatibility in effect." of the AI debug window, see attached screenshot.
* SuperSimpleAI v39 now crashes almost immediately after start, because it tries to call the AIGroup::CreateGroup with the 1.9 prototype, while the 1.2 API is active. See attached screenshot.

I can only guess that your installation is somehow broken, and OpenTTD can't find the compatibility scripts.
Attachments
supersimpleai39.png
(157.55 KiB) Not downloaded yet
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
Jaume
Engineer
Engineer
Posts: 60
Joined: 24 Aug 2007 15:43

Re: Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

Post by Jaume »

Thank you.

I had problems with my libraries, now runs ok.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest