|
|
|
@@ -0,0 +1,268 @@
|
|
|
|
|
## The island as a hand-drawn nautical chart (A11): paper land with ink contours every
|
|
|
|
|
## CONTOUR_METRES, a heavier coastline, the sea in the sea's ink, place names, the
|
|
|
|
|
## barge's route, the player's own mark, and the tide forecast: land the coming tide
|
|
|
|
|
## will flood is hatched, and land the sea has already taken is washed with the sea.
|
|
|
|
|
##
|
|
|
|
|
## The forecast comes from the client's own Flood (Flood.forecast), the same spreading
|
|
|
|
|
## the simulation does, so it accounts for the breakwaters standing now.
|
|
|
|
|
##
|
|
|
|
|
## One ChartMap draws either the full chart (FULL, opened with the map key) or the
|
|
|
|
|
## minimap (MINI: a square of the chart around the player, north up). setup() once per
|
|
|
|
|
## world; refresh() each frame with where the player is.
|
|
|
|
|
class_name ChartMap
|
|
|
|
|
extends Control
|
|
|
|
|
|
|
|
|
|
enum Mode { FULL, MINI }
|
|
|
|
|
|
|
|
|
|
## Chart pixels per side: 2 m each over the 1024 m world.
|
|
|
|
|
const PIXELS: int = 512
|
|
|
|
|
const CONTOUR_METRES: float = 5.0
|
|
|
|
|
const MAJOR_CONTOUR_METRES: float = 25.0
|
|
|
|
|
## The minimap shows this many metres across.
|
|
|
|
|
const MINI_METRES: float = 240.0
|
|
|
|
|
## How often the forecast overlay may be redrawn, in seconds.
|
|
|
|
|
const OVERLAY_SECONDS: float = 1.0
|
|
|
|
|
const FONT_SIZE: int = 13
|
|
|
|
|
|
|
|
|
|
var mode: Mode = Mode.FULL
|
|
|
|
|
var world: Simulation = null
|
|
|
|
|
## Place names and where they are, in world metres (x, z).
|
|
|
|
|
var places: Array[Dictionary] = []
|
|
|
|
|
## The barge's route ends, in world metres (x, z), or empty when there's no drop.
|
|
|
|
|
var route: PackedVector2Array = PackedVector2Array()
|
|
|
|
|
## The player: position (x, z) in metres, facing (radians, the simulation's yaw), and
|
|
|
|
|
## whether to draw them.
|
|
|
|
|
var me: Vector2 = Vector2.ZERO
|
|
|
|
|
var facing: float = 0.0
|
|
|
|
|
var show_me: bool = false
|
|
|
|
|
## Teammates' positions (x, z), drawn as hollow marks. Solo matches have none.
|
|
|
|
|
var mates: PackedVector2Array = PackedVector2Array()
|
|
|
|
|
## The forecast as last drawn: the level, and the cells that will newly flood.
|
|
|
|
|
var forecast_level: float = NAN
|
|
|
|
|
var forecast_cells: int = 0
|
|
|
|
|
|
|
|
|
|
var _base: ImageTexture = null
|
|
|
|
|
var _overlay: ImageTexture = null
|
|
|
|
|
var _overlay_image: Image = null
|
|
|
|
|
var _overlay_key: Array = []
|
|
|
|
|
var _overlay_age: float = INF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _init(chart_mode: Mode = Mode.FULL) -> void:
|
|
|
|
|
mode = chart_mode
|
|
|
|
|
mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Draws the chart of `simulation`'s island and names its places from `match_seed`.
|
|
|
|
|
## `drop` is the match's barge route, or null. `share` is another ChartMap already set
|
|
|
|
|
## up for the same world, whose paper and contours are used rather than drawn again.
|
|
|
|
|
func setup(simulation: Simulation, match_seed: int, drop: Drop = null, share: ChartMap = null) -> void:
|
|
|
|
|
world = simulation
|
|
|
|
|
if share != null and share.world == simulation and share._base != null:
|
|
|
|
|
_base = share._base
|
|
|
|
|
else:
|
|
|
|
|
_base = ImageTexture.create_from_image(base_image(simulation.ground))
|
|
|
|
|
_overlay_image = Image.create(PIXELS, PIXELS, false, Image.FORMAT_RGBA8)
|
|
|
|
|
_overlay = ImageTexture.create_from_image(_overlay_image)
|
|
|
|
|
_overlay_key = []
|
|
|
|
|
_overlay_age = INF
|
|
|
|
|
places.clear()
|
|
|
|
|
var kinds: Array[String] = []
|
|
|
|
|
for poi: Poi in simulation.pois.pois:
|
|
|
|
|
kinds.append(poi.kind)
|
|
|
|
|
var names: Array[String] = PlaceNames.names_for(match_seed, kinds)
|
|
|
|
|
for index: int in kinds.size():
|
|
|
|
|
var poi: Poi = simulation.pois.pois[index]
|
|
|
|
|
places.append({"name": names[index], "at": Vector2(poi.origin.x, poi.origin.z), "kind": poi.kind})
|
|
|
|
|
route = PackedVector2Array()
|
|
|
|
|
if drop != null:
|
|
|
|
|
route = PackedVector2Array([Vector2(drop.start.x, drop.start.z), Vector2(drop.finish.x, drop.finish.z)])
|
|
|
|
|
queue_redraw()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Where the player is now; redraws the forecast when the tide or the flood moved on.
|
|
|
|
|
func refresh(position: Vector3, yaw: float, visible_me: bool, delta: float) -> void:
|
|
|
|
|
me = Vector2(position.x, position.z)
|
|
|
|
|
facing = yaw
|
|
|
|
|
show_me = visible_me
|
|
|
|
|
_overlay_age += delta
|
|
|
|
|
if world != null and _overlay_age >= OVERLAY_SECONDS:
|
|
|
|
|
var key: Array = overlay_key(world)
|
|
|
|
|
if key != _overlay_key:
|
|
|
|
|
_overlay_key = key
|
|
|
|
|
_draw_overlay()
|
|
|
|
|
_overlay_age = 0.0
|
|
|
|
|
queue_redraw()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## What the overlay depends on: when none of it changed, the overlay can't have.
|
|
|
|
|
static func overlay_key(sim: Simulation) -> Array:
|
|
|
|
|
var level: float = sim.tide.forecast_at(sim.tick) if sim.tide != null else NAN
|
|
|
|
|
var pours: int = sim.flood.pours if sim.flood != null else -1
|
|
|
|
|
return [level, pours, sim.structures.count()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## The cells the coming tide will newly flood: the forecast less what's flooded now.
|
|
|
|
|
## Empty with no tide or no flood grid.
|
|
|
|
|
static func coming_flood(sim: Simulation) -> PackedByteArray:
|
|
|
|
|
if sim.tide == null or sim.flood == null:
|
|
|
|
|
return PackedByteArray()
|
|
|
|
|
var forecast: PackedByteArray = sim.flood.forecast(sim.tide.forecast_at(sim.tick), sim.structures)
|
|
|
|
|
for index: int in forecast.size():
|
|
|
|
|
if sim.flood.flooded[index] != 0:
|
|
|
|
|
forecast[index] = 0
|
|
|
|
|
return forecast
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## The chart's paper, sea and contours for `ground`, PIXELS square.
|
|
|
|
|
static func base_image(ground: SimGround) -> Image:
|
|
|
|
|
var image: Image = Image.create(PIXELS, PIXELS, false, Image.FORMAT_RGB8)
|
|
|
|
|
var metres: float = Tuning.WORLD_SIZE_METRES / PIXELS
|
|
|
|
|
var heights: PackedFloat32Array = PackedFloat32Array()
|
|
|
|
|
heights.resize((PIXELS + 1) * (PIXELS + 1))
|
|
|
|
|
for py: int in PIXELS + 1:
|
|
|
|
|
for px: int in PIXELS + 1:
|
|
|
|
|
heights[py * (PIXELS + 1) + px] = ground.terrain_height(minf(px * metres, Tuning.WORLD_SIZE_METRES - 0.01), minf(py * metres, Tuning.WORLD_SIZE_METRES - 0.01))
|
|
|
|
|
var sea_shallow: Color = Palette.SEA_INK.lightened(0.45)
|
|
|
|
|
var sea_deep: Color = Palette.SEA_INK.lightened(0.15)
|
|
|
|
|
for py: int in PIXELS:
|
|
|
|
|
for px: int in PIXELS:
|
|
|
|
|
var h: float = heights[py * (PIXELS + 1) + px]
|
|
|
|
|
var right: float = heights[py * (PIXELS + 1) + px + 1]
|
|
|
|
|
var down: float = heights[(py + 1) * (PIXELS + 1) + px]
|
|
|
|
|
var colour: Color
|
|
|
|
|
if h < Tuning.SEA_LEVEL_START:
|
|
|
|
|
colour = sea_shallow.lerp(sea_deep, clampf(-h / Tuning.ISLAND_SEA_DEPTH, 0.0, 1.0))
|
|
|
|
|
else:
|
|
|
|
|
# Paper, faintly shaded where the ground faces away from a north-west light.
|
|
|
|
|
var shade: float = clampf((right - h) * 0.06 + (down - h) * 0.06, -0.12, 0.12)
|
|
|
|
|
colour = Palette.PAPER.darkened(maxf(shade, 0.0)).lightened(maxf(-shade, 0.0) * 0.3)
|
|
|
|
|
var crossed: int = _crossing(h, right, down)
|
|
|
|
|
if crossed == 3:
|
|
|
|
|
colour = Palette.INK
|
|
|
|
|
elif crossed == 2:
|
|
|
|
|
colour = colour.lerp(Palette.INK, 0.65)
|
|
|
|
|
elif crossed == 1:
|
|
|
|
|
colour = colour.lerp(Palette.INK, 0.3)
|
|
|
|
|
image.set_pixel(px, py, colour)
|
|
|
|
|
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 3 for the coastline, 2 for a major contour, 1 for a minor one, 0 for none, between a
|
|
|
|
|
## height and its right and lower neighbours.
|
|
|
|
|
static func _crossing(h: float, right: float, down: float) -> int:
|
|
|
|
|
var sea: float = Tuning.SEA_LEVEL_START
|
|
|
|
|
if (h < sea) != (right < sea) or (h < sea) != (down < sea):
|
|
|
|
|
return 3
|
|
|
|
|
if h < sea:
|
|
|
|
|
return 0
|
|
|
|
|
if floori(h / MAJOR_CONTOUR_METRES) != floori(right / MAJOR_CONTOUR_METRES) or floori(h / MAJOR_CONTOUR_METRES) != floori(down / MAJOR_CONTOUR_METRES):
|
|
|
|
|
return 2
|
|
|
|
|
if floori(h / CONTOUR_METRES) != floori(right / CONTOUR_METRES) or floori(h / CONTOUR_METRES) != floori(down / CONTOUR_METRES):
|
|
|
|
|
return 1
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _draw_overlay() -> void:
|
|
|
|
|
_overlay_image.fill(Color.TRANSPARENT)
|
|
|
|
|
var flood: Flood = world.flood
|
|
|
|
|
forecast_level = world.tide.forecast_at(world.tick) if world.tide != null else NAN
|
|
|
|
|
forecast_cells = 0
|
|
|
|
|
if flood == null:
|
|
|
|
|
_overlay.update(_overlay_image)
|
|
|
|
|
return
|
|
|
|
|
var coming: PackedByteArray = coming_flood(world)
|
|
|
|
|
var per_cell: int = maxi(roundi(PIXELS / float(flood.side)), 1)
|
|
|
|
|
var taken: Color = Color(Palette.SEA_INK.lightened(0.3), 0.7)
|
|
|
|
|
var hatch: Color = Color(Palette.WARNING, 0.95)
|
|
|
|
|
var wash: Color = Color(Palette.WARNING, 0.16)
|
|
|
|
|
for cz: int in flood.side:
|
|
|
|
|
for cx: int in flood.side:
|
|
|
|
|
var index: int = cz * flood.side + cx
|
|
|
|
|
var flooded_land: bool = flood.flooded[index] != 0 and flood.floors[index] >= Tuning.SEA_LEVEL_START - 0.5
|
|
|
|
|
var will_flood: bool = not coming.is_empty() and coming[index] != 0
|
|
|
|
|
if not flooded_land and not will_flood:
|
|
|
|
|
continue
|
|
|
|
|
if will_flood:
|
|
|
|
|
forecast_cells += 1
|
|
|
|
|
for oy: int in per_cell:
|
|
|
|
|
for ox: int in per_cell:
|
|
|
|
|
var px: int = cx * per_cell + ox
|
|
|
|
|
var py: int = cz * per_cell + oy
|
|
|
|
|
if px >= PIXELS or py >= PIXELS:
|
|
|
|
|
continue
|
|
|
|
|
if will_flood:
|
|
|
|
|
# Diagonal hatching, so the warning isn't colour alone.
|
|
|
|
|
_overlay_image.set_pixel(px, py, hatch if (px + py) % 4 < 1 else wash)
|
|
|
|
|
else:
|
|
|
|
|
_overlay_image.set_pixel(px, py, taken)
|
|
|
|
|
_overlay.update(_overlay_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## World metres (x, z) to a point in `rect` showing `view` (a world rectangle).
|
|
|
|
|
static func to_chart(at: Vector2, view: Rect2, rect: Rect2) -> Vector2:
|
|
|
|
|
return rect.position + (at - view.position) / view.size * rect.size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _draw() -> void:
|
|
|
|
|
if _base == null:
|
|
|
|
|
return
|
|
|
|
|
var rect: Rect2 = Rect2(Vector2.ZERO, size)
|
|
|
|
|
var view: Rect2 = Rect2(0.0, 0.0, Tuning.WORLD_SIZE_METRES, Tuning.WORLD_SIZE_METRES)
|
|
|
|
|
if mode == Mode.MINI:
|
|
|
|
|
view = Rect2(me - Vector2(MINI_METRES, MINI_METRES) * 0.5, Vector2(MINI_METRES, MINI_METRES))
|
|
|
|
|
var scale: float = PIXELS / Tuning.WORLD_SIZE_METRES
|
|
|
|
|
var source: Rect2 = Rect2(view.position * scale, view.size * scale)
|
|
|
|
|
# Paper margin, then the chart.
|
|
|
|
|
draw_rect(rect.grow(6.0), Palette.PAPER)
|
|
|
|
|
draw_texture_rect_region(_base, rect, source)
|
|
|
|
|
draw_texture_rect_region(_overlay, rect, source)
|
|
|
|
|
var font: Font = ThemeDB.fallback_font
|
|
|
|
|
if route.size() == 2 and mode == Mode.FULL:
|
|
|
|
|
_dashed(to_chart(route[0], view, rect), to_chart(route[1], view, rect), Palette.INK, 2.0)
|
|
|
|
|
for place: Dictionary in places:
|
|
|
|
|
var at: Vector2 = to_chart(place["at"], view, rect)
|
|
|
|
|
if not rect.has_point(at):
|
|
|
|
|
continue
|
|
|
|
|
draw_circle(at, 3.5, Palette.INK)
|
|
|
|
|
if mode == Mode.FULL:
|
|
|
|
|
var text: String = String(place["name"])
|
|
|
|
|
var width: float = font.get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, FONT_SIZE).x
|
|
|
|
|
var spot: Vector2 = at + Vector2(-width * 0.5, -7.0)
|
|
|
|
|
spot.x = clampf(spot.x, rect.position.x + 2.0, rect.end.x - width - 2.0)
|
|
|
|
|
draw_string_outline(font, spot, text, HORIZONTAL_ALIGNMENT_LEFT, -1, FONT_SIZE, 4, Palette.PAPER)
|
|
|
|
|
draw_string(font, spot, text, HORIZONTAL_ALIGNMENT_LEFT, -1, FONT_SIZE, Palette.INK)
|
|
|
|
|
for mate: Vector2 in mates:
|
|
|
|
|
var at: Vector2 = to_chart(mate, view, rect)
|
|
|
|
|
if rect.has_point(at):
|
|
|
|
|
draw_arc(at, 6.0, 0.0, TAU, 16, Palette.INK, 2.0)
|
|
|
|
|
if show_me:
|
|
|
|
|
var at: Vector2 = to_chart(me, view, rect)
|
|
|
|
|
var ahead: Vector2 = Vector2(sin(facing), cos(facing))
|
|
|
|
|
var side: Vector2 = Vector2(ahead.y, -ahead.x)
|
|
|
|
|
var arrow: PackedVector2Array = PackedVector2Array([at + ahead * 11.0, at - ahead * 7.0 + side * 7.0, at - ahead * 3.0, at - ahead * 7.0 - side * 7.0])
|
|
|
|
|
draw_colored_polygon(arrow, Palette.OILSKIN)
|
|
|
|
|
arrow.append(arrow[0])
|
|
|
|
|
draw_polyline(arrow, Palette.INK, 2.0)
|
|
|
|
|
# North, and the frame's ink edge.
|
|
|
|
|
var north: Vector2 = rect.position + Vector2(rect.size.x - 18.0, 22.0)
|
|
|
|
|
draw_string_outline(font, north + Vector2(-5.0, 0.0), "N", HORIZONTAL_ALIGNMENT_LEFT, -1, FONT_SIZE + 2, 4, Palette.PAPER)
|
|
|
|
|
draw_string(font, north + Vector2(-5.0, 0.0), "N", HORIZONTAL_ALIGNMENT_LEFT, -1, FONT_SIZE + 2, Palette.INK)
|
|
|
|
|
draw_line(north + Vector2(0.0, 4.0), north + Vector2(0.0, 18.0), Palette.INK, 2.0)
|
|
|
|
|
draw_rect(rect, Palette.INK, false, 3.0)
|
|
|
|
|
if mode == Mode.FULL:
|
|
|
|
|
var legend: String = "Hatched: floods at the next tide (to %.0f m)" % forecast_level if forecast_cells > 0 else "Nothing more floods at the next tide"
|
|
|
|
|
if is_nan(forecast_level):
|
|
|
|
|
legend = "The tide hasn't turned yet"
|
|
|
|
|
var spot: Vector2 = rect.position + Vector2(10.0, rect.size.y - 10.0)
|
|
|
|
|
draw_string_outline(font, spot, legend, HORIZONTAL_ALIGNMENT_LEFT, -1, FONT_SIZE, 4, Palette.PAPER)
|
|
|
|
|
draw_string(font, spot, legend, HORIZONTAL_ALIGNMENT_LEFT, -1, FONT_SIZE, Palette.INK)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _dashed(from: Vector2, to: Vector2, colour: Color, width: float) -> void:
|
|
|
|
|
var length: float = from.distance_to(to)
|
|
|
|
|
var step: float = 12.0
|
|
|
|
|
var along: float = 0.0
|
|
|
|
|
while along < length:
|
|
|
|
|
draw_line(from.lerp(to, along / length), from.lerp(to, minf(along + step * 0.6, length) / length), colour, width)
|
|
|
|
|
along += step
|