Page 1 of 1

Clone vehicle multiple times

Posted: 05 Jan 2007 15:02
by nlmark
I just created my first patch, which allows the player to clone a vehicle multiple times (AFAIK this wasn't possible yet?). I'm not really new to programming in general, but I am to the OpenTTD source code, so I'm not really sure if I adhered to your standards properly.

Please check out the code and tell me what you think.

Some questions:
- How does the Window custom data container work? What I've done now is used a global scope variable to hold the number of clones. I did use a window data thingie, but I copied it from some other window's code and I'm not really sure how it works.
- What's the difference between window number and window classes?
- I added a string to the English language file. If this patch proves useful and later gets incorporated into the main or MiniIN branch, how does the translating to other languages occur?

Edit:
I forgot to add: use it by Ctrl+clicking on the 'Clone vehicles' button in a depot.

Posted: 05 Jan 2007 18:20
by MeusH
Looks great :)
I'd suggest you to change tabs between WC_CLONE_AMOUNT and = 0x5A into spaces, because we use tabs to align beginning of line, then we use spaces to align things in mid-line

Code: Select all

WC_CLONE_AMOUNT				= 0x5A
should be

Code: Select all

WC_CLONE_AMOUNT             = 0x5A

Posted: 06 Jan 2007 01:17
by nlmark
Thanks :)

I changed the spaces into tabs, and added new functionality: you can now distribute the first orders of the newly created clones evenly (so you don't get one big line of vehicles all moving to the same point).

See attached .diff and screenie.

Posted: 06 Jan 2007 01:58
by Sacro
:o WOW...that'd be so useful for aircraft distributing

Posted: 06 Jan 2007 02:30
by l_Blue_l
Thats so cool and Welcome to OpenTTD.

I think the new window could be incorporated into the existing depot window. The deport window could be an expandable window like the Station window for 'ratings'. When you click the 'Clone vehicles' button it could expand the window showing all the options.

Also I think there should also be a click able option for 'Clone vehicle and share orders' which is currently Ctrl-Click when cloning included with the other options.

edit, on a side note its common practice to include the revision number the Diff / patch was made on in the file name.

Posted: 06 Jan 2007 13:50
by Ploes
Would it not be possible to have the "number" of verhicals as a text field not the < and > arrows.

Otherwise if you want 10 buses and you have 1. You still have to click something 9 times.
(Tho even as it is the ability to spreed the first order is an amazingly useful addition!!)

Posted: 06 Jan 2007 15:03
by XeryusTC
Ploes wrote:Would it not be possible to have the "number" of verhicals as a text field not the < and > arrows.

Otherwise if you want 10 buses and you have 1. You still have to click something 9 times.
Isn't it possible to click the number and then type a new number in the input box, this is the way it works in the patches window.

Posted: 06 Jan 2007 18:40
by Bjarni
How about making a horizontal scrollbar to set the number?

Also another issue (that nobody mentioned). Say somebody decides to clone 50 vehicles, then this patch will use DoCommandP() 50 times. Each time it sends a package to the server. If the number is added to the arguments and the command itself loops, then DoCommandP() will only be called once. This will ensure that network usage will be constant nomatter how many clones are made. Creating too many commands at once can have a negative effect on performance as the server needs to send all of them to each client.

This can be done like:

Code: Select all

/** Clone a vehicle. If it is a train, it will clone all the cars too
 * @param tile tile of the depot where the cloned vehicle is build
 * @param p1 the original vehicle's index
 * @param p2 bit 0   = shared orders, else copied orders
 *           bit 1-8 = number of vehicles to clone
 */
int32 CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
  uint16 counter = 0;
  uint16 max = GB(p2, 8, 1);
  bool share_order = HASBIT(p2, 0);
  int32 temp_cost = 0, cost = 0;

  for (; counter < max; counter++) {
    temp_cost = CloneOneVehicle(tile, flags, p1, share_order);
    if (CMD_ERROR(temp_cost) return temp_cost;
    cost += temp_cost;
  }
  return cost;
}
CloneOneVehicle() would then be the new name of what is currently called CmdCloneVehicle(). You will need to modify all the places where cloning is called. Also this code would need cleanup as it's written quickly in a web browser... it looks horribly, but it's the idea in it that's important.

Also it goes without saying that testing will be needed as well, since I didn't even try to compile this :wink:

Posted: 06 Jan 2007 19:57
by nlmark
I resolved the large number issue by creating a text input box that pops up as suggested (and v2 already contains Ctrl+click shortcuts for the arrows, which increases the number by 5). I fixed some bugs, but I still have one I haven't figured out yet: Ctrl+clicking 'Clone vehicles' thus popping up the extra window and then clicking the 'Clone vehicles' button again throws an assertion error

Code: Select all

openttd: window.c:297: FindWindowZPosition: Assertion `wz < _last_z_window' failed.
I'm still working on that.

@ Bjarni
I didn't know DoCommandP sends commands to the server (explains a lot though :P), so besides updating the CmdCloneVehicle() function I'll need to create a function like CmdGotoToOrderNumber() which replaces the functionality in CcCloneVehicle() for distributing orders (they are not sent through a DoCommandP() wrapper function so it won't work in multiplayer)

Posted: 06 Jan 2007 20:09
by nlmark
Fixed the bug :)

Posted: 06 Jan 2007 20:33
by fabca2
Ooogh...
in your printscreen, you have two (separate) popup windows for the same stuff (defining the number of copy) ?!
that's not really user friendly.

by the way, I loved the idea of extending the existing window, as l_Blue_l told you.

just my 2 cents.

Posted: 06 Jan 2007 20:57
by nlmark
Yeah, I'm gonna try to implement that.

Re: Clone vehicle multiple times

Posted: 28 Jul 2013 01:22
by hughht5
Hi
was this ever implemented? How do i install?

Re: Clone vehicle multiple times

Posted: 28 Jul 2013 02:36
by kamnet
hughht5 wrote:was this ever implemented? How do i install?
You must either patch the source code for OpenTTD that was used to create this patch, or you must update the code to use it with the latest source code of OpenTTD. After that, you will need to compile the source code for OpenTTD yourself. Any savegames or scenario created with this compiled copy of OpenTTD will not longer be compatible with any other copy of OpenTTD.