Skip to content

Commit

Permalink
Add EngineService fromJSON, adapt a copy of resource & building examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoscur committed May 5, 2024
1 parent 9375046 commit 2368a22
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 0 deletions.
212 changes: 212 additions & 0 deletions src/app/engine/buildings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import {
ResourceTypes,
EnergyTypes,
MetallicResource,
CrystallineResource,
LiquidResource,
zeroResources,
zeroEnergy,
} from './resources';
export type Resources = ResourceTypes | EnergyTypes;
import { Stock, ResourceCollection } from '@phlame/engine';
import {
Building,
BuildingIdentifier as BuildingID,
ProsumptionLookup,
RequirementLookup,
} from '@phlame/engine';
import { BuildingRequirement } from '@phlame/engine';

export type BuildingIdentifier = BuildingID;

export const requirements: RequirementLookup<BuildingID, Resources> = {
// Metallic mine
1: new BuildingRequirement<BuildingID, Resources>(
1,
ResourceCollection.fromArray<Resources>([
new MetallicResource(60),
new CrystallineResource(15),
]),
1.5,
[],
),
// Crystalline mine
2: new BuildingRequirement<BuildingID, Resources>(
2,
ResourceCollection.fromArray<Resources>([
new MetallicResource(48),
new CrystallineResource(24),
]),
1.6,
[],
),
// Liquid mine
3: new BuildingRequirement<BuildingID, Resources>(
3,
ResourceCollection.fromArray<Resources>([
new MetallicResource(225),
new CrystallineResource(75),
]),
1.5,
[],
),
// power
4: new BuildingRequirement<BuildingID, Resources>(
4,
ResourceCollection.fromArray<Resources>([
new MetallicResource(75),
new CrystallineResource(30),
]),
1.5,
[],
),
// power based on Liquid
12: new BuildingRequirement<BuildingID, Resources>(
12,
ResourceCollection.fromArray<Resources>([
new MetallicResource(48),
new CrystallineResource(24),
]),
1.8,
[
{
type: 3,
level: 5,
},
{
type: 113,
level: 3,
},
],
),
// build buildings faster! TODO we need to add the time reducing factors
14: new BuildingRequirement<BuildingID, Resources>(
14, // robots factor=1/(1+level)
ResourceCollection.fromArray<Resources>([
new MetallicResource(400),
new CrystallineResource(120),
new LiquidResource(200),
]),
2,
[],
), // FASTER
15: new BuildingRequirement<BuildingID, Resources>(
15, // nanites factor=1/2^level
ResourceCollection.fromArray<Resources>([
new MetallicResource(1000000),
new CrystallineResource(500000),
new LiquidResource(100000),
]),
2,
[
{
type: 14,
level: 10,
},
{
type: 108,
level: 10,
},
],
),
// lab
31: new BuildingRequirement<BuildingID, Resources>(
31,
ResourceCollection.fromArray<Resources>([
new MetallicResource(200),
new CrystallineResource(400),
new LiquidResource(200),
]),
2,
[],
),
// tech (above 100)
109: new BuildingRequirement<BuildingID, Resources>(
113, // computer
ResourceCollection.fromArray<Resources>([
new CrystallineResource(800),
new LiquidResource(400),
]),
2,
[
{
type: 31,
level: 4,
},
],
),
113: new BuildingRequirement<BuildingID, Resources>(
113, // energy
ResourceCollection.fromArray<Resources>([
new CrystallineResource(800),
new LiquidResource(400),
]),
2,
[
{
type: 31,
level: 1,
},
],
),
// TODO add the remaining buildings and tech aswell as fleet (with ids above 200)
};

export const prosumption: ProsumptionLookup<BuildingID, Resources> = {
0: {
[ResourceTypes.Metallic]: (): number => {
return 0;
},
[ResourceTypes.Crystalline]: (): number => {
return 0;
},
[ResourceTypes.Liquid]: (): number => {
return 0;
},
[EnergyTypes.Electricity]: (): number => {
return 0;
},
[EnergyTypes.Heat]: (): number => {
return 0;
},
},
1: {
[ResourceTypes.Metallic]: (lvl: number) => 30 * lvl * lvl ** 1.1,
[EnergyTypes.Electricity]: (lvl: number) => -10 * lvl * lvl ** 1.1,
},
2: {
[ResourceTypes.Crystalline]: (lvl: number) => 20 * lvl * lvl ** 1.1,
[EnergyTypes.Electricity]: (lvl: number) => -10 * lvl * lvl ** 1.1,
},
3: {
// TODO actually [ResourceTypes.Liquid]: (lvl, planet) => 10 * lvl * lvl ** 1.1 * (-0.002 * planet.maxTemperature + 1.28),
[ResourceTypes.Liquid]: (lvl: number): number => {
return 10 * lvl * lvl ** 1.1;
},
[EnergyTypes.Electricity]: (lvl: number): number => {
return -10 * lvl * lvl ** 1.1;
},
},
4: {
[EnergyTypes.Electricity]: (lvl: number): number => 20 * lvl * lvl ** 1.1,
},
12: {
[ResourceTypes.Liquid]: (lvl: number): number => {
return -10 * lvl * lvl ** 1.1;
},
[EnergyTypes.Electricity]: (lvl: number): number => {
return 50 * lvl * lvl ** 1.1;
},
},
};

const b = new Building<BuildingID, Resources>(12, requirements, prosumption, 1, 100);
const bUpgraded = new Building<BuildingID, Resources>(12, requirements, prosumption, 2, 100);
const b0 = new Building<BuildingID, Resources>(0, requirements, prosumption, 1, 50); // 0 times 0,5 is still 0
const b3 = new Building<BuildingID, Resources>(3, requirements, prosumption, 1, 100);
const b1 = new Building<BuildingID, Resources>(1, requirements, prosumption, 2, 100);
const b2 = new Building<BuildingID, Resources>(2, requirements, prosumption, 1, 100);
const b4 = new Building<BuildingID, Resources>(4, requirements, prosumption, 1, 100);

export const defaultBuildings: Building<BuildingID, Resources>[] = [];
export const emptyStock = new Stock<ResourceTypes>(zeroResources);
9 changes: 9 additions & 0 deletions src/app/engine/economy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Economy } from '@phlame/engine';
import { Empire } from '@phlame/engine';
import type { Zeitgeber } from '../signals/zeitgeber';
import { defaultBuildings, emptyStock } from './buildings';

export function economyFactory(zeit: Zeitgeber) {}

export const economy = new Economy('Phlameplanet', emptyStock, defaultBuildings);
export const empire = new Empire('Phlameland', []);
107 changes: 107 additions & 0 deletions src/app/engine/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { BaseResources, Energy, Resource, ResourceCollection } from '@phlame/engine';

export enum ResourceTypes {
// (partially) liquid - reactive non-metals
Liquid = 'liquid',
// Hydrogen = 'H', // P1-1
// Deuterium = 'D', // P1-1 (2H)
// Tritium = 'T', // P1-1 (3H)
// Helium = 'He', // P2-18
// Lithium = 'Li', // P3-1-2
// Biomass = 'biomass',
// Carbon = 'C', // P6-14-2
// Nitrogen = 'N', // P7-15-3
// Oxygen = 'O', // P8-16-2
// Phosphorus = 'P', // P15-15-3
// crystalline - metalloids
Crystalline = 'crystalline',
// Boron = 'B', // P5-13-2
// Silicon = 'Si', // P14-14-3
// metallic metals
Metallic = 'metallic',
// Cobalt = 'Co', // P27-9-4
// Titanium = 'Ti', // P22-4-4
// Iron = 'Fe', // P26-8-4
// Copper = 'Cu', // P29-11-4
// [...] need more Elements from the periodic table? https://ptable.com
// Uranium = 'U', // P92-3-7
// TODO what do we have all those special resource types for!
// The current basic types should become placeholders or titles for resource summaries...
}
// TODO? refactor use "as const" (instead of enum)

export enum EnergyTypes {
Electricity = 'energy',
Heat = 'heat',
}

export type Types = ResourceTypes | EnergyTypes | BaseResources;

// Add new resources to known resource types - only categories for now
const newResourceTypes: Types[] = [
ResourceTypes.Metallic,
ResourceTypes.Crystalline,
ResourceTypes.Liquid,
];
const newEnergyTypes: Types[] = [EnergyTypes.Electricity]; // TODO? , EnergyTypes.Heat];
Resource.types.push(...newResourceTypes);
Energy.types.push(...newEnergyTypes);

export class MetallicResource extends Resource<ResourceTypes.Metallic> {
constructor(amount: number) {
super(ResourceTypes.Metallic, amount);
}
}

export class CrystallineResource extends Resource<ResourceTypes.Crystalline> {
constructor(amount: number) {
super(ResourceTypes.Crystalline, amount);
}
}

export class LiquidResource extends Resource<ResourceTypes.Liquid> {
constructor(amount: number) {
super(ResourceTypes.Liquid, amount);
}
}

export class EnergyResource extends Energy<EnergyTypes.Electricity> {
constructor(amount: number) {
super(EnergyTypes.Electricity, amount);
}
}
/* *
* Heat as another energy example type, yes!
* Geoengineering allows to change a planets temperature for example, which has a huge impact on the ecology
* The game physics of temperature could be similar to energy level
* /
export class HeatResource extends Energy<EnergyTypes.Heat> {
constructor(amount: number) {
super(EnergyTypes.Heat, amount);
}
}*/

export type ResourceType = MetallicResource | CrystallineResource | BaseResources;
export type EnergyType = EnergyResource;

export const zeroResources = ResourceCollection.fromArray<ResourceTypes>([
new MetallicResource(0),
new CrystallineResource(0),
new LiquidResource(0),
]);
export const zeroEnergy: Energy<Types> = new EnergyResource(0);

export class ResourceFactory {
fromJSON({ type, amount }: { type: Types; amount: number }): Resource<Types> {
switch (type) {
case ResourceTypes.Metallic:
return new MetallicResource(amount);
case ResourceTypes.Crystalline:
return new CrystallineResource(amount);
case ResourceTypes.Liquid:
return new LiquidResource(amount);
default:
return Resource.Null;
}
}
}
36 changes: 36 additions & 0 deletions src/app/engine/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { inject, injectable } from '@joist/di';
import {
ComparableResource,
Economy,
ID,
Phlame,
ResourceCollection,
ResourceJSON,
Stock,
} from '@phlame/engine';
import { Zeitgeber } from '../signals/zeitgeber';
import { ResourceFactory, type Types } from './resources';
import type { BuildingIdentifier } from './buildings';

@injectable
export class EngineService {
#zeit = inject(Zeitgeber);
#resource = inject(ResourceFactory);

createPhlame(id: ID): Phlame<Types, BuildingIdentifier> {
return new Phlame(id, this.createEconomy([]));
}
createEconomy(resources: ResourceJSON<Types>[]): Economy<BuildingIdentifier, Types> {
return new Economy('Eco', this.createStock(resources));
}
createStock(json: ResourceJSON<Types>[]): Stock<Types> {
return new Stock(this.createResources(json));
}
createResources(json: ResourceJSON<Types>[]): ResourceCollection<Types> {
return ResourceCollection.fromArray(json.map((j) => this.createResource(j)));
}
createResource(json: ResourceJSON<Types>): ComparableResource<Types> {
const factory = this.#resource();
return factory.fromJSON(json);
}
}

0 comments on commit 2368a22

Please sign in to comment.