Fix boundary logic & Add wastage behavior
This commit is contained in:
parent
da493bc6ae
commit
571e80d542
@ -11,6 +11,7 @@ import { Avoidance } from "@Behavior/Avoidance";
|
||||
import { DirectionCluster } from "@Behavior/DirectionCluster";
|
||||
import { CentralCluster } from "@Behavior/CentralCluster";
|
||||
import { Manufacture } from "@Behavior/Manufacture";
|
||||
import { Wastage } from "@Behavior/Wastage";
|
||||
|
||||
const AllBehaviors: IAnyBehaviorRecorder[] = [
|
||||
new BehaviorRecorder(Template),
|
||||
@ -25,6 +26,7 @@ const AllBehaviors: IAnyBehaviorRecorder[] = [
|
||||
new BehaviorRecorder(DirectionCluster),
|
||||
new BehaviorRecorder(CentralCluster),
|
||||
new BehaviorRecorder(Manufacture),
|
||||
new BehaviorRecorder(Wastage),
|
||||
]
|
||||
|
||||
/**
|
||||
|
@ -48,11 +48,17 @@ class BoundaryConstraint extends Behavior<IBoundaryConstraintBehaviorParameter,
|
||||
|
||||
if (ox || oy || oz) {
|
||||
|
||||
let currentFLen = individual.vectorLength(rx, ry, rz);
|
||||
const backFocus: number[] = [0, 0, 0];
|
||||
|
||||
if (ox) backFocus[0] = rx - rx * rangeList[i].radius[0] / Math.abs(rx);
|
||||
if (oy) backFocus[1] = ry - ry * rangeList[i].radius[1] / Math.abs(ry);
|
||||
if (oz) backFocus[2] = rz - rz * rangeList[i].radius[2] / Math.abs(rz);
|
||||
|
||||
let currentFLen = individual.vectorLength(backFocus);
|
||||
if (currentFLen < fLen) {
|
||||
fx = rx;
|
||||
fy = ry;
|
||||
fz = rz;
|
||||
fx = backFocus[0];
|
||||
fy = backFocus[1];
|
||||
fz = backFocus[2];
|
||||
fLen = currentFLen;
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ import { Model } from "@Model/Model";
|
||||
type IContactAssimilateBehaviorParameter = {
|
||||
target: "CLG",
|
||||
assimilate: "CG",
|
||||
self: "CG",
|
||||
success: "number",
|
||||
range: "number"
|
||||
}
|
||||
@ -27,15 +28,13 @@ class ContactAssimilate extends Behavior<IContactAssimilateBehaviorParameter, IC
|
||||
public override parameterOption = {
|
||||
target: { type: "CLG", name: "$Target" },
|
||||
assimilate: { type: "CG", name: "$Assimilate" },
|
||||
self: { type: "CG", name: "$Self" },
|
||||
range: { type: "number", name: "$Range", defaultValue: .05, numberMin: 0, numberStep: .01 },
|
||||
success: { type: "number", name: "$Success", defaultValue: 90, numberMin: 0, numberMax: 100, numberStep: 5 }
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
|
||||
let assimilateGroup = this.parameter.assimilate.objects;
|
||||
if (!assimilateGroup) return;
|
||||
|
||||
for (let i = 0; i < this.parameter.target.objects.length; i++) {
|
||||
const targetGroup = this.parameter.target.objects[i];
|
||||
|
||||
@ -50,7 +49,18 @@ class ContactAssimilate extends Behavior<IContactAssimilateBehaviorParameter, IC
|
||||
|
||||
// 成功判定
|
||||
if (Math.random() * 100 < this.parameter.success) {
|
||||
targetIndividual.transfer(assimilateGroup!);
|
||||
|
||||
// 同化目标
|
||||
let assimilateGroup = this.parameter.assimilate.objects;
|
||||
if (assimilateGroup) {
|
||||
targetIndividual.transfer(assimilateGroup);
|
||||
}
|
||||
|
||||
// 同化目标
|
||||
let selfGroup = this.parameter.self.objects;
|
||||
if (selfGroup) {
|
||||
individual.transfer(selfGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -70,6 +80,10 @@ class ContactAssimilate extends Behavior<IContactAssimilateBehaviorParameter, IC
|
||||
"ZH_CN": "到哪个群...",
|
||||
"EN_US": "To group..."
|
||||
},
|
||||
"$Self": {
|
||||
"ZH_CN": "自身同化到...",
|
||||
"EN_US": "Self assimilate to..."
|
||||
},
|
||||
"$Range": {
|
||||
"ZH_CN": "同化范围 (m)",
|
||||
"EN_US": "Assimilate range (m)"
|
||||
|
100
source/Behavior/Wastage.ts
Normal file
100
source/Behavior/Wastage.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import { Behavior } from "@Model/Behavior";
|
||||
import { Group } from "@Model/Group";
|
||||
import { Individual } from "@Model/Individual";
|
||||
import { Model } from "@Model/Model";
|
||||
|
||||
type IWastageBehaviorParameter = {
|
||||
key: "string",
|
||||
value: "number",
|
||||
speed: "number",
|
||||
threshold: "number",
|
||||
kill: "boolean",
|
||||
assimilate: "CG"
|
||||
}
|
||||
|
||||
type IWastageBehaviorEvent = {}
|
||||
|
||||
class Wastage extends Behavior<IWastageBehaviorParameter, IWastageBehaviorEvent> {
|
||||
|
||||
public override behaviorId: string = "Wastage";
|
||||
|
||||
public override behaviorName: string = "$Title";
|
||||
|
||||
public override iconName: string = "BackgroundColor";
|
||||
|
||||
public override describe: string = "$Intro";
|
||||
|
||||
public override category: string = "$Initiative";
|
||||
|
||||
public override parameterOption = {
|
||||
key: { type: "string", name: "$Key" },
|
||||
value: { type: "number", name: "$Value", defaultValue: 100, numberStep: 1 },
|
||||
speed: { type: "number", name: "$Speed", defaultValue: 1, numberStep: .1 },
|
||||
threshold: { type: "number", name: "$Threshold", defaultValue: 0, numberStep: 1 },
|
||||
kill: { type: "boolean", name: "$Kill", defaultValue: true },
|
||||
assimilate: { type: "CG", name: "$Assimilate", condition: { key: "kill", value: false } }
|
||||
};
|
||||
|
||||
public effect = (individual: Individual, group: Group, model: Model, t: number): void => {
|
||||
const key = this.parameter.key;
|
||||
if (!key) return;
|
||||
|
||||
let currentValue = individual.getData(`Wastage.${key}`) ?? this.parameter.value;
|
||||
currentValue -= this.parameter.speed * t;
|
||||
|
||||
// 超过阈值
|
||||
if (currentValue < this.parameter.threshold) {
|
||||
|
||||
currentValue = undefined;
|
||||
|
||||
// 杀死个体
|
||||
if (this.parameter.kill) {
|
||||
individual.die();
|
||||
}
|
||||
|
||||
// 开启同化
|
||||
else if (this.parameter.assimilate.objects) {
|
||||
individual.transfer(this.parameter.assimilate.objects);
|
||||
}
|
||||
}
|
||||
|
||||
individual.setData(`Wastage.${key}`, currentValue);
|
||||
}
|
||||
|
||||
public override terms: Record<string, Record<string, string>> = {
|
||||
"$Title": {
|
||||
"ZH_CN": "流逝",
|
||||
"EN_US": "Wastage"
|
||||
},
|
||||
"$Intro": {
|
||||
"ZH_CN": "随着时间流逝",
|
||||
"EN_US": "As time goes by"
|
||||
},
|
||||
"$Key": {
|
||||
"ZH_CN": "元数据",
|
||||
"EN_US": "Metadata"
|
||||
},
|
||||
"$Value": {
|
||||
"ZH_CN": "默认数值",
|
||||
"EN_US": "Default value"
|
||||
},
|
||||
"$Speed": {
|
||||
"ZH_CN": "流逝速度 (c/s)",
|
||||
"EN_US": "Passing speed (c/s)"
|
||||
},
|
||||
"$Threshold": {
|
||||
"ZH_CN": "阈值",
|
||||
"EN_US": "Threshold"
|
||||
},
|
||||
"$Kill": {
|
||||
"ZH_CN": "死亡",
|
||||
"EN_US": "Death"
|
||||
},
|
||||
"$Assimilate": {
|
||||
"ZH_CN": "同化",
|
||||
"EN_US": "Assimilate"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { Wastage };
|
Loading…
Reference in New Issue
Block a user