Skip to content

Commit 50667c0

Browse files
committed
Adding new ConstructedChunk abstract class within the API and cleaning...
...up region naming.
1 parent de28ee7 commit 50667c0

File tree

7 files changed

+68
-50
lines changed

7 files changed

+68
-50
lines changed

src/game-engine/net/outbound-packets.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { stringToLong } from '@engine/util/strings';
1212
import { LandscapeObject } from '@runejs/filestore';
1313
import { xteaRegions } from '@engine/config';
1414
import { world } from '@engine/game-server';
15-
import { ConstructedMap } from '@engine/world/map/region';
16-
import { map } from 'rxjs/operators';
17-
import { Room } from '@plugins/skills/construction/con-house';
15+
import { ConstructedChunk, ConstructedRegion } from '@engine/world/map/region';
16+
17+
1818

1919
/**
2020
* A helper class for sending various network packets back to the game client.
@@ -567,7 +567,7 @@ export class OutboundPackets {
567567
this.queue(packet);
568568
}
569569

570-
public constructMapRegion(mapData: ConstructedMap): void {
570+
public constructMapRegion(mapData: ConstructedRegion): void {
571571
const packet = new Packet(23, PacketType.DYNAMIC_LARGE);
572572

573573
packet.put(this.player.position.chunkLocalY, 'short');
@@ -578,23 +578,23 @@ export class OutboundPackets {
578578

579579
packet.openBitBuffer();
580580

581-
const mapWorldX = mapData.position.x;
582-
const mapWorldY = mapData.position.y;
581+
const mapWorldX = mapData.renderPosition.x;
582+
const mapWorldY = mapData.renderPosition.y;
583583

584584
const topCornerMapChunk = world.chunkManager.getChunkForWorldPosition(new Position(mapWorldX, mapWorldY, this.player.position.level));
585585
const playerChunk = world.chunkManager.getChunkForWorldPosition(this.player.position);
586586

587587
const offsetX = playerChunk.position.x - (topCornerMapChunk.position.x - 2);
588588
const offsetY = playerChunk.position.y - (topCornerMapChunk.position.y - 2);
589589

590-
mapData.centerOffsetX = offsetX - 6; // 6 === center
591-
mapData.centerOffsetY = offsetY - 6; // 6 === center
590+
mapData.drawOffsetX = offsetX - 6; // 6 === center
591+
mapData.drawOffsetY = offsetY - 6; // 6 === center
592592

593593
for(let level = 0; level < 4; level++) {
594594
for(let x = 0; x < 13; x++) {
595595
for(let y = 0; y < 13; y++) {
596-
let mapTileOffsetX = x + mapData.centerOffsetX;
597-
let mapTileOffsetY = y + mapData.centerOffsetY;
596+
let mapTileOffsetX = x + mapData.drawOffsetX;
597+
let mapTileOffsetY = y + mapData.drawOffsetY;
598598
if(mapTileOffsetX < 0) {
599599
mapTileOffsetX = 0;
600600
}
@@ -608,14 +608,15 @@ export class OutboundPackets {
608608
mapTileOffsetY = 12;
609609
}
610610

611-
const room: Room | null = mapData.rooms[level][x][y];
612-
packet.putBits(1, room === null ? 0 : 1)
613-
if (room !== null) {
614-
packet.putBits(2, room.position.level & 0x3)
615-
packet.putBits(10, room.position.x / 8)
616-
packet.putBits(11, room.position.y / 8)
617-
packet.putBits(2, room.rotation)
618-
packet.putBits(1, 0) //unused
611+
const constructedChunk: ConstructedChunk | null = mapData.chunks[level][x][y];
612+
packet.putBits(1, constructedChunk === null ? 0 : 1)
613+
if (constructedChunk !== null) {
614+
const { templatePosition, rotation } = constructedChunk;
615+
packet.putBits(2, templatePosition?.level & 0x3);
616+
packet.putBits(10, templatePosition?.x / 8);
617+
packet.putBits(11, templatePosition?.y / 8);
618+
packet.putBits(2, rotation || 0);
619+
packet.putBits(1, 0); // unused
619620
}
620621
}
621622
}

src/game-engine/world/action/region-change.action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const regionChangeActionFactory = (player: Player,
7878
};
7979

8080
if(originalMapRegionId !== currentMapRegionId) {
81-
regionTypes.push('map');
81+
regionTypes.push('region');
8282
}
8383

8484
if(!Coords.equals(originalChunkCoords, currentChunkCoords)) {

src/game-engine/world/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { loadActionFiles } from '@engine/world/action';
1919
import { LandscapeObject } from '@runejs/filestore';
2020
import { lastValueFrom, Subject } from 'rxjs';
2121
import { take } from 'rxjs/operators';
22-
import { ConstructedMap, getRotatedObjectX, getRotatedObjectY } from '@engine/world/map/region';
22+
import { ConstructedRegion, getRotatedLocalX, getRotatedLocalY } from '@engine/world/map/region';
2323
import { Chunk } from '@engine/world/map/chunk';
2424

2525

@@ -149,33 +149,33 @@ export class World {
149149
* @param objectPosition The position of the copied object to find the template of.
150150
*/
151151
public findCustomMapObject(actor: Actor, objectId: number, objectPosition: Position): LandscapeObject | null {
152-
const map = actor?.metadata?.customMap as ConstructedMap || null;
152+
const map = actor?.metadata?.customMap as ConstructedRegion || null;
153153

154154
if(!map) {
155155
return null;
156156
}
157157

158158
const objectChunk = this.chunkManager.getChunkForWorldPosition(objectPosition);
159-
const mapChunk = world.chunkManager.getChunkForWorldPosition(map.position);
159+
const mapChunk = world.chunkManager.getChunkForWorldPosition(map.renderPosition);
160160

161161
const chunkIndexX = objectChunk.position.x - (mapChunk.position.x - 2);
162162
const chunkIndexY = objectChunk.position.y - (mapChunk.position.y - 2);
163163

164-
const objectTile = map.tileData[actor.position.level][chunkIndexX][chunkIndexY];
164+
const objectTile = map.chunks[actor.position.level][chunkIndexX][chunkIndexY];
165165

166-
const tileX = objectTile >> 14 & 0x3ff;
167-
const tileY = objectTile >> 3 & 0x7ff;
168-
const tileOrientation = (0x6 & objectTile) >> 1;
166+
const tileX = objectTile.templatePosition.x;
167+
const tileY = objectTile.templatePosition.y;
168+
const tileOrientation = objectTile.rotation;
169169

170170
const objectLocalX = objectPosition.x - (objectChunk.position.x + 6) * 8;
171171
const objectLocalY = objectPosition.y - (objectChunk.position.y + 6) * 8;
172172

173-
const mapTemplateWorldX = tileX * 8;
174-
const mapTemplateWorldY = tileY * 8;
173+
const mapTemplateWorldX = tileX;
174+
const mapTemplateWorldY = tileY;
175175
const mapTemplateChunk = world.chunkManager.getChunkForWorldPosition(new Position(mapTemplateWorldX, mapTemplateWorldY, objectPosition.level));
176176

177-
const templateObjectPosition = new Position(mapTemplateWorldX + getRotatedObjectX(tileOrientation, objectLocalX, objectLocalY),
178-
mapTemplateWorldY + getRotatedObjectY(tileOrientation, objectLocalX, objectLocalY), objectPosition.level);
177+
const templateObjectPosition = new Position(mapTemplateWorldX + getRotatedLocalX(tileOrientation, objectLocalX, objectLocalY),
178+
mapTemplateWorldY + getRotatedLocalY(tileOrientation, objectLocalX, objectLocalY), objectPosition.level);
179179
const realObject = mapTemplateChunk.getFilestoreLandscapeObject(objectId, templateObjectPosition);
180180

181181
return realObject || null;

src/game-engine/world/map/region.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import { Position } from '@engine/world/position';
77
import { Room } from '@plugins/skills/construction/con-house';
88

9-
export type RegionType = 'map' | 'chunk';
9+
10+
export type RegionType = 'mapfile' | 'region' | 'chunk';
1011

1112
/**
1213
* A type for defining region tile sizes.
@@ -19,20 +20,37 @@ export type RegionSizeMap = {
1920
* A map of region types to tile sizes.
2021
*/
2122
export const regionSizes: RegionSizeMap = {
22-
'map': 64,
23+
'mapfile': 104,
24+
'region': 64,
2325
'chunk': 8
2426
};
2527

2628

27-
export interface ConstructedMap {
28-
position: Position;
29-
rooms: Room[][][];
30-
centerOffsetX?: number;
31-
centerOffsetY?: number;
29+
export abstract class ConstructedChunk {
30+
31+
public rotation: number;
32+
33+
protected constructor(rotation: number = 0) {
34+
this.rotation = rotation;
35+
}
36+
37+
public abstract getTemplatePosition(): Position;
38+
39+
public get templatePosition(): Position {
40+
return this.getTemplatePosition();
41+
}
42+
43+
}
44+
45+
export interface ConstructedRegion {
46+
renderPosition: Position;
47+
chunks: ConstructedChunk[][][];
48+
drawOffsetX?: number;
49+
drawOffsetY?: number;
3250
}
3351

3452

35-
export const getRotatedObjectX = (orientation: number, localX: number, localY: number): number => {
53+
export const getRotatedLocalX = (orientation: number, localX: number, localY: number): number => {
3654
if(orientation === 0) {
3755
return localX;
3856
}
@@ -53,7 +71,7 @@ export const getRotatedObjectX = (orientation: number, localX: number, localY: n
5371
return localY;
5472
};
5573

56-
export const getRotatedObjectY = (orientation: number, localX: number, localY: number): number => {
74+
export const getRotatedLocalY = (orientation: number, localX: number, localY: number): number => {
5775
if(orientation === 0) {
5876
return localY;
5977
}

src/plugins/music/music-regions.plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default {
4040
pluginId: 'rs:music_regions',
4141
hooks: [{
4242
type: 'region_change',
43-
regionType: 'map',
43+
regionType: 'region',
4444
handler: regionChangedHandler
4545
}, {
4646
type: 'player_init',

src/plugins/skills/construction/con-house.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MAP_SIZE, roomTemplates, RoomType } from '@plugins/skills/construction/con-constants';
22
import { Position } from '@engine/world/position';
3+
import { ConstructedChunk } from '@engine/world/map/region';
34

45

56
export class House {
@@ -23,18 +24,16 @@ export class House {
2324
}
2425

2526

26-
export class Room {
27+
export class Room extends ConstructedChunk {
2728

2829
public readonly type: RoomType;
2930

30-
public rotation: number;
31-
32-
public constructor(type: RoomType, orientation: number = 0) {
31+
public constructor(type: RoomType, rotation: number = 0) {
32+
super(rotation);
3333
this.type = type;
34-
this.rotation = orientation;
3534
}
3635

37-
public get position(): Position {
36+
public getTemplatePosition(): Position {
3837
return roomTemplates[this.type];
3938
}
4039

src/plugins/skills/construction/construction.plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Position } from '@engine/world/position';
22
import { Player } from '@engine/world/actor/player/player';
33
import { PlayerCommandAction } from '@engine/world/action/player-command.action';
44
import { PlayerInitAction } from '@engine/world/action/player-init.action';
5-
import { ConstructedMap } from '@engine/world/map/region';
5+
import { ConstructedRegion } from '@engine/world/map/region';
66

77
import { instance1, instance1Max, instance1PohSpawn, instance2, instance2Max, MAP_SIZE } from './con-constants';
88
import { House, Room } from './con-house';
@@ -49,9 +49,9 @@ const openHouse = (player: Player): void => {
4949
player.teleport(playerSpawn);
5050

5151
player.metadata.customMap = {
52-
position: pohPosition,
53-
rooms: house.rooms
54-
} as ConstructedMap;
52+
renderPosition: pohPosition,
53+
chunks: house.rooms
54+
} as ConstructedRegion;
5555

5656
player.sendMessage(`Welcome home.`);
5757
};

0 commit comments

Comments
 (0)