s2ts
Version:
A tool to automatically compile counter-strike TS files (.vts files)
155 lines (154 loc) • 5.56 kB
JavaScript
import { Instance } from "cspointscript";
import { Color } from "../color";
import { game } from "../game";
import { uniqueId } from "../helpers";
import { Vector } from "../vector";
import { addOutputByName } from "./addOutputByName";
import { runServerCommand } from "./util";
const PropDynamicSolid = {
none: 0,
bsp: 1,
bbox: 2,
obb: 3,
sphere: 4,
point: 5,
vphysics: 6,
capsule: 7,
last: 8
};
const PointWorldTextJustifyHorizontal = {
left: 0,
center: 1,
right: 2
};
const PointWorldTextJustifyVertical = {
top: 0,
center: 1,
bottom: 2
};
const PointWorldTextReorientMode = {
none: 0,
"rotate-around-up": 1
};
const LogicEventListenerTeam = {
"dont-care": -1,
terrorists: 2,
"counter-terrorists": 3
};
const PointSoundEventEntityIndexSelection = {
"use-world-index": 0,
"use-entity-index": 1
};
const customOutputs = {};
Instance.PublicMethod("s2ts-custom-output", (outputId) => {
const fn = customOutputs[outputId];
if (fn) {
fn();
}
});
export const createEntity = (entity) => {
const parsedCommonKeyValues = {
targetname: entity.keyValues?.targetName ?? uniqueId(),
origin: entity.keyValues?.origin,
angles: entity.keyValues?.angles
};
const parsedEntitySpecificKeyValues = parseEntitySpecificKeyValues({
class: entity.class,
keyValues: entity.keyValues ?? {},
outputs: entity.outputs ?? {}
});
const fullyParsedKeyValues = {
...parsedCommonKeyValues,
...parsedEntitySpecificKeyValues,
...(entity.keyValueOverrides ?? {})
};
const entityProps = Object.entries(fullyParsedKeyValues)
.filter(([, value]) => value !== undefined)
.map(([key, value]) => {
if (typeof value === "boolean") {
return [key, value ? "1" : "0"];
}
if (typeof value === "object") {
if ("x" in value && "y" in value && "z" in value) {
return [key, Vector.format(value)];
}
if ("red" in value && "green" in value && "blue" in value) {
return [key, Color.format(value)];
}
}
return [key, value];
})
.map(([key, value]) => `"${key}" "${value}"`)
.join(" ");
runServerCommand(`ent_create ${entity.class} { ${entityProps} }`);
Object.entries(entity.outputs ?? {}).forEach(([outputName, fn]) => {
const outputId = uniqueId();
customOutputs[outputId] = fn;
game.runAfterDelaySeconds(() => {
addOutputByName(parsedCommonKeyValues.targetname, {
outputName: outputName.charAt(0).toUpperCase() + outputName.slice(1),
targetName: "s2ts-script",
viaThisInput: "s2ts-custom-output",
parameter: outputId
});
}, 0.1);
});
};
const parseEntitySpecificKeyValues = (entity) => {
const { class: entityClass, keyValues } = entity;
switch (entityClass) {
case "prop_dynamic":
return {
model: keyValues.model,
solid: PropDynamicSolid[keyValues.solid ?? "vphysics"],
renderamt: keyValues.renderAmount,
rendercolor: keyValues.renderColor
};
case "logic_timer":
return {
refiretime: keyValues.refireTime
};
case "env_explosion":
return {
iMagnitude: keyValues.magnitude,
explosion_custom_sound: keyValues.sound
};
case "point_worldtext":
return {
message: keyValues.message,
enabled: keyValues.enabled ?? true,
font_size: keyValues.fontSize,
color: keyValues.color,
world_units_per_pixel: keyValues.worldUnitsPerPixel ?? 0.25,
font_name: keyValues.fontName ?? "Arial Black",
justify_horizontal: PointWorldTextJustifyHorizontal[keyValues.justifyHorizontal ?? "left"],
justify_vertical: PointWorldTextJustifyVertical[keyValues.justifyVertical ?? "bottom"],
reorient_mode: PointWorldTextReorientMode[keyValues.reorientMode ?? "none"],
depth_render_offset: keyValues.depthRenderOffset
};
case "env_fade":
return {
duration: keyValues.duration ?? 2,
holdtime: keyValues.holdTime ?? 0,
rendercolor: keyValues.renderColor ?? Color.black,
renderamt: keyValues.renderAmount ?? 255
};
case "logic_eventlistener":
return {
EventName: keyValues.eventName,
IsEnabled: keyValues.enabled ?? true,
TeamNum: LogicEventListenerTeam[keyValues.team ?? "dont-care"]
};
case "point_soundevent":
return {
soundName: keyValues.soundName ?? "",
sourceEntityName: keyValues.sourceEntityName ?? "",
startOnSpawn: keyValues.startOnSpawn ?? false,
toLocalPlayer: keyValues.toLocalPlayer ?? false,
stopOnNew: keyValues.retirggerStopsLast ?? true,
saveAndRestore: keyValues.saveAndRestore ?? false,
sourceEntityAttachment: keyValues.entityAttachmentName ?? "",
entityIndexSelection: PointSoundEventEntityIndexSelection[keyValues.entityIndexSelection ?? "use-world-index"]
};
}
};