AIOrder.SetOrderFlags operation - problem fixed.
Moderator: OpenTTD Developers
AIOrder.SetOrderFlags operation - problem fixed.
I am trying to use the AIOrder.SetOrderFlags function. It's operation seems a bit strange.
I have an order to a stop (Full load any cargo) initially added with AIOF_FULL_LOAD_ANY.
I want to change this to (Unload and leave empty) with SetOrderFlags(AIOF_UNLOAD | AIOF_NO_LOAD).
When I do this, I get (Unload and wait for any full load).
If I execute the same call a second time, I get the desired (Unload and leave empty)
Things are even stranger if I set just one order at a time:
Setting AIOF_FULL_LOAD_ANY on (Unload and leave empty) produces (No loading)
then setting AIOF_UNLOAD on (No loading) produces (Unload and leave empty)
How is this function supposed to be used?
I have an order to a stop (Full load any cargo) initially added with AIOF_FULL_LOAD_ANY.
I want to change this to (Unload and leave empty) with SetOrderFlags(AIOF_UNLOAD | AIOF_NO_LOAD).
When I do this, I get (Unload and wait for any full load).
If I execute the same call a second time, I get the desired (Unload and leave empty)
Things are even stranger if I set just one order at a time:
Setting AIOF_FULL_LOAD_ANY on (Unload and leave empty) produces (No loading)
then setting AIOF_UNLOAD on (No loading) produces (Unload and leave empty)
How is this function supposed to be used?
Last edited by Attila7 on 14 Jul 2010 23:31, edited 1 time in total.
Attila
"Artificial intelligence is no match for natural stupidity."
"Artificial intelligence is no match for natural stupidity."
Re: AIOrder.SetOrderFlags operation
IMO, AIOrderFlags are only bits set. You have to do bit operation to modify these flags.
Your question was still not clear enough. Are you modify existing flag order on a vehicle or just set a flag order on it. Perhaps, some line of code could explain what is in your mind.
Your question was still not clear enough. Are you modify existing flag order on a vehicle or just set a flag order on it. Perhaps, some line of code could explain what is in your mind.

Re: AIOrder.SetOrderFlags operation
Yes, I am trying to modify an existing order that has a vehicle going to a station with orders (Full load any cargo) initially added with AIOF_FULL_LOAD_ANY.
I then want to modify this order to be (Unload and leave empty).
I use a single line of code: AIOrder.SetOrderFlags(id,idx,AIOrder.AIOF_UNLOAD | AIOrder.AIOF_NO_LOAD);
When I do this, I get (Unload and wait for any full load) instead of what I want. Executing the above statement a second time results in the desired order, however I don't understand the logic behind this.
I guess I will check the source code and see what is going on. I would expect that this function should replace the flag bits, but it obviously does some bit manipulation instead.
I then want to modify this order to be (Unload and leave empty).
I use a single line of code: AIOrder.SetOrderFlags(id,idx,AIOrder.AIOF_UNLOAD | AIOrder.AIOF_NO_LOAD);
When I do this, I get (Unload and wait for any full load) instead of what I want. Executing the above statement a second time results in the desired order, however I don't understand the logic behind this.
I guess I will check the source code and see what is going on. I would expect that this function should replace the flag bits, but it obviously does some bit manipulation instead.
Attila
"Artificial intelligence is no match for natural stupidity."
"Artificial intelligence is no match for natural stupidity."
Re: AIOrder.SetOrderFlags operation
You can set AIOF_NONE before further messing with flags.
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
AIAI - AI for OpenTTD
Re: AIOrder.SetOrderFlags operation
This does not work either!Kogut wrote:You can set AIOF_NONE before further messing with flags.
Setting AIOF_NONE on an order that had AIOF_NON_STOP_INTERMEDIATE | AIOrder.AIOF_FULL_LOAD_ANY (Go non-stop xxx (Full load any cargo)) results in (Full load any cargo).
I looked at the source code and it appears rather complicated and its logic is not apparent. However there is the following comment in there:
Code: Select all
* Callback handler as SetOrderFlags possibly needs multiple DoCommand calls
* to be able to set all order flags correctly. As we need to wait till the
* command has completed before we know the next bits to change we need to
* call the function multiple times. Each time it'll reduce the difference
* between the wanted and the current order.
I think an AI script should only have to set the flag bits once and the internal code should take care of doing multiple commands to achieve the desired result or anyone trying to modify orders will be in for a lot of trial and error experimenting and code that seems to be redundant.
Attila
"Artificial intelligence is no match for natural stupidity."
"Artificial intelligence is no match for natural stupidity."
Re: AIOrder.SetOrderFlags operation
Code: Select all
local flag = AIOrder.GetOrderFlags(vhc, number);
//turn of flag
flag = flag & ~AIOrder.AIOF_FULL_LOAD_ANY;
//set new flag
flag = flag | AIOrder.AIOF_UNLOAD | AIOrder.AIOF_NO_LOAD;
//set on vehicle
AIOrder.SetOrderFlags(vhc, number, flag);
Re: AIOrder.SetOrderFlags operation
What you are essentially doing is resetting the current flags (not in the game), so your flag variable becomes zero, which you then OR with the desired flags. So, you are doing the same thing I am trying to do, except with far more instructions.
The bottom line is that when you call AIOrder.SetOrderFlags with some bits, the function returns True, when in fact it did not do what you want.
The more I look at this, the more I think this is clearly a bug.
The bottom line is that when you call AIOrder.SetOrderFlags with some bits, the function returns True, when in fact it did not do what you want.
The more I look at this, the more I think this is clearly a bug.
Attila
"Artificial intelligence is no match for natural stupidity."
"Artificial intelligence is no match for natural stupidity."
Re: AIOrder.SetOrderFlags operation
what if ....
or
* not tested yet
Code: Select all
local flag = AIOrder.GetOrderFlags(vhc, number);
//turn of flag
flag = flag & ~AIOrder.AIOF_FULL_LOAD_ANY;
//set on vehicle
AIOrder.SetOrderFlags(vhc, number, flag);
//set new flag
flag = AIOrder.AIOF_UNLOAD | AIOrder.AIOF_NO_LOAD;
//set on vehicle
AIOrder.SetOrderFlags(vhc, number, flag);
Code: Select all
local flag = AIOrder.GetOrderFlags(vhc, number);
//set new flag
flag = AIOrder.AIOF_UNLOAD | AIOrder.AIOF_NO_LOAD & ~AIOrder.AIOF_FULL_LOAD_ANY;
//set on vehicle
AIOrder.SetOrderFlags(vhc, number, flag);
Re: AIOrder.SetOrderFlags operation
I have tried to reproduce your behaviour in trunk but I cannot reproduce it. If I add two flags to a SetOrderFlags it sets both flags and it looks fine in-game. What version of OpenTTD are you using that has this (bad) behaviour?
Re: AIOrder.SetOrderFlags operation
Thanks for testing this for me. It appears the bug was in my code as it was not executing the orders I thought it was doing.
Sorry for the wild-goose chase.
Sorry for the wild-goose chase.
Attila
"Artificial intelligence is no match for natural stupidity."
"Artificial intelligence is no match for natural stupidity."
Who is online
Users browsing this forum: No registered users and 7 guests