> For the complete documentation index, see [llms.txt](https://notebook.lazyknight.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notebook.lazyknight.com/code-pieces.md).

# 代码片段

## Code Pieces

pieces of code here.

## A

### Build Game Unit

```lua
local MainQuest = {}
MainQuest.Units = {}

local u_enemy = CreateUnit(uid, GetPlayerPoint(player, 'MainEnemyPoint'), 270, 'Enemy')

function GetPlayerPoint(player, label)
    local pid = player:GetID()
    if label == 'MainEnemyPoint' then
        return {{-512,0}, {512,0}, {0,512}, {0,-512}}[pid]
    end
end

function CreateUnit(uid, point, face, type)
    local unit = ... stuffs here ...
    Matrix.SetupUnitPower(u, type)
    return unit
end

function Matrix.CalcUnitPower(u, type)
    local info = {}
    if type == 'Enemy' then
        info.ATK = G_DIFFICULT * G_CHAPTER * 300
        info.DEF = G_DIFFICULT * G_CHAPTER * 50
        info.HP = G_DIFFICULT * 1000 + G_CHAPTER * 200
    end
    return info
end

function Matrix.AssignUnitPower(u, info)
    if info.ATK then u:SetAttr('ATK', info.ATK) end
    if info.DEF then u:SetAttr('DEF', info.DEF) end
    if info.HP then u:SetAttr('HP', info.HP) end
    if info.MP then u:SetAttr('MP', info.MP) end
    if info.SPD then u:SetAttr('SPD', info.SPD) end
    if info.RNG then u:SetAttr('RNG', info.RNG) end
    if info.SIGHT then u:SetAttr('SIGHT', info.SIGHT) end
end

function Matrix.SetupUnitPower(u, type)
    local info = Matrix.CalcUnitPower(u, type)
    Matrix.AssignUnitPower(info)
end
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://notebook.lazyknight.com/code-pieces.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
