RenamerAI and some Squirrel questions

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
hempa
Engineer
Engineer
Posts: 21
Joined: 15 Oct 2008 16:30

RenamerAI and some Squirrel questions

Post by hempa »

Hello,

First, I'd like to say I'm new to Squirrel, and learning the ropes as I go, so excuse my newbieness :P As per this thread of mine, I'm going to try and make an AI that renames cloned vehicles with shared orders. So for example cloning a vehicle that is named "ARL-SEL Ps 1" will rename the clone to "ARL-SEL Ps 2".

I've already managed to find unnamed vehicles since they will always be "Road Vehicle #" for cars, and I did this by getting the list of vehicles, checking each name if it begins with "Road Vehicle"... I can imagine this being quite slow if done on a large number of vehicles though. Does anyone have suggestions as to how I could do this?

Also, I will need to somehow find out the number by the end of the renamed vehicles. It could be done by a regex, for instance with ".*([0-9]+)$" or something similar. I haven't found any pattern matching in the Squirrel language though. If anyone has a clue as to how to do this, please fill me in :)

Edit: I found out that trying to get the shared vehicles list locks up my OpenTTD. I use the following code:

Code: Select all

local vh_shared = AIVehicleList_SharedOrders(vh);
vh here is the vehicle I'm currently at in the list. If I try to use that line though, the game locks up and I have to do a dirty kill -9 on the process :cry:

Cheers,
Hempa
Last edited by hempa on 24 Feb 2009 13:33, edited 1 time in total.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: RenamerAI and some Squirrel questions

Post by Yexo »

hempa wrote:Hello,

First, I'd like to say I'm new to Squirrel, and learning the ropes as I go, so excuse my newbieness :P As per this thread of mine, I'm going to try and make an AI that renames cloned vehicles with shared orders. So for example cloning a vehicle that is named "ARL-SEL Ps 1" will rename the clone to "ARL-SEL Ps 2".
Personally I don't think it's a good choice to do this by writing an AI.
I've already managed to find unnamed vehicles since they will always be "Road Vehicle #" for cars, and I did this by getting the list of vehicles, checking each name if it begins with "Road Vehicle"... I can imagine this being quite slow if done on a large number of vehicles though. Does anyone have suggestions as to how I could do this?
Just don't loop over every vehicle every tick. It's not important your vehicles are renamed immediately, as long as they are renamed "soon enough". Please mind that your assumption that unnamed vehicles will start with "Road Vehicle #" holds only when you're playing in english.
Also, I will need to somehow find out the number by the end of the renamed vehicles. It could be done by a regex, for instance with ".*([0-9]+)$" or something similar. I haven't found any pattern matching in the Squirrel language though. If anyone has a clue as to how to do this, please fill me in :)
Use vehicle_name.slice(14).tointeger(). 14 is the amount of chars before the vehicle number ("Road Vehicle #" is 14 chars)
hempa
Engineer
Engineer
Posts: 21
Joined: 15 Oct 2008 16:30

Re: RenamerAI and some Squirrel questions

Post by hempa »

Yexo wrote:Personally I don't think it's a good choice to do this by writing an AI.
I do not feel I am proficient enough with string handling in C/C++ to make a patch for this, and it is also a way for me to learn Squirrel.
Yexo wrote:Please mind that your assumption that unnamed vehicles will start with "Road Vehicle #" holds only when you're playing in english.
That is also a reason why I'm asking if there's a way to find the vehicles that have not been renamed. If that was available in the API (and I haven't found it so far, still searching) I'd be able to find it regardless of localization.
Yexo wrote:Use vehicle_name.slice(14).tointeger(). 14 is the amount of chars before the vehicle number ("Road Vehicle #" is 14 chars)
This will of course only work for Road Vehicle # situations, but that's not the one I'd want to find. It's for vehicles with custom names. I name them like this: "<station>-<station> (<cargo>) <number>" and it's the number at the end I want to find.

Here's an example how I envision it:
I have two stations, Sluston (SLU) and Mudingworth (MUD). I create a bus that will go between those with passengers and name it "SLU-MUD (Ps) 1". This part is manual, I have to do this myself. Now, the stations start to fill up with passengers and I want to clone a couple extra busses. I clone them with shared orders, and will then get a "Road Vehicle 2" by default. On the next AI tick, this vehicle will be renamed to "SLU-MUD (Ps) 2" by help of the AI.

Yes, I agree it isn't the best way to do this through an AI, but it requires no patching.. Anyhow, if people think its not worth the trouble writing it in Squirrel, then I'll just not do it.

Cheers
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: RenamerAI and some Squirrel questions

Post by fanioz »

hempa wrote: I do not feel ......
Some people looks like to playing game with AI as their assistant (I'm used my AI to build road path for me :D )
Maybe If I can suggest you something to do with your AI :
1. List All Your Road Vehicle
2. Valuate the vehicle that have not renamed yet (filter by length of name, or check if the name contains '-', mean it has been renamed)
3. Foreach vehicle :
a.Check their orders , you get their station tile and also the name
b. use .slice(1,4) and you get the first 3 char of station name, uppercase it with .toupper()
c. do the same for every order (a vehicle at least has 2 order, right?)
d. Use AICargo.GetCargoLabel() - you'll get 'PASS' instead of '(Ps)'
Yexo wrote:
Please mind that your assumption that unnamed vehicles will start with "Road Vehicle #" holds only when you're playing in english.
e. use .slice(14) as Yexo explained above and you get number of your vehicle (in english) OR use ' text.slice(text.len()-2)' to make sure you only got 2 last character (if the number is only one digit then the first char is space) :idea: the problem is when your vehicle reach 100 :D
f. Now you can concatenate them all in format "<station>-<station> (<cargo>) <number>" ==> "SLU-MUD (PASS) 9"

:: You can set the routine to work for every xxx tick. Thus, you can just leave all of your road vehicle un-renamed. The AI will do it for you
:: AFAIK, all commands are available in squirrel and NoAI API

I'm sorry if this is not what you really want.. :lol:
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: RenamerAI and some Squirrel questions

Post by Zuu »

The AI might run when you just has built a new engine, but not gave any orders to it. So the AI need to skip all vehicles with less than two orders.

You need to put some thoughts into what orders are required. Perhaps don't count depot orders, and maybe even also exclude go via orders.


But you formulated the required steps very well otherwise.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
hempa
Engineer
Engineer
Posts: 21
Joined: 15 Oct 2008 16:30

Re: RenamerAI and some Squirrel questions

Post by hempa »

Thanks for all the input, there are some ideas here that I hadn't even thought of. I will surely try them out as I go.

I will explore the API when it comes to AIOrder, cause using the orders seem to be a smart plan. There are, of course, reason to put in strict conditions on when the AI can rename my vehicles, and care must be taken if there are more than 2 orders. I quite often put in service orders after unloading cargo, or even refit orders.

For starters, I think I'll make it pick the first and last station in the orders list and use those as the names, grab the cargo type, and add a number. Slice should work well there.

Has anyone encountered the same crashes I get with AIVehicleList_SharedOrders as described in the first post?
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: RenamerAI and some Squirrel questions

Post by Zuu »

About the crash, do you use that code inside a Valuator? (if you haven't yet learned what a valuator is, then you probably don't) When I use valuators, especially my custom class valuator function, doing the wrong thing can crash OpenTTD.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: RenamerAI and some Squirrel questions

Post by Yexo »

hempa wrote:Has anyone encountered the same crashes I get with AIVehicleList_SharedOrders as described in the first post?
Confirmed and fixed in r15587.
Zuu wrote:About the crash, do you use that code inside a Valuator? (if you haven't yet learned what a valuator is, then you probably don't) When I use valuators, especially my custom class valuator function, doing the wrong thing can crash OpenTTD.
Do you have example code that causes crashes?
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: RenamerAI and some Squirrel questions

Post by Zuu »

Zuu wrote:About the crash, do you use that code inside a Valuator? (if you haven't yet learned what a valuator is, then you probably don't) When I use valuators, especially my custom class valuator function, doing the wrong thing can crash OpenTTD.
Do you have example code that causes crashes?[/quote]

Not right now. Last time it happened when I wanted to add some debug placement of signs to a function, that I then realized was a valuator function. So I changed the valuate call to use my squirrel class valuator function (it take the class as parameter on top of function and arguments), but it still crashed. I worked around that problem by making a for loop over all tiles in a city elsewhere and print the debug information there. I realize now that It might be that I had two levels of valuator functions, where I never made sure that the outermost uses the squirrel valuate function. (I use the API valuator side by side with my own one.)

Whenever I run into the problem again I can dig a bit into it and see if I have an outer level of valuators and post it if I can't resolve it. But right now I don't have an example code.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
hempa
Engineer
Engineer
Posts: 21
Joined: 15 Oct 2008 16:30

Re: RenamerAI and some Squirrel questions

Post by hempa »

Yexo wrote:
Hempa wrote: Has anyone encountered the same crashes I get with AIVehicleList_SharedOrders as described in the first post?
Confirmed and fixed in r15587.
Thanks, I'll update and try that again.
Zuu wrote:About the crash, do you use that code inside a Valuator? (if you haven't yet learned what a valuator is, then you probably don't) When I use valuators, especially my custom class valuator function, doing the wrong thing can crash OpenTTD.
Nope, I don't use valuators yet, in fact I really don't know how they work heh heh. I will be sure to read into that.

Will put in some more work on this today, and see what I can come up with.

Cheers
hempa
Engineer
Engineer
Posts: 21
Joined: 15 Oct 2008 16:30

Re: RenamerAI and some Squirrel questions

Post by hempa »

:bow: Thank you peter1138 for uploading a patch to the svn doing just what I wanted it to! It works like a charm!

-Hempa
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 22 guests