Skip to content

Commit

Permalink
Pushing updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tynarus committed Apr 25, 2021
1 parent f2ec693 commit 8034d0d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 81 deletions.
15 changes: 7 additions & 8 deletions src/game-engine/net/outbound-packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ export class OutboundPackets {
}

public setWorldItem(worldItem: WorldItem, position: Position, offset: number = 0): void {

this.updateReferencePosition(position);

const packet = new Packet(175);
Expand Down Expand Up @@ -564,19 +563,19 @@ export class OutboundPackets {
this.queue(packet);
}

public constructHouseMaps(rooms: number[][][]): void {
public constructHouseMaps(position: Position, rooms: number[][][]): void {
const packet = new Packet(23, PacketType.DYNAMIC_LARGE);

const chunkX = this.player.position.chunkX + 6;
const chunkY = this.player.position.chunkY + 6;
const localX = this.player.position.chunkLocalX;
const localY = this.player.position.chunkLocalY;
const currentPlane = this.player.position.level;
const chunkX = position.chunkX + 6;
const chunkY = position.chunkY + 6;
const localX = position.chunkLocalX;
const localY = position.chunkLocalY;
const mapPlane = position.level;

packet.put(localY, 'short');
packet.put(localX, 'short', 'le');
packet.put(chunkX, 'short');
packet.put(currentPlane);
packet.put(mapPlane);
packet.put(chunkY, 'short');

packet.openBitBuffer();
Expand Down
11 changes: 9 additions & 2 deletions src/game-engine/world/actor/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ export class Player extends Actor {
return;
}

if(this.position.level > 3) {
this.position.level = 0;
}

world.playerTree.remove(this.quadtreeKey);
this.save();

Expand Down Expand Up @@ -387,7 +391,7 @@ export class Player extends Actor {
return new Promise<void>(resolve => {
this.walkingQueue.process();

if(this.updateFlags.mapRegionUpdateRequired && this.updateFlags.autoChunkUpdate) {
if(this.updateFlags.mapRegionUpdateRequired && !this.metadata.customMap) {
this.outgoingPackets.updateCurrentMapChunk();
}

Expand Down Expand Up @@ -417,6 +421,10 @@ export class Player extends Actor {
this.metadata['teleporting'] = null;
}

if(this.metadata.customMap) {
delete this.metadata.customMap;
}

resolve();
});
}
Expand Down Expand Up @@ -629,7 +637,6 @@ export class Player extends Actor {
* @param newPosition The player's new position.
*/
public teleport(newPosition: Position): void {
this.updateFlags.autoChunkUpdate = true;
const originalPosition = this.position;
const oldChunk = world.chunkManager.getChunkForWorldPosition(originalPosition);
const newChunk = world.chunkManager.getChunkForWorldPosition(newPosition);
Expand Down
2 changes: 0 additions & 2 deletions src/game-engine/world/actor/update-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export interface Damage {
*/
export class UpdateFlags {

public autoChunkUpdate: boolean = true;

private _mapRegionUpdateRequired: boolean;
private _appearanceUpdateRequired: boolean;
private _chatMessages: ChatMessage[];
Expand Down
16 changes: 12 additions & 4 deletions src/game-engine/world/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { World } from '@engine/world/index';
import { schedule } from '@engine/world/task';
import { CollisionMap } from '@engine/world/map/collision-map';
import { LandscapeObject } from '@runejs/filestore';
import { logger } from '@runejs/core';


/**
Expand Down Expand Up @@ -124,14 +125,21 @@ export class WorldInstance {

const { chunk: instancedChunk, mods } = this.getTileModifications(position);

mods.worldItems.push(worldItem);
if(owner) {
// If this world item is only visible to one player initially, we setup a timeout to spawn it for all other
// players after 100 game cycles.
try {
owner.outgoingPackets.setWorldItem(worldItem, worldItem.position);
} catch(error) {
logger.error(`Error spawning world item ${worldItem?.itemId} at ${worldItem?.position?.key}`, error);
return null;
}
}

mods.worldItems.push(worldItem);
instancedChunk.mods.set(position.key, mods);

if(owner) {
// If this world item is only visible to one player initially, we setup a timeout to spawn it for all other
// players after 100 game cycles.
owner.outgoingPackets.setWorldItem(worldItem, worldItem.position);
setTimeout(() => {
if(worldItem.removed) {
return;
Expand Down
20 changes: 17 additions & 3 deletions src/game-engine/world/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ export class Position {
return offsetX < 16 && offsetY < 16 && offsetX > -16 && offsetY > -16;
}

/**
* Checks to see if this position is within the two given boundaries of min and max.
* @param min The minimum coordinate to check within.
* @param max The maximum coordinate to check within.
* @param checkPlane Whether or not to check if the position is within the same plane. Defaults to true.
*/
public within(min: Position, max: Position, checkPlane: boolean = true): boolean {
if(checkPlane && (min.level !== max.level || max.level !== this.level)) {
return false;
}

return this.x >= min.x && this.x <= max.x && this.y >= min.y && this.y <= max.y;
}

public move(x: number, y: number, level?: number): Position {
this._x = x;
this._y = y;
Expand Down Expand Up @@ -174,10 +188,10 @@ export class Position {

/**
* Sets the value of Level and returns the current Position instance for chaining.
* @param level The new value to set the current Position's Elevation Level to.
* @param plane The new value to set the current Position's plane to.
*/
public setLevel(level: number): Position {
this._level = level;
public setLevel(plane: number): Position {
this._level = plane;
return this;
}

Expand Down
135 changes: 73 additions & 62 deletions src/plugins/skills/construction/construction.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Position } from '@engine/world/position';
import { Player } from '@engine/world/actor/player/player';
import { PlayerCommandAction } from '@engine/world/action/player-command.action';
import { playerInitActionHandler } from '@engine/world/action/player-init.action';
import { World } from '@engine/world';
import { world } from '@engine/game-server';
import { schedule } from '@engine/world/task';


const MAX_HOUSE_SIZE = 13;
Expand All @@ -9,6 +13,39 @@ const MAX_HOUSE_SIZE = 13;
type RoomType = 'empty' | 'empty_grass' | 'garden_1' | 'garden_2' | 'parlor';


type RoomTemplateMap = {
[key in RoomType]: Position;
}


const roomTemplates: RoomTemplateMap = {
empty: new Position(1856, 5056),
empty_grass: new Position(1864, 5056),
garden_1: new Position(1856, 5064),
garden_2: new Position(1872, 5064),
parlor: new Position(1856, 5112),
};


class Room {

public readonly type: RoomType;

public orientation: number;

public constructor(type: RoomType, orientation: number = 0) {
this.type = type;
this.orientation = orientation;
}

public get roomData(): number {
const { x, y, level } = roomTemplates[this.type];
return x / 8 << 14 | y / 8 << 3 | level % 4 << 24 | this.orientation % 4 << 1;
}

}


class House {

public readonly rooms: Room[][][];
Expand Down Expand Up @@ -45,71 +82,28 @@ class House {
}


interface RoomTemplate {
room: string;
worldPosition: Position;
}


class Room {

public template: RoomTemplate;
public orientation: number;

public get roomData(): number {
const { x, y, level } = this.template.worldPosition;
return x / 8 << 14 | y / 8 << 3 | level % 4 << 24 | this.orientation % 4 << 1;
}

}
const pohCoords = new Position(2048, 6272);

const pohMin = new Position(2016, 6240);
const pohMax = new Position(2079, 6303);

const homeLocation = new Position(2048, 6272, 0);


const roomTemplates: RoomTemplate[] = [
{
room: 'empty',
worldPosition: new Position(1856, 5056, 0)
},
{
room: 'empty_grass',
worldPosition: new Position(1864, 5056, 0)
},
{
room: 'garden_1',
worldPosition: new Position(1856, 5064, 0)
},
{
room: 'garden_2',
worldPosition: new Position(1872, 5064, 0)
},
{
room: 'parlor',
worldPosition: new Position(1856, 5112, 0)
}
];

const openHouse = async (player: Player): Promise<void> => {
const syntheticPlane = Math.floor(Math.random() * 64) * 4;
const pohLocation = pohCoords.copy().setLevel(syntheticPlane);
// if(!player.position.within(pohMin, pohMax, false)) {
player.teleport(pohLocation.copy());
// } else {
// player.teleport(player.position.copy().setLevel(syntheticPlane));
// }

const openHouse = (player: Player): void => {
player.teleport(homeLocation.clone());
player.updateFlags.autoChunkUpdate = false;
player.sendMessage(pohLocation.key);

const house = new House();

const firstRoom = new Room();
firstRoom.template = {
room: 'garden_1',
worldPosition: new Position(1856, 5064, 0)
};
firstRoom.orientation = 0;

const emptyRoom = new Room();
emptyRoom.template = {
room: 'empty_grass',
worldPosition: new Position(1864, 5056, 0)
};
emptyRoom.orientation = 0;
const gardenPortal = new Room('garden_1');
const firstParlor = new Room('parlor');
const emptySpace = new Room('empty_grass');

for(let x = 0; x < MAX_HOUSE_SIZE; x++) {
for(let y = 0; y < MAX_HOUSE_SIZE; y++) {
Expand All @@ -118,14 +112,25 @@ const openHouse = (player: Player): void => {
}

if(x === 6 && y === 6) {
house.rooms[0][x][y] = firstRoom;
house.rooms[0][x][y] = gardenPortal;
} else if(x === 7 && y === 6) {
house.rooms[0][x][y] = firstParlor;
} else {
house.rooms[0][x][y] = emptyRoom;
house.rooms[0][x][y] = emptySpace;
}
}
}

player.outgoingPackets.constructHouseMaps(house.getRoomData());
player.outgoingPackets.constructHouseMaps(pohLocation, house.getRoomData());
player.metadata.customMap = true;
};


const playerInitHomeCheck: playerInitActionHandler = ({ player }): void => {
if(player.position.within(pohMin, pohMax, false)) {
// @TODO TEMPORARY FOR TESTING!!!
setTimeout(() => openHouse(player), World.TICK_LENGTH);
}
};


Expand All @@ -135,7 +140,13 @@ export default {
{
type: 'player_command',
commands: [ 'con' ],
handler: ({ player }: PlayerCommandAction): void => openHouse(player)
handler: ({ player }: PlayerCommandAction): void => {
openHouse(player)
}
},
{
type: 'player_init',
handler: playerInitHomeCheck
}
]
};

0 comments on commit 8034d0d

Please sign in to comment.