Page 1 of 8

Cars

Posted: 28 Apr 2017, 12:25
by Lobby
In this thread I would like to show you how to create your own cars and how to use them. We highly recommend that you've already read our basics about plugin creation.

1. Car definition
As usual, car plugins are defined by using a json file as textual description of your plugin.

A car plugin's json file can look as simple as this:

Code: Select all

[
  {
    "id":"$lobby_carplugin00",
    "type":"car",
    "frames":[
      {"bmp":"carsample.png","w":18,"h":12,"count":4}
    ],
    "v2":true
  }
]
You will notice that I provide 4 frames here. That's needed as we need a frame (image) for each direction in which the car can drive. We indexed the directions as followed:
image.png
image.png (3.11 KiB) Viewed 36285 times
So our actual image carsample.png may look like this:
image.png
image.png (1007 Bytes) Viewed 36285 times
Notice how we load 4 frames from a single image by providing the width and height of each frame and the number of frames we want to extract. The plugin loader will automatically load "count" many frames of the given width and height from left to right out of the provided image. You can provide multiple cars within a single plugin by just providing more frames. The total number of frames has to be a multiple of 4.

It's worth to mention that "v2":true indicates that we want to use the second generation of car loading. We recommend that as it's much easier to use and takes less expensive texture space. However, a lot of cars are still internally defined using the old system. The provided frames there have to look like that:
image.png
image.png (638 Bytes) Viewed 36285 times
The border of free space of each frame (size 32x16) as well as the index for each direction differ from the v2 approach. The space is needed here as it's used for positioning.

2. Spawn cars
Now assume we want our sample building "$sample.plugin.unique.id.res00" from our sample plugin to spawn cars from our car plugin "$lobby_carplugin00". We can do that by defining a car spawner with the following attributes:
  • cars - An array of car ids
  • radius - Radius for the cars to be spawned. Big radius are heavy on computation, so try to avoid them. To cover the whole map you might use a value like 512
  • count - Number of cars that should be spawned
  • targets - An array of building ids that should be targeted by spawned cars. If empty, any buildings will be targeted (default behavior). Entry null will represent the building in which the car spawner is defined (for convenience).
So our plugin may look like

Code: Select all

[
  {
    "id":"$sample.plugin.unique.id.res00",
    "type":"residential",
    "author":"Lobby & theotheoderich",
    "width":1,
    "height":1,
    "frames":[{"bmp":"sample_bmp.png"}],
    "smoke":[{"id":"$smoke07","x":13,"y":-14}],
    "level":1,
    "car spawner":[
      {
        "cars":["$lobby_carplugin00"],
        "radius":10,
        "count":5
      }
    ]
  }
]
Please notice that you have to define your car before using it in any other plugin.


3. Overriding existing cars
⚠Overriding cars was only needed in the past to add your own residential/industrial cars into the game. See this thread on how this should be done since version 437.

As shown in this post you can override existing cars in the game by using the same id. This works as plugins are loaded after internal objects so that the existing object will be overridden (in most cases, it doesn't work well for ground objects for example).

These ids are used for predefined cars:
  • $carres00
  • $carres01
  • $carres02
  • $carind00
  • $carind01
  • $carind02
  • $carfirebrigade00
  • $carpolice00
  • $carbus00
  • $carswat00
  • $tank00
  • $mltry_truck00
  • $carmedic00
  • $car_null00 (a car without graphics)

4. Reusing car frames from the game
As discussed in this thread we allow you to use our car graphics in your own plugins. However, you have to mention us (me and theo) as authors when distributing it. The easiest way to "steal" our car frames is by using the frame stealing feature which we also used for our people animation. You can do that by using the car ids provided in section 3.

Another way to get car frames is by directly copy them into your own graphics. Here we provide some of our car graphics:
image.png
image.png (22.64 KiB) Viewed 36285 times
You'll find the bus graphics here.

Diagonal cars
See here for details about how to extend the prior definition to support driving on diagonals, too: viewtopic.php?p=177072#p177072

Posted: 03 May 2017, 14:26
by Sometheotown mayor
can i get the TV car id please

Posted: 03 May 2017, 21:28
by Lobby
It's just $cartv00 :)

Posted: 31 May 2017, 00:19
by Lobby
Since version 296 we have a new optional attribute for cars: speed

It's a multiplier for the car speed (similar to the speed attribute of roads). Usage:

Code: Select all

  {
    "id":"$lobby_carplugin00",
    "type":"car",
    "frames":[
      {"bmp":"carsample.png","w":18,"h":12,"count":4}
    ],
    "v2":true,
    "speed":0.9
  }
Default value is 1.0

Posted: 08 Jun 2017, 02:24
by KINGTUT10101
Do you need to replace existing cars or can we add custom ones with new ids?

Posted: 08 Jun 2017, 05:05
by CommanderABab
You may create new ones with new ids.

Posted: 08 Jun 2017, 06:05
by KINGTUT10101
Is there a way to make cars not despawn?

Posted: 08 Jun 2017, 08:19
by CommanderABab
Maybe give them a high capacity.

Posted: 08 Jun 2017, 08:47
by Lobby
Cars spawned by a specific building will normally not despawn if they always find a way. Normals cars like $carres00 will despawn by design.

Re: Cars

Posted: 05 Jul 2017, 23:09
by Lobby
In case you used the car attribute in a plugin, you have to replace it with another code since version 312 (will be released right now).

Replace this code

Code: Select all

"car":"ped.dusko.1",
"car count":4,
"car radius":25
with

Code: Select all

"car spawner":[
  {
    "cars":["ped.dusko.1"],
    "count":4,
    "radius":25
  }
]
(any similarity to existing plugin code is pure coincidence)

If you want to mimic the car local only-attribute from before (only the defining building will be targeted by cars) add the following into your car spawner:

Code: Select all

"targets":[""]
The changes we made were necessary in order to provide more flexible car spawning functionality. :space

Re: Cars

Posted: 05 Jul 2017, 23:10
by CommanderABab
Nice!

Re: Cars

Posted: 05 Jul 2017, 23:19
by Barky
Hmm maybe I'll try have a go with my plug-in again

Re: Cars

Posted: 05 Jul 2017, 23:45
by CommanderABab
QuestionI have a question in the comment below

Code: Select all

"car spawner":[
  {
    "cars":["ped.dusko.1"],
    "count":4,
    "radius":25,
    // do the targets go here?
    "targets":[""]
  }
]

Re: Cars

Posted: 06 Jul 2017, 09:36
by Lobby
Exactly :)

Re: Cars

Posted: 25 Jul 2017, 17:52
by actemendes
Probably this topic is most suitable for my question. I did not find an article about water transport. What code is needed for spawning ships and yachts? And please can I get their png texture?

Re: Cars

Posted: 25 Jul 2017, 18:59
by Lobby
Here some graphics:
yacht_green.png
yacht_green.png (993 Bytes) Viewed 44079 times
yacht_blue.png
yacht_blue.png (967 Bytes) Viewed 44079 times
ship.png
ship.png (5.67 KiB) Viewed 44079 times
As always, please give me and theo credit when you reuse it for a plugin. Don't use it outside of TheoTown without permission.

The json looks fairly simple. Use ship as type and provide at least 4 frames. For the yacht it may looks like

Code: Select all

[
  {
    "id":"$shipyacht00",
    "type":"ship",
    "frames":[
      {"bmp":"yacht_green.png","w":17,"h":15,"count":4,"handle x":8,"handle y":12},
      {"bmp":"yacht_blue.png","w":17,"h":15,"count":4,"handle x":8,"handle y":12}
    ]
  }
]
Here we provide 8 frames, 4 for each variant of the yacht. The game will pick a variant randomly when spawning a ship. Move frames using handle x and handle y until it looks good. Setting them correctly is difficult, so do it by trial and error.

If your ship is longer than one tile use

Code: Select all

"length":2,
to indicate this. However, the longer the ship the more likely are graphic issues. That's why we never added the long container ships.

Spawning ships from a building is fairly simple. It looks similar to car spawning before car spawners were added:

Code: Select all

"ships":["$shipyacht00"],
"ship count":4,
"ship radius":16,
"ship radius" is currently ignored. Use it for the future.

Re: Cars

Posted: 27 Jul 2017, 08:09
by Hadestia
how to make a decoration that place in water?

Re: Cars

Posted: 27 Jul 2017, 08:17
by Kulche
Simply write

"needs water":true,
"needs land":false,

8-)

Re: Cars

Posted: 27 Jul 2017, 10:08
by Hadestia
Kulche wrote:
27 Jul 2017, 08:17
Simply write

"needs water":true,
"needs land":false,

8-)
T you

Re: Cars

Posted: 30 Jul 2017, 15:06
by Hadestia
Lobby wrote:
25 Jul 2017, 18:59
Here some graphics:
yacht_green.pngyacht_blue.pngship.png
As always, please give me and theo credit when you reuse it for a plugin. Don't use it outside of TheoTown without permission.

The json looks fairly simple. Use ship as type and provide at least 4 frames. For the yacht it may looks like

Code: Select all

[
  {
    "id":"$shipyacht00",
    "type":"ship",
    "frames":[
      {"bmp":"yacht_green.png","w":17,"h":15,"count":4,"handle x":8,"handle y":12},
      {"bmp":"yacht_blue.png","w":17,"h":15,"count":4,"handle x":8,"handle y":12}
    ]
  }
]
Here we provide 8 frames, 4 for each variant of the yacht. The game will pick a variant randomly when spawning a ship. Move frames using handle x and handle y until it looks good. Setting them correctly is difficult, so do it by trial and error.

If your ship is longer than one tile use

Code: Select all

"length":2,
to indicate this. However, the longer the ship the more likely are graphic issues. That's why we never added the long container ships.

Spawning ships from a building is fairly simple. It looks similar to car spawning before car spawners were added:

Code: Select all

"ships":["$shipyacht00"],
"ship count":4,
"ship radius":16,
"ship radius" is currently ignored. Use it for the future.


How to make customize ship?