Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Modular Life v2.1] モジュールの実装 #164

Merged
merged 15 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/classes/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ export function toggleFullscreen(elementId: string): void {
export const strictEntries = <T extends Record<string, any>>(object: T): [keyof T, T[keyof T]][] => {
return Object.entries(object)
}

export class ValuedArrayMap<Key, Element> extends Map<Key, Array<Element>> {
public getValueFor(key: Key): Element[] {
const stored = this.get(key)
if (stored != null) {
return stored
}
const newArray: Element[] = []
this.set(key, newArray)
return newArray
}
}
94 changes: 70 additions & 24 deletions src/simulations/modular_life_v2/ancestor/ancestor.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,75 @@
import { Computer, Hull, Mover } from "../module/module"
import { SourceCode } from "../module/source_code"
import { createScopeData } from "../physics/scope"

/// 生命として成立する最小の祖先種
export const createMinimumAncestor = (code: SourceCode): unknown => { // TODO:
/*
- 計算機 x 1
- 外殻 x 1
- 推進器 x 1
*/
throw "not implemented"
}
export const Ancestor = {
/// 生命として成立する最小の祖先種
minimum(code: SourceCode): Hull {
/*
- 計算機 x 1
- 外殻 x 1
- 推進器 x 1
*/

/// 自己複製できる最小の祖先種
export const createMinimumSelfReproductionAncestor = (code: SourceCode): unknown => { // TODO:
/*
- 計算機 x 1
- 物質輸送口 x 1
- 外殻 x n
- 推進器 x 1
- 物質変換器 x n
- 組み立て機 x 1
*/
throw "not implemented"
}
const computer: Computer = {
case: "computer",
code,
}
const mover: Mover = {
case: "mover",
}

return {
case: "hull",
hits: 0,
hitsMax: 1000,
size: 4,
internalModules: {
computer: [computer],
assembler: [],
channel: [],
mover: [mover],
materialSynthesizer: [],
},
...createScopeData(1000),
}
},

/// 試験用
test(code: SourceCode): Hull {
return {
case: "hull",
hits: 0,
hitsMax: 1000,
size: 3,
internalModules: {
computer: [],
assembler: [],
channel: [{ case: "channel" }, { case: "channel" }, { case: "channel" }, { case: "channel" }],
mover: [{ case: "mover" }, { case: "mover" }, { case: "mover" }, { case: "mover" }],
materialSynthesizer: [],
},
...createScopeData(1000),
}
},


/// 自己複製できる最小の祖先種
minimumSelfReproduction(code: SourceCode): Hull { // TODO:
/*
- 計算機 x 1
- 物質輸送口 x 1
- 外殻 x n
- 推進器 x 1
- 物質変換器 x n
- 組み立て機 x 1
*/
throw "not implemented"
},

/// 自身より複雑な子孫を組み立てられる祖先種
export const createGeneralAssemblerAncestor = (code: SourceCode): unknown => { // TODO:
throw "not implemented"
/// 自身より複雑な子孫を組み立てられる祖先種
generalAssembler(code: SourceCode): Hull { // TODO:
throw "not implemented"
},

}
24 changes: 19 additions & 5 deletions src/simulations/modular_life_v2/ancestor/source_code.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { SourceCode } from "../module/source_code"
import type { ComputeArgument, SourceCode } from "../module/source_code"
import type { NeighbourDirection } from "../physics/direction"

/// ゲーム世界上で何も行わない
export const createStillCode = (): SourceCode => {
return () => {
console.log("still code")
export const AncestorCode = {
/// ゲーム世界上で何も行わない
stillCode(): SourceCode {
return ([, environment]: ComputeArgument) => {
if (environment.time % 100 === 0) {
console.log(`[still code] t: ${environment.time}`)
}
}
},

/// 一定方向へ移動するのみ
moveCode(direction: NeighbourDirection, moveInterval: number): SourceCode {
return ([api, environment]: ComputeArgument) => {
if (environment.time % moveInterval === 0) {
api.move(direction)
}
}
}
}
45 changes: 45 additions & 0 deletions src/simulations/modular_life_v2/api_request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Hull, ModuleType } from "./module/module"
import type { MaterialRecipeName, TransferrableMaterialType } from "./physics/material"
import type { NeighbourDirection } from "./physics/direction"

export type Life = Hull

// リクエストが関係するのはScopeの中だけなのでローカルに処理できるか?
export type ComputeRequestMove = {
readonly case: "move"
readonly direction: NeighbourDirection
}
export type ComputeRequestUptake = {
readonly case: "uptake"
readonly materialType: TransferrableMaterialType
readonly amount: number
}
export type ComputeRequestExcretion = {
readonly case: "excretion"
readonly materialType: TransferrableMaterialType
readonly amount: number
}
export type ComputeRequestSynthesize = {
readonly case: "synthesize"
readonly recipe: MaterialRecipeName
}
export type ComputeRequestAssemble = {
readonly case: "assemble"
readonly moduleType: ModuleType
}

export type IntraScopeMaterialTransferRequest = ComputeRequestSynthesize
| ComputeRequestAssemble
export type IntraScopeMaterialTransferRequestType = IntraScopeMaterialTransferRequest["case"]

export type InterScopeMaterialTransferRequest = ComputeRequestUptake
| ComputeRequestExcretion
export type InterScopeMaterialTransferRequestType = InterScopeMaterialTransferRequest["case"]

export type MaterialTransferRequest = IntraScopeMaterialTransferRequest
| InterScopeMaterialTransferRequest
export type MaterialTransferRequestType = MaterialTransferRequest["case"]

export type ComputeRequest = ComputeRequestMove
| MaterialTransferRequest
export type ComputeRequestType = ComputeRequest["case"]
6 changes: 3 additions & 3 deletions src/simulations/modular_life_v2/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { URLParameterParser } from "../../classes/url_parameter_parser_v3"
import { isLogLevel, LogLevel } from "./logger"
import { defaultMaterialProductionSpec } from "./physics/material"
import { materialProductionRecipes } from "./physics/material"
import type { PhysicalConstant } from "./physics/physical_constant"

const parameters = new URLParameterParser(document.location.search)
Expand All @@ -11,7 +11,7 @@ const physicalConstant: PhysicalConstant = {
heatLossRate: parameters.parseFloat("heat_loss", { alternativeKey: "ph.h", min: 0 }) ?? 0.25,
energyHeatConversionRate: parameters.parseFloat("energy_heat_conversion", { alternativeKey: "ph.e", min: 0 }) ?? 0.5,

materialProduction: defaultMaterialProductionSpec,
materialProductionRecipe: materialProductionRecipes,
}

export const constants = {
Expand All @@ -20,7 +20,7 @@ export const constants = {
logLevel,
},
simulation: {
cellSize: parameters.parseInt("cell_size", { alternativeKey: "si.c", min: 1 }) ?? 8,
cellSize: parameters.parseInt("cell_size", { alternativeKey: "si.c", min: 1 }) ?? 16,
worldSize: parameters.parseInt("world_size", { alternativeKey: "si.w", min: 4 }) ?? 50,
frameSkip: parameters.parseInt("frame_skip", { alternativeKey: "si.f", min: 1 }) ?? 2,
},
Expand Down
29 changes: 29 additions & 0 deletions src/simulations/modular_life_v2/engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Life, MaterialTransferRequest, MaterialTransferRequestType } from "./api_request"
import { PhysicalConstant } from "./physics/physical_constant"
import { Scope } from "./physics/scope"
import { TerrainCell } from "./terrain"

export type ScopeOperation = {
readonly life: Life
readonly requests: Map<MaterialTransferRequestType, MaterialTransferRequest[]>
}

export class Engine {
public constructor(
public readonly physicalConstant: PhysicalConstant,
) {
}

public celculateScope(scope: Scope, operations: ScopeOperation[]): void {

}

// TODO: celculateScopeに統合する?
public calculateCell(cell: TerrainCell): void {
const energyLoss = Math.floor(cell.amount.energy * this.physicalConstant.energyHeatConversionRate)
cell.amount.energy = cell.amount.energy - energyLoss + cell.energyProduction
cell.heat += energyLoss

cell.heat = Math.floor(cell.heat * (1 - this.physicalConstant.heatLossRate))
}
}
5 changes: 0 additions & 5 deletions src/simulations/modular_life_v2/module/any_module.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/simulations/modular_life_v2/module/api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
import type { NeighbourDirection } from "../physics/direction"
import type { MaterialRecipeName, MaterialType, TransferrableMaterialType } from "../physics/material"
import type { AnyModule, ModuleType } from "./module"

export type ComputerApi = {
getStoredAmount(materialType: MaterialType): number
getEnergyAmount(): number
getHeat(): number

modules(): AnyModule[]

move(direction: NeighbourDirection): void
uptake(materialType: TransferrableMaterialType, amount: number): void
excretion(materialType: TransferrableMaterialType, amount: number): void
synthesize(recipe: MaterialRecipeName): void
assemble(moduleType: ModuleType): void
}
54 changes: 0 additions & 54 deletions src/simulations/modular_life_v2/module/assemble.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/simulations/modular_life_v2/module/compute.ts

This file was deleted.

44 changes: 0 additions & 44 deletions src/simulations/modular_life_v2/module/hull.ts

This file was deleted.

Loading