diff --git a/packages/universe/src/resources/universe.js b/packages/universe/src/resources/universe.js index 03413d7..cbb1ff5 100644 --- a/packages/universe/src/resources/universe.js +++ b/packages/universe/src/resources/universe.js @@ -96,7 +96,7 @@ export default (latus) => class Universe extends JsonResource { const {entity} = player; const room = this.room(entity.currentRoom); entity.stopInforming(room); - room.removeEntityFromLayer(entity, 1); + room.removeEntityFromLayer(entity, 2); const index = this.#players.indexOf(player); if (-1 !== index) { this.#players.splice(player, 1); diff --git a/packages/universe/src/server/gen/alpha.js b/packages/universe/src/server/gen/alpha.js index 745ada0..7876557 100644 --- a/packages/universe/src/server/gen/alpha.js +++ b/packages/universe/src/server/gen/alpha.js @@ -1,47 +1,68 @@ -import {noise, noiseSeed, Vector} from '@avocado/math'; +import { + Generator, + noise as rawNoise, + noiseSeed, + Vector, +} from '@avocado/math'; + +const noise = ([x, y]) => (1 + rawNoise([x, y])) / 2; + +const indexComputer = (indices, randomizer) => ( + ([x, y]) => indices[Math.floor(indices.length * randomizer([x, y]))] +); + +const [w, h] = [64, 64]; + +const Plains = new Generator({ + calculate: indexComputer( + [1, 3, 4, 10, 11, 12], + ([x, y]) => noise(Vector.scale([x, y], 30)), + ), + covers: () => true, + size: [w, h], + children: [ + new Generator({ + calculate: indexComputer( + [342, 456], + ([x, y]) => noise(Vector.scale([x, y], 30)), + ), + covers: ([x, y]) => noise(Vector.scale([x, y], 10)) < 0.2, + size: [w, h], + }), + new Generator({ + calculate: indexComputer( + [103, 104], + ([x, y]) => noise(Vector.scale([x, y], 30)), + ), + covers: ([x, y]) => noise(Vector.scale([x, y], 5)) < 0.2, + size: [w, h], + }), + ], +}); + +const seed = 69420; +noiseSeed(seed); +Plains.generate(); export default async (room, latus) => { const {Entity} = latus.get('%resources'); - // const seed = Date.now(); - const seed = 69420; - noiseSeed(seed); - const tiles = { - water: [103, 104], - grass: [1, 3, 4, 10, 11, 12], - dirt: [342, 456], - stone: [407, 408, 423, 424], - }; - const rnd = (type, x, y) => { - const l = tiles[type]; - const v = (1 + noise(Vector.scale([x, y], 30), seed)) / 2; - return l[Math.floor(v * l.length)]; - }; - const tile = (x, y) => { - let v = (1 + noise(Vector.scale([x, y], 5), seed)) / 2; - if (v < 0.2) { - return 0; - } - v = (1 + noise(Vector.scale([x, y], 10), seed + 1)) / 2; - if (v < 0.2) { - return rnd('dirt', x, y); - } - return rnd('grass', x, y); - }; + room.layer(0).stampAt( + [0, 0, w, h], + Plains.matrix, + ); + room.layer(1).stampAt( + [0, 0, w, h], + Plains.children[0].matrix, + ); + room.layer(2).stampAt( + [0, 0, w, h], + Plains.children[1].matrix, + ); for (let y = 0; y < 64; ++y) { for (let x = 0; x < 64; ++x) { - room.layer(0).setTileAt([x, y], rnd('water', x, y)); - } - } - for (let y = 0; y < 64; ++y) { - for (let x = 0; x < 64; ++x) { - room.layer(1).setTileAt([x, y], tile(x, y)); - } - } - for (let y = 0; y < 64; ++y) { - for (let x = 0; x < 64; ++x) { - let v = (1 + noise(Vector.scale([x, y], 5), seed)) / 2; + let v = noise(Vector.scale([x, y], 5), seed); if (v > 0.2) { - v = (1 + noise(Vector.scale([x, y], 15), seed + 1)) / 2; + v = noise(Vector.scale([x, y], 15), seed + 1); if (v < 0.15) { // eslint-disable-next-line no-await-in-loop const shrub = await Entity.load({extends: '/shrub.entity.json'}); @@ -49,7 +70,7 @@ export default async (room, latus) => { Vector.add([8, 8], Vector.scale([x, y], 16)), [Math.random() * 8 - 4, Math.random() * 8 - 4], ))); - room.addEntityToLayer(shrub, 1); + room.addEntityToLayer(shrub, 2); } else if (v < 0.17) { // eslint-disable-next-line no-await-in-loop @@ -61,9 +82,9 @@ export default async (room, latus) => { Vector.add([8, 8], p), [Math.random() * 8 - 4, Math.random() * 8 - 4], ))); - room.addEntityToLayer(tree, 1); + room.addEntityToLayer(tree, 2); } - v = (1 + noise(Vector.scale([x, y], 9), seed + 1)) / 2; + v = noise(Vector.scale([x, y], 9), seed + 1); if (v < 0.08) { // eslint-disable-next-line no-await-in-loop const flower = await Entity.load({extends: '/flower.entity.json'}); @@ -71,7 +92,7 @@ export default async (room, latus) => { Vector.add([8, 8], Vector.scale([x, y], 16)), [Math.random() * 8 - 4, Math.random() * 8 - 4], ))); - room.addEntityToLayer(flower, 1); + room.addEntityToLayer(flower, 2); } } } diff --git a/packages/universe/src/traits/universed.js b/packages/universe/src/traits/universed.js index a62ee51..8fbf28c 100644 --- a/packages/universe/src/traits/universed.js +++ b/packages/universe/src/traits/universed.js @@ -29,7 +29,7 @@ export default () => class Universed extends decorate(Trait) { } if (this.entity.currentRoom) { const room = this.#universe.room(this.entity.currentRoom); - room.addEntityToLayer(this.entity, 1); + room.addEntityToLayer(this.entity, 2); this.entity.startInforming(room); } }