File size: 3,628 Bytes
6da8440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Renders a list of parts into the compact Luau build format Bloxy learns to write.

Every build Bloxy produces uses the same small helper, so the model only has to
learn one pattern:

    P(name, size, position, material, hexColor [, rotationDegrees [, transparency]])

Positions are relative to the build's origin (center of the footprint, on the
ground). The script puts the finished Model at the current camera focus so it
works when pasted into the Studio command bar.
"""

from __future__ import annotations

from dataclasses import dataclass, field

HEADER = """local V, M = Vector3.new, Enum.Material
local build = Instance.new("Model")
build.Name = "{model_name}"
local origin = workspace.CurrentCamera.Focus.Position * Vector3.new(1, 0, 1)

local function P(name, size, pos, material, color, rot, transparency)
\tlocal p = Instance.new("Part")
\tp.Name = name
\tp.Anchored = true
\tp.Size = size
\tp.Material = material
\tp.Color = Color3.fromHex(color)
\tp.TopSurface, p.BottomSurface = Enum.SurfaceType.Smooth, Enum.SurfaceType.Smooth
\tp.CFrame = CFrame.new(origin + pos)
\t\t* (rot and CFrame.Angles(math.rad(rot.X), math.rad(rot.Y), math.rad(rot.Z)) or CFrame.identity)
\tp.Transparency = transparency or 0
\tp.Parent = build
\treturn p
end
"""

FOOTER = """
build.Parent = workspace
"""


def _num(x: float) -> str:
    r = round(x, 2)
    if r == int(r):
        return str(int(r))
    return f"{r:.2f}".rstrip("0").rstrip(".")


def vec(v) -> str:
    return f"V({_num(v[0])}, {_num(v[1])}, {_num(v[2])})"


@dataclass
class Part:
    name: str
    size: tuple[float, float, float]
    pos: tuple[float, float, float]
    material: str  # Enum.Material item name, e.g. "WoodPlanks"
    color: str  # hex without '#'
    rot: tuple[float, float, float] | None = None
    transparency: float = 0.0

    def line(self) -> str:
        args = [
            f'"{self.name}"',
            vec(self.size),
            vec(self.pos),
            f"M.{self.material}",
            f'"{self.color.lower()}"',
        ]
        if self.rot or self.transparency:
            args.append(vec(self.rot) if self.rot else "nil")
        if self.transparency:
            args.append(_num(self.transparency))
        return "P(" + ", ".join(args) + ")"


@dataclass
class Build:
    model_name: str
    parts: list[Part] = field(default_factory=list)
    sections: list[tuple[str, list[Part]]] = field(default_factory=list)

    def add(self, *parts: Part) -> None:
        self.parts.extend(parts)

    def section(self, title: str, parts: list[Part]) -> None:
        self.sections.append((title, parts))

    def all_parts(self) -> list[Part]:
        out = list(self.parts)
        for _, ps in self.sections:
            out.extend(ps)
        return out

    def bounds(self) -> tuple[float, float, float]:
        ps = self.all_parts()
        if not ps:
            return (0, 0, 0)
        mins = [min(p.pos[i] - p.size[i] / 2 for p in ps) for i in range(3)]
        maxs = [max(p.pos[i] + p.size[i] / 2 for p in ps) for i in range(3)]
        return tuple(round(maxs[i] - mins[i], 1) for i in range(3))  # type: ignore[return-value]

    def to_luau(self) -> str:
        lines = [HEADER.format(model_name=self.model_name)]
        if self.parts:
            lines.extend(p.line() for p in self.parts)
        for title, ps in self.sections:
            lines.append(f"\n-- {title}")
            lines.extend(p.line() for p in ps)
        lines.append(FOOTER)
        return "\n".join(lines).strip() + "\n"


def answer(intro: str, build: Build) -> str:
    return f"{intro.strip()}\n\n```lua\n{build.to_luau()}```"