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 importToPreview?: boolean
autoEnable?: boolean autoEnable?: boolean
gdkUserId?: string gdkUserId?: string
fixInventoryItemRenderer?: boolean
} }
packinfo?: { packinfo?: {
name?: string name?: string

View file

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

View file

@ -49,20 +49,43 @@ export const defaultFunctions = {
} }
}, },
sqrt: n => { sqrt: input => {
const notAllow = RandomBindingString()
const g = RandomBindingString()
const h = RandomBindingString()
const ret = RandomBindingString() 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 { return {
genBindings: [ genBindings: [
{ {
source: `${n} / 2`, source: `(${input} < 2)`,
target: g, 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, 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 * Return a translatable string
* @param key * @param key