Skip to content

Commit

Permalink
feat(LocationSettings): Add drop ratio to grid settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruptein authored Jan 9, 2024
1 parent 4c876e9 commit 5e13397
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 26 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ tech changes will usually be stripped from release notes for the public

## Unreleased

### Added

- New location grid setting: drop ratio
- This is used to indicate how shapes with size info dropped on the map should be resized
- (e.g. a goblin_2x2 will take op 2x2 cells in any setup with dropRatio 1, with dropRatio 0.5 however it would only take up 1x1)
- This addresses an issue where this was not properly working with non ft setups

### Changed

- Vision blocking shapes will now ignore themselves if they are closed
Expand Down
2 changes: 2 additions & 0 deletions client/src/apiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ export interface ApiOptionalLocationOptions {
ground_map_background?: string | null;
underground_map_background?: string | null;
limit_movement_during_initiative?: boolean | null;
drop_ratio?: number | null;
}
export interface ApiLocationCore {
id: number;
Expand All @@ -792,6 +793,7 @@ export interface ApiLocationOptions {
ground_map_background: string;
underground_map_background: string;
limit_movement_during_initiative: boolean;
drop_ratio: number;
}
export interface ApiSpawnInfo {
position: PositionTuple;
Expand Down
13 changes: 9 additions & 4 deletions client/src/game/api/emits/location.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { ApiSpawnInfo, LocationClone, LocationOptionsSet, LocationRename } from "../../../apiTypes";
import type { ServerLocationOptions } from "../../systems/settings/location/models";
import type {
ApiLocationOptions,
ApiSpawnInfo,
LocationClone,
LocationOptionsSet,
LocationRename,
} from "../../../apiTypes";
import { wrapSocket } from "../helpers";
import { socket } from "../socket";

Expand All @@ -22,9 +27,9 @@ export async function requestSpawnInfo(location: number): Promise<ApiSpawnInfo[]
});
}

export function sendLocationOption<T extends keyof ServerLocationOptions>(
export function sendLocationOption<T extends keyof ApiLocationOptions>(
key: T,
value: ServerLocationOptions[T] | undefined,
value: ApiLocationOptions[T] | undefined,
location: number | undefined,
): void {
const data: LocationOptionsSet = { options: { [key]: value ?? null }, location };
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.drop_ratio !== undefined)
locationSettingsSystem.setDropRatio(options.drop_ratio ?? undefined, id, false);

// VISION

Expand Down
1 change: 1 addition & 0 deletions client/src/game/dropAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export async function dropAsset(
if (dimensions?.groups !== undefined) {
const dimX = Number.parseInt(dimensions.groups.x ?? "0");
const dimY = Number.parseInt(dimensions.groups.y ?? "0");
// DropRatio is already accounted for in applyTemplate!!
template = {
width: dimX * DEFAULT_GRID_SIZE,
height: dimY * DEFAULT_GRID_SIZE,
Expand Down
3 changes: 2 additions & 1 deletion client/src/game/shapes/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export function applyTemplate<T extends ApiShape>(shape: T, template: BaseTempla
shape.auras.push({ ...defaultAura, ...auraTemplate });
}

const gridRescale = 5 / locationSettingsState.raw.unitSize.value;
// This also handles dropAsset rescaling
const gridRescale = locationSettingsState.raw.dropRatio.value;

// Shape specific keys
for (const key of getTemplateKeys(shape.type_ as SHAPE_TYPE)) {
Expand Down
6 changes: 6 additions & 0 deletions client/src/game/systems/settings/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ class LocationSettingsSystem implements System {
if (sync) sendLocationOption("unit_size_unit", unitSizeUnit, location);
}

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

if (sync) sendLocationOption("drop_ratio", dropRatio, location);
}

// VISION

setFullFow(fullFow: boolean | undefined, location: number | undefined, sync: boolean): void {
Expand Down
21 changes: 1 addition & 20 deletions client/src/game/systems/settings/location/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,6 @@ export interface WithLocationDefault<T> {
location: Record<number, T | undefined>;
}

export interface ServerLocationOptions {
use_grid: boolean;
grid_type: string;
unit_size: number;
unit_size_unit: string;
full_fow: boolean;
fow_opacity: number;
fow_los: boolean;
vision_mode: string;
vision_min_range: number;
vision_max_range: number;
spawn_locations: string;
move_player_on_token_change: boolean;
limit_movement_during_initiative: boolean;

air_map_background: string;
ground_map_background: string;
underground_map_background: string;
}

export interface LocationOptions {
useGrid: boolean;
gridType: string;
Expand All @@ -47,6 +27,7 @@ export interface LocationOptions {
spawnLocations: GlobalId[];
movePlayerOnTokenChange: boolean;
limitMovementDuringInitiative: boolean;
dropRatio: number;

airMapBackground: string;
groundMapBackground: string;
Expand Down
1 change: 1 addition & 0 deletions client/src/game/systems/settings/location/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function getInitState(): State {
visionMinRange: init(0),
visionMode: init(""),
limitMovementDuringInitiative: init(false),
dropRatio: init(1),

airMapBackground: init("none"),
groundMapBackground: init("none"),
Expand Down
30 changes: 30 additions & 0 deletions client/src/game/ui/settings/location/GridSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ const unitSizeUnit = computed({
},
});
const dropRatio = computed({
get() {
return getOption($.dropRatio, location.value).value;
},
set(dropRatio: number | undefined) {
lss.setDropRatio(dropRatio, location.value, true);
},
});
function o(k: any): boolean {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return getOption(k, location.value).override !== undefined;
Expand Down Expand Up @@ -137,6 +146,27 @@ function o(k: any): boolean {
</div>
<div v-else></div>
</div>
<div class="row" :class="{ overwritten: !global && o($.dropRatio) }">
<div>
<label
:for="'dropRatioInput-' + location"
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."
>
Drop Ratio
</label>
</div>
<div>
<input :id="'dropRatioInput-' + location" v-model.number="dropRatio" type="number" step="any" />
</div>
<div
v-if="!global && o($.dropRatio)"
:title="t('game.ui.settings.common.reset_default')"
@click="dropRatio = undefined"
>
<font-awesome-icon icon="times-circle" />
</div>
<div v-else></div>
</div>
</div>
</template>

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 @@ -21,6 +21,7 @@ class ApiLocationOptions(BaseModel):
ground_map_background: str
underground_map_background: str
limit_movement_during_initiative: bool
drop_ratio: float


class ApiOptionalLocationOptions(TypeIdModel):
Expand All @@ -41,6 +42,7 @@ class ApiOptionalLocationOptions(TypeIdModel):
ground_map_background: str | None = Field(default=None, noneAsNull=True)
underground_map_background: str | None = Field(default=None, noneAsNull=True)
limit_movement_during_initiative: bool | None = Field(default=None, noneAsNull=True)
drop_ratio: float | None = Field(default=None, noneAsNull=True)


class LocationSettingsSet(BaseModel):
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 @@ -64,6 +64,7 @@ class LocationOptionKeys(TypedDict, total=False):
vision_min_range: float
vision_max_range: float
spawn_locations: str
drop_ratio: float


@sio.on("Location.Load", namespace=GAME_NS)
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 @@ -30,6 +30,7 @@ class LocationOptions(BaseDbModel):
limit_movement_during_initiative = cast(
bool | None, BooleanField(default=False, null=True)
)
drop_ratio = cast(float | None, FloatField(default=1.0, null=True))

@classmethod
def create_empty(cls):
Expand All @@ -49,6 +50,7 @@ def create_empty(cls):
ground_map_background=None,
underground_map_background=None,
limit_movement_during_initiative=None,
drop_ratio=None,
)

@overload
Expand Down Expand Up @@ -86,4 +88,5 @@ def as_pydantic(self, optional: bool):
underground_map_background=self.underground_map_background, # type: ignore
limit_movement_during_initiative=self.limit_movement_during_initiative, # type: ignore
spawn_locations=self.spawn_locations,
drop_ratio=self.drop_ratio, # type: ignore
)
11 changes: 10 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 = 89
SAVE_VERSION = 90

import json
import logging
Expand Down Expand Up @@ -611,6 +611,15 @@ def upgrade(db: SqliteExtDatabase, version: int):
'INSERT INTO "shape" ("uuid", "layer_id", "type_", "x", "y", "name", "name_visible", "fill_colour", "stroke_colour", "vision_obstruction", "movement_obstruction", "is_token", "draw_operator", "index", "options", "badge", "show_badge", "default_edit_access", "default_vision_access", "is_invisible", "default_movement_access", "is_locked", "angle", "stroke_width", "asset_id", "group_id", "ignore_zoom_size", "is_defeated", "is_door", "is_teleport_zone", "character_id") SELECT "uuid", "layer_id", "type_", "x", "y", "name", "name_visible", "fill_colour", "stroke_colour", "vision_obstruction", "movement_obstruction", "is_token", "draw_operator", "index", "options", "badge", "show_badge", "default_edit_access", "default_vision_access", "is_invisible", "default_movement_access", "is_locked", "angle", "stroke_width", "asset_id", "group_id", "ignore_zoom_size", "is_defeated", "is_door", "is_teleport_zone", "character_id" FROM _shape_88'
)
db.execute_sql("DROP TABLE _shape_88")
elif version == 89:
# Add LocationOptions.drop_ratio
with db.atomic():
db.execute_sql(
"ALTER TABLE location_options ADD COLUMN drop_ratio REAL DEFAULT 1"
)
db.execute_sql(
"UPDATE location_options SET drop_ratio = NULL WHERE id NOT IN (SELECT default_options_id FROM room)"
)
else:
raise UnknownVersionException(
f"No upgrade code for save format {version} was found."
Expand Down

0 comments on commit 5e13397

Please sign in to comment.