Add navmesh support to the new map.

https://github.com/dcs-liberation/dcs_liberation/issues/2039
This commit is contained in:
Dan Albert
2022-03-06 23:07:24 -08:00
parent b08b91ca2e
commit 15176223fa
11 changed files with 181 additions and 33 deletions

View File

@@ -9,6 +9,7 @@ from game.server.flights.models import FlightJs
from game.server.frontlines.models import FrontLineJs
from game.server.leaflet import LeafletPoint
from game.server.mapzones.models import ThreatZoneContainerJs
from game.server.navmesh.models import NavMeshesJs
from game.server.supplyroutes.models import SupplyRouteJs
from game.server.tgos.models import TgoJs
@@ -23,6 +24,7 @@ class GameJs(BaseModel):
front_lines: list[FrontLineJs]
flights: list[FlightJs]
threat_zones: ThreatZoneContainerJs
navmeshes: NavMeshesJs
map_center: LeafletPoint
class Config:
@@ -37,5 +39,6 @@ class GameJs(BaseModel):
front_lines=FrontLineJs.all_in_game(game),
flights=FlightJs.all_in_game(game, with_waypoints=True),
threat_zones=ThreatZoneContainerJs.for_game(game),
navmeshes=NavMeshesJs.from_game(game),
map_center=game.theater.terrain.map_view_default.position.latlng(),
)

View File

@@ -1,8 +1,14 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from pydantic import BaseModel
from game.server.leaflet import LeafletPoly
from game.server.leaflet import LeafletPoly, ShapelyUtil
if TYPE_CHECKING:
from game import Game
from game.navmesh import NavMesh
class NavMeshPolyJs(BaseModel):
@@ -11,3 +17,37 @@ class NavMeshPolyJs(BaseModel):
class Config:
title = "NavMeshPoly"
class NavMeshJs(BaseModel):
polys: list[NavMeshPolyJs]
class Config:
title = "NavMesh"
@staticmethod
def from_navmesh(navmesh: NavMesh, game: Game) -> NavMeshJs:
return NavMeshJs(
polys=[
NavMeshPolyJs(
poly=ShapelyUtil.poly_to_leaflet(p.poly, game.theater),
threatened=p.threatened,
)
for p in navmesh.polys
]
)
class NavMeshesJs(BaseModel):
blue: NavMeshJs
red: NavMeshJs
class Config:
title = "NavMeshes"
@staticmethod
def from_game(game: Game) -> NavMeshesJs:
return NavMeshesJs(
blue=NavMeshJs.from_navmesh(game.blue.nav_mesh, game),
red=NavMeshJs.from_navmesh(game.red.nav_mesh, game),
)

View File

@@ -2,21 +2,12 @@ from fastapi import APIRouter, Depends
from game import Game
from game.server import GameContext
from .models import NavMeshPolyJs
from ..leaflet import ShapelyUtil
from .models import NavMeshJs
router: APIRouter = APIRouter(prefix="/navmesh")
@router.get("/", operation_id="get_navmesh", response_model=list[NavMeshPolyJs])
def get(
for_player: bool, game: Game = Depends(GameContext.require)
) -> list[NavMeshPolyJs]:
@router.get("/", operation_id="get_navmesh", response_model=NavMeshJs)
def get(for_player: bool, game: Game = Depends(GameContext.require)) -> NavMeshJs:
mesh = game.coalition_for(for_player).nav_mesh
return [
NavMeshPolyJs(
poly=ShapelyUtil.poly_to_leaflet(p.poly, game.theater),
threatened=p.threatened,
)
for p in mesh.polys
]
return NavMeshJs.from_navmesh(mesh, game)