Cars

Learn here how to create and use plugins.

Moderator: Plugin Moderators

User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Cars

#1

Post 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 28458 times
So our actual image carsample.png may look like this:
image.png
image.png (1007 Bytes) Viewed 28458 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 28458 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 28458 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
Last edited by CommanderABab on 29 Nov 2017, 10:24, edited 2 times in total.

User avatar
Sometheotown mayor
Has uncountable inhabitants
Reactions:
Posts: 1548
Joined: 28 Jan 2017, 21:37
Location: Somewhere in the middle of russia
Plugins: Showcase Store

Platform

#2

Post by Sometheotown mayor »

can i get the TV car id please

User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

#3

Post by Lobby »

It's just $cartv00 :)

User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

#4

Post 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

User avatar
KINGTUT10101
1,000,000 inhabitants
Reactions:
Posts: 2220
Joined: 07 Jul 2016, 22:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

#5

Post by KINGTUT10101 »

Do you need to replace existing cars or can we add custom ones with new ids?

User avatar
CommanderABab
AB
Reactions:
Posts: 11086
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

#6

Post by CommanderABab »

You may create new ones with new ids.
Last edited by CommanderABab on 08 Jun 2017, 08:16, edited 1 time in total.

User avatar
KINGTUT10101
1,000,000 inhabitants
Reactions:
Posts: 2220
Joined: 07 Jul 2016, 22:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

#7

Post by KINGTUT10101 »

Is there a way to make cars not despawn?

User avatar
CommanderABab
AB
Reactions:
Posts: 11086
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

#8

Post by CommanderABab »

Maybe give them a high capacity.

User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

#9

Post 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.

User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Cars

#10

Post 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

User avatar
CommanderABab
AB
Reactions:
Posts: 11086
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Cars

#11

Post by CommanderABab »

Nice!

User avatar
Barky
TheoTown Cartographer
Reactions:
Posts: 2056
Joined: 17 Apr 2017, 22:13
Location: Ireland
Plugins: Showcase Store

Platform

Re: Cars

#12

Post by Barky »

Hmm maybe I'll try have a go with my plug-in again

User avatar
CommanderABab
AB
Reactions:
Posts: 11086
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Cars

#13

Post 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":[""]
  }
]

User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Cars

#14

Post by Lobby »

Exactly :)

User avatar
actemendes
Metropolitan
Reactions:
Posts: 131
Joined: 20 Jan 2017, 16:53
Location: Russia
Plugins: Showcase Store

Platform

Re: Cars

#15

Post 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?

User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Cars

#16

Post by Lobby »

Here some graphics:
yacht_green.png
yacht_green.png (993 Bytes) Viewed 36252 times
yacht_blue.png
yacht_blue.png (967 Bytes) Viewed 36252 times
ship.png
ship.png (5.67 KiB) Viewed 36252 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.

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Cars

#17

Post by Hadestia »

how to make a decoration that place in water?

User avatar
Kulche
Pluche
Reactions:
Posts: 1141
Joined: 07 Jun 2017, 20:28
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Cars

#18

Post by Kulche »

Simply write

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

8-)

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Cars

#19

Post by Hadestia »

Kulche wrote:
27 Jul 2017, 08:17
Simply write

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

8-)
T you

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Cars

#20

Post 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?

Post Reply Previous topicNext topic

Return to “Tutorials and Documentation”