Skip to content

Commit

Permalink
feat: Send static obj spawn events
Browse files Browse the repository at this point in the history
  • Loading branch information
Pazaz committed Feb 23, 2024
1 parent caf3216 commit ffb60a5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
21 changes: 1 addition & 20 deletions src/lostcity/engine/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,31 +1020,12 @@ class World {

addObj(obj: Obj, receiver: Player | null, duration: number) {
const zone = this.getZone(obj.x, obj.z, obj.level);
const existing = this.getObj(obj.x, obj.z, obj.level, obj.id);
if (existing && existing.id == obj.id) {
const type = ObjType.get(obj.type);
const nextCount = obj.count + existing.count;
if (type.stackable && nextCount <= Inventory.STACK_LIMIT) {
// if an obj of the same type exists and is stackable, then we merge them.
obj.count = nextCount;
zone.removeObj(existing, receiver);
}
}
zone.addObj(obj, receiver, duration);

obj.despawn = this.currentTick + duration;
obj.respawn = -1;
}

removeObj(obj: Obj, receiver: Player | null) {
// TODO
// stackable objs when they overflow are created into another slot on the floor
// currently when you pickup from a tile with multiple stackable objs
// you will pickup one of them and the other one disappears
const zone = this.getZone(obj.x, obj.z, obj.level);
zone.removeObj(obj, receiver, -1);
obj.despawn = this.currentTick;
obj.respawn = this.currentTick + ObjType.get(obj.type).respawnrate;
zone.removeObj(obj, receiver);
}

// ----
Expand Down
2 changes: 1 addition & 1 deletion src/lostcity/engine/script/handlers/LocOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const LocOps: CommandHandlers = {

const pos = Position.unpackCoord(coord);
const loc = World.getLoc(pos.x, pos.z, pos.level, locId);
if (!loc || loc.respawn !== -1) {
if (!loc) {
state.pushInt(0);
return;
}
Expand Down
32 changes: 27 additions & 5 deletions src/lostcity/engine/zone/Zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class Zone {
index = -1; // packed coord
level = 0;
buffer: Packet = new Packet();
events: Packet[] = [];

// zone entities
players: Set<number> = new Set(); // list of player uids
Expand All @@ -24,18 +25,19 @@ export default class Zone {
// loc info - bits 0-15 = type, bits 16-20 = shape, bits 21-23 = angle
locs: Set<number> = new Set();
locInfo: Map<number, number> = new Map();

events: Packet[] = [];
locDelEvent: Set<number> = new Set();
locAddEvent: Set<number> = new Set();

locDelCached: Map<number, Packet> = new Map();
locAddCached: Map<number, Packet> = new Map();

locDelTimer: Map<number, number> = new Map();
locAddTimer: Map<number, number> = new Map();
locChangeTimer: Map<number, number> = new Map();

staticObjs: Obj[] = []; // source of truth from map
staticObjAddCached: Packet[] = [];
staticObjDelCached: Packet[] = [];
objs: Obj[] = []; // objs actually in the zone

constructor(index: number, level: number) {
this.index = index;
this.level = level;
Expand Down Expand Up @@ -90,6 +92,10 @@ export default class Zone {
}
}

for (const obj of this.objs) {
//
}

// respawn
for (const [packed, timer] of this.locDelTimer) {
if (timer - World.currentTick <= 0) {
Expand Down Expand Up @@ -120,6 +126,10 @@ export default class Zone {
}
}

for (const obj of this.staticObjs) {
//
}

// shared events (this tick)
this.computeSharedEvents();
}
Expand Down Expand Up @@ -432,15 +442,27 @@ export default class Zone {
// ----

addStaticObj(obj: Obj) {
this.staticObjs.push(obj);
this.addObj(Obj.clone(obj), null, -1); // temp

const buf = Zone.write(ServerProt.OBJ_ADD, obj.x, obj.z, obj.type, obj.count);
this.staticObjAddCached.push(buf);
}

addObj(obj: Obj, receiver: Player | null, duration: number) {
this.objs.push(obj);
}

removeObj(obj: Obj, receiver: Player | null, subtractTick: number = 0) {
removeObj(obj: Obj, receiver: Player | null) {
}

getObj(x: number, z: number, type: number): Obj | null {
for (const obj of this.objs) {
if (obj.x === x && obj.z === z && obj.type === type) {
return obj;
}
}

return null;
}
}
4 changes: 4 additions & 0 deletions src/lostcity/entity/Obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default class Obj extends NonPathingEntity {
this.count = count;
}

static clone(obj: Obj) {
return new Obj(obj.level, obj.x, obj.z, obj.type, obj.count);
}

get id() {
return this.type;
}
Expand Down
16 changes: 13 additions & 3 deletions src/lostcity/entity/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,7 @@ export default class Player extends PathingEntity {

this.write(ServerProt.UPDATE_ZONE_FULL_FOLLOWS, x, z, this.loadedX, this.loadedZ);

const { locDelCached, locAddCached } = World.getZone(x << 3, z << 3, this.level);
const { locDelCached, locAddCached, staticObjAddCached, staticObjDelCached } = World.getZone(x << 3, z << 3, this.level);

for (const [packed, buf] of locDelCached) {
this.netOut.push(buf.copy());
Expand All @@ -2676,6 +2676,14 @@ export default class Player extends PathingEntity {
this.netOut.push(buf.copy());
}

for (const buf of staticObjAddCached) {
this.netOut.push(buf.copy());
}

for (const buf of staticObjDelCached) {
this.netOut.push(buf.copy());
}

if (locDelCached.size > 0 || locAddCached.size > 0) {
World.getZone(x << 3, z << 3, this.level).debug();
}
Expand All @@ -2686,16 +2694,18 @@ export default class Player extends PathingEntity {
const z = zone >> 11;

const { buffer, locAddTimer, locDelTimer, locChangeTimer } = World.getZone(x << 3, z << 3, this.level);

// shared events
if (buffer.length > 0) {
this.write(ServerProt.UPDATE_ZONE_PARTIAL_ENCLOSED, x, z, this.loadedX, this.loadedZ, buffer);

World.getZone(x << 3, z << 3, this.level).debug();
} else if (locAddTimer.size > 0 || locDelTimer.size > 0 || locChangeTimer.size > 0) {
World.getZone(x << 3, z << 3, this.level).debug();
}
}

// todo: obj events
// local events
}
}

// ----
Expand Down

0 comments on commit ffb60a5

Please sign in to comment.