optimized sqrt function

This commit is contained in:
Asaki Yuki 2026-02-13 12:41:47 +07:00
parent 796b89636d
commit ab83b33725
3 changed files with 70 additions and 38 deletions

1
config.d.ts vendored
View file

@ -7,6 +7,7 @@ export interface Config {
importToPreview?: boolean
autoEnable?: boolean
gdkUserId?: string
fixInventoryItemRenderer?: boolean
}
packinfo?: {
name?: string

View file

@ -1,42 +1,46 @@
import { AnimationKeyframe } from "../components/AnimationKeyframe.js";
import { AnimType } from "../types/enums/AnimType.js";
import { KeyframeAnimationProperties } from "../types/properties/element/Animation.js";
import { Binding } from "../types/properties/value.js";
import { AnimationKeyframe } from "../components/AnimationKeyframe.js"
import { AnimType } from "../types/enums/AnimType.js"
import { BagBinding } from "../types/enums/BagBinding.js"
import { KeyframeAnimationProperties } from "../types/properties/element/Animation.js"
import { Binding } from "../types/properties/value.js"
import { config } from "./Configuration.js"
export function FormatProperties(properties: any) {
const property_bag: Record<Binding, any> = {};
const property_bag: Record<Binding, any> = {}
for (const key in properties) {
const value = properties[key];
for (const key in properties) {
const value = properties[key]
if (key.startsWith("#")) {
property_bag[<Binding>key] = value;
delete properties[key];
}
}
if (key.startsWith("#")) {
property_bag[<Binding>key] = value
delete properties[key]
}
}
if (properties.anchor) {
properties.anchor_from = properties.anchor_to = properties.anchor;
delete properties.anchor;
}
if (config.compiler?.fixInventoryItemRenderer && property_bag[BagBinding.ITEM_ID_AUX]) {
property_bag[BagBinding.ITEM_ID_AUX] = `(${property_bag[BagBinding.ITEM_ID_AUX]} / 1)`
}
if (Object.keys(property_bag).length) {
if (properties.property_bag) {
properties.property_bag = { ...property_bag, ...properties.property_bag };
} else {
properties.property_bag = property_bag;
}
}
if (properties.anchor) {
properties.anchor_from = properties.anchor_to = properties.anchor
delete properties.anchor
}
return properties;
if (Object.keys(property_bag).length) {
if (properties.property_bag) {
properties.property_bag = { ...property_bag, ...properties.property_bag }
} else {
properties.property_bag = property_bag
}
}
return properties
}
export function FormatAnimationProperties(
properties: KeyframeAnimationProperties<AnimType>,
) {
if (properties.next instanceof AnimationKeyframe) {
properties.next = `${properties.next}`;
}
export function FormatAnimationProperties(properties: KeyframeAnimationProperties<AnimType>) {
if (properties.next instanceof AnimationKeyframe) {
properties.next = `${properties.next}`
}
return properties;
return properties
}

View file

@ -49,20 +49,43 @@ export const defaultFunctions = {
}
},
sqrt: n => {
const notAllow = RandomBindingString()
const g = RandomBindingString()
const h = RandomBindingString()
sqrt: input => {
const ret = RandomBindingString()
const isNegative = RandomBindingString()
const isLowerThanTwo = RandomBindingString()
const next = RandomBindingString()
const nextEqualOrGreaterThan = RandomBindingString()
const isNextEqualOrGreaterThanRet = `(${nextEqualOrGreaterThan} * ${ret})`
const isNotNextEqualOrGreaterThanRet = `((not ${nextEqualOrGreaterThan}) * ${next})`
const lowerThanTwoPart = `(${isLowerThanTwo} * ${input})`
const notLowerThanTwoPart = `((not ${isLowerThanTwo}) * (${isNextEqualOrGreaterThanRet} + ${isNotNextEqualOrGreaterThanRet}))`
const negativePart = `(${isNegative} * -1)`
const notNegativePart = `((not ${isNegative}) * (${lowerThanTwoPart} + ${notLowerThanTwoPart}))`
return {
genBindings: [
{
source: `${n} / 2`,
target: g,
source: `(${input} < 2)`,
target: isLowerThanTwo,
},
{
source: `(${notAllow} * -1)`,
source: input,
target: ret,
},
{
source: `(${ret} + ${input} / ${ret}) / 2`,
target: next,
},
{
source: `(${next} = ${ret}) or (${next} > ${ret})`,
target: nextEqualOrGreaterThan,
},
{
source: `${negativePart} + ${notNegativePart}`,
target: ret,
},
],
@ -86,6 +109,10 @@ export const defaultFunctions = {
}
},
// str_length: str => {
// if (!/\#\w+/.test(str)) throw new Error("Invalid string")
// }
/**
* Return a translatable string
* @param key