@korial29/tiny-fps 0.189.0 → 0.190.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.js CHANGED
@@ -7624,6 +7624,9 @@ var Bot = class {
7624
7624
  /** The way it strafes in a fight (1 left, -1 right) and the tick it may turn back. */
7625
7625
  strafing = 1;
7626
7626
  turnBack = 0;
7627
+ /** Why it goes where it goes, and the keys it last pressed: for its view(). */
7628
+ why = "";
7629
+ keys = 0;
7627
7630
  /** The balls it has weighed, by shooter and count: it decides once whether to get out of the way. */
7628
7631
  weighed = /* @__PURE__ */ new Set();
7629
7632
  /** Its errand to the orbital station, when it has one. */
@@ -7646,6 +7649,37 @@ var Bot = class {
7646
7649
  }
7647
7650
  /** The input of this tick. */
7648
7651
  think(s) {
7652
+ const i = this.decide(s);
7653
+ this.keys = i.keys;
7654
+ return i;
7655
+ }
7656
+ /**
7657
+ * What it is up to, for a player who watches it (the client's bot view): its goal and why, the
7658
+ * way there as points (its entry, the nodes of the edges, the goal), the index of the point it
7659
+ * goes to now, what it is doing on the way, the enemy it fights and the keys it last pressed.
7660
+ */
7661
+ view() {
7662
+ const { entry, edges } = this.way;
7663
+ const r = (v) => [
7664
+ v[0],
7665
+ v[1],
7666
+ v[2]
7667
+ ].map((n) => Math.round(n * 10) / 10);
7668
+ const path = [...entry ? [entry] : [], ...edges.map((e) => this.nav.nodes[e.to])];
7669
+ if (this.goal) path.push(this.goal);
7670
+ const e = edges[this.leg];
7671
+ return {
7672
+ id: this.id,
7673
+ why: this.why,
7674
+ path: path.map(r),
7675
+ next: this.leg < 0 ? 0 : this.leg + (entry ? 1 : 0),
7676
+ doing: !this.goal ? "no way" : this.leg < 0 ? "to entry" : e ? e.leg?.k ?? "walk" : "to goal",
7677
+ foe: this.foe,
7678
+ keys: this.keys
7679
+ };
7680
+ }
7681
+ /** think()'s work. */
7682
+ decide(s) {
7649
7683
  const t = ++this.ticks;
7650
7684
  const p = s.me.state;
7651
7685
  this.world = s.world;
@@ -7802,10 +7836,19 @@ var Bot = class {
7802
7836
  h.z
7803
7837
  ];
7804
7838
  };
7805
- if (theirs.state === FLAG$1.CARRIED && theirs.by === me.id) return home.base;
7806
- if (this.sortie.board) return this.sortie.board;
7839
+ if (theirs.state === FLAG$1.CARRIED && theirs.by === me.id) {
7840
+ this.why = "flag home";
7841
+ return home.base;
7842
+ }
7843
+ if (this.sortie.board) {
7844
+ this.why = "station";
7845
+ return this.sortie.board;
7846
+ }
7807
7847
  const gear = s.kit?.gear ?? true;
7808
- if (p.ammo < 0 && gear && home.rack && flat(p.x, p.z, home.rack[0], home.rack[2]) < ARM) return home.rack;
7848
+ if (p.ammo < 0 && gear && home.rack && flat(p.x, p.z, home.rack[0], home.rack[2]) < ARM) {
7849
+ this.why = "gun rack";
7850
+ return home.rack;
7851
+ }
7809
7852
  const eye = [
7810
7853
  p.x,
7811
7854
  p.y + eyeHeight(p),
@@ -7816,26 +7859,45 @@ var Bot = class {
7816
7859
  o.state.y + 1.2,
7817
7860
  o.state.z
7818
7861
  ]));
7819
- if (prey) return at(prey.state);
7862
+ if (prey) {
7863
+ this.why = "hunt";
7864
+ return at(prey.state);
7865
+ }
7820
7866
  if (mine.state === FLAG$1.CARRIED) {
7821
7867
  const h = holder(mine);
7822
- if (h) return h;
7868
+ if (h) {
7869
+ this.why = "chase carrier";
7870
+ return h;
7871
+ }
7872
+ }
7873
+ if (mine.state === FLAG$1.DROPPED) {
7874
+ this.why = "return flag";
7875
+ return [
7876
+ mine.x,
7877
+ mine.y,
7878
+ mine.z
7879
+ ];
7880
+ }
7881
+ if (this.id % GUARD === 7) {
7882
+ this.why = "guard";
7883
+ return home.base;
7823
7884
  }
7824
- if (mine.state === FLAG$1.DROPPED) return [
7825
- mine.x,
7826
- mine.y,
7827
- mine.z
7828
- ];
7829
- if (this.id % GUARD === 7) return home.base;
7830
7885
  if (theirs.state === FLAG$1.CARRIED) {
7831
7886
  const h = holder(theirs);
7832
- if (h) return h;
7887
+ if (h) {
7888
+ this.why = "escort";
7889
+ return h;
7890
+ }
7833
7891
  }
7834
- if (theirs.state === FLAG$1.DROPPED) return [
7835
- theirs.x,
7836
- theirs.y,
7837
- theirs.z
7838
- ];
7892
+ if (theirs.state === FLAG$1.DROPPED) {
7893
+ this.why = "take flag";
7894
+ return [
7895
+ theirs.x,
7896
+ theirs.y,
7897
+ theirs.z
7898
+ ];
7899
+ }
7900
+ this.why = "take flag";
7839
7901
  return world.def.teams[1 - team].base;
7840
7902
  }
7841
7903
  plan(w, p, goal) {
@@ -8662,6 +8724,8 @@ var MAX_CATCH_UP = 20;
8662
8724
  var SPAWN_CLEAR = 2;
8663
8725
  /** Ticks before a rover can run the same player over again. */
8664
8726
  var SPLAT_COOLDOWN = 30;
8727
+ /** Ticks between two of a bot's views to the players who watch it. */
8728
+ var WATCH = 6;
8665
8729
  /** The names of the bots, in turn. */
8666
8730
  var BOT_NAMES = [
8667
8731
  "Ada",
@@ -8883,7 +8947,8 @@ var Room = class {
8883
8947
  else if (p && msg?.t === "map" && p === this.host() && Object.hasOwn(MAPS, msg.map)) {
8884
8948
  this.next = msg.map === this.mapName ? void 0 : msg.map;
8885
8949
  this.roster();
8886
- } else if (p && msg?.t === "body") {
8950
+ } else if (p && msg?.t === "watch") p.watch = typeof msg.id === "number" ? msg.id : void 0;
8951
+ else if (p && msg?.t === "body") {
8887
8952
  const { id, name, team, color } = p.info;
8888
8953
  const info = {
8889
8954
  id,
@@ -10039,6 +10104,14 @@ var Room = class {
10039
10104
  kit: this.kit(p)
10040
10105
  });
10041
10106
  p.seq = (p.seq ?? -1) + 1 & 65535;
10107
+ const watchers = players.filter((q) => q.watch === p.info.id);
10108
+ if (watchers.length && this.ticks % WATCH === 0) {
10109
+ const text = JSON.stringify({
10110
+ t: "bot",
10111
+ ...p.bot.view()
10112
+ });
10113
+ watchers.forEach((q) => q.conn.send(text));
10114
+ }
10042
10115
  this.apply(p, onWire({
10043
10116
  ...i,
10044
10117
  seq: p.seq,
@@ -7,7 +7,7 @@ export declare const MULTI = 4;
7
7
  export declare const QUICK: (16 | 14 | 18 | 15 | 17)[];
8
8
  /** Frags without going down between two lines of a run; the last line for every SPREE after. */
9
9
  export declare const SPREE = 5;
10
- export declare const RUNS: (20 | 21 | 22 | 23 | 19)[];
10
+ export declare const RUNS: (20 | 19 | 22 | 23 | 21)[];
11
11
  /** Shots of the gun in one run judged for the aim, and the share of them that must hit. */
12
12
  export declare const AIM_SHOTS = 12;
13
13
  export declare const AIM_SHARE = 0.75;
@@ -0,0 +1,14 @@
1
+ import type { BotView } from '../shared/bot';
2
+ export interface Watch {
3
+ /** The bot watched, if any. */
4
+ readonly id: number | undefined;
5
+ /** The next of these bots after the one watched (the first when none), or none after the last. */
6
+ next(bots: number[]): number | undefined;
7
+ /** What the bot watched is up to, as the room last told it. */
8
+ take(v: BotView): void;
9
+ /** Its way and its panel over the frame just drawn, `project` the renderer's; with no bot watched, nothing. */
10
+ draw(project: (x: number, y: number, z: number) => [number, number] | undefined, name: (id: number) => string): void;
11
+ /** Takes its canvas and panel off the screen. */
12
+ destroy(): void;
13
+ }
14
+ export declare const createWatch: (screen: HTMLElement) => Watch;
@@ -1,3 +1,4 @@
1
+ import type { Vec3 } from './nav';
1
2
  import type { Flying } from './plasma';
2
3
  import type { Errand } from './sortie';
3
4
  import type { Kit } from './kit';
@@ -27,6 +28,21 @@ export type Intent = {
27
28
  yaw: number;
28
29
  pitch: number;
29
30
  };
31
+ /** What a bot is up to, sent to a player who watches it (the room's 'bot' text). */
32
+ export interface BotView {
33
+ id: number;
34
+ /** Why it goes where it goes: 'take flag', 'chase carrier', 'gun rack'… */
35
+ why: string;
36
+ /** Its way: the entry, the nodes it goes through, then its goal; `next` is the point it goes to now. */
37
+ path: Vec3[];
38
+ next: number;
39
+ /** What it does on the way: 'to entry', a leg's kind ('go', 'leap', 'slide'), 'walk', 'to goal', 'no way'. */
40
+ doing: string;
41
+ /** The enemy it fights, -1 for none. */
42
+ foe: number;
43
+ /** The KEY bits it last pressed. */
44
+ keys: number;
45
+ }
30
46
  /** The skill of a room's bots when its rules give none: 0 (novice) to 7 (godlike), as in UT2004. */
31
47
  export declare const SKILL = 4;
32
48
  /**
@@ -82,6 +98,9 @@ export declare class Bot {
82
98
  /** The way it strafes in a fight (1 left, -1 right) and the tick it may turn back. */
83
99
  private strafing;
84
100
  private turnBack;
101
+ /** Why it goes where it goes, and the keys it last pressed: for its view(). */
102
+ private why;
103
+ private keys;
85
104
  /** The balls it has weighed, by shooter and count: it decides once whether to get out of the way. */
86
105
  private weighed;
87
106
  /** Its errand to the orbital station, when it has one. */
@@ -96,6 +115,14 @@ export declare class Bot {
96
115
  private rnd;
97
116
  /** The input of this tick. */
98
117
  think(s: Sight): Intent;
118
+ /**
119
+ * What it is up to, for a player who watches it (the client's bot view): its goal and why, the
120
+ * way there as points (its entry, the nodes of the edges, the goal), the index of the point it
121
+ * goes to now, what it is doing on the way, the enemy it fights and the keys it last pressed.
122
+ */
123
+ view(): BotView;
124
+ /** think()'s work. */
125
+ private decide;
99
126
  /**
100
127
  * How it moves while it fights, as UT2004's bots do: it strafes, one way for a while then the
101
128
  * other, never toward a drop, and now and then jumps or dashes aside (its Jumpiness).
@@ -1,3 +1,4 @@
1
+ import type { BotView } from './bot';
1
2
  import type { Choice, Input, PlayerInfo, Snapshot } from './types';
2
3
  /**
3
4
  * join: the first frame of a peer, with what he picked on the title screen (Choice: the body, BODY,
@@ -13,7 +14,12 @@ export type ClientText = ({
13
14
  } | ({
14
15
  t: 'body';
15
16
  body: number;
16
- } & Choice);
17
+ } & Choice)
18
+ /** Watch a bot (its id) and get what it is up to, or stop (no id). */
19
+ | {
20
+ t: 'watch';
21
+ id?: number;
22
+ };
17
23
  /** What a player did in a match, for the end screen: it carries the name and the team, which a leaver takes out of the roster. */
18
24
  export interface Result {
19
25
  id: number;
@@ -69,7 +75,11 @@ export type ServerText =
69
75
  t: 'end';
70
76
  score: [number, number];
71
77
  players: Result[];
72
- };
78
+ }
79
+ /** What the bot a player watches is up to, ten times a second. */
80
+ | ({
81
+ t: 'bot';
82
+ } & BotView);
73
83
  export declare const MSG_INPUT = 1;
74
84
  export declare const MSG_SNAPSHOT = 2;
75
85
  export declare const INPUT_BYTES = 12;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@korial29/tiny-fps",
3
3
  "description": "Tiny multiplayer first-person rooftop game for the browser: raw WebGL, zero runtime dependencies, WebSocket or same-browser multi-tab play.",
4
4
  "license": "Apache-2.0",
5
- "version": "0.189.0",
5
+ "version": "0.190.0",
6
6
  "type": "module",
7
7
  "files": [
8
8
  "dist"