Sunday, December 12, 2021

Procedural Cities: More Power

I'm continuing to work on power lines connecting the buildings in my procedural cities. This is a follow up to the previous post where I first introduced power poles in residential cities. This time around I've improved the graphics, fixed wires intersecting buildings, added power poles and substations to cities, and connected cities together with transmission lines.

The graphics improvements are relatively minor. I replaced the simple truncated cone insulating standoffs used for wires with more complex accordion-like cone segments I see in reference images.

Power Lines to Houses

The screenshots in my last post showed some power wires connected from the lines near poles directly to the roofs of houses. I set the connection point as the point closest to the road on the highest part of the roof. That didn't look quite right, so I added those vertical metal conduits that protect the wires running into houses. This also avoids having wires clip through the edges of solar panels placed on the same section of roof as the wire start point.

I fixed the problem of wires sometimes clipping through other parts of the house or adjacent houses. The previous placement algorithm always connected each house to the nearest power pole. Now I do an intersection check of each wire with all nearby buildings, including the building the wire connects to. If an intersection is found, the next closest pole is chosen, until there are none left to try in the block the house resides in. Most of the time the closest (first) pole is valid and is selected. Occasionally this can still fail, but there were only around 20 failures out of the ~3000 connected houses.

Power connection from the roof of a house to the wires near a power pole. (Also, my above ground swimming pools now have ladders.)

Commercial Cities

As stated in the last post, most commercial office buildings are connected to the power grid through underground line rather than overhead wires. I do like the look of the power poles though, and they still need to provide power to city streetlights. I decided to add power poles and power lines to commercial cities, but have the building power routed into the ground through conduits. This is what I see in the office buildings and stores around my house in real life.

In addition, I found a "power substation" model that I can place on the concrete near the sidewalk on some blocks. This is the big metal transformer box with cooling fins that you tend to find behind buildings and on the sides of parking lots. These are placed when there's room for them. Here is what a power substation looks like placed in one of my cities.

Power substation/transformer next to the sidewalk.

Transmission Lines

I added transmission lines that connect the power grids of cities to their neighbors. Each line connects a pair of cities so that the entire set of cities is fully connected in a cycle free graph. This requires placing (num_cities - 1) transmission lines. I start with the closest two cities, connect them, then continue to add the closest city to the connected set until there are none left. In theory, it's possible that some connections fail and not all cities can be connected to the grid, but I haven't seen that happen yet.

I borrowed some of the path placement and cost estimate code from the city connector road placement algorithm. The placer will consider several points along the edges and corners of cities, typically within the pair of shared projecting edges of the two cities (if there is one). Otherwise, it will try to connect the nearest pair of corners to each other. A cost is calculated for each candidate, and the lowest cost legal path is chosen. Each connection is formed by a series of regularly spaced towers that follow the contour/elevation of the terrain, with three wires connected to insulating standoffs on each tower. Cost includes both the length and elevation change, similar to road placement.

If the terrain is very steep, the placer will try to insert additional towers that follow the terrain height to keep the wires from getting too close to the ground below. If this still isn't enough to fix the problem, the placement fails and another candidate path is chosen. If none of the paths are valid, these two cities are considered unconnectable, and a different pair of cities is chosen instead. This continues until each city has been connected to the grid, or all pairs of cities have been considered.

Transmission line with three wires on insulating standoffs, connecting power between two cities.


Transmission line routing is generally more likely to succeed than road placement because they can climb steeper terrain and aren't required to run in X (east-west) or Y (north-south) directions. Lines can cut across the terrain diagonally and can have any angle jogs to connect to the city power poles at their endpoints.

Next I had to ensure there were no trees or secondary buildings under or intersecting the transmission lines. The check for trees was actually quite difficult, and required various significant changes to the tree placement code. I also had to look up and implement some new math functions such as point-line segment distance calculations. In the end it worked well though, and gave me nice clear paths around the transmission lines like this.

Trees and other large plants have been cleared from the transmission line right of way.

Here's what a transmission line looks like when climbing over a mountain. In this case, the mountain range runs between the two cities with no convenient pass between them, so the only way to connect the cities is through the mountains. There's another mountain range on the other side of the city that blocks the power connection to its opposite neighbor. The placement algorithm examines multiple potential candidates before selecting this one.

Transmission lines are allowed to climb steep hills if there's no lower cost path. Towers are spaced more closely together in steep areas to keep the wires away from the ground.

That's looking pretty good, but there's still some work to do. I have to decide what to do about secondary buildings near power lines. I have checks in place that skip these buildings similar to how trees are skipped, but it doesn't look as good. The lines and towers are too close to houses, and the big gap with no buildings looks unrealistic. Plus it scrambles up the building placement so that I get randomly different buildings in the player's starting location every time I change the transmission line code. I feel the high-level building placement algorithm should somehow consider transmission lines when selecting building density or zoning in different areas.


  1. For the holiday is it okay if you could upload a new pre compiled build on github with the assets? The last one i compiled was back in July and i'm unsure if there have been changes to what visual studio works.

    1. Let me reply to your comment rather than the post. Please see the release:

  2. I'll look into it. I've switched to Visual Studio 2022, so I'm not sure what new files I need to package with the release. I also need to add more 3D model files. This is too large for GitHub, so I'll probably have to add it to Google Drive.

  3. Okay, I created a v1.2 release here:
    I also created a new textures + models zip file on Google Drive, with the path in the release notes. It's been a while so I'm not sure if I did everything correctly. Please let me know if you have any problems with this.

    1. Okay thanks man! Have a happy new year by the way!

    2. Thanks! You have a Happy New Year too!

    3. I sent a request for permission to download the model files from Gdrive.

    4. I shared that file with you. Let me know if you have any problems.

    5. Thanks everything is working great so far. The updated town generation is nice and the powerlines give off some nice details. Still this program has the most detailed interior gen i've seen yet and it's only getting better

      I do have 2 questions for you for the upcoming year. Have you though about experimenting with generating interiors for the commercial skyscrapers anytime soon? And also making curved roads possible for town generation?

    6. I'm glad to hear you have it working. I'm not aware of any other games/engines that procedurally generate that many building interiors. The cube-based commercial buildings outside the cities with brick and concrete block walls have interiors. Cube-based commercial buildings inside cities have limited interiors but not windows that you can see through. This is because I can't find a nice way to line up the windows in the office building textures with the interior rooms/windows. I haven't tried generating interiors for the curved buildings or buildings with non-90 degree angle sides because it's much more difficult. Right now I only generate rectangular rooms and axis aligned walls (though the buildings themselves can be arbitrarily rotated).

      Curved roads are difficult for several reasons. First, I can't find any good road textures that would tile/join properly to create curved roads with correct lines on them. Second, flattening the terrain under curved roads doesn't work well due to the XY grid nature, so you end up with jagged angles in the terrain. Third, the car AI is much more difficult because I have to model things like gradual turns. And fourth it would be less efficient for drawing and queries because I would have to represent the roads as many short segments. Maybe some day!

  4. Cool to see the transmission lines go in! The clearance zone under the lines is smaller than I think is normal, at least here in The States. Often the area under the lines is leased out, especially where they pass over roads, but that's another crazy corner case that probably won't be worth the trouble for the small improvement.
    There are also generally step-up sub-stations where the long distance transmission lines connect to the city, so there are less losses on the long-distance runs. Same thing at generator facilities, if you get that far. Pretty big visual improvement for just one infrastructure addition though!

    1. I increased the transmission line spacing a bit since I took those screenshots. I was previously using the distance to the tree trunk, and now it's the distance to the tree's bounding cylinder.

      I know I'm missing the substations. I haven't gotten that far, and I'm not sure exactly how to place them that looks correct. Right now the transmission lines simply connect two local city power grids. In reality there would be generators that connected to all the transmission lines, forming a tree. Then you would have a substation between each city grid and the nearest transmission line. I can't make a realistic global power grid without having generators somewhere. Maybe that's future work. For now, the transmission lines seem to be better than nothing.

    2. Certainly better than nothing!
      To add to the complexity, by the time you have skyscrapers downtown, several municipalities have merged together, but all the historic infrastructure remains. So there are usually multiple sub-stations throughout the city, that get surrounded as the city grows.