1// The server owns ships, hulls and loose pieces; this is the shape it reports, 2// plus the local-to-world transform every client needs to draw a hull. 3export type Kind = 'triangle' | 'square' | 'circle' | 'star'; 4 5// Piece position is local to the ship: the graph is rigid, so only the hull moves. 6export type Piece = { 7 id: number; 8 kind: Kind; 9 lx: number; 10 ly: number; 11 parent: number; 12 hp: number; 13 maxhp: number; 14 tier: number; 15}; 16export type Loose = { 17 id: number; 18 kind: Kind; 19 x: number; 20 y: number; 21 h: number; 22 hp: number; 23 maxhp: number; 24 tier: number; 25}; 26export type Ore = { id: number; x: number; y: number; h: number; towed: number }; 27export type Shot = { id: number; x: number; y: number; h: number }; 28export type Body = { id: number; kind: 'star' | 'shipyard' | 'barren'; r: number; x: number; y: number }; 29// The centre a system's bodies circle: one mark on the galaxy map. 30export type System = { id: number; x: number; y: number }; 31 32export type Ship = { 33 id: number; 34 name: string; 35 x: number; 36 y: number; 37 h: number; 38 tx: number; // where it is flying to, so the marker can show the order 39 ty: number; 40 order: boolean; // that order is still pending, so the marker is due 41 fuel: number; // what is in the tank 42 fuelmax: number; // capacity, summed from the hull 43 side: string; // police or rogue for a server hull, empty for a player 44 foe: number; // the ship this one is chasing, 0 = nobody 45 target: number; // piece of that ship its guns are pointed at, 0 = the nearest 46 ai: boolean; // a server-run hull, so it draws in its own colour 47 pieces: Piece[]; 48}; 49 50// The same rule the server shoots by, so the rim on a hull can never say hostile 51// while the guns say neutral: the rogue faction is at war with everything else on 52// sight, and anyone else is only at war once one of the two has declared it. 53export const hostile = (a?: Ship, b?: Ship): boolean => { 54 if (!a || !b || a.id === b.id) return false; 55 if (a.side === 'rogue' || b.side === 'rogue') return a.side !== b.side; 56 return a.foe === b.id || b.foe === a.id; 57}; 58 59export function toWorld(s: Ship, p: Piece): { x: number; y: number } { 60 const c = Math.cos(s.h); 61 const n = Math.sin(s.h); 62 return { x: s.x + p.lx * c - p.ly * n, y: s.y + p.lx * n + p.ly * c }; 63}