2019-01-11 19:08:04 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2019-01-21 19:12:06 +08:00
|
|
|
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
|
2019-01-11 19:08:04 +08:00
|
|
|
# Barcelona (UAB).
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the MIT license.
|
|
|
|
# For a copy, see <https://opensource.org/licenses/MIT>.
|
|
|
|
|
|
|
|
# Allows visualising a 2D map generated by vehicles.
|
|
|
|
|
|
|
|
"""
|
2019-02-07 02:40:39 +08:00
|
|
|
Welcome to CARLA No-Rendering Mode Visualizer
|
|
|
|
|
|
|
|
TAB : toggle hero mode
|
|
|
|
Mouse Wheel : zoom in / zoom out
|
|
|
|
Mouse Drag : move map (map mode only)
|
|
|
|
|
|
|
|
W : throttle
|
|
|
|
S : brake
|
|
|
|
AD : steer
|
|
|
|
Q : toggle reverse
|
|
|
|
Space : hand-brake
|
|
|
|
P : toggle autopilot
|
|
|
|
M : toggle manual transmission
|
|
|
|
,/. : gear up/down
|
|
|
|
|
|
|
|
F1 : toggle HUD
|
|
|
|
I : toggle actor ids
|
|
|
|
H/? : toggle help
|
|
|
|
ESC : quit
|
2019-01-11 19:08:04 +08:00
|
|
|
"""
|
|
|
|
|
2019-01-11 21:06:02 +08:00
|
|
|
# ==============================================================================
|
|
|
|
# -- find carla module ---------------------------------------------------------
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
import glob
|
|
|
|
import os
|
2019-01-11 19:08:04 +08:00
|
|
|
import sys
|
2019-01-11 21:06:02 +08:00
|
|
|
|
|
|
|
try:
|
2019-03-29 02:15:13 +08:00
|
|
|
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
|
2019-01-11 21:06:02 +08:00
|
|
|
sys.version_info.major,
|
|
|
|
sys.version_info.minor,
|
|
|
|
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
# -- imports -------------------------------------------------------------------
|
|
|
|
# ==============================================================================
|
2019-02-07 22:33:57 +08:00
|
|
|
|
2019-01-11 21:06:02 +08:00
|
|
|
import carla
|
2019-02-07 22:33:57 +08:00
|
|
|
from carla import TrafficLightState as tls
|
2019-01-11 21:06:02 +08:00
|
|
|
|
2019-01-11 19:08:04 +08:00
|
|
|
import argparse
|
2019-01-11 21:06:02 +08:00
|
|
|
import logging
|
2019-02-01 19:57:05 +08:00
|
|
|
import datetime
|
2019-01-15 01:44:58 +08:00
|
|
|
import weakref
|
2019-01-16 18:27:03 +08:00
|
|
|
import math
|
2019-01-17 22:46:46 +08:00
|
|
|
import random
|
2019-06-12 20:32:32 +08:00
|
|
|
import hashlib
|
2019-01-11 21:06:02 +08:00
|
|
|
|
2019-01-11 19:08:04 +08:00
|
|
|
try:
|
|
|
|
import pygame
|
2019-02-07 02:40:39 +08:00
|
|
|
from pygame.locals import KMOD_CTRL
|
2019-02-07 02:23:24 +08:00
|
|
|
from pygame.locals import KMOD_SHIFT
|
|
|
|
from pygame.locals import K_COMMA
|
|
|
|
from pygame.locals import K_DOWN
|
|
|
|
from pygame.locals import K_ESCAPE
|
2019-02-07 02:40:39 +08:00
|
|
|
from pygame.locals import K_F1
|
2019-02-07 02:23:24 +08:00
|
|
|
from pygame.locals import K_LEFT
|
|
|
|
from pygame.locals import K_PERIOD
|
|
|
|
from pygame.locals import K_RIGHT
|
|
|
|
from pygame.locals import K_SLASH
|
|
|
|
from pygame.locals import K_SPACE
|
2019-02-07 02:40:39 +08:00
|
|
|
from pygame.locals import K_TAB
|
2019-02-07 02:23:24 +08:00
|
|
|
from pygame.locals import K_UP
|
|
|
|
from pygame.locals import K_a
|
|
|
|
from pygame.locals import K_d
|
2019-01-17 02:32:39 +08:00
|
|
|
from pygame.locals import K_h
|
2019-01-21 23:41:43 +08:00
|
|
|
from pygame.locals import K_i
|
2019-02-05 17:14:10 +08:00
|
|
|
from pygame.locals import K_m
|
2019-02-05 17:19:15 +08:00
|
|
|
from pygame.locals import K_p
|
2019-02-07 02:23:24 +08:00
|
|
|
from pygame.locals import K_q
|
|
|
|
from pygame.locals import K_s
|
|
|
|
from pygame.locals import K_w
|
2019-01-11 19:08:04 +08:00
|
|
|
except ImportError:
|
|
|
|
raise RuntimeError('cannot import pygame, make sure pygame package is installed')
|
|
|
|
|
2019-01-17 00:54:50 +08:00
|
|
|
# ==============================================================================
|
2019-01-21 19:12:06 +08:00
|
|
|
# -- Constants -----------------------------------------------------------------
|
2019-01-17 00:54:50 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2019-01-21 19:12:06 +08:00
|
|
|
# Colors
|
2019-02-04 19:24:12 +08:00
|
|
|
|
|
|
|
# We will use the color palette used in Tango Desktop Project (Each color is indexed depending on brightness level)
|
|
|
|
# See: https://en.wikipedia.org/wiki/Tango_Desktop_Project
|
|
|
|
|
2019-02-04 19:33:23 +08:00
|
|
|
COLOR_BUTTER_0 = pygame.Color(252, 233, 79)
|
|
|
|
COLOR_BUTTER_1 = pygame.Color(237, 212, 0)
|
|
|
|
COLOR_BUTTER_2 = pygame.Color(196, 160, 0)
|
2019-02-04 19:24:12 +08:00
|
|
|
|
|
|
|
COLOR_ORANGE_0 = pygame.Color(252, 175, 62)
|
|
|
|
COLOR_ORANGE_1 = pygame.Color(245, 121, 0)
|
|
|
|
COLOR_ORANGE_2 = pygame.Color(209, 92, 0)
|
|
|
|
|
|
|
|
COLOR_CHOCOLATE_0 = pygame.Color(233, 185, 110)
|
|
|
|
COLOR_CHOCOLATE_1 = pygame.Color(193, 125, 17)
|
|
|
|
COLOR_CHOCOLATE_2 = pygame.Color(143, 89, 2)
|
|
|
|
|
2019-02-04 19:33:23 +08:00
|
|
|
COLOR_CHAMELEON_0 = pygame.Color(138, 226, 52)
|
|
|
|
COLOR_CHAMELEON_1 = pygame.Color(115, 210, 22)
|
|
|
|
COLOR_CHAMELEON_2 = pygame.Color(78, 154, 6)
|
2019-02-04 19:24:12 +08:00
|
|
|
|
2019-02-04 19:33:23 +08:00
|
|
|
COLOR_SKY_BLUE_0 = pygame.Color(114, 159, 207)
|
|
|
|
COLOR_SKY_BLUE_1 = pygame.Color(52, 101, 164)
|
|
|
|
COLOR_SKY_BLUE_2 = pygame.Color(32, 74, 135)
|
2019-02-04 19:24:12 +08:00
|
|
|
|
2019-02-04 19:33:23 +08:00
|
|
|
COLOR_PLUM_0 = pygame.Color(173, 127, 168)
|
|
|
|
COLOR_PLUM_1 = pygame.Color(117, 80, 123)
|
|
|
|
COLOR_PLUM_2 = pygame.Color(92, 53, 102)
|
2019-02-04 19:24:12 +08:00
|
|
|
|
|
|
|
COLOR_SCARLET_RED_0 = pygame.Color(239, 41, 41)
|
|
|
|
COLOR_SCARLET_RED_1 = pygame.Color(204, 0, 0)
|
|
|
|
COLOR_SCARLET_RED_2 = pygame.Color(164, 0, 0)
|
|
|
|
|
2019-02-04 19:33:23 +08:00
|
|
|
COLOR_ALUMINIUM_0 = pygame.Color(238, 238, 236)
|
|
|
|
COLOR_ALUMINIUM_1 = pygame.Color(211, 215, 207)
|
|
|
|
COLOR_ALUMINIUM_2 = pygame.Color(186, 189, 182)
|
|
|
|
COLOR_ALUMINIUM_3 = pygame.Color(136, 138, 133)
|
|
|
|
COLOR_ALUMINIUM_4 = pygame.Color(85, 87, 83)
|
2019-03-30 02:23:26 +08:00
|
|
|
COLOR_ALUMINIUM_4_5 = pygame.Color(66, 62, 64)
|
2019-02-04 19:33:23 +08:00
|
|
|
COLOR_ALUMINIUM_5 = pygame.Color(46, 52, 54)
|
2019-02-04 19:24:12 +08:00
|
|
|
|
|
|
|
|
2019-01-17 00:54:50 +08:00
|
|
|
COLOR_WHITE = pygame.Color(255, 255, 255)
|
|
|
|
COLOR_BLACK = pygame.Color(0, 0, 0)
|
|
|
|
|
2019-01-21 19:12:06 +08:00
|
|
|
# Module Defines
|
2019-06-11 17:20:02 +08:00
|
|
|
TITLE_WORLD = 'WORLD'
|
|
|
|
TITLE_HUD = 'HUD'
|
|
|
|
TITLE_INPUT = 'INPUT'
|
2019-01-14 18:49:29 +08:00
|
|
|
|
2019-02-27 00:46:46 +08:00
|
|
|
PIXELS_PER_METER = 12
|
2019-02-08 19:52:11 +08:00
|
|
|
|
2019-02-08 20:25:45 +08:00
|
|
|
MAP_DEFAULT_SCALE = 0.1
|
|
|
|
HERO_DEFAULT_SCALE = 1.0
|
2019-02-04 23:26:01 +08:00
|
|
|
|
|
|
|
PIXELS_AHEAD_VEHICLE = 150
|
2019-02-07 22:02:42 +08:00
|
|
|
|
2019-01-17 22:46:46 +08:00
|
|
|
# ==============================================================================
|
2019-02-07 22:02:42 +08:00
|
|
|
# -- Util -----------------------------------------------------------
|
2019-01-17 22:46:46 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
2019-02-07 22:53:51 +08:00
|
|
|
def get_actor_display_name(actor, truncate=250):
|
|
|
|
name = ' '.join(actor.type_id.replace('_', '.').title().split('.')[1:])
|
2019-02-21 22:30:16 +08:00
|
|
|
return (name[:truncate - 1] + u'\u2026') if len(name) > truncate else name
|
2019-02-07 22:53:51 +08:00
|
|
|
|
|
|
|
|
2019-01-30 19:15:24 +08:00
|
|
|
class Util(object):
|
2019-01-31 02:45:54 +08:00
|
|
|
|
2019-01-30 19:15:24 +08:00
|
|
|
@staticmethod
|
2019-02-06 00:34:29 +08:00
|
|
|
def blits(destination_surface, source_surfaces, rect=None, blend_mode=0):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Function that renders the all the source surfaces in a destination source"""
|
2019-02-06 00:34:29 +08:00
|
|
|
for surface in source_surfaces:
|
|
|
|
destination_surface.blit(surface[0], surface[1], rect, blend_mode)
|
2019-01-31 02:45:54 +08:00
|
|
|
|
2019-02-27 21:57:51 +08:00
|
|
|
@staticmethod
|
2019-02-04 19:05:58 +08:00
|
|
|
def length(v):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Returns the length of a vector"""
|
2019-02-28 21:14:50 +08:00
|
|
|
return math.sqrt(v.x**2 + v.y**2 + v.z**2)
|
2019-03-29 01:21:43 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_bounding_box(actor):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Gets the bounding box corners of an actor in world space"""
|
2019-03-29 01:21:43 +08:00
|
|
|
bb = actor.trigger_volume.extent
|
|
|
|
corners = [carla.Location(x=-bb.x, y=-bb.y),
|
|
|
|
carla.Location(x=bb.x, y=-bb.y),
|
|
|
|
carla.Location(x=bb.x, y=bb.y),
|
|
|
|
carla.Location(x=-bb.x, y=bb.y),
|
|
|
|
carla.Location(x=-bb.x, y=-bb.y)]
|
|
|
|
corners = [x + actor.trigger_volume.location for x in corners]
|
|
|
|
t = actor.get_transform()
|
|
|
|
t.transform(corners)
|
|
|
|
return corners
|
|
|
|
|
2019-02-07 02:23:24 +08:00
|
|
|
# ==============================================================================
|
|
|
|
# -- FadingText ----------------------------------------------------------------
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
class FadingText(object):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders texts that fades out after some seconds that the user specifies"""
|
|
|
|
|
2019-02-07 02:23:24 +08:00
|
|
|
def __init__(self, font, dim, pos):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Initializes variables such as text font, dimensions and position"""
|
2019-02-07 02:23:24 +08:00
|
|
|
self.font = font
|
|
|
|
self.dim = dim
|
|
|
|
self.pos = pos
|
|
|
|
self.seconds_left = 0
|
|
|
|
self.surface = pygame.Surface(self.dim)
|
|
|
|
|
2019-02-11 18:31:55 +08:00
|
|
|
def set_text(self, text, color=COLOR_WHITE, seconds=2.0):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Sets the text, color and seconds until fade out"""
|
2019-02-07 02:23:24 +08:00
|
|
|
text_texture = self.font.render(text, True, color)
|
|
|
|
self.surface = pygame.Surface(self.dim)
|
|
|
|
self.seconds_left = seconds
|
2019-02-11 18:31:55 +08:00
|
|
|
self.surface.fill(COLOR_BLACK)
|
2019-02-07 02:23:24 +08:00
|
|
|
self.surface.blit(text_texture, (10, 11))
|
|
|
|
|
|
|
|
def tick(self, clock):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Each frame, it shows the displayed text for some specified seconds, if any"""
|
2019-02-07 02:23:24 +08:00
|
|
|
delta_seconds = 1e-3 * clock.get_time()
|
|
|
|
self.seconds_left = max(0.0, self.seconds_left - delta_seconds)
|
|
|
|
self.surface.set_alpha(500.0 * self.seconds_left)
|
|
|
|
|
|
|
|
def render(self, display):
|
2019-09-17 00:15:36 +08:00
|
|
|
""" Renders the text in its surface and its position"""
|
2019-02-07 02:23:24 +08:00
|
|
|
display.blit(self.surface, self.pos)
|
|
|
|
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
# -- HelpText ------------------------------------------------------------------
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
class HelpText(object):
|
|
|
|
def __init__(self, font, width, height):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders the help text that shows the controls for using no rendering mode"""
|
2019-02-07 02:23:24 +08:00
|
|
|
lines = __doc__.split('\n')
|
|
|
|
self.font = font
|
|
|
|
self.dim = (680, len(lines) * 22 + 12)
|
|
|
|
self.pos = (0.5 * width - 0.5 * self.dim[0], 0.5 * height - 0.5 * self.dim[1])
|
|
|
|
self.seconds_left = 0
|
|
|
|
self.surface = pygame.Surface(self.dim)
|
2019-02-21 20:08:08 +08:00
|
|
|
self.surface.fill(COLOR_BLACK)
|
2019-02-07 02:23:24 +08:00
|
|
|
for n, line in enumerate(lines):
|
2019-02-21 20:08:08 +08:00
|
|
|
text_texture = self.font.render(line, True, COLOR_WHITE)
|
2019-02-07 02:23:24 +08:00
|
|
|
self.surface.blit(text_texture, (22, n * 22))
|
|
|
|
self._render = False
|
|
|
|
self.surface.set_alpha(220)
|
|
|
|
|
|
|
|
def toggle(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Toggles display of help text"""
|
2019-02-07 02:23:24 +08:00
|
|
|
self._render = not self._render
|
|
|
|
|
|
|
|
def render(self, display):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders the help text, if enabled"""
|
2019-02-07 02:23:24 +08:00
|
|
|
if self._render:
|
|
|
|
display.blit(self.surface, self.pos)
|
|
|
|
|
|
|
|
|
|
|
|
# ==============================================================================
|
2019-06-11 17:20:02 +08:00
|
|
|
# -- HUD -----------------------------------------------------------------
|
2019-02-07 02:23:24 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
class HUD (object):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Class encharged of rendering the HUD that displays information about the world and the hero vehicle"""
|
|
|
|
|
2019-01-15 01:44:58 +08:00
|
|
|
def __init__(self, name, width, height):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Initializes default HUD params and content data parameters that will be displayed"""
|
2019-01-23 00:23:46 +08:00
|
|
|
self.name = name
|
2019-02-07 02:23:24 +08:00
|
|
|
self.dim = (width, height)
|
2019-01-11 22:39:54 +08:00
|
|
|
self._init_hud_params()
|
2019-02-07 02:23:24 +08:00
|
|
|
self._init_data_params()
|
2019-01-11 22:39:54 +08:00
|
|
|
|
2019-01-16 00:17:10 +08:00
|
|
|
def start(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Does nothing since it does not need to use other modules"""
|
2019-01-16 00:17:10 +08:00
|
|
|
|
2019-01-11 22:39:54 +08:00
|
|
|
def _init_hud_params(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Initialized visual parameters such as font text and size"""
|
2019-10-28 19:02:00 +08:00
|
|
|
font_name = 'courier' if os.name == 'nt' else 'mono'
|
|
|
|
fonts = [x for x in pygame.font.get_fonts() if font_name in x]
|
2019-01-23 00:23:46 +08:00
|
|
|
default_font = 'ubuntumono'
|
|
|
|
mono = default_font if default_font in fonts else fonts[0]
|
|
|
|
mono = pygame.font.match_font(mono)
|
|
|
|
self._font_mono = pygame.font.Font(mono, 14)
|
2019-02-22 18:58:18 +08:00
|
|
|
self._header_font = pygame.font.SysFont('Arial', 14, True)
|
2019-02-07 02:23:24 +08:00
|
|
|
self.help = HelpText(pygame.font.Font(mono, 24), *self.dim)
|
|
|
|
self._notifications = FadingText(
|
|
|
|
pygame.font.Font(pygame.font.get_default_font(), 20),
|
|
|
|
(self.dim[0], 40), (0, self.dim[1] - 40))
|
2019-01-11 22:39:54 +08:00
|
|
|
|
2019-02-07 02:23:24 +08:00
|
|
|
def _init_data_params(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Initializes the content data structures"""
|
2019-01-31 02:09:30 +08:00
|
|
|
self.show_info = True
|
2019-02-07 01:50:57 +08:00
|
|
|
self.show_actor_ids = False
|
2019-01-23 00:23:46 +08:00
|
|
|
self._info_text = {}
|
2019-01-12 00:31:18 +08:00
|
|
|
|
2019-02-07 02:23:24 +08:00
|
|
|
def notification(self, text, seconds=2.0):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Shows fading texts for some specified seconds"""
|
2019-02-07 02:23:24 +08:00
|
|
|
self._notifications.set_text(text, seconds=seconds)
|
|
|
|
|
2019-01-11 22:39:54 +08:00
|
|
|
def tick(self, clock):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Updated the fading texts each frame"""
|
2019-02-07 02:23:24 +08:00
|
|
|
self._notifications.tick(clock)
|
2019-01-17 18:25:57 +08:00
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
def add_info(self, title, info):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Adds a block of information in the left HUD panel of the visualizer"""
|
2019-06-11 17:20:02 +08:00
|
|
|
self._info_text[title] = info
|
2019-01-11 22:39:54 +08:00
|
|
|
|
2019-02-26 00:38:35 +08:00
|
|
|
def render_vehicles_ids(self, vehicle_id_surface, list_actors, world_to_pixel, hero_actor, hero_transform):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""When flag enabled, it shows the IDs of the vehicles that are spawned in the world. Depending on the vehicle type,
|
|
|
|
it will render it in different colors"""
|
|
|
|
|
2019-01-29 23:07:54 +08:00
|
|
|
vehicle_id_surface.fill(COLOR_BLACK)
|
2019-02-07 01:50:57 +08:00
|
|
|
if self.show_actor_ids:
|
2019-01-25 02:29:05 +08:00
|
|
|
vehicle_id_surface.set_alpha(150)
|
2019-01-21 23:41:43 +08:00
|
|
|
for actor in list_actors:
|
2019-02-26 00:38:35 +08:00
|
|
|
x, y = world_to_pixel(actor[1].location)
|
2019-01-21 23:41:43 +08:00
|
|
|
|
2019-01-29 23:07:54 +08:00
|
|
|
angle = 0
|
2019-02-06 17:22:59 +08:00
|
|
|
if hero_actor is not None:
|
2019-02-26 00:38:35 +08:00
|
|
|
angle = -hero_transform.rotation.yaw - 90
|
2019-02-12 19:07:02 +08:00
|
|
|
|
2019-02-22 18:58:18 +08:00
|
|
|
color = COLOR_SKY_BLUE_0
|
2019-02-26 00:38:35 +08:00
|
|
|
if int(actor[0].attributes['number_of_wheels']) == 2:
|
2019-02-22 18:58:18 +08:00
|
|
|
color = COLOR_CHOCOLATE_0
|
2019-02-26 00:38:35 +08:00
|
|
|
if actor[0].attributes['role_name'] == 'hero':
|
2019-02-22 18:58:18 +08:00
|
|
|
color = COLOR_CHAMELEON_0
|
2019-02-12 19:07:02 +08:00
|
|
|
|
2019-02-26 00:38:35 +08:00
|
|
|
font_surface = self._header_font.render(str(actor[0].id), True, color)
|
2019-02-22 18:58:18 +08:00
|
|
|
rotated_font_surface = pygame.transform.rotate(font_surface, angle)
|
2019-02-21 22:30:16 +08:00
|
|
|
rect = rotated_font_surface.get_rect(center=(x, y))
|
2019-02-07 23:25:45 +08:00
|
|
|
vehicle_id_surface.blit(rotated_font_surface, rect)
|
2019-01-25 02:29:05 +08:00
|
|
|
|
|
|
|
return vehicle_id_surface
|
2019-01-21 23:41:43 +08:00
|
|
|
|
2019-01-11 22:39:54 +08:00
|
|
|
def render(self, display):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""If flag enabled, it renders all the information regarding the left panel of the visualizer"""
|
2019-01-31 02:09:30 +08:00
|
|
|
if self.show_info:
|
2019-01-29 03:21:52 +08:00
|
|
|
info_surface = pygame.Surface((240, self.dim[1]))
|
2019-01-11 22:39:54 +08:00
|
|
|
info_surface.set_alpha(100)
|
|
|
|
display.blit(info_surface, (0, 0))
|
2019-01-23 00:23:46 +08:00
|
|
|
v_offset = 4
|
|
|
|
bar_h_offset = 100
|
|
|
|
bar_width = 106
|
|
|
|
i = 0
|
2019-06-11 17:20:02 +08:00
|
|
|
for title, info in self._info_text.items():
|
|
|
|
if not info:
|
2019-02-07 23:51:30 +08:00
|
|
|
continue
|
2019-06-11 17:20:02 +08:00
|
|
|
surface = self._header_font.render(title, True, COLOR_ALUMINIUM_0).convert_alpha()
|
2019-01-17 18:25:57 +08:00
|
|
|
display.blit(surface, (8 + bar_width / 2, 18 * i + v_offset))
|
2019-02-07 23:51:30 +08:00
|
|
|
v_offset += 12
|
2019-01-17 18:25:57 +08:00
|
|
|
i += 1
|
2019-06-11 17:20:02 +08:00
|
|
|
for item in info:
|
2019-01-17 18:25:57 +08:00
|
|
|
if v_offset + 18 > self.dim[1]:
|
|
|
|
break
|
|
|
|
if isinstance(item, list):
|
|
|
|
if len(item) > 1:
|
2019-01-23 00:23:46 +08:00
|
|
|
points = [(x + 8, v_offset + 8 + (1.0 - y) * 30) for x, y in enumerate(item)]
|
2019-01-17 18:25:57 +08:00
|
|
|
pygame.draw.lines(display, (255, 136, 0), False, points, 2)
|
2019-01-23 00:23:46 +08:00
|
|
|
item = None
|
2019-01-17 18:25:57 +08:00
|
|
|
elif isinstance(item, tuple):
|
|
|
|
if isinstance(item[1], bool):
|
2019-01-23 00:23:46 +08:00
|
|
|
rect = pygame.Rect((bar_h_offset, v_offset + 8), (6, 6))
|
2019-02-04 19:24:12 +08:00
|
|
|
pygame.draw.rect(display, COLOR_ALUMINIUM_0, rect, 0 if item[1] else 1)
|
2019-01-11 22:39:54 +08:00
|
|
|
else:
|
2019-01-23 00:23:46 +08:00
|
|
|
rect_border = pygame.Rect((bar_h_offset, v_offset + 8), (bar_width, 6))
|
2019-02-04 19:24:12 +08:00
|
|
|
pygame.draw.rect(display, COLOR_ALUMINIUM_0, rect_border, 1)
|
2019-01-23 00:23:46 +08:00
|
|
|
f = (item[1] - item[2]) / (item[3] - item[2])
|
2019-01-17 18:25:57 +08:00
|
|
|
if item[2] < 0.0:
|
2019-01-23 00:23:46 +08:00
|
|
|
rect = pygame.Rect((bar_h_offset + f * (bar_width - 6), v_offset + 8), (6, 6))
|
2019-01-17 18:25:57 +08:00
|
|
|
else:
|
2019-01-23 00:23:46 +08:00
|
|
|
rect = pygame.Rect((bar_h_offset, v_offset + 8), (f * bar_width, 6))
|
2019-02-04 19:24:12 +08:00
|
|
|
pygame.draw.rect(display, COLOR_ALUMINIUM_0, rect)
|
2019-01-23 00:23:46 +08:00
|
|
|
item = item[0]
|
2019-01-17 18:25:57 +08:00
|
|
|
if item: # At this point has to be a str.
|
2019-02-04 19:24:12 +08:00
|
|
|
surface = self._font_mono.render(item, True, COLOR_ALUMINIUM_0).convert_alpha()
|
2019-01-17 18:25:57 +08:00
|
|
|
display.blit(surface, (8, 18 * i + v_offset))
|
|
|
|
v_offset += 18
|
2019-02-07 23:51:30 +08:00
|
|
|
v_offset += 24
|
2019-02-07 02:23:24 +08:00
|
|
|
self._notifications.render(display)
|
|
|
|
self.help.render(display)
|
2019-01-11 22:39:54 +08:00
|
|
|
|
2019-02-07 22:33:57 +08:00
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
# -- TrafficLightSurfaces ------------------------------------------------------
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
class TrafficLightSurfaces(object):
|
|
|
|
"""Holds the surfaces (scaled and rotated) for painting traffic lights"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
def make_surface(tl):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Draws a traffic light, which is composed of a dark background surface with 3 circles that indicate its color depending on the state"""
|
2019-02-07 22:33:57 +08:00
|
|
|
w = 40
|
|
|
|
surface = pygame.Surface((w, 3 * w), pygame.SRCALPHA)
|
2019-02-21 22:01:33 +08:00
|
|
|
surface.fill(COLOR_ALUMINIUM_5 if tl != 'h' else COLOR_ORANGE_2)
|
2019-02-07 22:33:57 +08:00
|
|
|
if tl != 'h':
|
2019-02-08 17:11:59 +08:00
|
|
|
hw = int(w / 2)
|
2019-02-21 22:01:33 +08:00
|
|
|
off = COLOR_ALUMINIUM_4
|
|
|
|
red = COLOR_SCARLET_RED_0
|
|
|
|
yellow = COLOR_BUTTER_0
|
|
|
|
green = COLOR_CHAMELEON_0
|
2019-09-17 00:15:36 +08:00
|
|
|
|
|
|
|
# Draws the corresponding color if is on, otherwise it will be gray if its off
|
2019-02-07 22:33:57 +08:00
|
|
|
pygame.draw.circle(surface, red if tl == tls.Red else off, (hw, hw), int(0.4 * w))
|
|
|
|
pygame.draw.circle(surface, yellow if tl == tls.Yellow else off, (hw, w + hw), int(0.4 * w))
|
|
|
|
pygame.draw.circle(surface, green if tl == tls.Green else off, (hw, 2 * w + hw), int(0.4 * w))
|
2019-09-17 00:15:36 +08:00
|
|
|
|
2019-02-07 22:33:57 +08:00
|
|
|
return pygame.transform.smoothscale(surface, (15, 45) if tl != 'h' else (19, 49))
|
2019-09-17 00:15:36 +08:00
|
|
|
|
2019-02-07 22:33:57 +08:00
|
|
|
self._original_surfaces = {
|
|
|
|
'h': make_surface('h'),
|
|
|
|
tls.Red: make_surface(tls.Red),
|
|
|
|
tls.Yellow: make_surface(tls.Yellow),
|
|
|
|
tls.Green: make_surface(tls.Green),
|
|
|
|
tls.Off: make_surface(tls.Off),
|
|
|
|
tls.Unknown: make_surface(tls.Unknown)
|
|
|
|
}
|
|
|
|
self.surfaces = dict(self._original_surfaces)
|
|
|
|
|
|
|
|
def rotozoom(self, angle, scale):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Rotates and scales the traffic light surface"""
|
2019-02-07 22:33:57 +08:00
|
|
|
for key, surface in self._original_surfaces.items():
|
|
|
|
self.surfaces[key] = pygame.transform.rotozoom(surface, angle, scale)
|
|
|
|
|
|
|
|
|
2019-01-12 00:31:18 +08:00
|
|
|
# ==============================================================================
|
|
|
|
# -- World ---------------------------------------------------------------------
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
class MapImage(object):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Class encharged of rendering a 2D image from top view of a carla world. Please note that a cache system is used, so if the OpenDrive content
|
|
|
|
of a Carla town has not changed, it will read and use the stored image if it was rendered in a previous execution"""
|
|
|
|
|
2019-04-01 22:36:12 +08:00
|
|
|
def __init__(self, carla_world, carla_map, pixels_per_meter, show_triggers, show_connections, show_spawn_points):
|
2019-09-17 00:15:36 +08:00
|
|
|
""" Renders the map image generated based on the world, its map and additional flags that provide extra information about the road network"""
|
2019-02-07 22:02:42 +08:00
|
|
|
self._pixels_per_meter = pixels_per_meter
|
|
|
|
self.scale = 1.0
|
2019-03-29 01:21:43 +08:00
|
|
|
self.show_triggers = show_triggers
|
2019-04-01 20:24:34 +08:00
|
|
|
self.show_connections = show_connections
|
2019-04-01 22:36:12 +08:00
|
|
|
self.show_spawn_points = show_spawn_points
|
2019-02-07 22:02:42 +08:00
|
|
|
|
|
|
|
waypoints = carla_map.generate_waypoints(2)
|
|
|
|
margin = 50
|
|
|
|
max_x = max(waypoints, key=lambda x: x.transform.location.x).transform.location.x + margin
|
|
|
|
max_y = max(waypoints, key=lambda x: x.transform.location.y).transform.location.y + margin
|
|
|
|
min_x = min(waypoints, key=lambda x: x.transform.location.x).transform.location.x - margin
|
|
|
|
min_y = min(waypoints, key=lambda x: x.transform.location.y).transform.location.y - margin
|
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
self.width = max(max_x - min_x, max_y - min_y)
|
2019-02-07 22:02:42 +08:00
|
|
|
self._world_offset = (min_x, min_y)
|
|
|
|
|
2019-05-22 23:31:44 +08:00
|
|
|
# Maximum size of a Pygame surface
|
|
|
|
width_in_pixels = (1 << 14) - 1
|
|
|
|
|
|
|
|
# Adapt Pixels per meter to make world fit in surface
|
|
|
|
surface_pixel_per_meter = int(width_in_pixels / self.width)
|
|
|
|
if surface_pixel_per_meter > PIXELS_PER_METER:
|
|
|
|
surface_pixel_per_meter = PIXELS_PER_METER
|
|
|
|
|
|
|
|
self._pixels_per_meter = surface_pixel_per_meter
|
2019-02-28 21:14:50 +08:00
|
|
|
width_in_pixels = int(self._pixels_per_meter * self.width)
|
2019-02-07 22:02:42 +08:00
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
self.big_map_surface = pygame.Surface((width_in_pixels, width_in_pixels)).convert()
|
2019-06-11 18:26:39 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Load OpenDrive content
|
2019-06-11 18:26:39 +08:00
|
|
|
opendrive_content = carla_map.to_opendrive()
|
2019-06-12 20:32:32 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Get hash based on content
|
2019-06-12 20:32:32 +08:00
|
|
|
hash_func = hashlib.sha1()
|
|
|
|
hash_func.update(opendrive_content.encode("UTF-8"))
|
|
|
|
opendrive_hash = str(hash_func.hexdigest())
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Build path for saving or loading the cached rendered map
|
2021-08-02 22:24:37 +08:00
|
|
|
filename = carla_map.name.split('/')[-1] + "_" + opendrive_hash + ".tga"
|
2019-09-17 00:15:36 +08:00
|
|
|
dirname = os.path.join("cache", "no_rendering_mode")
|
|
|
|
full_path = str(os.path.join(dirname, filename))
|
2019-06-12 20:32:32 +08:00
|
|
|
|
|
|
|
if os.path.isfile(full_path):
|
2019-06-11 18:26:39 +08:00
|
|
|
# Load Image
|
2019-06-12 20:32:32 +08:00
|
|
|
self.big_map_surface = pygame.image.load(full_path)
|
2019-06-11 18:26:39 +08:00
|
|
|
else:
|
|
|
|
# Render map
|
2019-09-17 00:15:36 +08:00
|
|
|
self.draw_road_map(
|
|
|
|
self.big_map_surface,
|
|
|
|
carla_world,
|
|
|
|
carla_map,
|
|
|
|
self.world_to_pixel,
|
|
|
|
self.world_to_pixel_width)
|
2019-06-11 18:26:39 +08:00
|
|
|
|
|
|
|
# If folders path does not exist, create it
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
|
|
|
|
|
|
|
# Remove files if selected town had a previous version saved
|
2019-09-17 00:15:36 +08:00
|
|
|
list_filenames = glob.glob(os.path.join(dirname, carla_map.name) + "*")
|
2019-06-11 18:26:39 +08:00
|
|
|
for town_filename in list_filenames:
|
|
|
|
os.remove(town_filename)
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Save rendered map for next executions of same map
|
2019-06-12 20:32:32 +08:00
|
|
|
pygame.image.save(self.big_map_surface, full_path)
|
2019-06-11 18:26:39 +08:00
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
self.surface = self.big_map_surface
|
2019-02-07 22:02:42 +08:00
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
def draw_road_map(self, map_surface, carla_world, carla_map, world_to_pixel, world_to_pixel_width):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Draws all the roads, including lane markings, arrows and traffic signs"""
|
2019-02-21 22:01:33 +08:00
|
|
|
map_surface.fill(COLOR_ALUMINIUM_4)
|
2019-03-30 01:02:15 +08:00
|
|
|
precision = 0.05
|
2019-02-07 22:02:42 +08:00
|
|
|
|
2019-03-30 19:27:47 +08:00
|
|
|
def lane_marking_color_to_tango(lane_marking_color):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Maps the lane marking color enum specified in PythonAPI to a Tango Color"""
|
2019-03-27 23:16:00 +08:00
|
|
|
tango_color = COLOR_BLACK
|
|
|
|
|
2019-03-30 19:27:47 +08:00
|
|
|
if lane_marking_color == carla.LaneMarkingColor.White:
|
2019-03-27 23:16:00 +08:00
|
|
|
tango_color = COLOR_ALUMINIUM_2
|
|
|
|
|
2019-03-30 19:27:47 +08:00
|
|
|
elif lane_marking_color == carla.LaneMarkingColor.Blue:
|
2019-03-27 23:16:00 +08:00
|
|
|
tango_color = COLOR_SKY_BLUE_0
|
|
|
|
|
2019-03-30 19:27:47 +08:00
|
|
|
elif lane_marking_color == carla.LaneMarkingColor.Green:
|
2019-03-27 23:16:00 +08:00
|
|
|
tango_color = COLOR_CHAMELEON_0
|
|
|
|
|
2019-03-30 19:27:47 +08:00
|
|
|
elif lane_marking_color == carla.LaneMarkingColor.Red:
|
2019-03-27 23:16:00 +08:00
|
|
|
tango_color = COLOR_SCARLET_RED_0
|
|
|
|
|
2019-03-30 19:27:47 +08:00
|
|
|
elif lane_marking_color == carla.LaneMarkingColor.Yellow:
|
2019-03-27 23:16:00 +08:00
|
|
|
tango_color = COLOR_ORANGE_0
|
|
|
|
|
|
|
|
return tango_color
|
|
|
|
|
2019-03-30 21:59:53 +08:00
|
|
|
def draw_solid_line(surface, color, closed, points, width):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Draws solid lines in a surface given a set of points, width and color"""
|
2019-03-27 23:16:00 +08:00
|
|
|
if len(points) >= 2:
|
|
|
|
pygame.draw.lines(surface, color, closed, points, width)
|
|
|
|
|
2019-03-30 21:59:53 +08:00
|
|
|
def draw_broken_line(surface, color, closed, points, width):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Draws broken lines in a surface given a set of points, width and color"""
|
|
|
|
# Select which lines are going to be rendered from the set of lines
|
2019-03-30 01:02:15 +08:00
|
|
|
broken_lines = [x for n, x in enumerate(zip(*(iter(points),) * 20)) if n % 3 == 0]
|
2019-09-17 00:15:36 +08:00
|
|
|
|
|
|
|
# Draw selected lines
|
2019-03-27 23:16:00 +08:00
|
|
|
for line in broken_lines:
|
|
|
|
pygame.draw.lines(surface, color, closed, line, width)
|
|
|
|
|
2019-03-30 21:59:53 +08:00
|
|
|
def get_lane_markings(lane_marking_type, lane_marking_color, waypoints, sign):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""For multiple lane marking types (SolidSolid, BrokenSolid, SolidBroken and BrokenBroken), it converts them
|
|
|
|
as a combination of Broken and Solid lines"""
|
2019-05-20 21:24:12 +08:00
|
|
|
margin = 0.25
|
|
|
|
marking_1 = [world_to_pixel(lateral_shift(w.transform, sign * w.lane_width * 0.5)) for w in waypoints]
|
2019-03-30 00:48:33 +08:00
|
|
|
if lane_marking_type == carla.LaneMarkingType.Broken or (lane_marking_type == carla.LaneMarkingType.Solid):
|
|
|
|
return [(lane_marking_type, lane_marking_color, marking_1)]
|
2019-05-20 21:24:12 +08:00
|
|
|
else:
|
2019-03-30 21:59:53 +08:00
|
|
|
marking_2 = [world_to_pixel(lateral_shift(w.transform,
|
|
|
|
sign * (w.lane_width * 0.5 + margin * 2))) for w in waypoints]
|
2019-05-20 21:24:12 +08:00
|
|
|
if lane_marking_type == carla.LaneMarkingType.SolidBroken:
|
|
|
|
return [(carla.LaneMarkingType.Broken, lane_marking_color, marking_1),
|
|
|
|
(carla.LaneMarkingType.Solid, lane_marking_color, marking_2)]
|
|
|
|
elif lane_marking_type == carla.LaneMarkingType.BrokenSolid:
|
|
|
|
return [(carla.LaneMarkingType.Solid, lane_marking_color, marking_1),
|
|
|
|
(carla.LaneMarkingType.Broken, lane_marking_color, marking_2)]
|
|
|
|
elif lane_marking_type == carla.LaneMarkingType.BrokenBroken:
|
|
|
|
return [(carla.LaneMarkingType.Broken, lane_marking_color, marking_1),
|
|
|
|
(carla.LaneMarkingType.Broken, lane_marking_color, marking_2)]
|
|
|
|
elif lane_marking_type == carla.LaneMarkingType.SolidSolid:
|
|
|
|
return [(carla.LaneMarkingType.Solid, lane_marking_color, marking_1),
|
|
|
|
(carla.LaneMarkingType.Solid, lane_marking_color, marking_2)]
|
2019-03-30 00:48:33 +08:00
|
|
|
|
2019-03-30 21:59:53 +08:00
|
|
|
return [(carla.LaneMarkingType.NONE, carla.LaneMarkingColor.Other, [])]
|
2019-03-30 00:48:33 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
def draw_lane(surface, lane, color):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders a single lane in a surface and with a specified color"""
|
2019-05-20 21:24:12 +08:00
|
|
|
for side in lane:
|
|
|
|
lane_left_side = [lateral_shift(w.transform, -w.lane_width * 0.5) for w in side]
|
|
|
|
lane_right_side = [lateral_shift(w.transform, w.lane_width * 0.5) for w in side]
|
|
|
|
|
|
|
|
polygon = lane_left_side + [x for x in reversed(lane_right_side)]
|
|
|
|
polygon = [world_to_pixel(x) for x in polygon]
|
|
|
|
|
|
|
|
if len(polygon) > 2:
|
|
|
|
pygame.draw.polygon(surface, color, polygon, 5)
|
|
|
|
pygame.draw.polygon(surface, color, polygon)
|
|
|
|
|
|
|
|
def draw_lane_marking(surface, waypoints):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Draws the left and right side of lane markings"""
|
2019-05-20 21:24:12 +08:00
|
|
|
# Left Side
|
|
|
|
draw_lane_marking_single_side(surface, waypoints[0], -1)
|
|
|
|
|
|
|
|
# Right Side
|
|
|
|
draw_lane_marking_single_side(surface, waypoints[1], 1)
|
|
|
|
|
|
|
|
def draw_lane_marking_single_side(surface, waypoints, sign):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Draws the lane marking given a set of waypoints and decides whether drawing the right or left side of
|
|
|
|
the waypoint based on the sign parameter"""
|
2019-03-30 19:27:47 +08:00
|
|
|
lane_marking = None
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-03-30 00:48:33 +08:00
|
|
|
marking_type = carla.LaneMarkingType.NONE
|
|
|
|
previous_marking_type = carla.LaneMarkingType.NONE
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-03-30 21:59:53 +08:00
|
|
|
marking_color = carla.LaneMarkingColor.Other
|
|
|
|
previous_marking_color = carla.LaneMarkingColor.Other
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
markings_list = []
|
2019-03-30 00:48:33 +08:00
|
|
|
temp_waypoints = []
|
|
|
|
current_lane_marking = carla.LaneMarkingType.NONE
|
|
|
|
for sample in waypoints:
|
|
|
|
lane_marking = sample.left_lane_marking if sign < 0 else sample.right_lane_marking
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-03-30 00:48:33 +08:00
|
|
|
if lane_marking is None:
|
2019-03-30 21:59:53 +08:00
|
|
|
continue
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-03-30 00:48:33 +08:00
|
|
|
marking_type = lane_marking.type
|
|
|
|
marking_color = lane_marking.color
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-03-30 00:48:33 +08:00
|
|
|
if current_lane_marking != marking_type:
|
2019-09-17 00:15:36 +08:00
|
|
|
# Get the list of lane markings to draw
|
2019-03-30 21:59:53 +08:00
|
|
|
markings = get_lane_markings(
|
|
|
|
previous_marking_type,
|
|
|
|
lane_marking_color_to_tango(previous_marking_color),
|
|
|
|
temp_waypoints,
|
|
|
|
sign)
|
2019-03-30 00:48:33 +08:00
|
|
|
current_lane_marking = marking_type
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Append each lane marking in the list
|
2019-03-30 00:48:33 +08:00
|
|
|
for marking in markings:
|
2019-05-20 21:24:12 +08:00
|
|
|
markings_list.append(marking)
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-03-30 00:48:33 +08:00
|
|
|
temp_waypoints = temp_waypoints[-1:]
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-03-30 00:48:33 +08:00
|
|
|
else:
|
|
|
|
temp_waypoints.append((sample))
|
|
|
|
previous_marking_type = marking_type
|
|
|
|
previous_marking_color = marking_color
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-03-30 00:48:33 +08:00
|
|
|
# Add last marking
|
2019-03-30 21:59:53 +08:00
|
|
|
last_markings = get_lane_markings(
|
|
|
|
previous_marking_type,
|
|
|
|
lane_marking_color_to_tango(previous_marking_color),
|
|
|
|
temp_waypoints,
|
|
|
|
sign)
|
2019-03-30 00:48:33 +08:00
|
|
|
for marking in last_markings:
|
2019-05-20 21:24:12 +08:00
|
|
|
markings_list.append(marking)
|
2019-03-27 23:16:00 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Once the lane markings have been simplified to Solid or Broken lines, we draw them
|
2019-05-20 21:24:12 +08:00
|
|
|
for markings in markings_list:
|
2019-03-30 00:48:33 +08:00
|
|
|
if markings[0] == carla.LaneMarkingType.Solid:
|
|
|
|
draw_solid_line(surface, markings[1], False, markings[2], 2)
|
|
|
|
elif markings[0] == carla.LaneMarkingType.Broken:
|
|
|
|
draw_broken_line(surface, markings[1], False, markings[2], 2)
|
2019-02-07 22:02:42 +08:00
|
|
|
|
2019-02-22 18:58:18 +08:00
|
|
|
def draw_arrow(surface, transform, color=COLOR_ALUMINIUM_2):
|
2019-09-17 00:15:36 +08:00
|
|
|
""" Draws an arrow with a specified color given a transform"""
|
2019-02-07 22:02:42 +08:00
|
|
|
transform.rotation.yaw += 180
|
|
|
|
forward = transform.get_forward_vector()
|
|
|
|
transform.rotation.yaw += 90
|
|
|
|
right_dir = transform.get_forward_vector()
|
2019-04-01 22:36:12 +08:00
|
|
|
end = transform.location
|
|
|
|
start = end - 2.0 * forward
|
2019-02-07 22:02:42 +08:00
|
|
|
right = start + 0.8 * forward + 0.4 * right_dir
|
|
|
|
left = start + 0.8 * forward - 0.4 * right_dir
|
2019-09-17 00:15:36 +08:00
|
|
|
|
|
|
|
# Draw lines
|
|
|
|
pygame.draw.lines(surface, color, False, [world_to_pixel(x) for x in [start, end]], 4)
|
|
|
|
pygame.draw.lines(surface, color, False, [world_to_pixel(x) for x in [left, start, right]], 4)
|
2019-02-07 22:02:42 +08:00
|
|
|
|
2019-04-01 21:41:37 +08:00
|
|
|
def draw_traffic_signs(surface, font_surface, actor, color=COLOR_ALUMINIUM_2, trigger_color=COLOR_PLUM_0):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Draw stop traffic signs and its bounding box if enabled"""
|
2019-03-29 01:21:43 +08:00
|
|
|
transform = actor.get_transform()
|
2019-02-28 21:14:50 +08:00
|
|
|
waypoint = carla_map.get_waypoint(transform.location)
|
|
|
|
|
|
|
|
angle = -waypoint.transform.rotation.yaw - 90.0
|
|
|
|
font_surface = pygame.transform.rotate(font_surface, angle)
|
|
|
|
pixel_pos = world_to_pixel(waypoint.transform.location)
|
|
|
|
offset = font_surface.get_rect(center=(pixel_pos[0], pixel_pos[1]))
|
|
|
|
surface.blit(font_surface, offset)
|
|
|
|
|
|
|
|
# Draw line in front of stop
|
|
|
|
forward_vector = carla.Location(waypoint.transform.get_forward_vector())
|
2019-02-04 19:05:58 +08:00
|
|
|
left_vector = carla.Location(-forward_vector.y, forward_vector.x,
|
|
|
|
forward_vector.z) * waypoint.lane_width / 2 * 0.7
|
2019-02-28 21:14:50 +08:00
|
|
|
|
|
|
|
line = [(waypoint.transform.location + (forward_vector * 1.5) + (left_vector)),
|
|
|
|
(waypoint.transform.location + (forward_vector * 1.5) - (left_vector))]
|
2019-02-04 19:05:58 +08:00
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
line_pixel = [world_to_pixel(p) for p in line]
|
|
|
|
pygame.draw.lines(surface, color, True, line_pixel, 2)
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Draw bounding box of the stop trigger
|
2019-03-29 01:21:43 +08:00
|
|
|
if self.show_triggers:
|
|
|
|
corners = Util.get_bounding_box(actor)
|
|
|
|
corners = [world_to_pixel(p) for p in corners]
|
|
|
|
pygame.draw.lines(surface, trigger_color, True, corners, 2)
|
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
# def draw_crosswalk(surface, transform=None, color=COLOR_ALUMINIUM_2):
|
2019-09-17 00:15:36 +08:00
|
|
|
# """Given two points A and B, draw white parallel lines from A to B"""
|
2019-05-20 21:24:12 +08:00
|
|
|
# a = carla.Location(0.0, 0.0, 0.0)
|
|
|
|
# b = carla.Location(10.0, 10.0, 0.0)
|
|
|
|
|
|
|
|
# ab = b - a
|
|
|
|
# length_ab = math.sqrt(ab.x**2 + ab.y**2)
|
|
|
|
# unit_ab = ab / length_ab
|
|
|
|
# unit_perp_ab = carla.Location(-unit_ab.y, unit_ab.x, 0.0)
|
|
|
|
|
|
|
|
# # Crosswalk lines params
|
|
|
|
# space_between_lines = 0.5
|
|
|
|
# line_width = 0.7
|
|
|
|
# line_height = 2
|
|
|
|
|
|
|
|
# current_length = 0
|
|
|
|
# while current_length < length_ab:
|
|
|
|
|
|
|
|
# center = a + unit_ab * current_length
|
|
|
|
|
|
|
|
# width_offset = unit_ab * line_width
|
|
|
|
# height_offset = unit_perp_ab * line_height
|
|
|
|
# list_point = [center - width_offset - height_offset,
|
|
|
|
# center + width_offset - height_offset,
|
|
|
|
# center + width_offset + height_offset,
|
|
|
|
# center - width_offset + height_offset]
|
|
|
|
|
|
|
|
# list_point = [world_to_pixel(p) for p in list_point]
|
|
|
|
# pygame.draw.polygon(surface, color, list_point)
|
|
|
|
# current_length += (line_width + space_between_lines) * 2
|
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
def lateral_shift(transform, shift):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Makes a lateral shift of the forward vector of a transform"""
|
2019-02-07 22:02:42 +08:00
|
|
|
transform.rotation.yaw += 90
|
|
|
|
return transform.location + shift * transform.get_forward_vector()
|
|
|
|
|
2019-03-30 21:59:53 +08:00
|
|
|
def draw_topology(carla_topology, index):
|
2019-09-17 00:15:36 +08:00
|
|
|
""" Draws traffic signs and the roads network with sidewalks, parking and shoulders by generating waypoints"""
|
2019-03-26 03:29:32 +08:00
|
|
|
topology = [x[index] for x in carla_topology]
|
|
|
|
topology = sorted(topology, key=lambda w: w.transform.location.z)
|
2019-05-20 21:24:12 +08:00
|
|
|
set_waypoints = []
|
2019-03-26 03:29:32 +08:00
|
|
|
for waypoint in topology:
|
|
|
|
waypoints = [waypoint]
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Generate waypoints of a road id. Stop when road id differs
|
2019-03-26 03:29:32 +08:00
|
|
|
nxt = waypoint.next(precision)
|
|
|
|
if len(nxt) > 0:
|
|
|
|
nxt = nxt[0]
|
|
|
|
while nxt.road_id == waypoint.road_id:
|
|
|
|
waypoints.append(nxt)
|
|
|
|
nxt = nxt.next(precision)
|
|
|
|
if len(nxt) > 0:
|
|
|
|
nxt = nxt[0]
|
|
|
|
else:
|
|
|
|
break
|
2019-05-20 21:24:12 +08:00
|
|
|
set_waypoints.append(waypoints)
|
2019-03-26 03:29:32 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
# Draw Shoulders, Parkings and Sidewalks
|
2019-03-30 02:23:26 +08:00
|
|
|
PARKING_COLOR = COLOR_ALUMINIUM_4_5
|
|
|
|
SHOULDER_COLOR = COLOR_ALUMINIUM_5
|
2019-05-20 21:24:12 +08:00
|
|
|
SIDEWALK_COLOR = COLOR_ALUMINIUM_3
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
shoulder = [[], []]
|
|
|
|
parking = [[], []]
|
|
|
|
sidewalk = [[], []]
|
2019-03-30 02:23:26 +08:00
|
|
|
|
|
|
|
for w in waypoints:
|
2019-09-17 00:15:36 +08:00
|
|
|
# Classify lane types until there are no waypoints by going left
|
2019-05-20 21:24:12 +08:00
|
|
|
l = w.get_left_lane()
|
|
|
|
while l and l.lane_type != carla.LaneType.Driving:
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
if l.lane_type == carla.LaneType.Shoulder:
|
|
|
|
shoulder[0].append(l)
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
if l.lane_type == carla.LaneType.Parking:
|
|
|
|
parking[0].append(l)
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
if l.lane_type == carla.LaneType.Sidewalk:
|
|
|
|
sidewalk[0].append(l)
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
l = l.get_left_lane()
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Classify lane types until there are no waypoints by going right
|
2019-05-20 21:24:12 +08:00
|
|
|
r = w.get_right_lane()
|
|
|
|
while r and r.lane_type != carla.LaneType.Driving:
|
|
|
|
|
|
|
|
if r.lane_type == carla.LaneType.Shoulder:
|
|
|
|
shoulder[1].append(r)
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-03-30 21:59:53 +08:00
|
|
|
if r.lane_type == carla.LaneType.Parking:
|
2019-05-20 21:24:12 +08:00
|
|
|
parking[1].append(r)
|
|
|
|
|
|
|
|
if r.lane_type == carla.LaneType.Sidewalk:
|
|
|
|
sidewalk[1].append(r)
|
|
|
|
|
|
|
|
r = r.get_right_lane()
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Draw classified lane types
|
2019-05-20 21:24:12 +08:00
|
|
|
draw_lane(map_surface, shoulder, SHOULDER_COLOR)
|
|
|
|
draw_lane(map_surface, parking, PARKING_COLOR)
|
|
|
|
draw_lane(map_surface, sidewalk, SIDEWALK_COLOR)
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
# Draw Roads
|
|
|
|
for waypoints in set_waypoints:
|
|
|
|
waypoint = waypoints[0]
|
|
|
|
road_left_side = [lateral_shift(w.transform, -w.lane_width * 0.5) for w in waypoints]
|
|
|
|
road_right_side = [lateral_shift(w.transform, w.lane_width * 0.5) for w in waypoints]
|
|
|
|
|
|
|
|
polygon = road_left_side + [x for x in reversed(road_right_side)]
|
2019-03-30 02:23:26 +08:00
|
|
|
polygon = [world_to_pixel(x) for x in polygon]
|
|
|
|
|
|
|
|
if len(polygon) > 2:
|
2019-05-20 21:24:12 +08:00
|
|
|
pygame.draw.polygon(map_surface, COLOR_ALUMINIUM_5, polygon, 5)
|
|
|
|
pygame.draw.polygon(map_surface, COLOR_ALUMINIUM_5, polygon)
|
2019-03-30 02:23:26 +08:00
|
|
|
|
2019-03-27 23:16:00 +08:00
|
|
|
# Draw Lane Markings and Arrows
|
2019-04-29 21:34:45 +08:00
|
|
|
if not waypoint.is_junction:
|
2019-05-20 21:24:12 +08:00
|
|
|
draw_lane_marking(map_surface, [waypoints, waypoints])
|
2019-03-26 03:29:32 +08:00
|
|
|
for n, wp in enumerate(waypoints):
|
2019-03-30 21:59:53 +08:00
|
|
|
if ((n + 1) % 400) == 0:
|
2019-03-26 03:29:32 +08:00
|
|
|
draw_arrow(map_surface, wp.transform)
|
|
|
|
|
|
|
|
topology = carla_map.get_topology()
|
|
|
|
draw_topology(topology, 0)
|
2019-02-04 19:05:58 +08:00
|
|
|
|
2019-04-01 22:36:12 +08:00
|
|
|
if self.show_spawn_points:
|
|
|
|
for sp in carla_map.get_spawn_points():
|
|
|
|
draw_arrow(map_surface, sp, color=COLOR_CHOCOLATE_0)
|
|
|
|
|
2019-04-01 20:24:34 +08:00
|
|
|
if self.show_connections:
|
|
|
|
dist = 1.5
|
2019-05-20 21:24:12 +08:00
|
|
|
|
|
|
|
def to_pixel(wp): return world_to_pixel(wp.transform.location)
|
2019-04-01 20:24:34 +08:00
|
|
|
for wp in carla_map.generate_waypoints(dist):
|
2019-04-29 21:34:45 +08:00
|
|
|
col = (0, 255, 255) if wp.is_junction else (0, 255, 0)
|
2019-04-01 20:24:34 +08:00
|
|
|
for nxt in wp.next(dist):
|
|
|
|
pygame.draw.line(map_surface, col, to_pixel(wp), to_pixel(nxt), 2)
|
|
|
|
if wp.lane_change & carla.LaneChange.Right:
|
|
|
|
r = wp.get_right_lane()
|
|
|
|
if r and r.lane_type == carla.LaneType.Driving:
|
|
|
|
pygame.draw.line(map_surface, col, to_pixel(wp), to_pixel(r), 2)
|
|
|
|
if wp.lane_change & carla.LaneChange.Left:
|
|
|
|
l = wp.get_left_lane()
|
|
|
|
if l and l.lane_type == carla.LaneType.Driving:
|
|
|
|
pygame.draw.line(map_surface, col, to_pixel(wp), to_pixel(l), 2)
|
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
actors = carla_world.get_actors()
|
2019-03-28 00:00:01 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Find and Draw Traffic Signs: Stops and Yields
|
2019-02-04 19:05:58 +08:00
|
|
|
font_size = world_to_pixel_width(1)
|
2019-02-28 21:14:50 +08:00
|
|
|
font = pygame.font.SysFont('Arial', font_size, True)
|
2019-03-28 00:00:01 +08:00
|
|
|
|
2019-03-29 01:21:43 +08:00
|
|
|
stops = [actor for actor in actors if 'stop' in actor.type_id]
|
|
|
|
yields = [actor for actor in actors if 'yield' in actor.type_id]
|
2019-03-28 00:00:01 +08:00
|
|
|
|
|
|
|
stop_font_surface = font.render("STOP", False, COLOR_ALUMINIUM_2)
|
2019-03-30 21:59:53 +08:00
|
|
|
stop_font_surface = pygame.transform.scale(
|
|
|
|
stop_font_surface, (stop_font_surface.get_width(), stop_font_surface.get_height() * 2))
|
2019-03-28 00:00:01 +08:00
|
|
|
|
|
|
|
yield_font_surface = font.render("YIELD", False, COLOR_ALUMINIUM_2)
|
2019-03-30 21:59:53 +08:00
|
|
|
yield_font_surface = pygame.transform.scale(
|
|
|
|
yield_font_surface, (yield_font_surface.get_width(), yield_font_surface.get_height() * 2))
|
2019-03-28 00:00:01 +08:00
|
|
|
|
2019-03-29 01:21:43 +08:00
|
|
|
for ts_stop in stops:
|
2019-04-01 21:41:37 +08:00
|
|
|
draw_traffic_signs(map_surface, stop_font_surface, ts_stop, trigger_color=COLOR_SCARLET_RED_1)
|
2019-03-28 00:00:01 +08:00
|
|
|
|
2019-03-29 01:21:43 +08:00
|
|
|
for ts_yield in yields:
|
2019-04-01 21:41:37 +08:00
|
|
|
draw_traffic_signs(map_surface, yield_font_surface, ts_yield, trigger_color=COLOR_ORANGE_1)
|
2019-02-07 22:02:42 +08:00
|
|
|
|
|
|
|
def world_to_pixel(self, location, offset=(0, 0)):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Converts the world coordinates to pixel coordinates"""
|
2019-02-07 22:02:42 +08:00
|
|
|
x = self.scale * self._pixels_per_meter * (location.x - self._world_offset[0])
|
|
|
|
y = self.scale * self._pixels_per_meter * (location.y - self._world_offset[1])
|
|
|
|
return [int(x - offset[0]), int(y - offset[1])]
|
|
|
|
|
|
|
|
def world_to_pixel_width(self, width):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Converts the world units to pixel units"""
|
2019-02-07 22:02:42 +08:00
|
|
|
return int(self.scale * self._pixels_per_meter * width)
|
|
|
|
|
|
|
|
def scale_map(self, scale):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Scales the map surface"""
|
2019-02-07 22:02:42 +08:00
|
|
|
if scale != self.scale:
|
|
|
|
self.scale = scale
|
2019-02-28 21:14:50 +08:00
|
|
|
width = int(self.big_map_surface.get_width() * self.scale)
|
|
|
|
self.surface = pygame.transform.smoothscale(self.big_map_surface, (width, width))
|
2019-02-07 22:02:42 +08:00
|
|
|
|
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
class World(object):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Class that contains all the information of a carla world that is running on the server side"""
|
|
|
|
|
2019-04-01 20:24:34 +08:00
|
|
|
def __init__(self, name, args, timeout):
|
2019-01-31 02:09:30 +08:00
|
|
|
self.client = None
|
2019-01-23 00:23:46 +08:00
|
|
|
self.name = name
|
2019-04-01 20:24:34 +08:00
|
|
|
self.args = args
|
2019-01-23 00:23:46 +08:00
|
|
|
self.timeout = timeout
|
2019-01-23 19:50:54 +08:00
|
|
|
self.server_fps = 0.0
|
2019-02-01 19:57:05 +08:00
|
|
|
self.simulation_time = 0
|
2019-01-23 00:23:46 +08:00
|
|
|
self.server_clock = pygame.time.Clock()
|
2019-01-16 00:17:10 +08:00
|
|
|
|
2019-01-31 02:09:30 +08:00
|
|
|
# World data
|
|
|
|
self.world = None
|
|
|
|
self.town_map = None
|
2019-02-26 00:38:35 +08:00
|
|
|
self.actors_with_transforms = []
|
2019-06-11 17:20:02 +08:00
|
|
|
|
|
|
|
self._hud = None
|
|
|
|
self._input = None
|
2019-01-31 02:45:54 +08:00
|
|
|
|
|
|
|
self.surface_size = [0, 0]
|
2019-01-31 20:20:58 +08:00
|
|
|
self.prev_scaled_size = 0
|
|
|
|
self.scaled_size = 0
|
2019-09-17 00:15:36 +08:00
|
|
|
|
2019-01-31 02:09:30 +08:00
|
|
|
# Hero actor
|
|
|
|
self.hero_actor = None
|
2019-04-01 22:50:20 +08:00
|
|
|
self.spawned_hero = None
|
2019-02-26 00:38:35 +08:00
|
|
|
self.hero_transform = None
|
|
|
|
|
2019-01-31 02:09:30 +08:00
|
|
|
self.scale_offset = [0, 0]
|
|
|
|
|
|
|
|
self.vehicle_id_surface = None
|
|
|
|
self.result_surface = None
|
|
|
|
|
2019-02-07 22:33:57 +08:00
|
|
|
self.traffic_light_surfaces = TrafficLightSurfaces()
|
2019-02-28 18:12:34 +08:00
|
|
|
self.affected_traffic_light = None
|
2019-02-04 19:05:58 +08:00
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
# Map info
|
|
|
|
self.map_image = None
|
|
|
|
self.border_round_surface = None
|
|
|
|
self.original_surface_size = None
|
|
|
|
self.hero_surface = None
|
|
|
|
self.actors_surface = None
|
2019-02-07 22:33:57 +08:00
|
|
|
|
2019-03-29 05:47:21 +08:00
|
|
|
def _get_data_from_carla(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Retrieves the data from the server side"""
|
2019-01-14 18:49:29 +08:00
|
|
|
try:
|
2019-04-01 20:24:34 +08:00
|
|
|
self.client = carla.Client(self.args.host, self.args.port)
|
2019-03-29 05:47:21 +08:00
|
|
|
self.client.set_timeout(self.timeout)
|
|
|
|
|
2019-04-01 20:24:34 +08:00
|
|
|
if self.args.map is None:
|
2019-03-29 05:47:21 +08:00
|
|
|
world = self.client.get_world()
|
|
|
|
else:
|
2019-04-01 20:24:34 +08:00
|
|
|
world = self.client.load_world(self.args.map)
|
2019-01-19 01:35:35 +08:00
|
|
|
|
2019-01-23 00:23:46 +08:00
|
|
|
town_map = world.get_map()
|
2019-02-21 20:08:08 +08:00
|
|
|
return (world, town_map)
|
2019-01-15 22:45:31 +08:00
|
|
|
|
2019-02-04 19:05:58 +08:00
|
|
|
except RuntimeError as ex:
|
2019-01-19 01:35:35 +08:00
|
|
|
logging.error(ex)
|
2019-01-17 00:54:50 +08:00
|
|
|
exit_game()
|
2019-01-15 22:45:31 +08:00
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
def start(self, hud, input_control):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Build the map image, stores the needed modules and prepares rendering in Hero Mode"""
|
2019-03-29 05:47:21 +08:00
|
|
|
self.world, self.town_map = self._get_data_from_carla()
|
2019-01-23 19:50:54 +08:00
|
|
|
|
2019-03-11 05:20:45 +08:00
|
|
|
settings = self.world.get_settings()
|
2019-04-01 20:24:34 +08:00
|
|
|
settings.no_rendering_mode = self.args.no_rendering
|
2019-03-11 05:20:45 +08:00
|
|
|
self.world.apply_settings(settings)
|
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
# Create Surfaces
|
2019-04-01 20:24:34 +08:00
|
|
|
self.map_image = MapImage(
|
|
|
|
carla_world=self.world,
|
|
|
|
carla_map=self.town_map,
|
|
|
|
pixels_per_meter=PIXELS_PER_METER,
|
|
|
|
show_triggers=self.args.show_triggers,
|
2019-04-01 22:36:12 +08:00
|
|
|
show_connections=self.args.show_connections,
|
|
|
|
show_spawn_points=self.args.show_spawn_points)
|
2019-02-07 22:02:42 +08:00
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud = hud
|
|
|
|
self._input = input_control
|
2019-01-23 19:50:54 +08:00
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
self.original_surface_size = min(self._hud.dim[0], self._hud.dim[1])
|
2019-02-28 21:14:50 +08:00
|
|
|
self.surface_size = self.map_image.big_map_surface.get_width()
|
2019-02-06 00:34:29 +08:00
|
|
|
|
|
|
|
self.scaled_size = int(self.surface_size)
|
2019-01-31 20:20:58 +08:00
|
|
|
self.prev_scaled_size = int(self.surface_size)
|
2019-01-23 19:50:54 +08:00
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
# Render Actors
|
|
|
|
self.actors_surface = pygame.Surface((self.map_image.surface.get_width(), self.map_image.surface.get_height()))
|
2019-02-07 03:15:43 +08:00
|
|
|
self.actors_surface.set_colorkey(COLOR_BLACK)
|
|
|
|
|
|
|
|
self.vehicle_id_surface = pygame.Surface((self.surface_size, self.surface_size)).convert()
|
|
|
|
self.vehicle_id_surface.set_colorkey(COLOR_BLACK)
|
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
self.border_round_surface = pygame.Surface(self._hud.dim, pygame.SRCALPHA).convert()
|
2019-02-08 02:14:30 +08:00
|
|
|
self.border_round_surface.set_colorkey(COLOR_WHITE)
|
2019-02-08 20:25:45 +08:00
|
|
|
self.border_round_surface.fill(COLOR_BLACK)
|
2019-02-07 23:02:55 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Used for Hero Mode, draws the map contained in a circle with white border
|
2019-06-11 17:20:02 +08:00
|
|
|
center_offset = (int(self._hud.dim[0] / 2), int(self._hud.dim[1] / 2))
|
|
|
|
pygame.draw.circle(self.border_round_surface, COLOR_ALUMINIUM_1, center_offset, int(self._hud.dim[1] / 2))
|
|
|
|
pygame.draw.circle(self.border_round_surface, COLOR_WHITE, center_offset, int((self._hud.dim[1] - 8) / 2))
|
2019-02-12 19:07:02 +08:00
|
|
|
|
2019-02-07 23:46:56 +08:00
|
|
|
scaled_original_size = self.original_surface_size * (1.0 / 0.9)
|
2019-02-08 02:14:30 +08:00
|
|
|
self.hero_surface = pygame.Surface((scaled_original_size, scaled_original_size)).convert()
|
2019-02-07 23:46:56 +08:00
|
|
|
|
2019-02-07 03:15:43 +08:00
|
|
|
self.result_surface = pygame.Surface((self.surface_size, self.surface_size)).convert()
|
|
|
|
self.result_surface.set_colorkey(COLOR_BLACK)
|
2019-01-23 19:50:54 +08:00
|
|
|
|
2019-02-21 20:08:08 +08:00
|
|
|
# Start hero mode by default
|
|
|
|
self.select_hero_actor()
|
|
|
|
self.hero_actor.set_autopilot(False)
|
2019-06-11 17:20:02 +08:00
|
|
|
self._input.wheel_offset = HERO_DEFAULT_SCALE
|
|
|
|
self._input.control = carla.VehicleControl()
|
2019-02-21 20:08:08 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Register event for receiving server tick
|
2019-01-17 00:54:50 +08:00
|
|
|
weak_self = weakref.ref(self)
|
2019-06-11 17:20:02 +08:00
|
|
|
self.world.on_tick(lambda timestamp: World.on_world_tick(weak_self, timestamp))
|
2019-01-12 00:31:18 +08:00
|
|
|
|
2019-02-05 17:14:10 +08:00
|
|
|
def select_hero_actor(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Selects only one hero actor if there are more than one. If there are not any, it will spawn one."""
|
|
|
|
hero_vehicles = [actor for actor in self.world.get_actors()
|
|
|
|
if 'vehicle' in actor.type_id and actor.attributes['role_name'] == 'hero']
|
2019-01-30 21:37:02 +08:00
|
|
|
if len(hero_vehicles) > 0:
|
|
|
|
self.hero_actor = random.choice(hero_vehicles)
|
2019-02-26 00:38:35 +08:00
|
|
|
self.hero_transform = self.hero_actor.get_transform()
|
2019-01-23 01:05:47 +08:00
|
|
|
else:
|
2019-02-05 17:14:10 +08:00
|
|
|
self._spawn_hero()
|
|
|
|
|
|
|
|
def _spawn_hero(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Spawns the hero actor when the script runs"""
|
2019-02-05 17:14:10 +08:00
|
|
|
# Get a random blueprint.
|
2019-04-01 20:24:34 +08:00
|
|
|
blueprint = random.choice(self.world.get_blueprint_library().filter(self.args.filter))
|
2019-02-05 17:14:10 +08:00
|
|
|
blueprint.set_attribute('role_name', 'hero')
|
|
|
|
if blueprint.has_attribute('color'):
|
|
|
|
color = random.choice(blueprint.get_attribute('color').recommended_values)
|
|
|
|
blueprint.set_attribute('color', color)
|
|
|
|
# Spawn the player.
|
|
|
|
while self.hero_actor is None:
|
|
|
|
spawn_points = self.world.get_map().get_spawn_points()
|
|
|
|
spawn_point = random.choice(spawn_points) if spawn_points else carla.Transform()
|
|
|
|
self.hero_actor = self.world.try_spawn_actor(blueprint, spawn_point)
|
2019-02-26 00:38:35 +08:00
|
|
|
self.hero_transform = self.hero_actor.get_transform()
|
2019-02-07 02:23:24 +08:00
|
|
|
|
2019-04-01 22:50:20 +08:00
|
|
|
# Save it in order to destroy it when closing program
|
|
|
|
self.spawned_hero = self.hero_actor
|
|
|
|
|
2019-01-12 00:31:18 +08:00
|
|
|
def tick(self, clock):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Retrieves the actors for Hero and Map modes and updates de HUD based on that"""
|
2019-02-26 00:38:35 +08:00
|
|
|
actors = self.world.get_actors()
|
2019-09-17 00:15:36 +08:00
|
|
|
|
|
|
|
# We store the transforms also so that we avoid having transforms of
|
|
|
|
# previous tick and current tick when rendering them.
|
2019-02-26 00:38:35 +08:00
|
|
|
self.actors_with_transforms = [(actor, actor.get_transform()) for actor in actors]
|
|
|
|
if self.hero_actor is not None:
|
2019-02-28 21:14:50 +08:00
|
|
|
self.hero_transform = self.hero_actor.get_transform()
|
2019-09-17 00:15:36 +08:00
|
|
|
|
2019-01-17 18:51:53 +08:00
|
|
|
self.update_hud_info(clock)
|
2019-01-17 18:25:57 +08:00
|
|
|
|
2019-01-17 18:51:53 +08:00
|
|
|
def update_hud_info(self, clock):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Updates the HUD info regarding simulation, hero mode and whether there is a traffic light affecting the hero actor"""
|
|
|
|
|
2019-01-17 18:51:53 +08:00
|
|
|
hero_mode_text = []
|
2019-01-17 22:46:46 +08:00
|
|
|
if self.hero_actor is not None:
|
2019-01-18 17:32:32 +08:00
|
|
|
hero_speed = self.hero_actor.get_velocity()
|
2019-01-29 00:24:29 +08:00
|
|
|
hero_speed_text = 3.6 * math.sqrt(hero_speed.x ** 2 + hero_speed.y ** 2 + hero_speed.z ** 2)
|
2019-02-04 19:05:58 +08:00
|
|
|
|
2019-02-28 18:12:34 +08:00
|
|
|
affected_traffic_light_text = 'None'
|
|
|
|
if self.affected_traffic_light is not None:
|
2019-02-28 21:14:50 +08:00
|
|
|
state = self.affected_traffic_light.state
|
2019-04-01 20:24:34 +08:00
|
|
|
if state == carla.TrafficLightState.Green:
|
2019-02-28 21:14:50 +08:00
|
|
|
affected_traffic_light_text = 'GREEN'
|
2019-04-01 20:24:34 +08:00
|
|
|
elif state == carla.TrafficLightState.Yellow:
|
2019-02-28 21:14:50 +08:00
|
|
|
affected_traffic_light_text = 'YELLOW'
|
|
|
|
else:
|
|
|
|
affected_traffic_light_text = 'RED'
|
2019-02-28 18:12:34 +08:00
|
|
|
|
|
|
|
affected_speed_limit_text = self.hero_actor.get_speed_limit()
|
2019-06-12 20:32:32 +08:00
|
|
|
if math.isnan(affected_speed_limit_text):
|
|
|
|
affected_speed_limit_text = 0.0
|
2019-01-17 18:51:53 +08:00
|
|
|
hero_mode_text = [
|
2019-02-07 23:51:30 +08:00
|
|
|
'Hero Mode: ON',
|
|
|
|
'Hero ID: %7d' % self.hero_actor.id,
|
|
|
|
'Hero Vehicle: %14s' % get_actor_display_name(self.hero_actor, truncate=14),
|
|
|
|
'Hero Speed: %3d km/h' % hero_speed_text,
|
2019-01-29 00:24:29 +08:00
|
|
|
'Hero Affected by:',
|
2019-02-28 18:12:34 +08:00
|
|
|
' Traffic Light: %12s' % affected_traffic_light_text,
|
|
|
|
' Speed Limit: %3d km/h' % affected_speed_limit_text
|
2019-01-17 18:51:53 +08:00
|
|
|
]
|
2019-01-17 18:25:57 +08:00
|
|
|
else:
|
2019-02-07 23:51:30 +08:00
|
|
|
hero_mode_text = ['Hero Mode: OFF']
|
2019-01-17 18:25:57 +08:00
|
|
|
|
2019-02-01 00:23:45 +08:00
|
|
|
self.server_fps = self.server_clock.get_fps()
|
2019-02-08 17:11:59 +08:00
|
|
|
self.server_fps = 'inf' if self.server_fps == float('inf') else round(self.server_fps)
|
2019-06-11 17:20:02 +08:00
|
|
|
info_text = [
|
2019-02-08 17:11:59 +08:00
|
|
|
'Server: % 16s FPS' % self.server_fps,
|
2019-02-01 00:23:45 +08:00
|
|
|
'Client: % 16s FPS' % round(clock.get_fps()),
|
2019-02-07 23:51:30 +08:00
|
|
|
'Simulation Time: % 12s' % datetime.timedelta(seconds=int(self.simulation_time)),
|
2019-02-26 00:38:35 +08:00
|
|
|
'Map Name: %10s' % self.town_map.name,
|
2019-01-17 18:25:57 +08:00
|
|
|
]
|
2019-01-17 18:51:53 +08:00
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud.add_info(self.name, info_text)
|
|
|
|
self._hud.add_info('HERO', hero_mode_text)
|
2019-01-15 01:44:58 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def on_world_tick(weak_self, timestamp):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Updates the server tick"""
|
2019-01-15 01:44:58 +08:00
|
|
|
self = weak_self()
|
|
|
|
if not self:
|
|
|
|
return
|
2019-01-17 18:25:57 +08:00
|
|
|
|
|
|
|
self.server_clock.tick()
|
2019-01-31 02:38:37 +08:00
|
|
|
self.server_fps = self.server_clock.get_fps()
|
2019-02-01 19:57:05 +08:00
|
|
|
self.simulation_time = timestamp.elapsed_seconds
|
2019-01-12 00:31:18 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
def _show_nearby_vehicles(self, vehicles):
|
|
|
|
"""Shows nearby vehicles of the hero actor"""
|
|
|
|
info_text = []
|
|
|
|
if self.hero_actor is not None and len(vehicles) > 1:
|
|
|
|
location = self.hero_transform.location
|
|
|
|
vehicle_list = [x[0] for x in vehicles if x[0].id != self.hero_actor.id]
|
|
|
|
|
|
|
|
def distance(v): return location.distance(v.get_location())
|
|
|
|
for n, vehicle in enumerate(sorted(vehicle_list, key=distance)):
|
|
|
|
if n > 15:
|
|
|
|
break
|
|
|
|
vehicle_type = get_actor_display_name(vehicle, truncate=22)
|
|
|
|
info_text.append('% 5d %s' % (vehicle.id, vehicle_type))
|
|
|
|
self._hud.add_info('NEARBY VEHICLES', info_text)
|
|
|
|
|
2019-02-12 22:28:55 +08:00
|
|
|
def _split_actors(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Splits the retrieved actors by type id"""
|
2019-01-18 02:47:51 +08:00
|
|
|
vehicles = []
|
|
|
|
traffic_lights = []
|
|
|
|
speed_limits = []
|
2019-01-21 21:27:21 +08:00
|
|
|
walkers = []
|
2019-01-18 02:47:51 +08:00
|
|
|
|
2019-02-26 00:38:35 +08:00
|
|
|
for actor_with_transform in self.actors_with_transforms:
|
|
|
|
actor = actor_with_transform[0]
|
2019-01-18 02:47:51 +08:00
|
|
|
if 'vehicle' in actor.type_id:
|
2019-02-26 00:38:35 +08:00
|
|
|
vehicles.append(actor_with_transform)
|
2019-01-18 02:47:51 +08:00
|
|
|
elif 'traffic_light' in actor.type_id:
|
2019-02-26 00:38:35 +08:00
|
|
|
traffic_lights.append(actor_with_transform)
|
2019-01-18 02:47:51 +08:00
|
|
|
elif 'speed_limit' in actor.type_id:
|
2019-02-26 00:38:35 +08:00
|
|
|
speed_limits.append(actor_with_transform)
|
2019-07-04 23:53:32 +08:00
|
|
|
elif 'walker.pedestrian' in actor.type_id:
|
2019-02-26 00:38:35 +08:00
|
|
|
walkers.append(actor_with_transform)
|
2019-01-17 02:32:39 +08:00
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
return (vehicles, traffic_lights, speed_limits, walkers)
|
2019-01-21 19:12:06 +08:00
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
def _render_traffic_lights(self, surface, list_tl, world_to_pixel):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders the traffic lights and shows its triggers and bounding boxes if flags are enabled"""
|
2019-02-28 18:12:34 +08:00
|
|
|
self.affected_traffic_light = None
|
|
|
|
|
2019-02-06 23:25:12 +08:00
|
|
|
for tl in list_tl:
|
2019-02-27 21:57:51 +08:00
|
|
|
world_pos = tl.get_location()
|
|
|
|
pos = world_to_pixel(world_pos)
|
2019-03-30 02:39:52 +08:00
|
|
|
|
2019-04-01 20:24:34 +08:00
|
|
|
if self.args.show_triggers:
|
2019-03-30 02:39:52 +08:00
|
|
|
corners = Util.get_bounding_box(tl)
|
|
|
|
corners = [world_to_pixel(p) for p in corners]
|
2019-04-01 21:41:37 +08:00
|
|
|
pygame.draw.lines(surface, COLOR_BUTTER_1, True, corners, 2)
|
2019-03-30 02:39:52 +08:00
|
|
|
|
2019-03-29 01:21:43 +08:00
|
|
|
if self.hero_actor is not None:
|
|
|
|
corners = Util.get_bounding_box(tl)
|
2019-02-27 21:57:51 +08:00
|
|
|
corners = [world_to_pixel(p) for p in corners]
|
|
|
|
tl_t = tl.get_transform()
|
|
|
|
|
|
|
|
transformed_tv = tl_t.transform(tl.trigger_volume.location)
|
2019-02-28 17:14:31 +08:00
|
|
|
hero_location = self.hero_actor.get_location()
|
|
|
|
d = hero_location.distance(transformed_tv)
|
2019-02-27 21:57:51 +08:00
|
|
|
s = Util.length(tl.trigger_volume.extent) + Util.length(self.hero_actor.bounding_box.extent)
|
2019-02-04 19:05:58 +08:00
|
|
|
if (d <= s):
|
2019-02-28 21:14:50 +08:00
|
|
|
# Highlight traffic light
|
|
|
|
self.affected_traffic_light = tl
|
|
|
|
srf = self.traffic_light_surfaces.surfaces['h']
|
|
|
|
surface.blit(srf, srf.get_rect(center=pos))
|
2019-02-04 19:05:58 +08:00
|
|
|
|
2019-02-07 22:33:57 +08:00
|
|
|
srf = self.traffic_light_surfaces.surfaces[tl.state]
|
|
|
|
surface.blit(srf, srf.get_rect(center=pos))
|
2019-02-06 23:25:12 +08:00
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
def _render_speed_limits(self, surface, list_sl, world_to_pixel, world_to_pixel_width):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders the speed limits by drawing two concentric circles (outer is red and inner white) and a speed limit text"""
|
2019-02-07 02:23:24 +08:00
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
font_size = world_to_pixel_width(2)
|
|
|
|
radius = world_to_pixel_width(2)
|
2019-02-06 23:25:12 +08:00
|
|
|
font = pygame.font.SysFont('Arial', font_size)
|
|
|
|
|
|
|
|
for sl in list_sl:
|
2019-02-07 02:23:24 +08:00
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
x, y = world_to_pixel(sl.get_location())
|
2019-02-06 23:25:12 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Render speed limit concentric circles
|
2019-02-06 23:25:12 +08:00
|
|
|
white_circle_radius = int(radius * 0.75)
|
|
|
|
|
2019-02-21 22:01:33 +08:00
|
|
|
pygame.draw.circle(surface, COLOR_SCARLET_RED_1, (x, y), radius)
|
|
|
|
pygame.draw.circle(surface, COLOR_ALUMINIUM_0, (x, y), white_circle_radius)
|
2019-02-07 02:23:24 +08:00
|
|
|
|
2019-02-06 23:25:12 +08:00
|
|
|
limit = sl.type_id.split('.')[2]
|
2019-02-22 18:58:18 +08:00
|
|
|
font_surface = font.render(limit, True, COLOR_ALUMINIUM_5)
|
2019-02-06 23:25:12 +08:00
|
|
|
|
2019-04-01 20:59:55 +08:00
|
|
|
if self.args.show_triggers:
|
|
|
|
corners = Util.get_bounding_box(sl)
|
|
|
|
corners = [world_to_pixel(p) for p in corners]
|
2019-04-01 23:49:11 +08:00
|
|
|
pygame.draw.lines(surface, COLOR_PLUM_2, True, corners, 2)
|
2019-04-01 20:59:55 +08:00
|
|
|
|
2019-02-06 23:25:12 +08:00
|
|
|
# Blit
|
|
|
|
if self.hero_actor is not None:
|
2019-09-17 00:15:36 +08:00
|
|
|
# In hero mode, Rotate font surface with respect to hero vehicle front
|
2019-02-26 00:38:35 +08:00
|
|
|
angle = -self.hero_transform.rotation.yaw - 90.0
|
2019-02-07 01:09:51 +08:00
|
|
|
font_surface = pygame.transform.rotate(font_surface, angle)
|
2019-02-06 23:25:12 +08:00
|
|
|
offset = font_surface.get_rect(center=(x, y))
|
|
|
|
surface.blit(font_surface, offset)
|
|
|
|
|
|
|
|
else:
|
2019-09-17 00:15:36 +08:00
|
|
|
# In map mode, there is no need to rotate the text of the speed limit
|
2019-02-07 02:23:24 +08:00
|
|
|
surface.blit(font_surface, (x - radius / 2, y - radius / 2))
|
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
def _render_walkers(self, surface, list_w, world_to_pixel):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders the walkers' bounding boxes"""
|
2019-02-07 01:09:51 +08:00
|
|
|
for w in list_w:
|
2019-02-22 18:58:18 +08:00
|
|
|
color = COLOR_PLUM_0
|
2019-02-07 01:09:51 +08:00
|
|
|
|
|
|
|
# Compute bounding box points
|
2019-02-26 00:38:35 +08:00
|
|
|
bb = w[0].bounding_box.extent
|
2019-02-07 01:09:51 +08:00
|
|
|
corners = [
|
|
|
|
carla.Location(x=-bb.x, y=-bb.y),
|
|
|
|
carla.Location(x=bb.x, y=-bb.y),
|
|
|
|
carla.Location(x=bb.x, y=bb.y),
|
|
|
|
carla.Location(x=-bb.x, y=bb.y)]
|
2019-02-07 02:23:24 +08:00
|
|
|
|
2019-02-26 00:38:35 +08:00
|
|
|
w[1].transform(corners)
|
2019-02-07 22:02:42 +08:00
|
|
|
corners = [world_to_pixel(p) for p in corners]
|
2019-02-07 01:09:51 +08:00
|
|
|
pygame.draw.polygon(surface, color, corners)
|
2019-01-31 02:45:54 +08:00
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
def _render_vehicles(self, surface, list_v, world_to_pixel):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders the vehicles' bounding boxes"""
|
2019-02-07 01:09:51 +08:00
|
|
|
for v in list_v:
|
2019-02-22 18:58:18 +08:00
|
|
|
color = COLOR_SKY_BLUE_0
|
2019-02-26 00:38:35 +08:00
|
|
|
if int(v[0].attributes['number_of_wheels']) == 2:
|
2019-02-22 18:58:18 +08:00
|
|
|
color = COLOR_CHOCOLATE_1
|
2019-02-26 00:38:35 +08:00
|
|
|
if v[0].attributes['role_name'] == 'hero':
|
2019-02-22 18:58:18 +08:00
|
|
|
color = COLOR_CHAMELEON_0
|
2019-02-07 01:09:51 +08:00
|
|
|
# Compute bounding box points
|
2019-02-26 00:38:35 +08:00
|
|
|
bb = v[0].bounding_box.extent
|
2019-02-07 02:23:24 +08:00
|
|
|
corners = [carla.Location(x=-bb.x, y=-bb.y),
|
|
|
|
carla.Location(x=bb.x - 0.8, y=-bb.y),
|
|
|
|
carla.Location(x=bb.x, y=0),
|
|
|
|
carla.Location(x=bb.x - 0.8, y=bb.y),
|
2019-02-07 23:02:55 +08:00
|
|
|
carla.Location(x=-bb.x, y=bb.y),
|
|
|
|
carla.Location(x=-bb.x, y=-bb.y)
|
|
|
|
]
|
2019-02-26 00:38:35 +08:00
|
|
|
v[1].transform(corners)
|
2019-02-07 22:02:42 +08:00
|
|
|
corners = [world_to_pixel(p) for p in corners]
|
2019-02-04 19:05:58 +08:00
|
|
|
pygame.draw.lines(surface, color, False, corners, int(math.ceil(4.0 * self.map_image.scale)))
|
2019-02-26 00:38:35 +08:00
|
|
|
|
2019-02-28 21:14:50 +08:00
|
|
|
def render_actors(self, surface, vehicles, traffic_lights, speed_limits, walkers):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders all the actors"""
|
2019-02-26 00:38:35 +08:00
|
|
|
# Static actors
|
|
|
|
self._render_traffic_lights(surface, [tl[0] for tl in traffic_lights], self.map_image.world_to_pixel)
|
|
|
|
self._render_speed_limits(surface, [sl[0] for sl in speed_limits], self.map_image.world_to_pixel,
|
2019-02-07 22:02:42 +08:00
|
|
|
self.map_image.world_to_pixel_width)
|
2019-02-28 21:14:50 +08:00
|
|
|
|
|
|
|
# Dynamic actors
|
|
|
|
self._render_vehicles(surface, vehicles, self.map_image.world_to_pixel)
|
|
|
|
self._render_walkers(surface, walkers, self.map_image.world_to_pixel)
|
|
|
|
|
2019-01-29 23:07:54 +08:00
|
|
|
def clip_surfaces(self, clipping_rect):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Used to improve perfomance. Clips the surfaces in order to render only the part of the surfaces that are going to be visible"""
|
2019-02-07 01:09:51 +08:00
|
|
|
self.actors_surface.set_clip(clipping_rect)
|
2019-01-29 23:07:54 +08:00
|
|
|
self.vehicle_id_surface.set_clip(clipping_rect)
|
|
|
|
self.result_surface.set_clip(clipping_rect)
|
|
|
|
|
2019-02-08 20:25:45 +08:00
|
|
|
def _compute_scale(self, scale_factor):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Based on the mouse wheel and mouse position, it will compute the scale and move the map so that it is zoomed in or out based on mouse position"""
|
2019-06-11 17:20:02 +08:00
|
|
|
m = self._input.mouse_pos
|
2019-01-23 23:28:28 +08:00
|
|
|
|
2019-02-08 19:52:11 +08:00
|
|
|
# Percentage of surface where mouse position is actually
|
2019-02-08 20:25:45 +08:00
|
|
|
px = (m[0] - self.scale_offset[0]) / float(self.prev_scaled_size)
|
|
|
|
py = (m[1] - self.scale_offset[1]) / float(self.prev_scaled_size)
|
2019-01-24 21:19:54 +08:00
|
|
|
|
2019-02-08 19:52:11 +08:00
|
|
|
# Offset will be the previously accumulated offset added with the
|
|
|
|
# difference of mouse positions in the old and new scales
|
|
|
|
diff_between_scales = ((float(self.prev_scaled_size) * px) - (float(self.scaled_size) * px),
|
2019-02-21 22:30:16 +08:00
|
|
|
(float(self.prev_scaled_size) * py) - (float(self.scaled_size) * py))
|
2019-01-25 18:26:20 +08:00
|
|
|
|
2019-02-08 20:25:45 +08:00
|
|
|
self.scale_offset = (self.scale_offset[0] + diff_between_scales[0],
|
2019-02-21 22:30:16 +08:00
|
|
|
self.scale_offset[1] + diff_between_scales[1])
|
2019-01-24 21:19:54 +08:00
|
|
|
|
2019-02-08 19:52:11 +08:00
|
|
|
# Update previous scale
|
|
|
|
self.prev_scaled_size = self.scaled_size
|
2019-01-25 18:26:20 +08:00
|
|
|
|
2019-02-08 19:52:11 +08:00
|
|
|
# Scale performed
|
2019-02-08 20:25:45 +08:00
|
|
|
self.map_image.scale_map(scale_factor)
|
2019-01-24 21:19:54 +08:00
|
|
|
|
2019-02-08 19:52:11 +08:00
|
|
|
def render(self, display):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Renders the map and all the actors in hero and map mode"""
|
2019-02-26 00:38:35 +08:00
|
|
|
if self.actors_with_transforms is None:
|
2019-02-28 21:14:50 +08:00
|
|
|
return
|
2019-02-08 19:52:11 +08:00
|
|
|
self.result_surface.fill(COLOR_BLACK)
|
2019-09-17 00:15:36 +08:00
|
|
|
|
|
|
|
# Split the actors by vehicle type id
|
2019-02-28 21:14:50 +08:00
|
|
|
vehicles, traffic_lights, speed_limits, walkers = self._split_actors()
|
2019-02-08 19:52:11 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Zoom in and out
|
2019-06-11 17:20:02 +08:00
|
|
|
scale_factor = self._input.wheel_offset
|
2019-02-28 21:14:50 +08:00
|
|
|
self.scaled_size = int(self.map_image.width * scale_factor)
|
2019-02-08 19:52:11 +08:00
|
|
|
if self.scaled_size != self.prev_scaled_size:
|
2019-02-08 20:25:45 +08:00
|
|
|
self._compute_scale(scale_factor)
|
2019-01-19 01:35:35 +08:00
|
|
|
|
2019-02-07 01:09:51 +08:00
|
|
|
# Render Actors
|
2019-02-07 22:02:42 +08:00
|
|
|
self.actors_surface.fill(COLOR_BLACK)
|
2019-02-22 18:58:18 +08:00
|
|
|
self.render_actors(
|
|
|
|
self.actors_surface,
|
|
|
|
vehicles,
|
|
|
|
traffic_lights,
|
|
|
|
speed_limits,
|
2019-02-28 21:14:50 +08:00
|
|
|
walkers)
|
2019-02-07 22:02:42 +08:00
|
|
|
|
|
|
|
# Render Ids
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud.render_vehicles_ids(self.vehicle_id_surface, vehicles,
|
2019-09-17 00:15:36 +08:00
|
|
|
self.map_image.world_to_pixel, self.hero_actor, self.hero_transform)
|
|
|
|
# Show nearby actors from hero mode
|
|
|
|
self._show_nearby_vehicles(vehicles)
|
2019-01-28 18:34:38 +08:00
|
|
|
|
2019-01-25 23:39:26 +08:00
|
|
|
# Blit surfaces
|
2019-02-07 22:02:42 +08:00
|
|
|
surfaces = ((self.map_image.surface, (0, 0)),
|
2019-02-07 01:09:51 +08:00
|
|
|
(self.actors_surface, (0, 0)),
|
2019-01-28 18:34:38 +08:00
|
|
|
(self.vehicle_id_surface, (0, 0)),
|
|
|
|
)
|
2019-02-07 02:23:24 +08:00
|
|
|
|
2019-02-26 00:38:35 +08:00
|
|
|
angle = 0.0 if self.hero_actor is None else self.hero_transform.rotation.yaw + 90.0
|
2019-02-07 22:33:57 +08:00
|
|
|
self.traffic_light_surfaces.rotozoom(-angle, self.map_image.scale)
|
|
|
|
|
2019-02-06 22:01:38 +08:00
|
|
|
center_offset = (0, 0)
|
2019-01-29 03:21:52 +08:00
|
|
|
if self.hero_actor is not None:
|
2019-09-17 00:15:36 +08:00
|
|
|
# Hero Mode
|
2019-02-26 00:38:35 +08:00
|
|
|
hero_location_screen = self.map_image.world_to_pixel(self.hero_transform.location)
|
|
|
|
hero_front = self.hero_transform.get_forward_vector()
|
2019-09-17 00:15:36 +08:00
|
|
|
translation_offset = (hero_location_screen[0] - self.hero_surface.get_width() / 2 + hero_front.x * PIXELS_AHEAD_VEHICLE,
|
|
|
|
(hero_location_screen[1] - self.hero_surface.get_height() / 2 + hero_front.y * PIXELS_AHEAD_VEHICLE))
|
2019-02-06 22:01:38 +08:00
|
|
|
|
2019-01-29 23:07:54 +08:00
|
|
|
# Apply clipping rect
|
2019-02-07 22:02:42 +08:00
|
|
|
clipping_rect = pygame.Rect(translation_offset[0],
|
|
|
|
translation_offset[1],
|
2019-02-07 23:02:55 +08:00
|
|
|
self.hero_surface.get_width(),
|
|
|
|
self.hero_surface.get_height())
|
2019-01-29 23:07:54 +08:00
|
|
|
self.clip_surfaces(clipping_rect)
|
2019-02-12 19:07:02 +08:00
|
|
|
|
2019-01-30 19:15:24 +08:00
|
|
|
Util.blits(self.result_surface, surfaces)
|
2019-01-29 23:07:54 +08:00
|
|
|
|
2019-02-08 02:14:30 +08:00
|
|
|
self.border_round_surface.set_clip(clipping_rect)
|
2019-02-12 19:07:02 +08:00
|
|
|
|
2019-02-21 22:01:33 +08:00
|
|
|
self.hero_surface.fill(COLOR_ALUMINIUM_4)
|
2019-02-07 23:02:55 +08:00
|
|
|
self.hero_surface.blit(self.result_surface, (-translation_offset[0],
|
|
|
|
-translation_offset[1]))
|
2019-01-29 20:29:03 +08:00
|
|
|
|
2019-02-08 02:14:30 +08:00
|
|
|
rotated_result_surface = pygame.transform.rotozoom(self.hero_surface, angle, 0.9).convert()
|
2019-02-05 19:00:49 +08:00
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
center = (display.get_width() / 2, display.get_height() / 2)
|
|
|
|
rotation_pivot = rotated_result_surface.get_rect(center=center)
|
|
|
|
display.blit(rotated_result_surface, rotation_pivot)
|
2019-02-12 19:07:02 +08:00
|
|
|
|
2019-02-21 22:30:16 +08:00
|
|
|
display.blit(self.border_round_surface, (0, 0))
|
2019-01-29 03:21:52 +08:00
|
|
|
else:
|
2019-09-17 00:15:36 +08:00
|
|
|
# Map Mode
|
2019-02-06 22:01:38 +08:00
|
|
|
# Translation offset
|
2019-06-11 17:20:02 +08:00
|
|
|
translation_offset = (self._input.mouse_offset[0] * scale_factor + self.scale_offset[0],
|
|
|
|
self._input.mouse_offset[1] * scale_factor + self.scale_offset[1])
|
2019-02-26 00:38:35 +08:00
|
|
|
center_offset = (abs(display.get_width() - self.surface_size) / 2 * scale_factor, 0)
|
2019-02-07 02:23:24 +08:00
|
|
|
|
2019-01-29 23:07:54 +08:00
|
|
|
# Apply clipping rect
|
2019-01-29 21:52:43 +08:00
|
|
|
clipping_rect = pygame.Rect(-translation_offset[0] - center_offset[0], -translation_offset[1],
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud.dim[0], self._hud.dim[1])
|
2019-01-29 23:07:54 +08:00
|
|
|
self.clip_surfaces(clipping_rect)
|
2019-01-30 19:15:24 +08:00
|
|
|
Util.blits(self.result_surface, surfaces)
|
2019-01-29 23:07:54 +08:00
|
|
|
|
2019-02-07 22:02:42 +08:00
|
|
|
display.blit(self.result_surface, (translation_offset[0] + center_offset[0],
|
|
|
|
translation_offset[1]))
|
2019-01-15 01:44:58 +08:00
|
|
|
|
2019-04-01 22:50:20 +08:00
|
|
|
def destroy(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Destroy the hero actor when class instance is destroyed"""
|
2019-04-01 22:50:20 +08:00
|
|
|
if self.spawned_hero is not None:
|
|
|
|
self.spawned_hero.destroy()
|
2019-09-17 00:15:36 +08:00
|
|
|
|
2019-01-11 22:39:54 +08:00
|
|
|
# ==============================================================================
|
2019-01-15 01:44:58 +08:00
|
|
|
# -- Input -----------------------------------------------------------
|
2019-01-11 22:39:54 +08:00
|
|
|
# ==============================================================================
|
2019-01-11 19:08:04 +08:00
|
|
|
|
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
class InputControl(object):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Class that handles input received such as keyboard and mouse."""
|
|
|
|
|
2019-01-15 01:44:58 +08:00
|
|
|
def __init__(self, name):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Initializes input member variables when instance is created."""
|
2019-01-15 01:44:58 +08:00
|
|
|
self.name = name
|
2019-01-15 19:23:50 +08:00
|
|
|
self.mouse_pos = (0, 0)
|
2019-01-16 00:38:43 +08:00
|
|
|
self.mouse_offset = [0.0, 0.0]
|
2019-02-08 20:25:45 +08:00
|
|
|
self.wheel_offset = 0.1
|
2019-02-08 19:52:11 +08:00
|
|
|
self.wheel_amount = 0.025
|
2019-02-05 17:14:10 +08:00
|
|
|
self._steer_cache = 0.0
|
2019-02-28 21:14:50 +08:00
|
|
|
self.control = None
|
2019-02-21 20:08:08 +08:00
|
|
|
self._autopilot_enabled = False
|
2019-09-17 00:15:36 +08:00
|
|
|
|
|
|
|
# Modules that input will depend on
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud = None
|
|
|
|
self._world = None
|
2019-01-31 02:45:54 +08:00
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
def start(self, hud, world):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Assigns other initialized modules that input module needs."""
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud = hud
|
|
|
|
self._world = world
|
|
|
|
|
|
|
|
self._hud.notification("Press 'H' or '?' for help.", seconds=4.0)
|
2019-01-16 00:17:10 +08:00
|
|
|
|
2019-01-14 18:49:29 +08:00
|
|
|
def render(self, display):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Does nothing. Input module does not need render anything."""
|
2019-01-14 18:49:29 +08:00
|
|
|
|
|
|
|
def tick(self, clock):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Executed each frame. Calls method for parsing input."""
|
2019-02-05 17:14:10 +08:00
|
|
|
self.parse_input(clock)
|
2019-01-11 19:08:04 +08:00
|
|
|
|
|
|
|
def _parse_events(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Parses input events. These events are executed only once when pressing a key."""
|
2019-01-24 03:10:56 +08:00
|
|
|
self.mouse_pos = pygame.mouse.get_pos()
|
2019-01-11 19:08:04 +08:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
2019-01-15 01:44:58 +08:00
|
|
|
exit_game()
|
2019-01-11 19:08:04 +08:00
|
|
|
elif event.type == pygame.KEYUP:
|
2019-02-07 02:40:39 +08:00
|
|
|
if self._is_quit_shortcut(event.key):
|
2019-01-11 21:06:02 +08:00
|
|
|
exit_game()
|
2019-02-07 02:40:39 +08:00
|
|
|
elif event.key == K_h or (event.key == K_SLASH and pygame.key.get_mods() & KMOD_SHIFT):
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud.help.toggle()
|
2019-02-07 02:40:39 +08:00
|
|
|
elif event.key == K_TAB:
|
2019-09-17 00:15:36 +08:00
|
|
|
# Toggle between hero and map mode
|
2019-06-11 17:20:02 +08:00
|
|
|
if self._world.hero_actor is None:
|
|
|
|
self._world.select_hero_actor()
|
2019-02-08 20:25:45 +08:00
|
|
|
self.wheel_offset = HERO_DEFAULT_SCALE
|
2019-02-28 21:14:50 +08:00
|
|
|
self.control = carla.VehicleControl()
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud.notification('Hero Mode')
|
2019-02-06 22:01:38 +08:00
|
|
|
else:
|
2019-02-08 20:25:45 +08:00
|
|
|
self.wheel_offset = MAP_DEFAULT_SCALE
|
2019-02-06 22:01:38 +08:00
|
|
|
self.mouse_offset = [0, 0]
|
2019-02-21 22:30:16 +08:00
|
|
|
self.mouse_pos = [0, 0]
|
2019-06-11 17:20:02 +08:00
|
|
|
self._world.scale_offset = [0, 0]
|
|
|
|
self._world.hero_actor = None
|
|
|
|
self._hud.notification('Map Mode')
|
2019-02-07 02:40:39 +08:00
|
|
|
elif event.key == K_F1:
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud.show_info = not self._hud.show_info
|
2019-02-07 02:40:39 +08:00
|
|
|
elif event.key == K_i:
|
2019-06-11 17:20:02 +08:00
|
|
|
self._hud.show_actor_ids = not self._hud.show_actor_ids
|
2019-02-28 21:14:50 +08:00
|
|
|
elif isinstance(self.control, carla.VehicleControl):
|
2019-02-05 17:14:10 +08:00
|
|
|
if event.key == K_q:
|
2019-02-28 21:14:50 +08:00
|
|
|
self.control.gear = 1 if self.control.reverse else -1
|
2019-02-05 17:14:10 +08:00
|
|
|
elif event.key == K_m:
|
2019-02-28 21:14:50 +08:00
|
|
|
self.control.manual_gear_shift = not self.control.manual_gear_shift
|
2019-06-11 17:20:02 +08:00
|
|
|
self.control.gear = self._world.hero_actor.get_control().gear
|
|
|
|
self._hud.notification('%s Transmission' % (
|
2019-02-28 21:14:50 +08:00
|
|
|
'Manual' if self.control.manual_gear_shift else 'Automatic'))
|
|
|
|
elif self.control.manual_gear_shift and event.key == K_COMMA:
|
|
|
|
self.control.gear = max(-1, self.control.gear - 1)
|
|
|
|
elif self.control.manual_gear_shift and event.key == K_PERIOD:
|
|
|
|
self.control.gear = self.control.gear + 1
|
2019-02-05 17:19:15 +08:00
|
|
|
elif event.key == K_p:
|
2019-09-17 00:15:36 +08:00
|
|
|
# Toggle autopilot
|
2019-06-11 17:20:02 +08:00
|
|
|
if self._world.hero_actor is not None:
|
2019-02-21 22:30:16 +08:00
|
|
|
self._autopilot_enabled = not self._autopilot_enabled
|
2019-06-11 17:20:02 +08:00
|
|
|
self._world.hero_actor.set_autopilot(self._autopilot_enabled)
|
|
|
|
self._hud.notification('Autopilot %s' % ('On' if self._autopilot_enabled else 'Off'))
|
2019-01-15 19:23:50 +08:00
|
|
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
2019-09-17 00:15:36 +08:00
|
|
|
# Handle mouse wheel for zooming in and out
|
2019-01-15 19:23:50 +08:00
|
|
|
if event.button == 4:
|
2019-01-31 20:20:58 +08:00
|
|
|
self.wheel_offset += self.wheel_amount
|
2019-02-08 20:25:45 +08:00
|
|
|
if self.wheel_offset >= 1.0:
|
|
|
|
self.wheel_offset = 1.0
|
2019-02-07 02:40:39 +08:00
|
|
|
elif event.button == 5:
|
2019-01-31 20:20:58 +08:00
|
|
|
self.wheel_offset -= self.wheel_amount
|
2019-02-08 20:25:45 +08:00
|
|
|
if self.wheel_offset <= 0.1:
|
|
|
|
self.wheel_offset = 0.1
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-02-05 17:14:10 +08:00
|
|
|
def _parse_keys(self, milliseconds):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Parses keyboard input when keys are pressed"""
|
2019-02-05 17:14:10 +08:00
|
|
|
keys = pygame.key.get_pressed()
|
2019-02-28 21:14:50 +08:00
|
|
|
self.control.throttle = 1.0 if keys[K_UP] or keys[K_w] else 0.0
|
2019-02-05 17:14:10 +08:00
|
|
|
steer_increment = 5e-4 * milliseconds
|
|
|
|
if keys[K_LEFT] or keys[K_a]:
|
|
|
|
self._steer_cache -= steer_increment
|
|
|
|
elif keys[K_RIGHT] or keys[K_d]:
|
|
|
|
self._steer_cache += steer_increment
|
|
|
|
else:
|
|
|
|
self._steer_cache = 0.0
|
|
|
|
self._steer_cache = min(0.7, max(-0.7, self._steer_cache))
|
2019-02-28 21:14:50 +08:00
|
|
|
self.control.steer = round(self._steer_cache, 1)
|
|
|
|
self.control.brake = 1.0 if keys[K_DOWN] or keys[K_s] else 0.0
|
|
|
|
self.control.hand_brake = keys[K_SPACE]
|
2019-02-05 17:14:10 +08:00
|
|
|
|
2019-01-15 19:23:50 +08:00
|
|
|
def _parse_mouse(self):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Parses mouse input"""
|
2019-01-15 19:23:50 +08:00
|
|
|
if pygame.mouse.get_pressed()[0]:
|
|
|
|
x, y = pygame.mouse.get_pos()
|
2019-02-21 22:30:16 +08:00
|
|
|
self.mouse_offset[0] += (1.0 / self.wheel_offset) * (x - self.mouse_pos[0])
|
|
|
|
self.mouse_offset[1] += (1.0 / self.wheel_offset) * (y - self.mouse_pos[1])
|
2019-01-15 19:23:50 +08:00
|
|
|
self.mouse_pos = (x, y)
|
|
|
|
|
2019-02-05 17:14:10 +08:00
|
|
|
def parse_input(self, clock):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Parses the input, which is classified in keyboard events and mouse"""
|
2019-01-11 19:08:04 +08:00
|
|
|
self._parse_events()
|
2019-01-15 19:23:50 +08:00
|
|
|
self._parse_mouse()
|
2019-02-05 17:14:10 +08:00
|
|
|
if not self._autopilot_enabled:
|
2019-02-28 21:14:50 +08:00
|
|
|
if isinstance(self.control, carla.VehicleControl):
|
2019-02-05 17:14:10 +08:00
|
|
|
self._parse_keys(clock.get_time())
|
2019-02-28 21:14:50 +08:00
|
|
|
self.control.reverse = self.control.gear < 0
|
2019-06-11 17:20:02 +08:00
|
|
|
if (self._world.hero_actor is not None):
|
|
|
|
self._world.hero_actor.apply_control(self.control)
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-02-07 02:40:39 +08:00
|
|
|
@staticmethod
|
|
|
|
def _is_quit_shortcut(key):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Returns True if one of the specified keys are pressed"""
|
2019-02-07 02:40:39 +08:00
|
|
|
return (key == K_ESCAPE) or (key == K_q and pygame.key.get_mods() & KMOD_CTRL)
|
|
|
|
|
2019-01-21 19:12:06 +08:00
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
# -- Game Loop ---------------------------------------------------------------
|
|
|
|
# ==============================================================================
|
2019-01-15 01:44:58 +08:00
|
|
|
|
|
|
|
|
2019-01-11 22:39:54 +08:00
|
|
|
def game_loop(args):
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Initialized, Starts and runs all the needed modules for No Rendering Mode"""
|
2019-04-01 22:50:20 +08:00
|
|
|
try:
|
|
|
|
# Init Pygame
|
|
|
|
pygame.init()
|
|
|
|
display = pygame.display.set_mode(
|
|
|
|
(args.width, args.height),
|
|
|
|
pygame.HWSURFACE | pygame.DOUBLEBUF)
|
2019-09-17 00:15:36 +08:00
|
|
|
|
|
|
|
# Place a title to game window
|
2019-04-01 22:50:20 +08:00
|
|
|
pygame.display.set_caption(args.description)
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Show loading screen
|
2019-04-01 22:50:20 +08:00
|
|
|
font = pygame.font.Font(pygame.font.get_default_font(), 20)
|
|
|
|
text_surface = font.render('Rendering map...', True, COLOR_WHITE)
|
|
|
|
display.blit(text_surface, text_surface.get_rect(center=(args.width / 2, args.height / 2)))
|
|
|
|
pygame.display.flip()
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-06-11 17:20:02 +08:00
|
|
|
# Init
|
|
|
|
input_control = InputControl(TITLE_INPUT)
|
|
|
|
hud = HUD(TITLE_HUD, args.width, args.height)
|
|
|
|
world = World(TITLE_WORLD, args, timeout=2.0)
|
2019-01-11 22:39:54 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# For each module, assign other modules that are going to be used inside that module
|
2019-06-11 17:20:02 +08:00
|
|
|
input_control.start(hud, world)
|
|
|
|
hud.start()
|
|
|
|
world.start(hud, input_control)
|
2019-01-16 00:17:10 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Game loop
|
2019-04-01 22:50:20 +08:00
|
|
|
clock = pygame.time.Clock()
|
|
|
|
while True:
|
|
|
|
clock.tick_busy_loop(60)
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Tick all modules
|
2019-06-11 17:20:02 +08:00
|
|
|
world.tick(clock)
|
|
|
|
hud.tick(clock)
|
|
|
|
input_control.tick(clock)
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Render all modules
|
2019-06-11 17:20:02 +08:00
|
|
|
display.fill(COLOR_ALUMINIUM_4)
|
|
|
|
world.render(display)
|
|
|
|
hud.render(display)
|
|
|
|
input_control.render(display)
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-04-01 22:50:20 +08:00
|
|
|
pygame.display.flip()
|
2019-01-12 00:31:18 +08:00
|
|
|
|
2019-04-01 22:50:20 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print('\nCancelled by user. Bye!')
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-04-01 22:50:20 +08:00
|
|
|
finally:
|
2019-06-11 17:20:02 +08:00
|
|
|
if world is not None:
|
|
|
|
world.destroy()
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
|
2019-01-11 22:39:54 +08:00
|
|
|
def exit_game():
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Shuts down program and PyGame"""
|
2019-01-11 22:39:54 +08:00
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
# ==============================================================================
|
2019-01-15 01:44:58 +08:00
|
|
|
# -- Main --------------------------------------------------------------------
|
2019-01-11 22:39:54 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
2019-01-11 19:08:04 +08:00
|
|
|
def main():
|
2019-09-17 00:15:36 +08:00
|
|
|
"""Parses the arguments received from commandline and runs the game loop"""
|
|
|
|
|
|
|
|
# Define arguments that will be received and parsed
|
2019-01-11 19:08:04 +08:00
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
description='CARLA No Rendering Mode Visualizer')
|
2019-01-11 21:06:02 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'-v', '--verbose',
|
|
|
|
action='store_true',
|
|
|
|
dest='debug',
|
|
|
|
help='print debug information')
|
|
|
|
argparser.add_argument(
|
|
|
|
'--host',
|
|
|
|
metavar='H',
|
|
|
|
default='127.0.0.1',
|
2019-03-29 05:47:21 +08:00
|
|
|
help='IP of the host server (default: 127.0.0.1)')
|
2019-01-11 21:06:02 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'-p', '--port',
|
|
|
|
metavar='P',
|
|
|
|
default=2000,
|
|
|
|
type=int,
|
|
|
|
help='TCP port to listen to (default: 2000)')
|
2019-01-11 19:08:04 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'--res',
|
|
|
|
metavar='WIDTHxHEIGHT',
|
|
|
|
default='1280x720',
|
|
|
|
help='window resolution (default: 1280x720)')
|
2019-02-05 17:14:10 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'--filter',
|
|
|
|
metavar='PATTERN',
|
|
|
|
default='vehicle.*',
|
|
|
|
help='actor filter (default: "vehicle.*")')
|
2019-03-29 05:47:21 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'--map',
|
|
|
|
metavar='TOWN',
|
|
|
|
default=None,
|
|
|
|
help='start a new episode at the given TOWN')
|
2019-03-11 05:20:45 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'--no-rendering',
|
2019-03-29 05:47:21 +08:00
|
|
|
action='store_true',
|
|
|
|
help='switch off server rendering')
|
2019-03-29 01:21:43 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'--show-triggers',
|
|
|
|
action='store_true',
|
|
|
|
help='show trigger boxes of traffic signs')
|
2019-04-01 20:24:34 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'--show-connections',
|
|
|
|
action='store_true',
|
|
|
|
help='show waypoint connections')
|
2019-04-01 22:36:12 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'--show-spawn-points',
|
|
|
|
action='store_true',
|
|
|
|
help='show recommended spawn points')
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Parse arguments
|
2019-01-11 19:08:04 +08:00
|
|
|
args = argparser.parse_args()
|
|
|
|
args.description = argparser.description
|
|
|
|
args.width, args.height = [int(x) for x in args.res.split('x')]
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Print server information
|
2019-01-11 21:06:02 +08:00
|
|
|
log_level = logging.DEBUG if args.debug else logging.INFO
|
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)
|
|
|
|
|
|
|
|
logging.info('listening to server %s:%s', args.host, args.port)
|
|
|
|
print(__doc__)
|
|
|
|
|
2019-09-17 00:15:36 +08:00
|
|
|
# Run game loop
|
2019-04-01 22:50:20 +08:00
|
|
|
game_loop(args)
|
2019-01-11 19:08:04 +08:00
|
|
|
|
2019-05-20 21:24:12 +08:00
|
|
|
|
2019-01-11 19:08:04 +08:00
|
|
|
if __name__ == '__main__':
|
2019-09-17 00:15:36 +08:00
|
|
|
main()
|