Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ri): optimistic stamina during movement #88

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 36 additions & 13 deletions packages/ri/client/src/layers/Headless/api/moveEntity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { getComponentValue, EntityIndex, EntityID, setComponent, Type, Component, World } from "@latticexyz/recs";
import {
getComponentValue,
EntityIndex,
EntityID,
setComponent,
Type,
Component,
World,
ComponentValue,
} from "@latticexyz/recs";
import { ActionSystem } from "../systems";
import { NetworkLayer } from "../../Network";
import { WorldCoord } from "../../../types";
Expand All @@ -16,7 +25,7 @@ export function moveEntity(
) {
const {
network: {
components: { Position, Movable, Untraversable, OwnedBy },
components: { Position, Movable, Untraversable, OwnedBy, Stamina },
network: { connectedAddress },
api: networkApi,
},
Expand Down Expand Up @@ -45,22 +54,35 @@ export function moveEntity(

actions.add<
// Need to debug why typescript can't automatically infer these in this case, but for now manually typing removes the error
{ Position: typeof Position; Untraversable: typeof Untraversable; LocalStamina: typeof LocalStamina },
{ targetPosition: WorldCoord; path: WorldCoord[]; netStamina: number }
{
Position: typeof Position;
Untraversable: typeof Untraversable;
LocalStamina: typeof LocalStamina;
Stamina: typeof Stamina;
},
{ targetPosition: WorldCoord; path: WorldCoord[]; newStamina: ComponentValue }
>({
id: actionID,
components: {
Position,
Untraversable,
LocalStamina,
Stamina,
},
requirement: ({ LocalStamina, Position }) => {
requirement: ({ LocalStamina, Stamina, Position }) => {
const localStamina = getComponentValue(LocalStamina, entity);
if (!localStamina) {
console.warn("no local stamina");
actions.cancel(actionID);
return null;
}

const stamina = getComponentValue(Stamina, entity);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to check if the real Stamina instead of LocalStamina exists? is the optimistic stamina update not removing local stamina in every case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah this was only accessed because i thought we couldn't do partial update, fixing

if (!stamina) {
actions.cancel(actionID);
return null;
}

const netStamina = localStamina.current - 1;
if (netStamina < 0) {
console.warn("net stamina below 0");
Expand All @@ -82,25 +104,26 @@ export function moveEntity(
return {
targetPosition,
path,
netStamina,
newStamina: {
...stamina,
current: netStamina,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the update declaration accepts partial overrides, so only setting the new current value would be sufficient. (Would we still need to get the current Stamina value (line 80) if that was the case?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep you're right, we can lose it, just fixed

};
},
updates: (_, { targetPosition, netStamina }) => [
updates: (_, { targetPosition, newStamina }) => [
{
component: "Position",
entity: entity,
value: targetPosition,
},
{
component: "LocalStamina",
component: "Stamina",
entity,
value: { current: netStamina },
value: newStamina,
},
],
execute: async ({ path, netStamina }) => {
const tx = await networkApi.moveEntity(world.entities[entity], path);
await tx.wait();
setComponent(LocalStamina, entity, { current: netStamina });
execute: async ({ path }) => {
return networkApi.moveEntity(world.entities[entity], path);
},
});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { defineQuery, defineRxSystem, EntityIndex, getComponentValueStrict, Has, setComponent } from "@latticexyz/recs";
import {
defineComponentSystem,
defineQuery,
defineRxSystem,
EntityIndex,
getComponentValueStrict,
Has,
setComponent,
UpdateType,
} from "@latticexyz/recs";
import { getCurrentTurn } from "@latticexyz/std-client";
import { merge } from "rxjs";
import { HeadlessLayer } from "../../types";

export function createCurrentStaminaSystem(layer: HeadlessLayer) {
Expand All @@ -13,13 +21,17 @@ export function createCurrentStaminaSystem(layer: HeadlessLayer) {
components: { Stamina, LastActionTurn, GameConfig },
},
},
actions: { withOptimisticUpdates },
components: { LocalStamina },
} = layer;

const updateLocalStaminaToTurn = (entity: EntityIndex, turn: number) => {
const contractStamina = getComponentValueStrict(Stamina, entity);
const optimisticStamina = withOptimisticUpdates(Stamina);

const setLocalStaminaToCurrentTurn = (entity: EntityIndex) => {
const currentTurn = getCurrentTurn(layer.world, GameConfig, clock);
const contractStamina = getComponentValueStrict(optimisticStamina, entity);
const lastActionTurn = getComponentValueStrict(LastActionTurn, entity).value;
const staminaTicks = (turn - lastActionTurn) * contractStamina.regeneration;
const staminaTicks = (currentTurn - lastActionTurn) * contractStamina.regeneration;

let localStamina = contractStamina.current + staminaTicks;
if (localStamina > contractStamina.max) localStamina = contractStamina.max;
Expand All @@ -28,14 +40,19 @@ export function createCurrentStaminaSystem(layer: HeadlessLayer) {
setComponent(LocalStamina, entity, { current: localStamina });
};

defineComponentSystem(world, optimisticStamina, ({ entity }) => {
setComponent(LocalStamina, entity, { current: getComponentValueStrict(optimisticStamina, entity).current });
});

const staminaQuery = defineQuery([Has(Stamina), Has(LastActionTurn)]);
const staminaUpdate$ = merge(turn$, staminaQuery.update$);

defineRxSystem(world, staminaUpdate$, () => {
const currentTurn = getCurrentTurn(layer.world, GameConfig, clock);
defineRxSystem(world, staminaQuery.update$, ({ entity, type }) => {
if (type === UpdateType.Enter) setLocalStaminaToCurrentTurn(entity);
});

defineRxSystem(world, turn$, () => {
for (const entity of staminaQuery.matching) {
updateLocalStaminaToTurn(entity, currentTurn);
setLocalStaminaToCurrentTurn(entity);
}
});
}