Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ tech changes will usually be stripped from release notes for the public
- Logging:
- Enabling configuration of the logger via a new logging section
- Allows for multiple logging file streams at different levels
- Ruler:
- Enabling configuration of grid-mode's style of measuring diagonal
movement.

### Changed

Expand Down
14 changes: 8 additions & 6 deletions client/src/apiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export type DefaultNoteFilter = "NO_FILTER" | "ACTIVE_FILTER" | "NO_LINK_FILTER"
export type InitiativeDirection = -1 | 0 | 1;

export interface ApiShapeSize {
x: number;
y: number;
x: number;
y: number;
}

export interface ApiAsset {
Expand Down Expand Up @@ -817,9 +817,9 @@ export interface ShapeTemplateAdd {
shapeId: GlobalId;
name: string;
}
export interface ShapeSetSizeValue {
shape: GlobalId;
value: ApiShapeSize;
export interface ShapeSetSizeValue {
shape: GlobalId;
value: ApiShapeSize;
}
export interface ShapeTextSizeUpdate {
uuid: GlobalId;
Expand Down Expand Up @@ -856,7 +856,7 @@ export interface ToggleCompositeVariant {
shape: GlobalId;
variant: GlobalId;
}
export interface TypeIdModel {}
export interface TypeIdModel { }
export interface ApiLocation {
id: number;
name: string;
Expand All @@ -867,6 +867,7 @@ export interface ApiOptionalLocationOptions {
unit_size?: number | null;
unit_size_unit?: string | null;
use_grid?: boolean | null;
grid_mode_ruler_type?: string | null;
full_fow?: boolean | null;
fow_opacity?: number | null;
fow_los?: boolean | null;
Expand All @@ -891,6 +892,7 @@ export interface ApiLocationOptions {
unit_size: number;
unit_size_unit: string;
use_grid: boolean;
grid_mode_ruler_type: string;
full_fow: boolean;
fow_opacity: number;
fow_los: boolean;
Expand Down
8 changes: 8 additions & 0 deletions client/src/core/grid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export enum GridType {
FlatHex = "FLAT_HEX",
}

export enum GridModeRulerType {
Unchanged = "UNCHANGED",
Alternating = "ALTERNATING",
Manhattan = "MANHATTAN",
Euclidean = "EUCLIDEAN",
EuclideanApprox = "EUCLIDEAN_APPROX",
}

export function getClosestCellCenter(position: GlobalPoint, gridType: GridType): GlobalPoint {
return getCellCenter(getCellFromPoint(position, gridType), gridType);
}
Expand Down
2 changes: 2 additions & 0 deletions client/src/game/api/events/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function setLocationOptions(id: number | undefined, options: ApiOptionalLocation
locationSettingsSystem.setUnitSize(options.unit_size ?? undefined, id, false);
if (overwrite_all || options.unit_size_unit !== undefined)
locationSettingsSystem.setUnitSizeUnit(options.unit_size_unit ?? undefined, id, false);
if (overwrite_all || options.grid_mode_ruler_type !== undefined)
locationSettingsSystem.setGridModeRulerType(options.grid_mode_ruler_type ?? undefined, id, false);
if (overwrite_all || options.drop_ratio !== undefined)
locationSettingsSystem.setDropRatio(options.drop_ratio ?? undefined, id, false);

Expand Down
13 changes: 13 additions & 0 deletions client/src/game/systems/settings/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class LocationSettingsSystem implements System {
gridType: this.setGridType.bind(this),
unitSize: this.setUnitSize.bind(this),
unitSizeUnit: this.setUnitSizeUnit.bind(this),
gridModeRulerType: this.setGridModeRulerType.bind(this),
dropRatio: this.setDropRatio.bind(this),
fullFow: this.setFullFow.bind(this),
fowLos: this.setFowLos.bind(this),
Expand Down Expand Up @@ -138,6 +139,18 @@ class LocationSettingsSystem implements System {
if (sync) sendLocationOption("unit_size_unit", unitSizeUnit, location);
}

setGridModeRulerType(gridModeRulerType: string | undefined, location: number | undefined, sync: boolean): void {
if (
gridModeRulerType !== undefined &&
!["UNCHANGED", "ALTERNATING", "MANHATTAN", "EUCLIDEAN", "EUCLIDEAN_APPROX"].includes(gridModeRulerType)
) {
throw new Error("Unknown grid type set");
}
if (!this.setValue($.gridModeRulerType, gridModeRulerType, location)) return;

if (sync) sendLocationOption("grid_mode_ruler_type", gridModeRulerType, location);
}

setDropRatio(dropRatio: number | undefined, location: number | undefined, sync: boolean): void {
if (!this.setValue($.dropRatio, dropRatio, location)) return;

Expand Down
3 changes: 2 additions & 1 deletion client/src/game/systems/settings/location/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GridType } from "../../../../core/grid";
import type { GridType, GridModeRulerType } from "../../../../core/grid";
import type { GlobalId } from "../../../../core/id";

export interface WithDefault<T> {
Expand All @@ -17,6 +17,7 @@ export interface LocationOptions {
gridType: GridType;
unitSize: number;
unitSizeUnit: string;
gridModeRulerType: GridModeRulerType;
fullFow: boolean;
fowOpacity: number;
fowLos: boolean;
Expand Down
3 changes: 2 additions & 1 deletion client/src/game/systems/settings/location/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GridType } from "../../../../core/grid";
import { GridModeRulerType, GridType } from "../../../../core/grid";
import { buildState } from "../../../../core/systems/state";

import type { LocationOptions, WithDefault, WithLocationDefault } from "./models";
Expand All @@ -15,6 +15,7 @@ function getInitState(): _S {
fowLos: init(false),
fowOpacity: init(0),
gridType: init(GridType.Square),
gridModeRulerType: init(GridModeRulerType.Unchanged),
fullFow: init(false),
movePlayerOnTokenChange: init(false),
spawnLocations: init([]),
Expand Down
64 changes: 63 additions & 1 deletion client/src/game/tools/variants/ruler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
getCellFromPoint,
getCellVertices,
getClosestCellCenter,
GridModeRulerType,
GridType,
snapPointToGrid,
} from "../../../core/grid";
import { InvalidationMode, SyncMode } from "../../../core/models/types";
Expand Down Expand Up @@ -60,9 +62,15 @@ class RulerTool extends Tool implements ITool {
private text: Text | undefined = undefined;

private currentLength = 0;
private currentBonusCells = 0;
private currentCellDistance = 0;
private currentLastCell: GlobalPoint | null = null;
private currentDiagonalEndState = true;
private previousLength = 0;
private previousBonusCells = 0;
private previousCellDistance = 0;
private previousLastCell: GlobalPoint | null = null;
private previousDiagonalEndState = true;

get permittedTools(): ToolPermission[] {
return [{ name: ToolName.Select, features: { enabled: [SelectFeatures.Context] } }];
Expand Down Expand Up @@ -199,6 +207,42 @@ class RulerTool extends Tool implements ITool {
const ydiff = Math.abs(end.y - start.y);

let cellDistance = cells.length;
let bonusCells = 0;
let isOddDiagonal = this.previousDiagonalEndState;
const isAlternatingRuler = locationSettingsState.raw.gridModeRulerType.value === GridModeRulerType.Alternating;
const bonusValue = (() => {
switch (locationSettingsState.raw.gridModeRulerType.value) {
case GridModeRulerType.Alternating:
case GridModeRulerType.Manhattan:
return 1;
case GridModeRulerType.Euclidean:
return Math.sqrt(2) - 1;
case GridModeRulerType.EuclideanApprox:
return 0.5;
default:
return 0;
}
})();
const addBonusValue = (): void => {
if (isAlternatingRuler) {
if (!isOddDiagonal) {
bonusCells += bonusValue;
}
isOddDiagonal = !isOddDiagonal;
} else {
bonusCells += bonusValue;
}
};
if (locationSettingsState.raw.gridType.value === GridType.Square) {
if (this.previousLastCell !== null && cells.length > 0 && isDiagonal(this.previousLastCell, cells[0])) {
addBonusValue();
}
for (let i = 1; i < cells.length; i++) {
if (isDiagonal(cells[i - 1], cells[i])) {
addBonusValue();
}
}
}
let unitDistance = cellDistance;
if (!this.gridMode.value) {
unitDistance =
Expand All @@ -207,11 +251,16 @@ class RulerTool extends Tool implements ITool {
if (!this.previousCellDistance) {
cellDistance = Math.max(cellDistance - 1, 0);
}
unitDistance = cellDistance * locationSettingsState.raw.unitSize.value;
unitDistance =
cellDistance * locationSettingsState.raw.unitSize.value +
bonusCells * locationSettingsState.raw.unitSize.value;
}

this.currentLength = unitDistance;
this.currentBonusCells = bonusCells * locationSettingsState.raw.unitSize.value;
this.currentCellDistance = cellDistance;
this.currentLastCell = cells.length > 0 ? (cells[cells.length - 1] ?? null) : null;
this.currentDiagonalEndState = isOddDiagonal;
unitDistance += this.previousLength;
cellDistance += this.previousCellDistance;

Expand Down Expand Up @@ -284,7 +333,10 @@ class RulerTool extends Tool implements ITool {

this.createNewRuler(lastRuler.endPoint, lastRuler.endPoint);
this.previousLength += this.currentLength;
this.previousBonusCells += this.currentBonusCells;
this.previousCellDistance += this.currentCellDistance;
this.previousLastCell = this.currentLastCell;
this.previousDiagonalEndState = this.currentDiagonalEndState;

layer.moveShapeOrder(
this.text!,
Expand Down Expand Up @@ -343,8 +395,18 @@ class RulerTool extends Tool implements ITool {
this.startPoint = this.text = undefined;
this.rulers = [];
this.previousLength = 0;
this.previousBonusCells = 0;
this.previousCellDistance = 0;
this.previousLastCell = null;
this.previousDiagonalEndState = true;
}
}

// Assumes cell1 and cell2 are square cells and are 1 cell away from each other
function isDiagonal(cell1: GlobalPoint | undefined, cell2: GlobalPoint | undefined): boolean {
if (cell1 === undefined || cell2 === undefined) return false;

return !(cell1.x === cell2.x || cell1.y === cell2.y);
}

export const rulerTool = new RulerTool();
18 changes: 18 additions & 0 deletions client/src/game/ui/settings/location/GridSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const location = computed(() => (props.global ? undefined : uiState.reactive.ope

const useGrid = useLocationSettings("useGrid", location);
const gridType = useLocationSettings("gridType", location);
const gridModeRulerType = useLocationSettings("gridModeRulerType", location);

const unitSize = useLocationSettings("unitSize", location);
const unitSizeUnit = useLocationSettings("unitSizeUnit", location);
Expand Down Expand Up @@ -69,6 +70,23 @@ const dropRatio = useLocationSettings("dropRatio", location);
<input :id="'unitSizeInput-' + location" v-model.number="unitSize" type="number" step="any" />
</div>
</ResetWrapper>
<ResetWrapper :global="global" :location="location" setting="gridModeRulerType">
<label
:for="'gridModeRulerType-' + location"
:title="t('game.ui.settings.GridSettings.grid_mode_ruler_type_title')"
>
{{ t("game.ui.settings.GridSettings.grid_mode_ruler_type") }}
</label>
<div>
<select :id="'gridModeRulerType-' + location" v-model="gridModeRulerType">
<option value="UNCHANGED">{{ t("game.ui.settings.GridSettings.UNCHANGED") }}</option>
<option value="ALTERNATING">{{ t("game.ui.settings.GridSettings.ALTERNATING") }}</option>
<option value="MANHATTAN">{{ t("game.ui.settings.GridSettings.MANHATTAN") }}</option>
<option value="EUCLIDEAN">{{ t("game.ui.settings.GridSettings.EUCLIDEAN") }}</option>
<option value="EUCLIDEAN_APPROX">{{ t("game.ui.settings.GridSettings.EUCLIDEAN_APPROX") }}</option>
</select>
</div>
</ResetWrapper>
<ResetWrapper :global="global" :location="location" setting="dropRatio">
<div>
<label :for="'dropRatioInput-' + location" :title="t('game.ui.settings.GridSettings.drop_ratio_title')">
Expand Down
7 changes: 7 additions & 0 deletions client/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,18 @@
"GridSettings": {
"unit_size_in_UNIT": "Unit Size (in {unit})",
"use_grid": "Use grid",
"grid_mode_ruler_type": "Grid-Mode Ruler Diagonal Behavior",
"grid_mode_ruler_type_title": "Different systems handle square-grid diagonals differently. This setting changes how the ruler treats diagonal movement in Grid Mode.",
"size_unit": "Size Unit",
"grid_type": "Grid Type",
"SQUARE": "Squares",
"POINTY_HEX": "Pointy Hexagons",
"FLAT_HEX": "Flat Hexagons",
"UNCHANGED": "Default (D&D>=4e)",
"ALTERNATING": "Alternating (PF/D&D<=3.5e)",
"MANHATTAN": "Manhattan (2x)",
"EUCLIDEAN": "Euclidean (√2x)",
"EUCLIDEAN_APPROX": "Approximation (1.5x)",
"drop_ratio": "Drop Ratio",
"drop_ratio_title": "The drop ratio is used for shapes with size information (e.g. goblin_1x1). The drop ratio is applied to this info to determine the final size."
},
Expand Down
2 changes: 2 additions & 0 deletions server/src/api/models/location/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ApiLocationOptions(BaseModel):
unit_size: float
unit_size_unit: str
use_grid: bool
grid_mode_ruler_type: str
full_fow: bool
fow_opacity: float
fow_los: bool
Expand All @@ -29,6 +30,7 @@ class ApiOptionalLocationOptions(TypeIdModel):
unit_size: float | None = None
unit_size_unit: str | None = None
use_grid: bool | None = None
grid_mode_ruler_type: str | None = None
full_fow: bool | None = None
fow_opacity: float | None = None
fow_los: bool | None = None
Expand Down
1 change: 1 addition & 0 deletions server/src/api/socket/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class LocationOptionKeys(TypedDict, total=False):
unit_size: float
unit_size_unit: str
use_grid: bool
grid_mode_ruler_type: str
grid_type: str
full_fow: bool
fow_opacity: float
Expand Down
3 changes: 3 additions & 0 deletions server/src/db/models/location_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class LocationOptions(BaseDbModel):
unit_size = cast(float | None, FloatField(default=5, null=True))
unit_size_unit = cast(str | None, TextField(default="ft", null=True))
use_grid = cast(bool | None, BooleanField(default=True, null=True))
grid_mode_ruler_type = cast(str | None, TextField(default="UNCHANGED", null=True))
full_fow = cast(bool | None, BooleanField(default=False, null=True))
fow_opacity = cast(float | None, FloatField(default=0.3, null=True))
fow_los = cast(bool | None, BooleanField(default=False, null=True))
Expand All @@ -35,6 +36,7 @@ def create_empty(cls):
unit_size_unit=None,
grid_type=None,
use_grid=None,
grid_mode_ruler_type=None,
full_fow=None,
fow_opacity=None,
fow_los=None,
Expand Down Expand Up @@ -67,6 +69,7 @@ def as_pydantic(self, optional: bool):
unit_size_unit=self.unit_size_unit, # type: ignore
grid_type=self.grid_type, # type: ignore
use_grid=self.use_grid, # type: ignore
grid_mode_ruler_type=self.grid_mode_ruler_type, # type: ignore
full_fow=self.full_fow, # type: ignore
fow_opacity=self.fow_opacity, # type: ignore
fow_los=self.fow_los, # type: ignore
Expand Down
9 changes: 8 additions & 1 deletion server/src/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- e.g. a column added to Circle also needs to be added to CircularToken
"""

SAVE_VERSION = 113
SAVE_VERSION = 114

import asyncio
import json
Expand Down Expand Up @@ -691,6 +691,13 @@ def upgrade(
db.execute_sql(
'INSERT INTO "text" ("shape_id", "text", "font_size") SELECT "shape_id", "text", "font_size" FROM _text_112'
)
elif version == 113:
# Add grid_mode_ruler_type column to location options
with db.atomic():
db.execute_sql("ALTER TABLE location_options ADD COLUMN grid_mode_ruler_type TEXT DEFAULT 'UNCHANGED'")
db.execute_sql(
"UPDATE location_options SET grid_mode_ruler_type = NULL WHERE id NOT IN (SELECT default_options_id FROM room)"
)
else:
raise UnknownVersionException(f"No upgrade code for save format {version} was found.")
inc_save_version(db)
Expand Down
Loading