Compare commits
357 Commits
shitcode
...
cb6e046b81
| Author | SHA1 | Date | |
|---|---|---|---|
| cb6e046b81 | |||
| 21e0b8ec22 | |||
| 99b8c1d0b6 | |||
| 0a20ac8987 | |||
| 98dc1e17b7 | |||
| c0cbf86c8b | |||
| 592bbf9c58 | |||
| 3854410041 | |||
| 9ee71e9261 | |||
| 9389b02cda | |||
| 3eb8b375ec | |||
| 0a6d19102a | |||
| 78c1556f26 | |||
| c6e12ebb08 | |||
| 44952915e7 | |||
| dc0d5dc2c1 | |||
| 8a5b358d79 | |||
| 0051dea067 | |||
| 83aac87ab9 | |||
| bdef2fffaf | |||
| c578e6d5b1 | |||
| 85aaf470f7 | |||
| bd1059f9d7 | |||
| 73905c8bc6 | |||
| e410afaffc | |||
| ab140b4373 | |||
| 832ff491f6 | |||
| 9bbdbe4d2b | |||
| edd20882ed | |||
| 35de78bdec | |||
| 0ee21106d5 | |||
| ac8cb8a99f | |||
| 1a97abe49e | |||
| 3255b919e2 | |||
| 2b7600b745 | |||
| 5265024231 | |||
| 089105d3e9 | |||
| 88abb30041 | |||
| 566fbc36d7 | |||
| c143e95b27 | |||
| 35699c1489 | |||
| 17a2aabfcc | |||
| 4d11335470 | |||
| f60faa4293 | |||
| c902aff4af | |||
| 4a8a6245a6 | |||
| 08dde36326 | |||
| 964a60ed7c | |||
| 6c64e2fe27 | |||
| 3feefb2e96 | |||
| e8210924bf | |||
| bbaf18f352 | |||
| 95bba1b02d | |||
| b314f7adf8 | |||
| 0b372111cf | |||
| c2782dda61 | |||
| a9f96d97f2 | |||
| 18d152bfa2 | |||
| bbafab8e00 | |||
| c871b549d8 | |||
| 7c241af96a | |||
| e4f0799487 | |||
| 757221e668 | |||
| 3793afebbd | |||
| af3ce8c769 | |||
| fcae65e8f8 | |||
| 1b98571eae | |||
| 51394d1df6 | |||
| c6ec550ee4 | |||
| b881039368 | |||
| 1b5d122205 | |||
| 7c7a318d22 | |||
| ad521ce2b7 | |||
| 0d986dafd5 | |||
| f34c12c59f | |||
| 89bc05d2cb | |||
| 99093493c0 | |||
| 31d66277ae | |||
| 93d4b3145c | |||
| ed9a80b2cf | |||
| 9fd665aaa5 | |||
| 8e09f8fa53 | |||
| 97084c9060 | |||
| 7e46c858cc | |||
| ac28f9e92b | |||
| b62e19d10a | |||
| 6dba6f8c78 | |||
| 0a97ef3a83 | |||
| 7661cacbfe | |||
| c172198ff7 | |||
| f1c82d9220 | |||
| b4290ad1f9 | |||
| f31277f8b0 | |||
| 0189b29257 | |||
| b0aad2823a | |||
| 76f74743ba | |||
| cc8f896d6e | |||
| b579418b9a | |||
| bddcb4e36c | |||
| d5c6101c0f | |||
| b53753c6a5 | |||
| 427c674103 | |||
| 91603e21f8 | |||
| f5fba0b779 | |||
| d5b9504cc5 | |||
| a1e74978a5 | |||
| 1dc6fb3f39 | |||
| 89c52c80bc | |||
| 162f8d2639 | |||
| 7af83a87d8 | |||
| 0e59cf799a | |||
| 89de48b5a8 | |||
| 362d626f9b | |||
| defd3531bc | |||
| 584e710d5c | |||
| 8a0b5785ab | |||
| 9873e059cb | |||
| d05af7dacc | |||
| 9ed2319b14 | |||
| d6cc39a0b2 | |||
| 2935c4d10b | |||
| 53801a56a8 | |||
| f1610056cc | |||
| 914ea881a4 | |||
| cb87d16560 | |||
| ceb35639b8 | |||
| 79df8f2f52 | |||
| 44916bcd3b | |||
| 3483cd650a | |||
| c4104a1834 | |||
| e98935cf6f | |||
| 338cc7a36b | |||
| 18f06d13d3 | |||
| e986a4c538 | |||
| 93bda75727 | |||
| 1531abc3e2 | |||
| a34822ed54 | |||
| 145f65d4af | |||
| e359c4be09 | |||
| c009493484 | |||
| fab86c0486 | |||
| cb15f25d60 | |||
| 867238e11b | |||
| df247108ef | |||
| e34fd29a4b | |||
| 41cf4710da | |||
| c6573ae308 | |||
| 4ef2476771 | |||
| 2c13e1697f | |||
| 7b4839e63b | |||
| 2813d9d5fe | |||
| 652200f28c | |||
| eeffae6852 | |||
| fe3298d112 | |||
| 7d49bbf535 | |||
| efcbc909e0 | |||
| 17413df9c4 | |||
| 2eede05436 | |||
| b863c88838 | |||
| 692105abfa | |||
| 1a6086190e | |||
| e20c3b4ef4 | |||
| 8b55ccff72 | |||
| c7cfe26335 | |||
| 51e6763457 | |||
| c47ab6ac3c | |||
| 6fd8295500 | |||
| b12f890740 | |||
| 4c90041c68 | |||
| b50d642bcf | |||
| e0b9024458 | |||
| 745eaa8569 | |||
| b6051e5724 | |||
| 5c936025f1 | |||
| 5be847bf58 | |||
| 0198771c51 | |||
| 5272294aeb | |||
| 2eb7d04039 | |||
| c70bf96734 | |||
| b64c8e9350 | |||
| 319f155849 | |||
| ed4094d964 | |||
| 6b074c7a8c | |||
| 00633226cf | |||
| 6156a4bb6f | |||
| b6f354f6fe | |||
| cfc4502cb0 | |||
| bafe26983c | |||
| 7b43def5fb | |||
| f4896b89ef | |||
| c58467f22b | |||
| 1d5f93c35a | |||
| 46427f565f | |||
| 8fb4e4db00 | |||
| 9c56db6ac4 | |||
| 15bb906df7 | |||
| e9818fba24 | |||
| a6fa46bb96 | |||
| 44adcf9535 | |||
| 97292d6205 | |||
| f2af5e725a | |||
| 25f38d1acd | |||
| 1d43ae1255 | |||
| 9426ac01dd | |||
| 21232bd1f7 | |||
| 0c02c0045d | |||
| 205cbc10c2 | |||
| 660c96ab5c | |||
| ec8499ff76 | |||
| 430ac78ea1 | |||
| f7140fb3cc | |||
| 6153b053f6 | |||
| bc03aa4ba6 | |||
| 04f90a7b2d | |||
| 66380fb873 | |||
| 169dd64bce | |||
| 613b7ead83 | |||
| 94dc3fa819 | |||
| 6c77b5a38c | |||
| c41ee05b55 | |||
| 87c6e5f3f3 | |||
| e435b21b43 | |||
| 0baa7af759 | |||
| 83304bbfc1 | |||
| 7dbd2f3be8 | |||
| b41448da7b | |||
| 865f46b734 | |||
| 3dd28858f5 | |||
| 9119582ddd | |||
| a12091fc93 | |||
| cfadf47951 | |||
| b0fc925750 | |||
| e12b0d64e5 | |||
| efcee33a02 | |||
| ce778673c7 | |||
| 5ec4bdd933 | |||
| 6332f23bf3 | |||
| 966af2328a | |||
| d08ee33400 | |||
| ba97a8f36f | |||
| ef6c0d9fe6 | |||
| c3bdb29852 | |||
| 6904090d19 | |||
| f3c5beb341 | |||
| fc90ae54c0 | |||
| 0952e96452 | |||
| 19d6637a29 | |||
| 6fb06b7109 | |||
| d392414cb7 | |||
| 8c7aaefe09 | |||
| 1d1591accc | |||
| ec614e6080 | |||
| c32d324bfb | |||
| 37d759e48a | |||
| 4b9afcf97d | |||
| c9ce8e5f60 | |||
| b7124419e7 | |||
| 8742b86a44 | |||
| 79111b3cbd | |||
| 087a8eee86 | |||
| cb853778fe | |||
| bdd35af484 | |||
| 882165c9e7 | |||
| e294c34801 | |||
| b6e07d04e3 | |||
| 3d03477467 | |||
| c75e06d274 | |||
| 6d3e9af2f8 | |||
| cabe562dc4 | |||
| 43bee3dae0 | |||
| 4f4b268d3c | |||
| c2b0c54236 | |||
| 907230e67d | |||
| 03ce7983eb | |||
| e81e2141a6 | |||
| 3d0a906a59 | |||
| 18dbce95c0 | |||
| b646a8e3e3 | |||
| 7ec4f906c8 | |||
| 39dbf29ff6 | |||
| a4b1ff2ee5 | |||
| 0c5f508152 | |||
| 50dd702e6e | |||
| 426fea3691 | |||
| 299dad5e11 | |||
| 0e8edc4f05 | |||
| f13a0b1273 | |||
| 57de1f4b42 | |||
| d0eba35e8d | |||
| 8640e55415 | |||
| 84295c56c4 | |||
| eaf350e9ce | |||
| dfd8021808 | |||
| d9f9bd7a6c | |||
|
|
0ff45e4da1 | ||
| 395f45aeb9 | |||
| d9aa50fc91 | |||
| 876b24e1fe | |||
| 37ad60d348 | |||
| bc50096293 | |||
| 1c5038ff14 | |||
| 562ff7d24c | |||
| a08934c6e4 | |||
| 6236a12ca9 | |||
| f7ec69bd54 | |||
| bf107cb167 | |||
| cb49582a19 | |||
| cf4ca6f8dc | |||
| 2032082ba8 | |||
| 9557642805 | |||
| 6db2e9f77a | |||
| 275f54c7b7 | |||
| 47dc024b6c | |||
| efeb5c75cd | |||
| c0575e2c82 | |||
| 4e823bfee6 | |||
| 44ba5c7cfa | |||
| 475b199ddb | |||
| 96b51caaaf | |||
| 514e676e9a | |||
| 7773e25a81 | |||
| 96289375b1 | |||
| 40c1d34124 | |||
| 2655127a22 | |||
| b1c3e50445 | |||
| 9c919343cf | |||
| 13391b085e | |||
| 27004ebf45 | |||
| 7b951b64ac | |||
| c57a7ceadd | |||
| 95084d5daf | |||
| 3e9996e6a0 | |||
| 3646ec78cd | |||
| cf141f9797 | |||
| 475b5fab0c | |||
| 6f742e7291 | |||
| b5994d7383 | |||
| c5a71d5bdc | |||
| 1ac87b464a | |||
| 5b22c900d5 | |||
| 0b749564af | |||
| a699367bf4 | |||
| 720b7edcdb | |||
| bbd5c781e4 | |||
| 0870ce37e9 | |||
| a5c8846130 | |||
| 745fdb8dd3 | |||
| a0a1db6179 | |||
| 1d576dcaf5 | |||
| 414d5008d3 | |||
| 7908f5fc9f | |||
| af87c2912c | |||
| e44420d0ab | |||
| ace217832b | |||
| f9eccced09 | |||
| d07b7c811f | |||
| f556e4349e |
@@ -24,13 +24,16 @@
|
|||||||
"enables": [],
|
"enables": [],
|
||||||
"globals": [
|
"globals": [
|
||||||
"core",
|
"core",
|
||||||
|
"Smc",
|
||||||
"table",
|
"table",
|
||||||
|
"Events",
|
||||||
"VoxelArea",
|
"VoxelArea",
|
||||||
"VoxelManip",
|
"VoxelManip",
|
||||||
"vector",
|
"vector",
|
||||||
"Ensure",
|
"Ensure",
|
||||||
"string",
|
"string",
|
||||||
"string_utf8"
|
"string_utf8",
|
||||||
|
"Profiler"
|
||||||
],
|
],
|
||||||
"globalsRegex": []
|
"globalsRegex": []
|
||||||
},
|
},
|
||||||
|
|||||||
52
.gitea/issue_template/bug.yaml
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
name: Сообщение об ошибке
|
||||||
|
about: Сообщить о недоработке игры, которую необходимо исправить.
|
||||||
|
title: "[Bug]: "
|
||||||
|
labels:
|
||||||
|
- bug/need confirmed
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
id: bug-desc
|
||||||
|
attributes:
|
||||||
|
label: Описание ошибки
|
||||||
|
description: Опишите, что произошло, в чем по вашему мнению заключается ошибка и какое поведение игры вы ожидали.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: playback-order
|
||||||
|
attributes:
|
||||||
|
label: Как воспроизвести баг?
|
||||||
|
description: Опишите порядок действий, как воспроизвести описываемую ошибку.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: dropdown
|
||||||
|
id: game-version
|
||||||
|
attributes:
|
||||||
|
label: Версия игры
|
||||||
|
description: Сообщите на каких версиях клиента игры была замечена ошибка.
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- "5.15"
|
||||||
|
- "5.14"
|
||||||
|
- "5.13"
|
||||||
|
- "5.12"
|
||||||
|
- "5.11"
|
||||||
|
- "5.10"
|
||||||
|
- "< 5.10"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: bug-server
|
||||||
|
attributes:
|
||||||
|
label: Где была замечена ошибка?
|
||||||
|
description: Сообщите, где была замечена ошибка.
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- Игровой сервер
|
||||||
|
- Локальный мир
|
||||||
|
- Тестовый сервер
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Спасибо, что уделили время и заполнили этот отчет об ошибке!
|
||||||
19
.gitea/issue_template/idea.yaml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
name: Предложение
|
||||||
|
about: Описать предложение для рассмотрения мейнтейнерами проекта.
|
||||||
|
labels:
|
||||||
|
- idea/need a maintainer
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
id: task-desc
|
||||||
|
attributes:
|
||||||
|
label: Описание
|
||||||
|
description: Подробно опишите в чем заключается ваша идея, а так же объясните необходимость её реализации.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: task-additions
|
||||||
|
attributes:
|
||||||
|
label: Дополнения
|
||||||
|
description: Приложите дополнительные сведенья, файлы или изображения.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
19
.gitea/issue_template/task.yaml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
name: Задача
|
||||||
|
about: Сформировать и описать задачу, которую должен будет выполнить разработчик. Этот шаблон предназначен для мейнтейнеров проекта.
|
||||||
|
labels:
|
||||||
|
- task/need developer
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
id: task-desc
|
||||||
|
attributes:
|
||||||
|
label: Описание задачи
|
||||||
|
description: Подробно опишите в чем заключается задача для разработчика.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: task-additions
|
||||||
|
attributes:
|
||||||
|
label: Дополнения
|
||||||
|
description: Приложите дополнительные сведенья, файлы или изображения необходимые для выполнения задачи.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
35
.gitea/pull_request_template.yml
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
name: PR-template
|
||||||
|
about: PR-template
|
||||||
|
labels:
|
||||||
|
- pr/wait review
|
||||||
|
- review/need a maintainer
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
id: pr-desc
|
||||||
|
attributes:
|
||||||
|
label: Описание изменений
|
||||||
|
description: Опишите, что было изменено в запросе на слияние и по какой причине.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: testing
|
||||||
|
attributes:
|
||||||
|
label: Тестирование
|
||||||
|
description: Опишите, в наличии каких объектов или в работоспособности каких систем необходимо убедиться.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: checkboxes
|
||||||
|
id: terms
|
||||||
|
attributes:
|
||||||
|
label: Требования
|
||||||
|
options:
|
||||||
|
- label: Я ознакомлен с соглашением по разработке и мои изменения следуют принятым стандартам.
|
||||||
|
required: true
|
||||||
|
- label: Я запускал игру и убедился в том, что она работает.
|
||||||
|
required: false
|
||||||
|
- label: Я указал в описании запроса метки (label) с префиксом `Edit:` в соответствии с теми изменениями, которые сделал.
|
||||||
|
required: false
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Спасибо, что прислали это запрос на слияние и помогли нам в разработке игры!
|
||||||
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
|
IGNORE
|
||||||
/mods/IGNORE
|
/mods/IGNORE
|
||||||
7
.gitmodules
vendored
@@ -1,7 +1,6 @@
|
|||||||
[submodule "mods/API/Utf8"]
|
|
||||||
path = mods/API/Utf8
|
|
||||||
url = https://github.com/ApancoreStudio/utf8_api.git
|
|
||||||
branch = main
|
|
||||||
[submodule "util/ide-helper"]
|
[submodule "util/ide-helper"]
|
||||||
path = util/ide-helper
|
path = util/ide-helper
|
||||||
url = https://github.com/Voxrame/luanti-ide-helper.git
|
url = https://github.com/Voxrame/luanti-ide-helper.git
|
||||||
|
[submodule "mods/SYSTEM/Utf8"]
|
||||||
|
path = mods/SYSTEM/Utf8
|
||||||
|
url = https://git.apancore.ru/Apancore.Studio/utf8_api.git
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
Source code licensing:
|
# Source code licensing
|
||||||
|
|
||||||
The MIT License (MIT)
|
**The MIT License (MIT)**
|
||||||
Copyright (c) 2025 Apancore.Studio, Doloment
|
|
||||||
|
Copyright (c) 2025-2026 Apancore.Studio, Van (varttist), Doloment
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
@@ -11,9 +12,13 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|||||||
|
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
Sprites licensing:
|
# Sprites licensing
|
||||||
|
|
||||||
CC BY 4.0
|
**CC BY 4.0**
|
||||||
Copyright (c) 2025 Apancore.Studio, Doloment and others (see list below)
|
|
||||||
|
Copyright (c) 2025-2026 Apancore.Studio, Van (varttist) and others (see list below)
|
||||||
|
|
||||||
|
All textures except those listed here are copyrighted by Van (varttist) and are distributed under the CC BY 4.0.
|
||||||
|
|
||||||
List of all textures and authors:
|
List of all textures and authors:
|
||||||
|
- ...
|
||||||
13
README.md
@@ -1,3 +1,12 @@
|
|||||||
Simple Makes Complex is a Luanti engine-based game focused on the multiplayer gameplay
|

|
||||||
|
|
||||||

|
# Simple Makes Complex
|
||||||
|
Simple Makes Complex is a Luanti engine-based game focused on the multiplayer gameplay.
|
||||||
|
|
||||||
|
# Licensing
|
||||||
|
- The project's core code is licensed under [MIT](/LICENSE).
|
||||||
|
- Submodules may contain their own license.
|
||||||
|
- Media assets are used and distributed under [CC-BY-4.0](https://creativecommons.org/licenses/by/4.0/).
|
||||||
|
|
||||||
|
# Assets used
|
||||||
|
- Font used in the `/menu/last_header.png`: https://fontstruct.com/fontstructions/show/2722233/gbjve (danil_2461, [CC0](http://creativecommons.org/publicdomain/zero/1.0/))
|
||||||
|
|||||||
@@ -1 +1,4 @@
|
|||||||
title = SMC
|
title = SMC
|
||||||
|
|
||||||
|
allowed_mapgens = singlenode
|
||||||
|
default_mapgen = singlenode
|
||||||
BIN
menu/background.png
Normal file
|
After Width: | Height: | Size: 7.3 MiB |
BIN
menu/header.png
Normal file
|
After Width: | Height: | Size: 3.2 KiB |
BIN
menu/header_scale.png
Normal file
|
After Width: | Height: | Size: 770 KiB |
BIN
menu/icon.png
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 1.2 KiB |
BIN
menu/last_header.png
Normal file
|
After Width: | Height: | Size: 1.1 MiB |
@@ -1,91 +0,0 @@
|
|||||||
Api = {}
|
|
||||||
|
|
||||||
---@alias Domain table<string,Mod|SubDomain> Global table for accessing public classes provided by mods.
|
|
||||||
---@alias SubDomain table<string,Mod>
|
|
||||||
|
|
||||||
---@type Domain
|
|
||||||
Core = {}
|
|
||||||
|
|
||||||
---@type Domain
|
|
||||||
Environment = {}
|
|
||||||
|
|
||||||
---@type Domain
|
|
||||||
Game = {}
|
|
||||||
|
|
||||||
---@type SubDomain
|
|
||||||
Game.Items = {}
|
|
||||||
|
|
||||||
---@type SubDomain
|
|
||||||
Game.Mobs = {}
|
|
||||||
|
|
||||||
---@type SubDomain
|
|
||||||
Game.Structures = {}
|
|
||||||
Ui = {}
|
|
||||||
|
|
||||||
local allowedDomains = {
|
|
||||||
Api = true,
|
|
||||||
Core = true,
|
|
||||||
Environment = true,
|
|
||||||
Game = true,
|
|
||||||
Ui = true
|
|
||||||
}
|
|
||||||
|
|
||||||
--- Adds a mod to the global space under specified domain structure
|
|
||||||
---@param mod Mod The mod table containing functionality to expose
|
|
||||||
---@param location string Dot-separated path within domain structure (e.g. "Items.Log")
|
|
||||||
---@param force boolean? Whether to overwrite the target location.
|
|
||||||
Api.addModToGlobalSpace = function(mod, location, force)
|
|
||||||
-- Ensure `mod` type and that it's not empty
|
|
||||||
Ensure.argType(mod, 'table', 1, 'Api.addModToGlobalSpace')
|
|
||||||
|
|
||||||
-- Ensure `location` type and that it's not empty
|
|
||||||
Ensure.argType(location, 'string', 2, 'Api.addModToGlobalSpace')
|
|
||||||
Ensure.stringArgNotEmpty(location, 2, 'Api.addModToGlobalSpace')
|
|
||||||
|
|
||||||
-- Ensure `location` matches this format: 'field.subfield.subsubfield.<...>'
|
|
||||||
-- TODO: баг: ликвидные имена не определяются шаблоном
|
|
||||||
--[[if not location:match('^[%a_][%w_]*([%.][%a_][%w_]*)*$') then
|
|
||||||
error('bad argument #2 to \'Api.addModToGlobalSpace\' (invalid format)')
|
|
||||||
end--]]
|
|
||||||
|
|
||||||
-- Split `location` into fields -- TODO: !!!Make a String function!!!
|
|
||||||
local fields = {}
|
|
||||||
local domain = nil
|
|
||||||
for field in string.gmatch(location, '[^.]+') do
|
|
||||||
if domain ~= nil then
|
|
||||||
table.insert(fields, field)
|
|
||||||
else
|
|
||||||
domain = field
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Ensure the domain is allowed
|
|
||||||
Ensure.argIsAllowed(domain, allowedDomains , 2, 'Api.addModToGlobalSpace')
|
|
||||||
|
|
||||||
-- Handle case where we're modifying the domain root directly
|
|
||||||
if #fields == 0 then
|
|
||||||
error('Not allowed to directly write into the \''.. domain ..'\' domain')
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Start traversing from the domain's table
|
|
||||||
local current = _G[domain]
|
|
||||||
assert(type(current) == 'table', 'The global domain is not a table')
|
|
||||||
|
|
||||||
-- Traverse each field of the location
|
|
||||||
local num_fields = #fields
|
|
||||||
for i = 1, num_fields - 1 do
|
|
||||||
local field = fields[i]
|
|
||||||
if current[field] == nil then
|
|
||||||
current[field] = {}
|
|
||||||
end
|
|
||||||
current = current[field]
|
|
||||||
assert(type(current) == 'table', 'intermediate field is not a table')
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Assign mod to the final field
|
|
||||||
local last_field = fields[num_fields]
|
|
||||||
if force and current[last_field] ~= nil then
|
|
||||||
error(string.format('field \'%s\' already exists in \'%s\'', last_field, location))
|
|
||||||
end
|
|
||||||
current[last_field] = mod
|
|
||||||
end
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
name = smc__api__api_loader
|
|
||||||
description = Include this mod in your `depends` to ensure all necessary APIs are loaded for your mod
|
|
||||||
depends = smc__api__ensure, smc__api__math, smc__api__mod, smc__api__require, smc__api__table, smc__api__string, smc__api__utf8,
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
name = smc__api__ensure
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
name = smc__api__logger
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
local math = math
|
|
||||||
|
|
||||||
math.lerp = function (a, b, t)
|
|
||||||
return a + (b - a) * t
|
|
||||||
end
|
|
||||||
|
|
||||||
math.clamp = function(value, min, max)
|
|
||||||
return math.min(math.max(value, min), max)
|
|
||||||
end
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
name = smc__api__math
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
name = smc__api__mod
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
name = smc__api__require
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
name = smc__api__string
|
|
||||||
depends = smc__api__ensure
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
name = smc__api__table
|
|
||||||
depends = smc__api__ensure
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
name = smc__api
|
|
||||||
4
mods/CORE/BaseProcessingStation/init.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__base_processing_station')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
return require('BaseProcessingStation')
|
||||||
2
mods/CORE/BaseProcessingStation/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
name = smc__core__base_processing_station
|
||||||
|
depends = smc__core__craft_system, smc__core__node
|
||||||
100
mods/CORE/BaseProcessingStation/src/BaseProcessingStation.lua
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
local TICKRATE = 1
|
||||||
|
|
||||||
|
local assert = assert
|
||||||
|
local coreGetMeta, coreGetTimer, coreSwapNode = core.get_meta, core.get_node_timer, core.swap_node
|
||||||
|
|
||||||
|
local CraftSystem = Smc.requireCraftSystem()
|
||||||
|
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||||
|
|
||||||
|
---An abstract class representing nodes that implement crafting of the `processing` type.
|
||||||
|
---@class BaseProcessingStation : Class, NodeSuperClass
|
||||||
|
---@field craftType CraftSystem.ProcessingCraftType
|
||||||
|
---@field handler CraftSystem.ProcessingCraftHandler
|
||||||
|
---@field tab PlayerInventoryUI.getTabFunc
|
||||||
|
---@field nodeName string?
|
||||||
|
---@field activeNodeName string?
|
||||||
|
local BaseProcessingStation = Class:extend({})
|
||||||
|
|
||||||
|
---Definition table for the `BaseProcessingStation`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class BaseProcessingStationDef
|
||||||
|
---@field craftType CraftSystem.ProcessingCraftType
|
||||||
|
---@field tab PlayerInventoryUI.getTabFunc
|
||||||
|
|
||||||
|
---@alias BaseProcessingStation.NodesDefinition {settings:Node.Settings, activeSettings:Node.Settings?, callbacks:Node.Callbacks?}
|
||||||
|
|
||||||
|
---@param def BaseProcessingStationDef
|
||||||
|
---@param nodesDef BaseProcessingStation.NodesDefinition?
|
||||||
|
function BaseProcessingStation:new(def, nodesDef)
|
||||||
|
local handler = CraftSystem:getProcessingCraftHandler(def.craftType)
|
||||||
|
|
||||||
|
---@type BaseProcessingStation
|
||||||
|
local instance = self:getInstance({})
|
||||||
|
instance.craftType = def.craftType
|
||||||
|
instance.handler = handler
|
||||||
|
instance.tab = def.tab
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
---A wrapper around `core.get_meta()` for convenience
|
||||||
|
---and returning the correct `MetaDataRef` type.
|
||||||
|
---@static
|
||||||
|
---@param pos MapVector
|
||||||
|
---@return MetaDataRef
|
||||||
|
function BaseProcessingStation:getMeta(pos)
|
||||||
|
return coreGetMeta(pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Opens the player's inventory, adding the node tab described in `self.tab`.
|
||||||
|
---@param pos MapVector
|
||||||
|
---@param player Player
|
||||||
|
function BaseProcessingStation:showForm(pos, player)
|
||||||
|
local playerName = player:get_player_name()
|
||||||
|
|
||||||
|
PlayerInventoryUI:addActiveNodeForm(pos, playerName)
|
||||||
|
PlayerInventoryUI:setContex(playerName, 'left_tab_name', 'node_tab')
|
||||||
|
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', pos)
|
||||||
|
PlayerInventoryUI:setContex(playerName, 'node_tab', self.tab)
|
||||||
|
|
||||||
|
PlayerInventoryUI:showForm(playerName)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Starts the station timer only if it is not running.
|
||||||
|
---
|
||||||
|
---Returns `true` if the timer has been started.
|
||||||
|
---@static
|
||||||
|
---@param pos MapVector
|
||||||
|
---@param timeout number? If is `nil`, the global `TICKRATE` setting will be used.
|
||||||
|
---@return boolean
|
||||||
|
function BaseProcessingStation:startTimer(pos, timeout)
|
||||||
|
local timer = coreGetTimer(pos)
|
||||||
|
|
||||||
|
if not timer:is_started() then
|
||||||
|
timer:start(timeout or TICKRATE)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
---Replaces the node with active or inactive depending on the status of the process.
|
||||||
|
---
|
||||||
|
---If an active node is not specified for a station,
|
||||||
|
---then no replacement will occur regardless of the status of the process.
|
||||||
|
---@param isProcessing boolean The stations process status. If `true`, the process is running.
|
||||||
|
---@param pos MapVector
|
||||||
|
function BaseProcessingStation:swapNode(isProcessing, pos)
|
||||||
|
assert(self.nodeName ~= nil)
|
||||||
|
|
||||||
|
if self.activeNodeName ~= nil then
|
||||||
|
if isProcessing then
|
||||||
|
coreSwapNode(pos, {name = self.activeNodeName})
|
||||||
|
else
|
||||||
|
coreSwapNode(pos, {name = self.nodeName})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return BaseProcessingStation
|
||||||
4
mods/CORE/BaseUI/init.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__base_ui')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
return require('BaseUI')
|
||||||
2
mods/CORE/BaseUI/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
name = smc__core__base_ui
|
||||||
|
depends = smc__system__system_loader
|
||||||
135
mods/CORE/BaseUI/src/BaseUI.lua
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
---@class BaseUI
|
||||||
|
---Context for transferring data between displaying
|
||||||
|
---the form spec to the player and handling callbacks.
|
||||||
|
---@field name string
|
||||||
|
---@field getFormSpecString fun(self:BaseUI,playerName:string,...):string
|
||||||
|
---@field contex table<string, table>
|
||||||
|
local BaseUI = {
|
||||||
|
contex = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
---@param name string The name of the formspec that will be shown to the player. It is recommended that the name be unique.
|
||||||
|
---@param getFormSpecString fun(self:BaseUI,playerName:string,...):string
|
||||||
|
---@param clearContex boolean? Whether to clear the context stored for a player when that player closes the form. Default `true`.
|
||||||
|
function BaseUI:new(name, getFormSpecString, clearContex)
|
||||||
|
if clearContex == nil then
|
||||||
|
clearContex = true
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type BaseUI
|
||||||
|
local instance = getInstance(self, {})
|
||||||
|
instance.name = name
|
||||||
|
instance.getFormSpecString = getFormSpecString
|
||||||
|
|
||||||
|
if clearContex then
|
||||||
|
-- Ensure that when the form is closed, the context is cleared.
|
||||||
|
instance:registerOnFormQuit(function(player)
|
||||||
|
instance:clearContex(player)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
instance:registerOnFormQuit(function(player)
|
||||||
|
Smc.clearActiveFormspec(player:get_player_name())
|
||||||
|
end)
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
---Set a value for a specific key.
|
||||||
|
---Recommended for use where `core.show_formspec` is used.
|
||||||
|
---
|
||||||
|
---In the future, the key value can be obtained in the formspec callback.
|
||||||
|
---@param player string|Player Player as an object (entity) or his name
|
||||||
|
---@param key string
|
||||||
|
---@param value any
|
||||||
|
function BaseUI:setContex(player, key, value)
|
||||||
|
if type(player) ~= 'string' then
|
||||||
|
player = player:get_player_name()
|
||||||
|
end
|
||||||
|
|
||||||
|
local contex = self.contex[player]
|
||||||
|
|
||||||
|
if contex == nil then
|
||||||
|
self.contex[player] = {[key]=value}
|
||||||
|
else
|
||||||
|
contex[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Get the value of a specific key.
|
||||||
|
---Recommended for use in a formspec callback.
|
||||||
|
---@param player string|Player Player as an object (entity) or his name
|
||||||
|
---@param key string
|
||||||
|
---@return any?
|
||||||
|
function BaseUI:getContex(player, key)
|
||||||
|
if type(player) ~= 'string' then
|
||||||
|
player = player:get_player_name()
|
||||||
|
end
|
||||||
|
|
||||||
|
local contex = self.contex[player] or {}
|
||||||
|
return contex[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
---Clear saved context data for a player.
|
||||||
|
---Recommended for use when closing formspec.
|
||||||
|
---@param player string|Player Player as an object (entity) or his name
|
||||||
|
function BaseUI:clearContex(player)
|
||||||
|
if type(player) ~= 'string' then
|
||||||
|
player = player:get_player_name()
|
||||||
|
end
|
||||||
|
|
||||||
|
self.contex[player] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
---Register a callback for a specific player action in FormSpec.
|
||||||
|
---
|
||||||
|
---The callback is guaranteed to be called only for the specified field.
|
||||||
|
---@param fieldName string the name of the field for which the callback will be called.
|
||||||
|
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
|
||||||
|
--- * `player` - the player who performed the action.
|
||||||
|
--- * `fieldName` - the name of the field on which the callback is called.
|
||||||
|
--- * `fieldValue` - the field value that is passed in the `fields` table.
|
||||||
|
function BaseUI:registerOnFormFieldAction(fieldName, callback)
|
||||||
|
Smc.registerOnFormFieldAction(self.name, fieldName, callback)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Register a callback function to close the FormSpec.
|
||||||
|
---
|
||||||
|
---The callback is guaranteed to be called only for the specified form.
|
||||||
|
---@param callback fun(player:Player) Callback to handle the close. Parameters:
|
||||||
|
--- * `player` - the player who closed the form.
|
||||||
|
function BaseUI:registerOnFormQuit(callback)
|
||||||
|
Smc.registerOnFormQuit(self.name, callback)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Register a callback for a specific player action in FormSpec.
|
||||||
|
---
|
||||||
|
---The callback function is guaranteed to be called only for the specified form and
|
||||||
|
---for the field whose name matches the pattern.
|
||||||
|
---@param fieldNamePattern string the name pattern of the field for which the callback will be called.
|
||||||
|
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
|
||||||
|
--- * `player` - the player who performed the action.
|
||||||
|
--- * `fieldName` - the name of the field on which the callback is called (will match the pattern).
|
||||||
|
--- * `fieldValue` - the field value that is passed in the `fields` table.
|
||||||
|
function BaseUI:registerOnFormFieldActionMatch( fieldNamePattern, callback)
|
||||||
|
Smc.registerOnFormFieldActionMatch(self.name, fieldNamePattern, callback)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Shows the player the FormSpec registered by the instance of this UI.
|
||||||
|
---@param playerName string
|
||||||
|
function BaseUI:showForm(playerName, ...)
|
||||||
|
core.show_formspec(playerName, self.name, self:getFormSpecString(playerName, ...))
|
||||||
|
Smc.setActiveFormspec(playerName, self.name)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Close the player the FormSpec registered by the instance of this UI.
|
||||||
|
---@param playerName string
|
||||||
|
function BaseUI:closeForm(playerName)
|
||||||
|
core.close_formspec(playerName, self.name)
|
||||||
|
Smc.clearActiveFormspec(playerName)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
return BaseUI
|
||||||
4
mods/CORE/Control/init.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__control')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
return require('Control')
|
||||||
2
mods/CORE/Control/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
name = smc__core__control
|
||||||
|
depends = smc__system__system_loader
|
||||||
136
mods/CORE/Control/src/Control.lua
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
---@class Events.Control
|
||||||
|
local Control = {}
|
||||||
|
|
||||||
|
-- control.on_press
|
||||||
|
|
||||||
|
---@param callbackFunc fun(player:Player, controlName:string)
|
||||||
|
function Control.subcribeOnPress(callbackFunc)
|
||||||
|
Events:subscribe('control.on_press', callbackFunc)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- control.on_release
|
||||||
|
|
||||||
|
---@param callbackFunc fun(player:Player, controlName:string, holdTime:number)
|
||||||
|
function Control.subcribeOnRelease(callbackFunc)
|
||||||
|
Events:subscribe('control.on_release', callbackFunc)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- control.on_hold
|
||||||
|
|
||||||
|
---@param callbackFunc fun(player:Player, controlName:string, holdTime:number, dtime:number)
|
||||||
|
function Control.subcribeOnHold(callbackFunc)
|
||||||
|
Events:subscribe('control.on_hold', callbackFunc)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- control.on_wield_change
|
||||||
|
|
||||||
|
---@param callbackFunc fun(player:Player, player_wield_index:number, player_last_wield_index:number)
|
||||||
|
function Control.subcribeOnWieldChange(callbackFunc)
|
||||||
|
Events:subscribe('control.on_wield_change', callbackFunc)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- --- Call events ---
|
||||||
|
|
||||||
|
local ControlPressKeys = {
|
||||||
|
'up', 'down', 'left', 'right', 'jump', 'aux1', 'sneak', 'dig', 'place', 'zoom'
|
||||||
|
}
|
||||||
|
|
||||||
|
local ControlMovementKeys = {
|
||||||
|
'movement_x', 'movement_y'
|
||||||
|
}
|
||||||
|
|
||||||
|
local playersLastControl = {}
|
||||||
|
local playersControlTimer = {}
|
||||||
|
|
||||||
|
core.register_on_joinplayer(function(player, last_login)
|
||||||
|
local playerName = player:get_player_name()
|
||||||
|
if playerName == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local nowControl = player:get_player_control()
|
||||||
|
if nowControl == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local controlTimer = {}
|
||||||
|
for _, key in ipairs(ControlPressKeys) do
|
||||||
|
controlTimer[key] = 0.0
|
||||||
|
end
|
||||||
|
|
||||||
|
playersLastControl[playerName] = nowControl
|
||||||
|
playersControlTimer[playerName] = controlTimer
|
||||||
|
end)
|
||||||
|
|
||||||
|
core.register_on_leaveplayer(function(player, timed_out)
|
||||||
|
local playerName = player:get_player_name()
|
||||||
|
if playerName == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
playersLastControl[playerName] = nil
|
||||||
|
playersControlTimer[playerName] = nil
|
||||||
|
end)
|
||||||
|
|
||||||
|
Smc.registerGlobalStepForEachPlayer(0, function(deltaTime, player)
|
||||||
|
if player == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local playerName = player:get_player_name()
|
||||||
|
if playerName == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local lastControl = playersLastControl[playerName]
|
||||||
|
if lastControl == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local controlTimer = playersControlTimer[playerName]
|
||||||
|
if controlTimer == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local nowControl = player:get_player_control()
|
||||||
|
if nowControl == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, key in ipairs(ControlPressKeys) do
|
||||||
|
local lastPressed = lastControl[key]
|
||||||
|
local nowPressed = nowControl[key]
|
||||||
|
|
||||||
|
-- on_press
|
||||||
|
if not lastPressed and nowPressed then
|
||||||
|
Events:call('control.on_press', key)
|
||||||
|
|
||||||
|
controlTimer[key] = 0
|
||||||
|
|
||||||
|
-- on_release
|
||||||
|
elseif lastPressed and not nowPressed then
|
||||||
|
Events:call('control.on_release', key, controlTimer[key])
|
||||||
|
|
||||||
|
controlTimer[key] = 0
|
||||||
|
|
||||||
|
-- on_hold
|
||||||
|
elseif lastPressed and nowPressed then
|
||||||
|
Events:call('control.on_hold', key, controlTimer[key], deltaTime)
|
||||||
|
|
||||||
|
controlTimer[key] = controlTimer[key] + deltaTime
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--TODO: add player movement callbacks
|
||||||
|
|
||||||
|
--TODO: add mouse movement callbacks on dir
|
||||||
|
|
||||||
|
playersLastControl[playerName] = nowControl
|
||||||
|
playersControlTimer[playerName] = controlTimer
|
||||||
|
end)
|
||||||
|
|
||||||
|
return Control
|
||||||
4
mods/CORE/CraftSystem/init.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
return require('CraftSystem')
|
||||||
2
mods/CORE/CraftSystem/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
name = smc__core__craft_system
|
||||||
|
depends = smc__system__system_loader, smc__core__creature
|
||||||
234
mods/CORE/CraftSystem/src/CraftSystem.lua
Normal file
@@ -0,0 +1,234 @@
|
|||||||
|
local tableJoin = table.join
|
||||||
|
|
||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type CraftSystem.PrimitiveCraftHandler
|
||||||
|
local PrimitiveCraftHandler = require('CraftSystem.PrimitiveCraftHandler')
|
||||||
|
---@type CraftSystem.ImmersiveCraftHandler
|
||||||
|
local ImmersiveCraftHandler= require('CraftSystem.ImmersiveCraftHandler')
|
||||||
|
---@type CraftSystem.ProcessingCraftHandler
|
||||||
|
local ProcessingCraftHandler= require('CraftSystem.ProcessingCraftHandler')
|
||||||
|
|
||||||
|
---@alias CraftSystem.CraftGroup string
|
||||||
|
---@alias CraftSystem.CraftRecipe ItemStackString[]
|
||||||
|
|
||||||
|
---@class CraftSystem
|
||||||
|
---@field PrimitiveCraftHandlers table<string, CraftSystem.PrimitiveCraftHandler>
|
||||||
|
---@field ImmersiveCraftHandlers table<string, CraftSystem.ImmersiveCraftHandler>
|
||||||
|
---@field ProcessingCraftHandlers table<string, CraftSystem.ProcessingCraftHandler>
|
||||||
|
local CraftSystem = {
|
||||||
|
PrimitiveCraftHandlers = {},
|
||||||
|
ImmersiveCraftHandlers = {},
|
||||||
|
ProcessingCraftHandlers = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--- Craft Types ---
|
||||||
|
|
||||||
|
---@alias CraftSystem.BaseCraftType 'primitive'|'immersive'|'processing'
|
||||||
|
---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory'
|
||||||
|
---@alias CraftSystem.ImmersiveCraftType 'workbench' -- DEBUG
|
||||||
|
---@alias CraftSystem.ProcessingCraftType 'test' -- DEBUG
|
||||||
|
---@alias CraftSystem.CraftType CraftSystem.PrimitiveCraftType|CraftSystem.ImmersiveCraftType|CraftSystem.ProcessingCraftType
|
||||||
|
|
||||||
|
---@param type CraftSystem.PrimitiveCraftType
|
||||||
|
---@param def CraftSystem.PrimitiveCraftHandlerDef
|
||||||
|
local function registerPrimitiveCraftType(type, def)
|
||||||
|
assert(CraftSystem.PrimitiveCraftHandlers[type] == nil, ('A primitive craft type `%s` already exists.'):format(type))
|
||||||
|
|
||||||
|
local handler = PrimitiveCraftHandler:new('primitive', type, def)
|
||||||
|
CraftSystem.PrimitiveCraftHandlers[type] = handler
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param type CraftSystem.ImmersiveCraftType
|
||||||
|
---@param def CraftSystem.ImmersiveCraftHandlerDef
|
||||||
|
local function registerImmersiveCraftType(type, def)
|
||||||
|
assert(CraftSystem.ImmersiveCraftHandlers[type] == nil, ('A immersive craft type `%s` already exists.'):format(type))
|
||||||
|
|
||||||
|
local handler = ImmersiveCraftHandler:new('immersive', type, def)
|
||||||
|
CraftSystem.ImmersiveCraftHandlers[type] = handler
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param type CraftSystem.ProcessingCraftType
|
||||||
|
---@param def CraftSystem.ProcessingCraftHandlerDef
|
||||||
|
local function registerProcessingCraftType(type, def)
|
||||||
|
assert(CraftSystem.ProcessingCraftHandlers[type] == nil, ('A processing craft type `%s` already exists.'):format(type))
|
||||||
|
|
||||||
|
local handler = ProcessingCraftHandler:new('processing', type, def)
|
||||||
|
CraftSystem.ProcessingCraftHandlers[type] = handler
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param type CraftSystem.PrimitiveCraftType
|
||||||
|
---@return CraftSystem.PrimitiveCraftHandler
|
||||||
|
function CraftSystem:getPrimitiveCraftHandler(type)
|
||||||
|
local handler = self.PrimitiveCraftHandlers[type]
|
||||||
|
assert(handler ~= nil)
|
||||||
|
|
||||||
|
return handler
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param type CraftSystem.ImmersiveCraftType
|
||||||
|
---@return CraftSystem.ImmersiveCraftHandler
|
||||||
|
function CraftSystem:getImmersiveCraftHandler(type)
|
||||||
|
local handler = self.ImmersiveCraftHandlers[type]
|
||||||
|
assert(handler ~= nil)
|
||||||
|
|
||||||
|
return handler
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param type CraftSystem.ProcessingCraftType
|
||||||
|
---@return CraftSystem.ProcessingCraftHandler
|
||||||
|
function CraftSystem:getProcessingCraftHandler(type)
|
||||||
|
local handler = self.ProcessingCraftHandlers[type]
|
||||||
|
assert(handler ~= nil)
|
||||||
|
|
||||||
|
return handler
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Registration of craft types:
|
||||||
|
registerPrimitiveCraftType('inside_player_inventory', {})
|
||||||
|
-- Debug:
|
||||||
|
registerImmersiveCraftType('workbench', {})
|
||||||
|
registerProcessingCraftType('test', {})
|
||||||
|
|
||||||
|
|
||||||
|
--- Registration methods ---
|
||||||
|
|
||||||
|
---@param type CraftSystem.PrimitiveCraftType
|
||||||
|
---@param craftName string
|
||||||
|
---@param def CraftSystem.PrimitiveCraftDef
|
||||||
|
function CraftSystem:registerPrimitiveCraft(type, craftName, def)
|
||||||
|
local handler = self:getPrimitiveCraftHandler(type)
|
||||||
|
|
||||||
|
handler:registerCraft(craftName, def)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param type CraftSystem.ImmersiveCraftType
|
||||||
|
---@param craftName string
|
||||||
|
---@param def CraftSystem.ImmersiveCraftDef
|
||||||
|
function CraftSystem:registerImmersiveCraft(type, craftName, def)
|
||||||
|
local handler = self:getImmersiveCraftHandler(type)
|
||||||
|
|
||||||
|
handler:registerCraft(craftName, def)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param type CraftSystem.ProcessingCraftType
|
||||||
|
---@param craftName string
|
||||||
|
---@param def CraftSystem.ProcessingCraftDef
|
||||||
|
function CraftSystem:registerProcessingCraft(type, craftName, def)
|
||||||
|
local handler = self:getProcessingCraftHandler(type)
|
||||||
|
|
||||||
|
handler:registerCraft(craftName, def)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Methods for performing crafting ---
|
||||||
|
|
||||||
|
|
||||||
|
---@param craft CraftSystem.BaseCraft
|
||||||
|
---@param recipeIndex integer
|
||||||
|
---@param count integer
|
||||||
|
---@return ItemStack[] materialStacks A table containing the items required for crafting.
|
||||||
|
---@return table<string, integer> materialGroups A table containing the item groups required for crafting.
|
||||||
|
local function getMaterials(craft, recipeIndex, count)
|
||||||
|
local materialStacks = {}
|
||||||
|
local materialGroups = {}
|
||||||
|
|
||||||
|
for _, itemName in ipairs(craft:getRecipe(recipeIndex)) do
|
||||||
|
local group = itemName:match('group:(%S+)')
|
||||||
|
local materialCount = itemName:match('group:%S+ (%S+)') or 1
|
||||||
|
|
||||||
|
if group == nil then
|
||||||
|
local item = ItemStack(itemName)
|
||||||
|
item:set_count(item:get_count() * count)
|
||||||
|
|
||||||
|
table.insert(materialStacks, item)
|
||||||
|
else
|
||||||
|
materialGroups[group] = (tonumber(materialCount) or 1) * count
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return materialStacks, materialGroups
|
||||||
|
end
|
||||||
|
|
||||||
|
---Check if the player has the required items.
|
||||||
|
---@param materialStacks ItemStack[]
|
||||||
|
---@param inventory InvRef
|
||||||
|
---@return boolean
|
||||||
|
local function checkMaterialStacks(materialStacks, inventory)
|
||||||
|
for _, item in ipairs(materialStacks) do
|
||||||
|
if not inventory:contains_item('main', item) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
---Check whether the player has the required number of items with the required group for crafting.
|
||||||
|
---@param materialGroups table<string, integer>
|
||||||
|
---@param inventory InvRef
|
||||||
|
---@return boolean
|
||||||
|
---@return ItemStack[] materialStacks A list of item stacks that match the required material groups.
|
||||||
|
local function checkMaterialGroups(materialGroups, inventory)
|
||||||
|
local materialStacks = {}
|
||||||
|
|
||||||
|
local mainList = inventory:get_list('main')
|
||||||
|
for group, count in pairs(materialGroups) do
|
||||||
|
for _, item in ipairs(mainList) do
|
||||||
|
if core.get_item_group(item:get_name(), group) > 0 then
|
||||||
|
local itemCount = item:get_count()
|
||||||
|
if count > itemCount then
|
||||||
|
count = count - itemCount
|
||||||
|
table.insert(materialStacks, item)
|
||||||
|
else
|
||||||
|
item:set_count(count)
|
||||||
|
table.insert(materialStacks, item)
|
||||||
|
count = 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if count > 0 then
|
||||||
|
return false, {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true, materialStacks
|
||||||
|
end
|
||||||
|
|
||||||
|
---Checks whether the player can perform crafting (whether he has the required materials in his inventory).
|
||||||
|
---@param player Player
|
||||||
|
---@param craft CraftSystem.BaseCraft
|
||||||
|
---@param recipeIndex integer
|
||||||
|
---@param count integer
|
||||||
|
---@return boolean check true if craft can be produced
|
||||||
|
---@return ItemStack[] materialStacks list of itemStacks for crafting
|
||||||
|
function CraftSystem:checkPossiblePlayerInventoryCrafting(player, craft, recipeIndex, count, flags)
|
||||||
|
local inventory = player:get_inventory()
|
||||||
|
|
||||||
|
local OutputStack = ItemStack(craft.output)
|
||||||
|
OutputStack:set_count(OutputStack:get_count() * count)
|
||||||
|
|
||||||
|
local materialStacks, materialGroups = getMaterials(craft, recipeIndex, count)
|
||||||
|
|
||||||
|
local isHaveStacks = checkMaterialStacks(materialStacks, inventory)
|
||||||
|
if not isHaveStacks then
|
||||||
|
core.chat_send_player(player:get_player_name(), 'Not enough materials for crafting!')
|
||||||
|
return false, {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local isHaveGroups, materialGroupStacks = checkMaterialGroups(materialGroups, inventory)
|
||||||
|
if not isHaveGroups then
|
||||||
|
core.chat_send_player(player:get_player_name(), 'Not enough materials for crafting!')
|
||||||
|
return false, {}
|
||||||
|
end
|
||||||
|
|
||||||
|
materialStacks = tableJoin(materialStacks, materialGroupStacks)
|
||||||
|
|
||||||
|
|
||||||
|
return true, materialStacks
|
||||||
|
end
|
||||||
|
|
||||||
|
return CraftSystem
|
||||||
156
mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
---A text indicator of the presence or absence
|
||||||
|
---of a material in the player's inventory.
|
||||||
|
local CHECK = {YES = '[x]', NO = '[ ]', EMPTY = '-'}
|
||||||
|
|
||||||
|
---@class CraftSystem.BaseCraft : Class
|
||||||
|
---@field name string
|
||||||
|
---@field recipe ItemStackString[]
|
||||||
|
---@field altRecipes CraftSystem.CraftRecipe[]
|
||||||
|
---@field output ItemStackString
|
||||||
|
---@field group CraftSystem.CraftGroup
|
||||||
|
local BaseCraft = Class:extend({})
|
||||||
|
|
||||||
|
---Definition table for the `CraftSystem.BaseCraft`.
|
||||||
|
---@class CraftSystem.BaseCraftDef
|
||||||
|
---@field recipe ItemStackString[]
|
||||||
|
---@field altRecipes CraftSystem.CraftRecipe[]?
|
||||||
|
---@field output ItemStackString
|
||||||
|
---@field group CraftSystem.CraftGroup?
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param def CraftSystem.BaseCraftDef
|
||||||
|
---@return CraftSystem.BaseCraft
|
||||||
|
function BaseCraft:new(name, def)
|
||||||
|
---@type CraftSystem.BaseCraft
|
||||||
|
local instance = self:getInstance({})
|
||||||
|
instance.name = name
|
||||||
|
instance.recipe = def.recipe
|
||||||
|
instance.altRecipes = def.altRecipes or {}
|
||||||
|
instance.output = def.output
|
||||||
|
instance.group = def.group
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param material ItemStack
|
||||||
|
---@param count number
|
||||||
|
---@param list ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
local function getCheckMaterialItem(material, count, list)
|
||||||
|
if list == nil then
|
||||||
|
return CHECK.EMPTY
|
||||||
|
end
|
||||||
|
|
||||||
|
local materialName = material:get_name()
|
||||||
|
|
||||||
|
for _, item in ipairs(list) do
|
||||||
|
if materialName == item:get_name() then
|
||||||
|
local itemCount = item:get_count()
|
||||||
|
if count > itemCount then
|
||||||
|
count = count - itemCount
|
||||||
|
else
|
||||||
|
return CHECK.YES
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return CHECK.NO
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param material string
|
||||||
|
---@param count number
|
||||||
|
---@param list ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
local function getCheckMaterialGroup(material, count, list)
|
||||||
|
if list == nil then
|
||||||
|
return CHECK.EMPTY
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, item in ipairs(list) do
|
||||||
|
if core.get_item_group(item:get_name(), material) > 0 then
|
||||||
|
local itemCount = item:get_count()
|
||||||
|
if count > itemCount then
|
||||||
|
count = count - itemCount
|
||||||
|
else
|
||||||
|
return CHECK.YES
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return CHECK.NO
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns the crafting recipe corresponding to the index.
|
||||||
|
---@param index integer Recipe number. If `0` is specified,
|
||||||
|
--- the primary craft (`craft.recipe`) will be used;
|
||||||
|
--- if `1` or greater, the alternative craft
|
||||||
|
--- corresponding to the index will be used (`craft.altRecipes`).
|
||||||
|
---@return CraftSystem.CraftRecipe
|
||||||
|
function BaseCraft:getRecipe(index)
|
||||||
|
local recipe
|
||||||
|
if index == 0 or self.altRecipes == nil then
|
||||||
|
recipe = self.recipe
|
||||||
|
else
|
||||||
|
if index > #self.altRecipes then
|
||||||
|
recipe = self.altRecipes[#self.altRecipes]
|
||||||
|
else
|
||||||
|
recipe = self.altRecipes[index]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(recipe ~= nil)
|
||||||
|
|
||||||
|
return recipe
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param recipeIndex integer?
|
||||||
|
---@param mainList ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
function BaseCraft:recipeToString(recipeIndex, mainList)
|
||||||
|
local recipe
|
||||||
|
if recipeIndex == nil or recipeIndex == 0 or self.altRecipes == nil then
|
||||||
|
recipe = self.recipe
|
||||||
|
else
|
||||||
|
if recipeIndex > #self.altRecipes then
|
||||||
|
recipe = self.altRecipes[#self.altRecipes]
|
||||||
|
else
|
||||||
|
recipe = self.altRecipes[recipeIndex]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(recipe ~= nil)
|
||||||
|
|
||||||
|
local recipeDesc = ''
|
||||||
|
for _, itemName in ipairs(recipe) do
|
||||||
|
local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1
|
||||||
|
|
||||||
|
if group == nil then
|
||||||
|
local recipeItem = ItemStack(itemName)
|
||||||
|
recipeDesc = recipeDesc..('%s %s x%s\n'):format(
|
||||||
|
getCheckMaterialItem(recipeItem, recipeItem:get_count(), mainList),
|
||||||
|
core.strip_colors(recipeItem:get_definition()._title),
|
||||||
|
recipeItem:get_count())
|
||||||
|
else
|
||||||
|
recipeDesc = recipeDesc..('%s any item from the group %s x%s\n'):format(
|
||||||
|
getCheckMaterialGroup(group, count, mainList),
|
||||||
|
group,
|
||||||
|
count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return recipeDesc
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param recipeIndex integer?
|
||||||
|
---@param mainList ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
function BaseCraft:toString(recipeIndex, mainList)
|
||||||
|
local outputItem = ItemStack(self.output)
|
||||||
|
local outputItemSettings = Smc.getItemDefinition(outputItem).settings
|
||||||
|
|
||||||
|
local craftDesc = ('%s\n\nNeeded:\n%s'):format(
|
||||||
|
outputItemSettings._title,
|
||||||
|
self:recipeToString(recipeIndex, mainList))
|
||||||
|
|
||||||
|
return craftDesc
|
||||||
|
end
|
||||||
|
|
||||||
|
return BaseCraft
|
||||||
125
mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
local tableInsert = table.insert
|
||||||
|
local stringMatch = string.match
|
||||||
|
|
||||||
|
local PLAYERS_KNOW_ALL_RECIPES = true
|
||||||
|
|
||||||
|
---Abstract class for handling crafting.
|
||||||
|
---@class CraftSystem.BaseCraftHandler : Class
|
||||||
|
---@field baseType CraftSystem.BaseCraftType
|
||||||
|
---@field type CraftSystem.CraftType
|
||||||
|
---@field defaultCraftDef CraftSystem.BaseCraftDef
|
||||||
|
---
|
||||||
|
---@field craftsByName table<string, CraftSystem.BaseCraft>
|
||||||
|
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.BaseCraft[]>
|
||||||
|
---@field craftsList CraftSystem.BaseCraft[]
|
||||||
|
---@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]
|
||||||
|
---@field learnedCrafts table<string, CraftSystem.BaseCraft[]>
|
||||||
|
---@field learnedGroupedCrafts table<string, (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]>
|
||||||
|
local BaseCraftHandler = Class:extend({
|
||||||
|
craftsByName = {},
|
||||||
|
craftsByGroup = {},
|
||||||
|
craftsList = {},
|
||||||
|
groupedCrafts = {},
|
||||||
|
learnedCrafts = {},
|
||||||
|
learnedGroupedCrafts = {},
|
||||||
|
})
|
||||||
|
|
||||||
|
---@class CraftSystem.BaseCraftHandlerDef
|
||||||
|
---@field defaultCraftDef CraftSystem.BaseCraftDef?
|
||||||
|
|
||||||
|
---@param baseType CraftSystem.BaseCraftType
|
||||||
|
---@param type CraftSystem.CraftType
|
||||||
|
---@param def CraftSystem.BaseCraftHandlerDef
|
||||||
|
---@return CraftSystem.BaseCraftHandler
|
||||||
|
function BaseCraftHandler:new(baseType, type, def)
|
||||||
|
---@type CraftSystem.BaseCraftHandler
|
||||||
|
local instance = self:getInstance({})
|
||||||
|
instance.baseType = baseType
|
||||||
|
instance.type = type
|
||||||
|
instance.defaultCraftDef = def.defaultCraftDef or {}
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param craftName string
|
||||||
|
---@return CraftSystem.BaseCraft
|
||||||
|
function BaseCraftHandler:getCraftByName(craftName)
|
||||||
|
local craft = self.craftsByName[craftName]
|
||||||
|
|
||||||
|
assert(craft ~= nil, ('A craft `%s` not exists.'):format(
|
||||||
|
craftName))
|
||||||
|
|
||||||
|
return craft
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param craftGroup CraftSystem.CraftGroup
|
||||||
|
---@return CraftSystem.BaseCraft[]
|
||||||
|
function BaseCraftHandler:getCraftsByGroup(craftGroup)
|
||||||
|
local crafts = self.craftsByGroup[craftGroup]
|
||||||
|
|
||||||
|
assert(crafts ~= nil, ('A craft with group `%s` not exists.'):format(
|
||||||
|
craftGroup))
|
||||||
|
|
||||||
|
return crafts
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param playerName string
|
||||||
|
---@return (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]
|
||||||
|
function BaseCraftHandler:getLearnedCrafts(playerName)
|
||||||
|
if PLAYERS_KNOW_ALL_RECIPES then
|
||||||
|
return self.groupedCrafts
|
||||||
|
end
|
||||||
|
|
||||||
|
local crafts = self.learnedGroupedCrafts[playerName]
|
||||||
|
|
||||||
|
if crafts == nil then
|
||||||
|
crafts = {}
|
||||||
|
self.learnedGroupedCrafts[playerName] = crafts
|
||||||
|
end
|
||||||
|
|
||||||
|
return crafts
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param crafts CraftSystem.BaseCraft[]
|
||||||
|
---@param pattern string
|
||||||
|
---@return CraftSystem.BaseCraft[]
|
||||||
|
local function getMathedCrafts(crafts, pattern)
|
||||||
|
local matchedCrafts = {}
|
||||||
|
|
||||||
|
for _, craft in ipairs(crafts) do
|
||||||
|
local outputItem = ItemStack(craft.output)
|
||||||
|
local itemSettings = Smc.getItemDefinition(outputItem).settings
|
||||||
|
local ruName = core.get_translated_string('ru', itemSettings._title)
|
||||||
|
local enName = core.get_translated_string('en', itemSettings._title)
|
||||||
|
|
||||||
|
if stringMatch(outputItem:get_name(), pattern) or
|
||||||
|
stringMatch(ruName, pattern) or
|
||||||
|
stringMatch(enName, pattern) then
|
||||||
|
tableInsert(matchedCrafts, craft)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return matchedCrafts
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param playerName string
|
||||||
|
---@param pattern string
|
||||||
|
---@return CraftSystem.BaseCraft[]
|
||||||
|
function BaseCraftHandler:getLearnedCraftsMatch(playerName, pattern)
|
||||||
|
if PLAYERS_KNOW_ALL_RECIPES then
|
||||||
|
return getMathedCrafts(self.craftsList, pattern)
|
||||||
|
end
|
||||||
|
|
||||||
|
local crafts = self.learnedCrafts[playerName]
|
||||||
|
|
||||||
|
if crafts == nil then
|
||||||
|
crafts = {}
|
||||||
|
self.learnedCrafts[playerName] = crafts
|
||||||
|
end
|
||||||
|
|
||||||
|
return crafts
|
||||||
|
end
|
||||||
|
|
||||||
|
--TODO: сохранять выученные крафты игроков в сторадже
|
||||||
|
|
||||||
|
return BaseCraftHandler
|
||||||
196
mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type CraftSystem.BaseCraft
|
||||||
|
local BaseCraft = require('CraftSystem.BaseCraft')
|
||||||
|
---@type CraftSystem.ImmersiveCraftCounter
|
||||||
|
local ImmersiveCraftCounter = require('CraftSystem.ImmersiveCraftCounter')
|
||||||
|
|
||||||
|
---Types of immersive stages.
|
||||||
|
---@class CraftSystem.StagesTypes : table<CraftSystem.ImmersiveCraftStage.Type, CraftSystem.ImmersiveCraftStage>
|
||||||
|
local stageTypes = {
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStage
|
||||||
|
tapping = require('CraftSystem.ImmersiveCraftStage.TappingStage'),
|
||||||
|
}
|
||||||
|
|
||||||
|
---Immersive crafting aimed at a more logical process of crafting items.
|
||||||
|
---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft
|
||||||
|
---@field startStageName string
|
||||||
|
---@field stagesByName table<string, CraftSystem.ImmersiveCraftStage>
|
||||||
|
local ImmersiveCraft = BaseCraft:extend({})
|
||||||
|
|
||||||
|
---Definition table for the `CraftSystem.ImmersiveCraft`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class CraftSystem.ImmersiveCraftDef : CraftSystem.BaseCraftDef
|
||||||
|
---@field startStageName string
|
||||||
|
---@field stages CraftSystem.ImmersiveCraftStageDef[]
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param def CraftSystem.ImmersiveCraftDef
|
||||||
|
---@return CraftSystem.ImmersiveCraft
|
||||||
|
function ImmersiveCraft:new(name, def)
|
||||||
|
---@type table<string, CraftSystem.ImmersiveCraftStage>
|
||||||
|
local stagesByName = {}
|
||||||
|
|
||||||
|
for _, stageDef in ipairs(def.stages) do
|
||||||
|
local StageClass = stageTypes[stageDef.type]
|
||||||
|
|
||||||
|
stageDef.craftName = name
|
||||||
|
local stage = StageClass:new(stageDef)
|
||||||
|
|
||||||
|
stagesByName[stageDef.name] = stage
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type CraftSystem.ImmersiveCraft
|
||||||
|
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||||
|
instance.startStageName = def.startStageName
|
||||||
|
instance.stagesByName = stagesByName
|
||||||
|
|
||||||
|
-- Subscribe to an event that will determine that the player has completed the crafting stage.
|
||||||
|
Events:subscribe('craft_system:next_immersive_stage:'..name,
|
||||||
|
---@param eventName string
|
||||||
|
---@param stage CraftSystem.ImmersiveCraftStage
|
||||||
|
---@param pos MapVector
|
||||||
|
---@param Player Player
|
||||||
|
function(eventName, stage, pos, Player)
|
||||||
|
local nextStageName = stage.nextStage
|
||||||
|
|
||||||
|
if nextStageName == nil then
|
||||||
|
instance:endCraft(pos, Player)
|
||||||
|
else
|
||||||
|
local nextStage = instance.stagesByName[nextStageName]
|
||||||
|
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
meta:set_string('stage_name', nextStage.name)
|
||||||
|
|
||||||
|
nextStage:startHandler(pos, Player)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
---Initialize the start of the immersive crafting process.
|
||||||
|
---
|
||||||
|
---Returns `true` if the crafting process was running.
|
||||||
|
---@param pos MapVector
|
||||||
|
---@param player Player
|
||||||
|
---@param amount integer
|
||||||
|
---@return boolean
|
||||||
|
function ImmersiveCraft:startCraft(pos, player, amount)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
local craftName = meta:get_string('craft_name')
|
||||||
|
if craftName ~= '' then
|
||||||
|
core.chat_send_player(player:get_player_name(), 'The workbench is occupied by another craft.')
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if not ImmersiveCraftCounter:addCraft(pos, player:get_player_name()) then
|
||||||
|
core.chat_send_player(player:get_player_name(),
|
||||||
|
'The limit of simultaneously active crafts has been reached: '..
|
||||||
|
'complete or cancel active crafts to start a new one')
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local stage = self.stagesByName[self.startStageName]
|
||||||
|
|
||||||
|
meta:set_string('player_name', player:get_player_name())
|
||||||
|
meta:set_string('craft_name', self.name)
|
||||||
|
meta:set_string('stage_name', stage.name)
|
||||||
|
meta:set_float('craft_amount', amount)
|
||||||
|
|
||||||
|
stage:startHandler(pos, player)
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
---Complete the immersive crafting process.
|
||||||
|
---@param pos MapVector
|
||||||
|
---@param player Player
|
||||||
|
function ImmersiveCraft:endCraft(pos, player)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
local playerName = meta:get_string('player_name')
|
||||||
|
assert(playerName ~= '')
|
||||||
|
local amount = meta:get_float('craft_amount')
|
||||||
|
assert(amount ~= 0)
|
||||||
|
|
||||||
|
-- Drop the result of crafting.
|
||||||
|
local dropPos = vector.new(pos) + vector.new(0, 0.9,0)
|
||||||
|
for i=1, amount do
|
||||||
|
core.item_drop(ItemStack(self.output), nil, dropPos)
|
||||||
|
end
|
||||||
|
|
||||||
|
ImmersiveCraftCounter:removeCraft(pos, playerName)
|
||||||
|
|
||||||
|
-- Clear craft data
|
||||||
|
meta:set_string('player_name', '')
|
||||||
|
meta:set_string('craft_name', '')
|
||||||
|
meta:set_string('stage_name', '')
|
||||||
|
meta:set_float('craft_amount', 0)
|
||||||
|
meta:set_string('stage_data', '')
|
||||||
|
end
|
||||||
|
|
||||||
|
---Stop the crafting process and free up the workbench.
|
||||||
|
---@param pos MapVector
|
||||||
|
function ImmersiveCraft:abortCraft(pos)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
local playerName = meta:get_string('player_name')
|
||||||
|
assert(playerName ~= '')
|
||||||
|
local stageName = meta:get_string('stage_name')
|
||||||
|
assert(stageName ~= '')
|
||||||
|
|
||||||
|
local stage = self.stagesByName[stageName]
|
||||||
|
stage:abortHandler(pos)
|
||||||
|
|
||||||
|
-- Clear craft data
|
||||||
|
meta:set_string('player_name', '')
|
||||||
|
meta:set_string('craft_name', '')
|
||||||
|
meta:set_string('stage_name', '')
|
||||||
|
meta:set_float('craft_amount', 0)
|
||||||
|
meta:set_string('stage_data', '')
|
||||||
|
|
||||||
|
ImmersiveCraftCounter:removeCraft(pos, playerName)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns a human-readable description of the crafting process.
|
||||||
|
---@return string
|
||||||
|
function ImmersiveCraft:recipeToString(recipeIndex, mainList)
|
||||||
|
local recipeDesc = ''
|
||||||
|
---@type string?
|
||||||
|
local stageName = self.startStageName
|
||||||
|
local stages = self.stagesByName
|
||||||
|
|
||||||
|
local count = 1
|
||||||
|
while stageName ~= nil do
|
||||||
|
local stage = stages[stageName]
|
||||||
|
|
||||||
|
recipeDesc = recipeDesc..tostring(count)..'. '..stage:toString()..'\n'
|
||||||
|
|
||||||
|
stageName = stage.nextStage
|
||||||
|
end
|
||||||
|
|
||||||
|
return recipeDesc
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param recipeIndex integer?
|
||||||
|
---@param mainList ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
function ImmersiveCraft:toString(recipeIndex, mainList)
|
||||||
|
local outputItem = ItemStack(self.output)
|
||||||
|
local outputItemSettings = Smc.getItemDefinition(outputItem).settings
|
||||||
|
|
||||||
|
local craftDesc = ('%s\n\nRecipe:\n%s\nActions:\n%s'):format(
|
||||||
|
outputItemSettings._title,
|
||||||
|
BaseCraft.recipeToString(self, recipeIndex, mainList),
|
||||||
|
self:recipeToString(recipeIndex, mainList))
|
||||||
|
|
||||||
|
return craftDesc
|
||||||
|
end
|
||||||
|
|
||||||
|
return ImmersiveCraft
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
local posToString, getSize = core.pos_to_string, table.get_size
|
||||||
|
|
||||||
|
local CRAFT_LIMIT_PER_PLAYER = 2
|
||||||
|
local CRAFT_LIMIT_PER_SERVER = 10
|
||||||
|
|
||||||
|
local modStorage = core.get_mod_storage()
|
||||||
|
|
||||||
|
---@param key string
|
||||||
|
---@return table
|
||||||
|
local function getStorageTable(key)
|
||||||
|
return core.deserialize(modStorage:get_string(key)) or {}
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param key string
|
||||||
|
---@param t table
|
||||||
|
local function setStorageTable(key, t)
|
||||||
|
modStorage:set_string(key, core.serialize(t))
|
||||||
|
end
|
||||||
|
|
||||||
|
---Designed to keep track of active immersive
|
||||||
|
---crafts so as not to overload the server.
|
||||||
|
---
|
||||||
|
---The counter is individual for each player,
|
||||||
|
---but the total number of active crafts is also tracked.
|
||||||
|
---@class CraftSystem.ImmersiveCraftCounter
|
||||||
|
---@field craftsCountForPlayers table<string, table<string, boolean>>
|
||||||
|
---@field craftsCountForServer table<string, boolean>
|
||||||
|
local ImmersiveCraftCounter = {
|
||||||
|
craftsCountForPlayers = getStorageTable('crafts_count_for_players'),
|
||||||
|
craftsCountForServer = getStorageTable('crafts_count_for_serves')
|
||||||
|
}
|
||||||
|
|
||||||
|
---Add craft to the counter.
|
||||||
|
---
|
||||||
|
---Returns `false` if the counter is already full.
|
||||||
|
---@param pos MapVector
|
||||||
|
---@param playerName string
|
||||||
|
---@return boolean
|
||||||
|
function ImmersiveCraftCounter:addCraft(pos, playerName)
|
||||||
|
if self.craftsCountForPlayers[playerName] == nil then
|
||||||
|
self.craftsCountForPlayers[playerName] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
if getSize(self.craftsCountForPlayers[playerName]) >= CRAFT_LIMIT_PER_PLAYER then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if getSize(self.craftsCountForServer) >= CRAFT_LIMIT_PER_SERVER then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local posString = posToString(pos)
|
||||||
|
self.craftsCountForPlayers[playerName][posString] = true
|
||||||
|
self.craftsCountForServer[posString] = true
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
---Remove craft from the counter.
|
||||||
|
---@param pos MapVector
|
||||||
|
---@param playerName string
|
||||||
|
function ImmersiveCraftCounter:removeCraft(pos, playerName)
|
||||||
|
local playerCrafts = self.craftsCountForPlayers[playerName]
|
||||||
|
if playerCrafts == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local posString = posToString(pos)
|
||||||
|
playerCrafts[posString] = nil
|
||||||
|
self.craftsCountForServer[posString] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
core.register_on_shutdown(function()
|
||||||
|
setStorageTable('crafts_count_for_players', ImmersiveCraftCounter.craftsCountForPlayers)
|
||||||
|
setStorageTable('crafts_count_for_server', ImmersiveCraftCounter.craftsCountForServer)
|
||||||
|
end)
|
||||||
|
|
||||||
|
return ImmersiveCraftCounter
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
local tableInsert, tableSort, tableJoin = table.insert, table.sort, table.join
|
||||||
|
|
||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type CraftSystem.BaseCraftHandler
|
||||||
|
local BaseCraftHandler = require('CraftSystem.BaseCraftHandler')
|
||||||
|
---@type CraftSystem.ImmersiveCraft
|
||||||
|
local ImmersiveCraft = require('CraftSystem.ImmersiveCraft')
|
||||||
|
|
||||||
|
---A handler for immersive crafting that requires the player to complete
|
||||||
|
---crafting stages and interact with entities (crafting nodes)
|
||||||
|
---@class CraftSystem.ImmersiveCraftHandler : CraftSystem.BaseCraftHandler
|
||||||
|
---@field defaultCraftDef CraftSystem.BaseCraftDef
|
||||||
|
---
|
||||||
|
---@field new fun(self:CraftSystem.ImmersiveCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.ImmersiveCraftType, def:CraftSystem.ImmersiveCraftHandlerDef):CraftSystem.ImmersiveCraftHandler
|
||||||
|
---@field getCraftByName fun(self, craftName:string):CraftSystem.ImmersiveCraft
|
||||||
|
---@field getCraftsByGroup fun(self, craftGroup):CraftSystem.ImmersiveCraft[]
|
||||||
|
---@field getLearnedCrafts fun(self, playerName):table<string, CraftSystem.ImmersiveCraft>
|
||||||
|
local ImmersiveCraftHandler = BaseCraftHandler:extend({})
|
||||||
|
|
||||||
|
---@class CraftSystem.ImmersiveCraftHandlerDef : CraftSystem.BaseCraftHandlerDef
|
||||||
|
---@field defaultCraftDef CraftSystem.ImmersiveCraftDef?
|
||||||
|
|
||||||
|
---Register an immersive crafting experience that requires the player to complete
|
||||||
|
---one or more crafting stages that involve interaction with entities (crafting nodes)
|
||||||
|
---@param craftName string
|
||||||
|
---@param def CraftSystem.ImmersiveCraftDef
|
||||||
|
function ImmersiveCraftHandler:registerCraft(craftName, def)
|
||||||
|
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
||||||
|
craftName))
|
||||||
|
|
||||||
|
local def = table.merge(def, self.defaultCraftDef)
|
||||||
|
|
||||||
|
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
||||||
|
def.group = group
|
||||||
|
|
||||||
|
local craft = (ImmersiveCraft:new(craftName, def))
|
||||||
|
|
||||||
|
if self.craftsByGroup[group] == nil then
|
||||||
|
self.craftsByGroup[group] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
self.craftsByName[craftName] = craft
|
||||||
|
tableInsert(self.craftsByGroup[group], craft)
|
||||||
|
tableInsert(self.craftsList, craft)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||||
|
-- Provides a convenient list of crafting items in the player's inventory.
|
||||||
|
core.register_on_mods_loaded(function()
|
||||||
|
for groupName, crafts in pairs(ImmersiveCraftHandler.craftsByGroup) do
|
||||||
|
tableSort(crafts, function(a, b)
|
||||||
|
if a.output < b.output then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
if groupName ~= 'craft_group:UNDEFINED' then
|
||||||
|
tableInsert(ImmersiveCraftHandler.groupedCrafts, crafts)
|
||||||
|
else
|
||||||
|
ImmersiveCraftHandler.groupedCrafts = tableJoin(ImmersiveCraftHandler.groupedCrafts, crafts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tableSort(ImmersiveCraftHandler.groupedCrafts, function(a, b)
|
||||||
|
local outputA = (a[1] and a[1].output) or a.output
|
||||||
|
local outputB = (b[1] and b[1].output) or b.output
|
||||||
|
|
||||||
|
if outputA < outputB then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
return ImmersiveCraftHandler
|
||||||
14
mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftNode.lua
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
---@type Creature
|
||||||
|
local Creature = Mod.getInfo('smc__core__creature').require('Creature')
|
||||||
|
|
||||||
|
---The entity the player interacts with during immersive crafting.
|
||||||
|
---@class CraftSystem.ImmersiveCraftNode : Creature
|
||||||
|
local ImmersiveCraftNode = Creature:getExtended({
|
||||||
|
settings = {
|
||||||
|
_name = 'craft_system:immersive_craft_node',
|
||||||
|
_add_mod_name = false,
|
||||||
|
initial_properties = {},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return ImmersiveCraftNode
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type CraftSystem.ImmersiveCraftNode
|
||||||
|
local ImmersiveCraftNode = require('CraftSystem.ImmersiveCraftNode')
|
||||||
|
|
||||||
|
---A class that extends `Creature.LuaEntity` to annotate custom fields needed for immersive crafting.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class CraftSystem.ImmersiveCraftNode.LuaTappingNode : Creature.LuaEntity
|
||||||
|
---The name of the item that the crafting node display.
|
||||||
|
---@field _item_name ItemStackString
|
||||||
|
---Sets the entity display of the item. If the item stack was not transferred, `self._item_name` is used.
|
||||||
|
---@field _set_item fun(self:CraftSystem.ImmersiveCraftNode.LuaTappingNode, itemName:ItemStackString?)
|
||||||
|
---@field _craft_name string
|
||||||
|
---@field _stage_name string
|
||||||
|
---@field _work_station_pos MapVector
|
||||||
|
|
||||||
|
-- A crafting node (entity) that is used in the tapping crafting stage.
|
||||||
|
local tappingNode = ImmersiveCraftNode:new({
|
||||||
|
settings = {
|
||||||
|
_name = 'craft_system:tapping_node',
|
||||||
|
_add_mod_name = false,
|
||||||
|
|
||||||
|
---@type ObjectProperties
|
||||||
|
initial_properties = {
|
||||||
|
visual = 'wielditem',
|
||||||
|
visual_size = {x=0.5, y=0.5, z=0.5},
|
||||||
|
collisionbox = {
|
||||||
|
-0.375, -0.375, -0.375,
|
||||||
|
0.375, 0.375, 0.375
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
_item_name = '',
|
||||||
|
_craft_name = '',
|
||||||
|
_stage_name = '',
|
||||||
|
_work_station_pos = {},
|
||||||
|
},
|
||||||
|
callbacks = {
|
||||||
|
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
||||||
|
on_activate = function(luaEntity, staticdata, dtime)
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingNode.InitParams?
|
||||||
|
local params = core.deserialize(staticdata)
|
||||||
|
|
||||||
|
if params ~= nil then
|
||||||
|
luaEntity._item_name = params.targetItem
|
||||||
|
luaEntity._craft_name = params.craftName
|
||||||
|
luaEntity._stage_name = params.stageName
|
||||||
|
luaEntity._work_station_pos = params.WorkStationPos
|
||||||
|
end
|
||||||
|
|
||||||
|
-- we set the appearance of the item to be processed on the crafting node.
|
||||||
|
luaEntity:_set_item(luaEntity._item_name)
|
||||||
|
-- We add an immortal group to the entity so that it cannot be killed.
|
||||||
|
luaEntity.object:set_armor_groups({immortal = 1})
|
||||||
|
end,
|
||||||
|
|
||||||
|
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
||||||
|
on_punch = function(luaEntity, puncher, time_from_last_punch, tool_capabilities, dir, damage)
|
||||||
|
-- We simply trigger the event that the player hits the crafting node, bound to the work station with coordinates.
|
||||||
|
Events:call(('craft_system:punch_tapping_node:%s:%s'):format(
|
||||||
|
luaEntity._craft_name, luaEntity._stage_name), luaEntity._work_station_pos, puncher, luaEntity)
|
||||||
|
|
||||||
|
return false
|
||||||
|
end,
|
||||||
|
|
||||||
|
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
||||||
|
get_staticdata = function(luaEntity)
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingNode.InitParams?
|
||||||
|
local params = {
|
||||||
|
targetItem = luaEntity._item_name,
|
||||||
|
craftName = luaEntity._craft_name,
|
||||||
|
stageName = luaEntity._stage_name,
|
||||||
|
WorkStationPos = luaEntity._work_station_pos
|
||||||
|
}
|
||||||
|
|
||||||
|
return core.serialize(params)
|
||||||
|
end,
|
||||||
|
|
||||||
|
---Sets the entity display of the item. If the item stack was not transferred, `self._item_name` is used.
|
||||||
|
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
||||||
|
---@param itemName ItemStackString?
|
||||||
|
_set_item = function(luaEntity, itemName)
|
||||||
|
-- We change the entity field so that the engine itself displays it as a given item.
|
||||||
|
luaEntity.object:set_properties({
|
||||||
|
wield_item = itemName or luaEntity._item_name,
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return tappingNode
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
---@alias CraftSystem.ImmersiveCraftStage.Type 'tapping'
|
||||||
|
|
||||||
|
---One of the stages of immersive crafting.
|
||||||
|
---@class CraftSystem.ImmersiveCraftStage
|
||||||
|
---@field name string
|
||||||
|
---@field type CraftSystem.ImmersiveCraftStage.Type
|
||||||
|
---@field craftName string
|
||||||
|
---@field startHandler fun(self, pos:Position, player:Player)
|
||||||
|
---@field processHandler fun(self, pos:Position, player:Player)
|
||||||
|
---@field endHandler fun(self, pos:Position, player:Player)
|
||||||
|
---@field abortHandler fun(self, pos:Position)
|
||||||
|
---@field toString fun(self):string
|
||||||
|
---@field nextStage string?
|
||||||
|
---@field returnItems ItemStackString[]?
|
||||||
|
local ImmersiveCraftStage = {}
|
||||||
|
|
||||||
|
---Definition table for the `CraftSystem.ImmersiveCraft`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class CraftSystem.ImmersiveCraftStageDef
|
||||||
|
---@field name string
|
||||||
|
---@field type CraftSystem.ImmersiveCraftStage.Type
|
||||||
|
---@field craftName string?
|
||||||
|
---@field nextStage string?
|
||||||
|
---@field returnItems ItemStackString[]?
|
||||||
|
|
||||||
|
---@param def CraftSystem.ImmersiveCraftStageDef
|
||||||
|
---@return CraftSystem.ImmersiveCraftStage
|
||||||
|
function ImmersiveCraftStage:new(def)
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage
|
||||||
|
local instance = getInstance(self, {})
|
||||||
|
instance.name = def.name
|
||||||
|
instance.type = def.type
|
||||||
|
instance.craftName = def.craftName
|
||||||
|
instance.nextStage = def.nextStage
|
||||||
|
instance.returnItems = def.returnItems
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
return ImmersiveCraftStage
|
||||||
@@ -0,0 +1,189 @@
|
|||||||
|
local extend = Class.extend
|
||||||
|
|
||||||
|
local require = Mod.getInfo('smc__core__craft_system').require
|
||||||
|
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage
|
||||||
|
local ImmersiveCraftStage = require('CraftSystem.ImmersiveCraftStage')
|
||||||
|
|
||||||
|
---A crafting step that requires the player to hit a crafting node (entity) a certain number of times.
|
||||||
|
---@class CraftSystem.ImmersiveCraftStage.TappingStage : CraftSystem.ImmersiveCraftStage
|
||||||
|
---@field targetItem string
|
||||||
|
---@field clicksNeed string
|
||||||
|
---@field toolGroup string?
|
||||||
|
---@field startHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapVector, Player:Player)
|
||||||
|
---@field processHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapVector, Player:Player)
|
||||||
|
---@field endHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapVector, Player:Player)
|
||||||
|
---@field toString fun(self:CraftSystem.ImmersiveCraftStage.TappingStage):string
|
||||||
|
local TappingStage = extend(ImmersiveCraftStage, {})
|
||||||
|
|
||||||
|
---Definition table for the `CraftSystem.ImmersiveCraftStage.TappingStage`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class CraftSystem.ImmersiveCraftStage.TappingStageDef : CraftSystem.ImmersiveCraftStageDef
|
||||||
|
---@field targetItem string
|
||||||
|
---@field clicksNeed string
|
||||||
|
---@field toolGroup string?
|
||||||
|
|
||||||
|
---Data table for transferring various time parameters between calls.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class CraftSystem.ImmersiveCraftStage.TappingStageData
|
||||||
|
---How many clicks were made by the player on the entity.
|
||||||
|
---@field clickCount integer
|
||||||
|
---Unique identifier of the entity (craft node).
|
||||||
|
---@field entityGUID string
|
||||||
|
|
||||||
|
---@param def CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
function TappingStage:new(def)
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStage
|
||||||
|
local instance = ImmersiveCraftStage:new(def)
|
||||||
|
instance.targetItem = def.targetItem
|
||||||
|
instance.clicksNeed = def.clicksNeed
|
||||||
|
instance.toolGroup = def.toolGroup
|
||||||
|
|
||||||
|
-- Registering the crafting stage start handler.
|
||||||
|
instance.startHandler = function(tappingStage , pos, player)
|
||||||
|
local entityPos = vector.new(pos) + vector.new(0,0.875,0)
|
||||||
|
|
||||||
|
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
||||||
|
local params = {
|
||||||
|
targetItem = tappingStage.targetItem,
|
||||||
|
craftName = tappingStage.craftName,
|
||||||
|
stageName = tappingStage.name,
|
||||||
|
WorkStationPos = pos,
|
||||||
|
}
|
||||||
|
|
||||||
|
local obj = core.add_entity(entityPos, 'craft_system:tapping_node', core.serialize(params))
|
||||||
|
|
||||||
|
if obj == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData
|
||||||
|
local stageData = {
|
||||||
|
clickCount = 0,
|
||||||
|
entityGUID = obj:get_guid()
|
||||||
|
}
|
||||||
|
|
||||||
|
--@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
meta:set_string('stage_data', core.serialize(stageData))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Registering the crafting stage process handler.
|
||||||
|
-- Called when a player clicks on a crafting node (entity).
|
||||||
|
instance.processHandler = function(tappingStage, pos, player)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||||
|
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||||
|
assert(stageData ~= nil)
|
||||||
|
|
||||||
|
local clickCount = stageData.clickCount
|
||||||
|
clickCount = clickCount + 1
|
||||||
|
|
||||||
|
if clickCount >= tappingStage.clicksNeed then
|
||||||
|
tappingStage:endHandler(pos, player)
|
||||||
|
else
|
||||||
|
stageData.clickCount = clickCount
|
||||||
|
meta:set_string('stage_data', core.serialize(stageData))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Registering the crafting stage end handler.
|
||||||
|
instance.endHandler = function(tappingStage, pos, player)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||||
|
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||||
|
assert(stageData ~= nil)
|
||||||
|
|
||||||
|
|
||||||
|
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
||||||
|
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
||||||
|
|
||||||
|
entity:remove()
|
||||||
|
|
||||||
|
Events:call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, player)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Registering the aborting the crafting process handler.
|
||||||
|
---@param tappingStage CraftSystem.ImmersiveCraftStage.TappingStage
|
||||||
|
instance.abortHandler = function(tappingStage, pos)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||||
|
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||||
|
assert(stageData ~= nil)
|
||||||
|
local amount = meta:get_float('craft_amount')
|
||||||
|
assert(amount ~= 0)
|
||||||
|
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||||
|
local stageData = stageData
|
||||||
|
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||||
|
|
||||||
|
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
||||||
|
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
||||||
|
|
||||||
|
entity:remove()
|
||||||
|
|
||||||
|
-- We drop the item as it was before processing.
|
||||||
|
local dropPos = vector.new(pos)+vector.new(0, 0.9,0)
|
||||||
|
for i=1, amount do
|
||||||
|
core.item_drop(ItemStack(tappingStage.targetItem), nil, dropPos)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Returns a human-readable description of the crafting stage.
|
||||||
|
instance.toString = function(tappingStage)
|
||||||
|
local stageDesc
|
||||||
|
if tappingStage.toolGroup ~= nil then
|
||||||
|
stageDesc = ('Tapping x%s tool with a group "%s"'):format(tappingStage.clicksNeed, tappingStage.toolGroup)
|
||||||
|
else
|
||||||
|
stageDesc = ('Tapping x%s'):format(tappingStage.clicksNeed)
|
||||||
|
end
|
||||||
|
|
||||||
|
return stageDesc
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Subscribe to an event that means the player hit the entity.
|
||||||
|
Events:subscribe(('craft_system:punch_tapping_node:%s:%s'):format(instance.craftName, instance.name),
|
||||||
|
---@param craftNode LuaEntity
|
||||||
|
---@param player Player
|
||||||
|
function(eventName, pos, player, craftNode)
|
||||||
|
local toolDef = player:get_wielded_item():get_definition()
|
||||||
|
|
||||||
|
-- If a crafting stage requires a specific tool group and he doesn't have it then we don't do anything.
|
||||||
|
if instance.toolGroup and (not toolDef.groups or not toolDef.groups[instance.toolGroup]) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- DRAFT START:
|
||||||
|
-- Creating particles
|
||||||
|
for i=0, 10 do
|
||||||
|
core.add_particle({
|
||||||
|
pos = vector.new(pos)+vector.new(0,0.5,0),
|
||||||
|
velocity = {x=math.random(-1,1), y=4, z=math.random(-1,1)},
|
||||||
|
acceleration = {x=0, y=-9.8, z=0},
|
||||||
|
expirationtime = 2,
|
||||||
|
collision_removal = true,
|
||||||
|
node = {name=instance.targetItem}
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Making the entity shake (silly method :P).
|
||||||
|
craftNode.object:set_rotation(vector.new(0.01,0.1,0.01))
|
||||||
|
core.after(0.1, function() craftNode.object:set_rotation(vector.new(-0.01,-0.1,-0.01))end)
|
||||||
|
core.after(0.2, function() craftNode.object:set_rotation(vector.new(0,0,0))end)
|
||||||
|
-- DRAFT END
|
||||||
|
|
||||||
|
instance:processHandler(pos, player)
|
||||||
|
end)
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Registering an entity for immersive tapping crafting.
|
||||||
|
require('CraftSystem.ImmersiveCraftNode.tappingNode')
|
||||||
|
|
||||||
|
return TappingStage
|
||||||
30
mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraft.lua
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
local getInstance, extend = Class.getInstance, Class.extend
|
||||||
|
|
||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type CraftSystem.BaseCraft
|
||||||
|
local BaseCraft = require('CraftSystem.BaseCraft')
|
||||||
|
|
||||||
|
---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft
|
||||||
|
---@field returnItems ItemStackString[]?
|
||||||
|
local PrimitiveCraft = BaseCraft:extend({})
|
||||||
|
|
||||||
|
---Definition table for the `CraftSystem.PrimitiveCraft`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef
|
||||||
|
---@field returnItems ItemStackString[]?
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param def CraftSystem.PrimitiveCraftDef
|
||||||
|
---@return CraftSystem.PrimitiveCraft
|
||||||
|
function PrimitiveCraft:new(name, def)
|
||||||
|
---@type CraftSystem.PrimitiveCraft
|
||||||
|
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||||
|
instance.returnItems = def.returnItems
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
return PrimitiveCraft
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
local extend = Class.extend
|
||||||
|
|
||||||
|
local tableInsert, tableSort, tableJoin = table.insert, table.sort, table.join
|
||||||
|
|
||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type CraftSystem.BaseCraftHandler
|
||||||
|
local BaseCraftHandler = require('CraftSystem.BaseCraftHandler')
|
||||||
|
|
||||||
|
---@type CraftSystem.PrimitiveCraft
|
||||||
|
local PrimitiveCraft = require('CraftSystem.PrimitiveCraft')
|
||||||
|
|
||||||
|
---A primitive crafting processor that doesn't require much interaction and provides
|
||||||
|
---simple crafting based on a recipe-result scheme.
|
||||||
|
---@class CraftSystem.PrimitiveCraftHandler : CraftSystem.BaseCraftHandler
|
||||||
|
---@field defaultCraftDef CraftSystem.PrimitiveCraftDef
|
||||||
|
---
|
||||||
|
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.PrimitiveCraft[]>
|
||||||
|
---@field groupedCrafts (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
||||||
|
---
|
||||||
|
---@field new fun(self:CraftSystem.PrimitiveCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.PrimitiveCraftType, def:CraftSystem.PrimitiveCraftHandlerDef):CraftSystem.PrimitiveCraftHandler
|
||||||
|
---@field getCraftByName fun(self, craftName:string):CraftSystem.PrimitiveCraft
|
||||||
|
---@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.PrimitiveCraft[]
|
||||||
|
---@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
||||||
|
local PrimitiveCraftHandler = BaseCraftHandler:extend({})
|
||||||
|
|
||||||
|
---@class CraftSystem.PrimitiveCraftHandlerDef : CraftSystem.BaseCraftHandlerDef
|
||||||
|
---@field defaultCraftDef CraftSystem.PrimitiveCraftDef?
|
||||||
|
|
||||||
|
---Register a primitive craft that does not require special interaction.
|
||||||
|
---@param craftName string
|
||||||
|
---@param def CraftSystem.PrimitiveCraftDef
|
||||||
|
function PrimitiveCraftHandler:registerCraft(craftName, def)
|
||||||
|
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
||||||
|
craftName))
|
||||||
|
|
||||||
|
local def = table.merge(def, self.defaultCraftDef)
|
||||||
|
|
||||||
|
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
||||||
|
def.group = group
|
||||||
|
|
||||||
|
local craft = PrimitiveCraft:new(craftName, def)
|
||||||
|
|
||||||
|
if self.craftsByGroup[group] == nil then
|
||||||
|
self.craftsByGroup[group] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
self.craftsByName[craftName] = craft
|
||||||
|
tableInsert(self.craftsByGroup[group], craft)
|
||||||
|
tableInsert(self.craftsList, craft)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Adds alternative recipes to the craft with the specified name.
|
||||||
|
---
|
||||||
|
---If a craft with the specified name does not exist, an error will call.
|
||||||
|
---@param craftName string
|
||||||
|
---@param recipes CraftSystem.CraftRecipe[]
|
||||||
|
function PrimitiveCraftHandler:registerAlternativeRecipes(craftName, recipes)
|
||||||
|
local craft = PrimitiveCraftHandler:getCraftByName(craftName)
|
||||||
|
|
||||||
|
craft.altRecipes = tableJoin(craft.altRecipes or {}, recipes)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||||
|
-- Provides a convenient list of crafting items in the player's inventory.
|
||||||
|
core.register_on_mods_loaded(function()
|
||||||
|
for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do
|
||||||
|
tableSort(crafts, function(a, b)
|
||||||
|
if a.output < b.output then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
if groupName ~= 'craft_group:UNDEFINED' then
|
||||||
|
tableInsert(PrimitiveCraftHandler.groupedCrafts, crafts)
|
||||||
|
else
|
||||||
|
PrimitiveCraftHandler.groupedCrafts = tableJoin(PrimitiveCraftHandler.groupedCrafts, crafts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tableSort(PrimitiveCraftHandler.groupedCrafts, function(a, b)
|
||||||
|
local outputA = (a[1] and a[1].output) or a.output
|
||||||
|
local outputB = (b[1] and b[1].output) or b.output
|
||||||
|
|
||||||
|
if outputA < outputB then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
return PrimitiveCraftHandler
|
||||||
41
mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraft.lua
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
local getInstance, extend = Class.getInstance, Class.extend
|
||||||
|
|
||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type CraftSystem.BaseCraft
|
||||||
|
local BaseCraft = require('CraftSystem.BaseCraft')
|
||||||
|
|
||||||
|
---@class CraftSystem.ProcessingCraft : CraftSystem.BaseCraft
|
||||||
|
---@field recipe CraftSystem.CraftRecipe
|
||||||
|
---@field time number
|
||||||
|
---@field startHandler fun()
|
||||||
|
---@field processHandler fun()
|
||||||
|
---@field endHandler fun()
|
||||||
|
local ProcessingCraft = BaseCraft:extend({})
|
||||||
|
|
||||||
|
---Definition table for the `CraftSystem.ProcessingCraft`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class CraftSystem.ProcessingCraftDef : CraftSystem.BaseCraftDef
|
||||||
|
---@field recipe CraftSystem.CraftRecipe
|
||||||
|
---@field time number
|
||||||
|
---@field startHandler fun()?
|
||||||
|
---@field processHandler fun()?
|
||||||
|
---@field endHandler fun()?
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param def CraftSystem.ProcessingCraftDef
|
||||||
|
---@return CraftSystem.ProcessingCraft
|
||||||
|
function ProcessingCraft:new(name, def)
|
||||||
|
---@type CraftSystem.ProcessingCraft
|
||||||
|
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||||
|
instance.time = def.time
|
||||||
|
instance.startHandler = def.startHandler or function() end
|
||||||
|
instance.processHandler = def.processHandler or function() end
|
||||||
|
instance.endHandler = def.endHandler or function() end
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
return ProcessingCraft
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
local HASH_SEPARATOR = ','
|
||||||
|
|
||||||
|
local tableInsert, tableCopy, tableSort, tableConcat, tableIsEmpty = table.insert, table.copy, table.sort, table.concat, table.is_empty
|
||||||
|
|
||||||
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type CraftSystem.BaseCraftHandler
|
||||||
|
local BaseCraftHandler = require('CraftSystem.BaseCraftHandler')
|
||||||
|
|
||||||
|
---@type CraftSystem.ProcessingCraft
|
||||||
|
local ProcessingCraft = require('CraftSystem.ProcessingCraft')
|
||||||
|
|
||||||
|
---A primitive crafting processor that doesn't require much interaction and provides
|
||||||
|
---simple crafting based on a recipe-result scheme.
|
||||||
|
---@class CraftSystem.ProcessingCraftHandler : CraftSystem.BaseCraftHandler
|
||||||
|
---@field defaultCraftDef CraftSystem.ProcessingCraftDef
|
||||||
|
---
|
||||||
|
---@field craftsByHash table<string, CraftSystem.ProcessingCraft>
|
||||||
|
---
|
||||||
|
---@field new fun(self:CraftSystem.ProcessingCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.ProcessingCraftType, def:CraftSystem.ProcessingCraftHandlerDef):CraftSystem.ProcessingCraftHandler
|
||||||
|
---@field getCraftByName fun(self, craftName:string):CraftSystem.ProcessingCraft
|
||||||
|
---@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.ProcessingCraft[]
|
||||||
|
---@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.ProcessingCraft[]|CraftSystem.ProcessingCraft)[]
|
||||||
|
local ProcessingCraftHandler = BaseCraftHandler:extend({
|
||||||
|
craftsByHash = {}
|
||||||
|
})
|
||||||
|
|
||||||
|
---@class CraftSystem.ProcessingCraftHandlerDef : CraftSystem.BaseCraftHandlerDef
|
||||||
|
---@field defaultCraftDef CraftSystem.ProcessingCraftDef?
|
||||||
|
|
||||||
|
---@param recipe CraftSystem.CraftRecipe
|
||||||
|
local function getCraftHash(recipe)
|
||||||
|
recipe = tableCopy(recipe)
|
||||||
|
|
||||||
|
tableSort(recipe)
|
||||||
|
return tableConcat(recipe, HASH_SEPARATOR)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Register a primitive craft that does not require special interaction.
|
||||||
|
---@param craftName string
|
||||||
|
---@param def CraftSystem.ProcessingCraftDef
|
||||||
|
function ProcessingCraftHandler:registerCraft(craftName, def)
|
||||||
|
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
||||||
|
craftName))
|
||||||
|
|
||||||
|
local def = table.merge(def, self.defaultCraftDef)
|
||||||
|
|
||||||
|
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
||||||
|
def.group = group
|
||||||
|
|
||||||
|
local craft = ProcessingCraft:new(craftName, def)
|
||||||
|
|
||||||
|
if self.craftsByGroup[group] == nil then
|
||||||
|
self.craftsByGroup[group] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local craftHash = getCraftHash(craft.recipe)
|
||||||
|
assert(self.craftsByHash[craftHash] == nil, 'A craft with this recipe already exists.')
|
||||||
|
|
||||||
|
self.craftsByName[craftName] = craft
|
||||||
|
tableInsert(self.craftsByGroup[group], craft)
|
||||||
|
tableInsert(self.craftsList, craft)
|
||||||
|
self.craftsByHash[craftHash] = craft
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param input ItemStack[]
|
||||||
|
---@return CraftSystem.ProcessingCraft?
|
||||||
|
function ProcessingCraftHandler:getCraftByRecipe(input)
|
||||||
|
local recipe = {}
|
||||||
|
for _, item in ipairs(input) do
|
||||||
|
if not item:is_empty() then
|
||||||
|
tableInsert(recipe, item:get_name())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if tableIsEmpty(recipe) then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- TODO: добавить поиск по группам крафтов
|
||||||
|
|
||||||
|
local craftHash = getCraftHash(recipe)
|
||||||
|
local craft = self.craftsByHash[craftHash]
|
||||||
|
|
||||||
|
return craft
|
||||||
|
end
|
||||||
|
|
||||||
|
return ProcessingCraftHandler
|
||||||
4
mods/CORE/Creature/init.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__creature')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
return require('Creature')
|
||||||
1
mods/CORE/Creature/mod.conf
Normal file
@@ -0,0 +1 @@
|
|||||||
|
name = smc__core__creature
|
||||||
109
mods/CORE/Creature/src/Creature.lua
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
local getInstance, extend = Class.getInstance, Class.extend
|
||||||
|
|
||||||
|
local tableCopy, tableMerge = table.copy, table.merge
|
||||||
|
|
||||||
|
---@class Creature
|
||||||
|
---@field entityDef Creature.CreatureDefinition
|
||||||
|
---@field luantiDef EntityDefinition
|
||||||
|
---@field defaultDef Creature.CreatureDefinition
|
||||||
|
local Creature = {
|
||||||
|
defaultDef = {
|
||||||
|
settings = {
|
||||||
|
_name = '',
|
||||||
|
_add_mod_name = true,
|
||||||
|
initial_properties = {},
|
||||||
|
},
|
||||||
|
callbacks = {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
---@param entityDef Creature.CreatureDefinition
|
||||||
|
---@return EntityDefinition
|
||||||
|
local function entityDefToLuantiDef(entityDef)
|
||||||
|
entityDef = tableCopy(entityDef)
|
||||||
|
local s = entityDef.settings
|
||||||
|
local c = entityDef.callbacks or {}
|
||||||
|
|
||||||
|
---@type EntityDefinition
|
||||||
|
local luantiDef = tableMerge(s, c)
|
||||||
|
|
||||||
|
return luantiDef
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param entityDef Creature.CreatureDefinition
|
||||||
|
---@return Creature
|
||||||
|
function Creature:new(entityDef)
|
||||||
|
-- Adding default parameters.
|
||||||
|
---@type Creature.CreatureDefinition
|
||||||
|
entityDef = tableMerge(entityDef, self.defaultDef, true)
|
||||||
|
|
||||||
|
-- The callback table may not be specified.
|
||||||
|
if entityDef.callbacks == nil then
|
||||||
|
---@type Creature.Callbacks
|
||||||
|
entityDef.callbacks = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local s = entityDef.settings
|
||||||
|
local c = entityDef.callbacks
|
||||||
|
|
||||||
|
-- Add the short mod name prefix to the entity name.
|
||||||
|
if s._add_mod_name then
|
||||||
|
s._name = Mod.getInfo().shortName .. ':' .. s._name
|
||||||
|
end
|
||||||
|
|
||||||
|
local onActivate = c.on_activate or function() end
|
||||||
|
|
||||||
|
-- We rewrite the entity initialization function so that custom fields
|
||||||
|
-- can be passed to it directly via `core.add_entity(pos, name, [staticdata])`.
|
||||||
|
c.on_activate = function(luaEntity, staticdata, dtime)
|
||||||
|
local initCustomFields = {}
|
||||||
|
|
||||||
|
if staticdata and staticdata ~= '' then
|
||||||
|
initCustomFields = core.deserialize(staticdata) or {}
|
||||||
|
end
|
||||||
|
|
||||||
|
-- We reset the fields that should not be overwritten.
|
||||||
|
initCustomFields.name = nil
|
||||||
|
initCustomFields.object = nil
|
||||||
|
|
||||||
|
---@type LuaEntity
|
||||||
|
self = tableMerge(self, initCustomFields)
|
||||||
|
|
||||||
|
-- If an initialization callback was specified for the entity, then we call it.
|
||||||
|
if c.on_activate ~= nil then
|
||||||
|
onActivate(luaEntity, staticdata, dtime)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type Creature
|
||||||
|
local instance = getInstance(self, {})
|
||||||
|
|
||||||
|
local luantiDef = entityDefToLuantiDef(entityDef)
|
||||||
|
|
||||||
|
instance.entityDef = entityDef
|
||||||
|
instance.luantiDef = luantiDef
|
||||||
|
|
||||||
|
core.register_entity(':'..s._name, luantiDef)
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
---Allows you to create a child class of an entity by specifying
|
||||||
|
---default parameters that will be added to the entity's parameters
|
||||||
|
---when an instance is received.
|
||||||
|
---@param defaultDef Creature.CreatureDefinition
|
||||||
|
---@return Creature
|
||||||
|
function Creature:getExtended(defaultDef)
|
||||||
|
-- Adding default parameters.
|
||||||
|
---@type Creature.CreatureDefinition
|
||||||
|
defaultDef = tableMerge(self.defaultDef, defaultDef)
|
||||||
|
|
||||||
|
---@type Creature
|
||||||
|
local ChildClass = extend(self, {
|
||||||
|
defaultDef = defaultDef,
|
||||||
|
})
|
||||||
|
|
||||||
|
return ChildClass
|
||||||
|
end
|
||||||
|
|
||||||
|
return Creature
|
||||||
72
mods/CORE/Creature/src/Creature/Callbacks.lua
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
---@class Creature.Callbacks
|
||||||
|
local Callbacks = {}
|
||||||
|
|
||||||
|
---Called when the object is instantiated.
|
||||||
|
--- * `dtime_s` is the time passed since the object was unloaded,
|
||||||
|
---which can be used for updating the entity state.
|
||||||
|
---@type fun(self:LuaEntity, staticdata:string, dtime_s:number)?
|
||||||
|
Callbacks.on_activate = nil
|
||||||
|
|
||||||
|
---Called when the object is about to get removed or unloaded.
|
||||||
|
--- * `removal`: boolean indicating whether the object is about to get removed.
|
||||||
|
--- Calling `object:remove()` on an active object will call this with `removal=true`.
|
||||||
|
--- The mapblock the entity resides in being unloaded will call this with `removal=false`.
|
||||||
|
---
|
||||||
|
---Note that this won't be called if the object hasn't been activated in the first place.
|
||||||
|
---In particular, `core.clear_objects({mode = "full"})` won't call this,
|
||||||
|
---whereas `core.clear_objects({mode = "quick"})` might call this.
|
||||||
|
---@type fun(self:LuaEntity, removal:boolean)?
|
||||||
|
Callbacks.on_deactivate = nil
|
||||||
|
|
||||||
|
---Called on every server tick, after movement and collision processing.
|
||||||
|
--- * `dtime`: elapsed time since last call
|
||||||
|
--- * `s`: table with collision info (only available if `physical=true`)
|
||||||
|
---@type fun(self:LuaEntity, dtime:number, moveresult:table)? -- TODO: дописать более точный тип moveresult
|
||||||
|
Callbacks.on_step = nil
|
||||||
|
|
||||||
|
---Called when somebody punches the object.
|
||||||
|
---Note that you probably want to handle most punches using the automatic armor group system.
|
||||||
|
--- * `puncher`: an `ObjectRef `(can be `nil`)
|
||||||
|
--- * `time_from_last_punch`: Meant for disallowing spamming of clicks (can be `nil`).
|
||||||
|
--- * `tool_capabilities`: capability table of used item
|
||||||
|
--- * `dir`: unit vector of direction of punch. Always defined. Points from the puncher to the punched.
|
||||||
|
--- * `damage``: damage that will be done to entity.
|
||||||
|
---
|
||||||
|
---Can return `true` to prevent the default damage mechanism.
|
||||||
|
---@type (fun(self:LuaEntity, puncher:ObjectRef|Player|nil, time_from_last_punch:number|nil, tool_capabilities:table|nil, dir:vector, damage:number):boolean)? -- TODO: дописать более точный тип для tool_capabilities
|
||||||
|
Callbacks.on_punch = nil
|
||||||
|
|
||||||
|
---Called when the object dies.
|
||||||
|
--- * `killer`: an `ObjectRef` (can be `nil`)
|
||||||
|
---@type fun(self:LuaEntity, killer:ObjectRef|Player|nil)?
|
||||||
|
Callbacks.on_death = nil
|
||||||
|
|
||||||
|
---Called when `clicker` pressed the 'place/use' key while
|
||||||
|
---pointing to the object (not necessarily an actual rightclick)
|
||||||
|
--- * `clicker`: an `ObjectRef` (may or may not be a player)
|
||||||
|
---@type fun(self:LuaEntity, clicker:ObjectRef|Player)?
|
||||||
|
Callbacks.on_rightclick = nil
|
||||||
|
|
||||||
|
---Called after another object is attached to this object.
|
||||||
|
--- * `child`: an `ObjectRef` of the child
|
||||||
|
---@type fun(self:LuaEntity, child:ObjectRef)?
|
||||||
|
Callbacks.on_attach_child = nil
|
||||||
|
|
||||||
|
---Called after another object has detached from this object.
|
||||||
|
--- * `child`: an `ObjectRef` of the child
|
||||||
|
---@type fun(self:LuaEntity, child:ObjectRef)?
|
||||||
|
Callbacks.on_detach_child = nil
|
||||||
|
|
||||||
|
---Called after detaching from another object.
|
||||||
|
--- * `parent`: an ObjectRef from where it got detached
|
||||||
|
---
|
||||||
|
---**Note**: this is also called before removal from the world.
|
||||||
|
---@type fun(self:LuaEntity, parent)?
|
||||||
|
Callbacks.on_detach = nil
|
||||||
|
|
||||||
|
---Should return a string that will be passed to `on_activate` when the object
|
||||||
|
---is instantiated the next time.
|
||||||
|
---@type (fun(self:LuaEntity):string)?
|
||||||
|
Callbacks.get_staticdata = nil
|
||||||
|
|
||||||
|
return Callbacks
|
||||||
6
mods/CORE/Creature/src/Creature/CreatureDefinition.lua
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
---@class Creature.CreatureDefinition
|
||||||
|
---@field settings Creature.Settings
|
||||||
|
---@field callbacks Creature.Callbacks?
|
||||||
|
local CreatureDefinition = {}
|
||||||
|
|
||||||
|
return CreatureDefinition
|
||||||
9
mods/CORE/Creature/src/Creature/LuaEntity.lua
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
---A class that extends `LuaEntity` to annotate custom fields.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class Creature.LuaEntity : LuaEntity
|
||||||
|
---@field _name string
|
||||||
|
---@field _add_mod_name boolean
|
||||||
|
local LuaEntity = {}
|
||||||
|
|
||||||
|
return LuaEntity
|
||||||
14
mods/CORE/Creature/src/Creature/Settings.lua
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
---@class Creature.Settings
|
||||||
|
local Settings = {}
|
||||||
|
|
||||||
|
---Registration name for the entity. Must be unique.
|
||||||
|
---@type string
|
||||||
|
Settings._name = nil
|
||||||
|
|
||||||
|
---Whether to add the mod name to the settings._name. Default: `true`
|
||||||
|
Settings._add_mod_name = nil
|
||||||
|
|
||||||
|
---@type ObjectProperties
|
||||||
|
Settings.initial_properties = nil
|
||||||
|
|
||||||
|
return Settings
|
||||||
@@ -1 +0,0 @@
|
|||||||
name = smc__core__entity
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
local Enum = Mod:new()
|
|
||||||
|
|
||||||
Enum.Class = Enum.require('Enum')
|
|
||||||
|
|
||||||
Api.addModToGlobalSpace(Enum, 'Core.Enum')
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
name = smc__core__enum
|
|
||||||
depends = smc__api__api_loader
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
---Abstract class implementing enumeration. It is a table<ENUM_KEY, ENUM_KEY>. Read-only table!
|
|
||||||
---@abstract
|
|
||||||
---@class Enum
|
|
||||||
local Enum = {}
|
|
||||||
|
|
||||||
---@param enum table[]
|
|
||||||
---@return Enum
|
|
||||||
function Enum:new(enum)
|
|
||||||
local metaEnum = {}
|
|
||||||
|
|
||||||
-- Create table<ENUM_KEY, ENUM_KEY>
|
|
||||||
for _, v in ipairs(enum) do
|
|
||||||
metaEnum[v] = v
|
|
||||||
end
|
|
||||||
|
|
||||||
---@type Enum
|
|
||||||
local instance = setmetatable({}, {
|
|
||||||
__index = metaEnum,
|
|
||||||
__newindex = function()
|
|
||||||
error('The Enum table is read only!')
|
|
||||||
end,
|
|
||||||
__metatable = false
|
|
||||||
})
|
|
||||||
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
---@alias Enum.EnumKey string Key from enumeration
|
|
||||||
|
|
||||||
return Enum
|
|
||||||
4
mods/CORE/Formspec/init.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__formspec')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
return require('Formspec')
|
||||||
2
mods/CORE/Formspec/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
name = smc__core__formspec
|
||||||
|
depends = smc__system__system_loader
|
||||||
1198
mods/CORE/Formspec/src/Formspec.lua
Normal file
4
mods/CORE/ImmersiveWorkStation/init.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__immersive_work_station')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
return require('ImmersiveWorkStation')
|
||||||
2
mods/CORE/ImmersiveWorkStation/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
name = smc__core__immersive_work_station
|
||||||
|
depends = smc__core__craft_system, smc__core__node, smc__ui__work_station
|
||||||
218
mods/CORE/ImmersiveWorkStation/src/ImmersiveWorkStation.lua
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
local CraftSystem = Smc.requireCraftSystem()
|
||||||
|
|
||||||
|
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||||
|
|
||||||
|
local Node = Smc.requireNode()
|
||||||
|
|
||||||
|
-- Default settings that a work station node should have.
|
||||||
|
local ImmersiveWorkStationNode = Node:getExtended({
|
||||||
|
settings = {
|
||||||
|
name = '',
|
||||||
|
drawtype = 'normal',
|
||||||
|
paramtype = 'light',
|
||||||
|
groups = { immersive_station = 1, dig_immediate = 2 }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
---A work station (node) that enables player interaction
|
||||||
|
---with the immersive crafting mechanics.
|
||||||
|
---@class ImmersiveWorkStation
|
||||||
|
---@field name string
|
||||||
|
---@field craftType CraftSystem.ImmersiveCraftType
|
||||||
|
---@field handler CraftSystem.ImmersiveCraftHandler
|
||||||
|
---@field tab PlayerInventoryUI.getTabFunc
|
||||||
|
local ImmersiveWorkStation = {}
|
||||||
|
|
||||||
|
---Definition table for the `ImmersiveWorkStation`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class ImmersiveWorkStationDef
|
||||||
|
---@field craftType CraftSystem.ImmersiveCraftType
|
||||||
|
---@field tab PlayerInventoryUI.getTabFunc
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param def ImmersiveWorkStationDef
|
||||||
|
---@param nodeDef Node.NodeDefinition
|
||||||
|
function ImmersiveWorkStation:new(name, def, nodeDef)
|
||||||
|
local handler = CraftSystem:getImmersiveCraftHandler(def.craftType)
|
||||||
|
|
||||||
|
---@type ImmersiveWorkStation
|
||||||
|
local instance = getInstance(self, {})
|
||||||
|
instance.name = name
|
||||||
|
instance.craftType = def.craftType
|
||||||
|
instance.handler = handler
|
||||||
|
instance.tab = def.tab
|
||||||
|
|
||||||
|
if nodeDef.callbacks == nil then
|
||||||
|
---@type Node.Callbacks
|
||||||
|
nodeDef.callbacks = {}
|
||||||
|
end
|
||||||
|
local settings = nodeDef.settings
|
||||||
|
local callbacks = nodeDef.callbacks
|
||||||
|
|
||||||
|
-- Callback when the work station block is installed in the world
|
||||||
|
-- Ensures initialization of necessary parameters
|
||||||
|
callbacks.on_construct = function(pos)
|
||||||
|
instance:init(pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Callback for interaction with a work station (start of crafting)
|
||||||
|
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
|
instance:showForm(pos, clicker)
|
||||||
|
end
|
||||||
|
|
||||||
|
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
|
||||||
|
instance:abortCraft(pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
callbacks.on_dig = function(pos, node, digger)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
|
||||||
|
local craftName = meta:get_string('craft_name')
|
||||||
|
if craftName == '' then
|
||||||
|
return core.node_dig(pos, node, digger)
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Registering a node for Luanti.
|
||||||
|
ImmersiveWorkStationNode:new(nodeDef)
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
function ImmersiveWorkStation:showForm(pos, player)
|
||||||
|
local playerName = player:get_player_name()
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
|
||||||
|
local craftName = meta:get_string('craft_name')
|
||||||
|
if craftName ~= '' then
|
||||||
|
core.chat_send_player(playerName, 'The workbench is occupied by another craft.')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
PlayerInventoryUI:addActiveNodeForm(pos, playerName)
|
||||||
|
PlayerInventoryUI:setContex(playerName, 'left_tab_name', 'node_tab')
|
||||||
|
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', pos)
|
||||||
|
PlayerInventoryUI:setContex(playerName, 'node_tab', self.tab)
|
||||||
|
|
||||||
|
PlayerInventoryUI:showForm(playerName)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param pos MapVector
|
||||||
|
function ImmersiveWorkStation:init(pos)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param pos MapVector
|
||||||
|
function ImmersiveWorkStation:abortCraft(pos)
|
||||||
|
---@type ImmersiveWorkStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
|
||||||
|
local craftName = meta:get_string('craft_name')
|
||||||
|
if craftName == '' then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local craft = self.handler:getCraftByName(craftName)
|
||||||
|
craft:abortCraft(pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- --- DEBUG ---
|
||||||
|
|
||||||
|
local handler = CraftSystem:getImmersiveCraftHandler('workbench')
|
||||||
|
|
||||||
|
local require = Mod.getInfo('smc__ui__player_inventory').require
|
||||||
|
local baseGetCraftTab = require('PlayerInventoryUI.baseGetCraftTab')
|
||||||
|
|
||||||
|
---@param self PlayerInventoryUI
|
||||||
|
---@param playerName string
|
||||||
|
---@param tabType PlayerInventoryUI.TabType
|
||||||
|
---@return Formspec
|
||||||
|
local function tab(self, playerName, tabType)
|
||||||
|
return baseGetCraftTab(self, playerName, tabType, 'node_tab', handler)
|
||||||
|
end
|
||||||
|
|
||||||
|
ImmersiveWorkStation:new('test_station', {
|
||||||
|
craftType = 'workbench',
|
||||||
|
tab = tab,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
settings = {
|
||||||
|
name = 'test_station',
|
||||||
|
tiles = {'test_station.png'}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
handler:registerCraft('bricks', {
|
||||||
|
output = 'rocks:sylite_bricks',
|
||||||
|
recipe = {'rocks:sylite'},
|
||||||
|
startStageName = 'tapping',
|
||||||
|
group = 'test',
|
||||||
|
stages = {
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping',
|
||||||
|
targetItem = 'rocks:sylite',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 3,
|
||||||
|
nextStage = 'tapping_2',
|
||||||
|
},
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping_2',
|
||||||
|
targetItem = 'rocks:sylite_block',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 5,
|
||||||
|
nextStage = 'tapping_3',
|
||||||
|
},
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping_3',
|
||||||
|
targetItem = 'rocks:sylite_bricks',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 1,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
handler:registerCraft('block', {
|
||||||
|
output = 'rocks:sylite_block',
|
||||||
|
recipe = {'rocks:sylite'},
|
||||||
|
startStageName = 'tapping',
|
||||||
|
group = 'test',
|
||||||
|
stages = {
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping',
|
||||||
|
targetItem = 'rocks:sylite',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 5,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
handler:registerCraft('marentine_planks', {
|
||||||
|
output = 'wood:marentine_planks',
|
||||||
|
recipe = {'rocks:sylite'},
|
||||||
|
startStageName = 'tapping',
|
||||||
|
group = 'test',
|
||||||
|
stages = {
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping',
|
||||||
|
targetItem = 'wood:marentine_log',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 5,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
--]]
|
||||||
|
return ImmersiveWorkStation
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
---@alias ImmersiveWorkStation.MetaData.StringKey 'player_name'|'craft_name'|'stage_name'|'stage_data'
|
||||||
|
---@alias ImmersiveWorkStation.MetaData.FloatKey 'craft_amount'
|
||||||
|
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class ImmersiveWorkStation.MetaData : NodeMetaRef
|
||||||
|
---@field get_string fun(self, key:ImmersiveWorkStation.MetaData.StringKey):string
|
||||||
|
---@field set_string fun(self, key:ImmersiveWorkStation.MetaData.StringKey, value:string)
|
||||||
|
---@field get_float fun(self, key:ImmersiveWorkStation.MetaData.FloatKey):number
|
||||||
|
---@field set_float fun(self, key:ImmersiveWorkStation.MetaData.FloatKey, value:number)
|
||||||
BIN
mods/CORE/ImmersiveWorkStation/textures/test_station.png
Normal file
|
After Width: | Height: | Size: 1.2 KiB |
@@ -1,5 +1,4 @@
|
|||||||
local Item = Mod:new()
|
local modInfo = Mod.getInfo('smc__core__item')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
Item.Class = Item.require('Item')
|
return require('Item')
|
||||||
|
|
||||||
Api.addModToGlobalSpace(Item, 'Core.Item')
|
|
||||||
|
|||||||
9
mods/CORE/Item/locale/item.ru.tr
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# textdomain: item
|
||||||
|
|
||||||
|
Rarity=Редкость
|
||||||
|
common=простой
|
||||||
|
uncommon=необычный
|
||||||
|
distinguished=выдающийся
|
||||||
|
eminent=исключительный
|
||||||
|
prime=Превосходный
|
||||||
|
unique=Уникальный
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
name = smc__core__item
|
name = smc__core__item
|
||||||
depends = smc__api__api_loader, smc__core__enum, smc__core__item_factory
|
depends = smc__system__system_loader, smc__core__item_factory
|
||||||
|
|||||||
@@ -1,98 +1,212 @@
|
|||||||
local modInfo = Mod.getInfo()
|
local tableCopy, tableMerge, colorize = table.copy, table.merge, core.colorize
|
||||||
local require = modInfo.require
|
|
||||||
|
|
||||||
local VisualEnum = require('Item.VisualEnum')
|
local ItemFactory = Smc.requireItemFactory()
|
||||||
|
|
||||||
---@class Item
|
---Colors the title according to the rarity of the item.
|
||||||
---@field itemDef Item.ItemDefinition
|
---
|
||||||
---@field luantiDef table
|
---Note: that if `true`, the `_title_color` will be ignored.
|
||||||
---@field getVisual fun(): string
|
local TITLE_RARITY_COLOR = true
|
||||||
|
|
||||||
|
---Shows rarity on a separate line.
|
||||||
|
local SHOW_RARITY = false
|
||||||
|
|
||||||
|
---Determines whether common rarity will be shown in item descriptions or not.
|
||||||
|
local SHOW_COMMON_RARITY = true
|
||||||
|
|
||||||
|
---Colors corresponding to the rarity color in the item description.
|
||||||
|
---@type table<Item.Rarity, ColorString>
|
||||||
|
local RARITY_COLORS = {
|
||||||
|
['common'] = '#C0C0C0',
|
||||||
|
['uncommon'] = '#7cd2e2',
|
||||||
|
['distinguished'] = '#7ce2da',
|
||||||
|
['eminent'] = '#44d1a9',
|
||||||
|
['prime'] = '#9CF0B1',
|
||||||
|
['unique'] = '#f4c95a'
|
||||||
|
}
|
||||||
|
|
||||||
|
---Whether to show the numeric index in the rarity description.
|
||||||
|
local SHOW_RARITY_INDEX = true
|
||||||
|
|
||||||
|
---@type table<Item.Rarity, integer>
|
||||||
|
local RARITY_INDEX = {
|
||||||
|
['common'] = 1,
|
||||||
|
['uncommon'] = 2,
|
||||||
|
['distinguished'] = 3,
|
||||||
|
['eminent'] = 4,
|
||||||
|
['prime'] = 5,
|
||||||
|
['unique'] = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
---@class Item : Class
|
||||||
|
---@field itemDef Item.ItemDefinition
|
||||||
|
---@field luantiDef ItemDefinition
|
||||||
|
---@field getType fun():string
|
||||||
---@field defaultDef Item.ItemDefinition
|
---@field defaultDef Item.ItemDefinition
|
||||||
local Item = {
|
local Item = Class:extend({
|
||||||
defaultDef = {
|
defaultDef = {
|
||||||
settings = {
|
settings = {
|
||||||
name = "",
|
name = '',
|
||||||
title = "",
|
_title = '',
|
||||||
description = "",
|
_description = '',
|
||||||
|
_title_color = '#FFFFFF',
|
||||||
|
_description_color = '#838383',
|
||||||
|
_rarity = 'common',
|
||||||
|
_add_mod_name = true,
|
||||||
|
stack_max = 128,
|
||||||
},
|
},
|
||||||
callbacks = {}
|
callbacks = {}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
|
---Adds an item's rarity tag to the description.
|
||||||
|
---@param rarity Item.Rarity
|
||||||
|
---@param desc string
|
||||||
|
---@return string
|
||||||
|
local function addRarityToDesc(rarity, desc)
|
||||||
|
if not SHOW_RARITY then
|
||||||
|
return desc
|
||||||
|
end
|
||||||
|
|
||||||
|
if not SHOW_COMMON_RARITY and rarity == 'common' then
|
||||||
|
return desc
|
||||||
|
end
|
||||||
|
|
||||||
|
local S = core.get_translator('item')
|
||||||
|
local color = RARITY_COLORS[rarity]
|
||||||
|
|
||||||
|
local rarityString = S('Rarity')..': '..S(rarity)
|
||||||
|
|
||||||
|
if SHOW_RARITY_INDEX then
|
||||||
|
rarityString = rarityString..' ['..RARITY_INDEX[rarity]..']'
|
||||||
|
end
|
||||||
|
|
||||||
|
return desc..'\n'..colorize(color, rarityString)
|
||||||
|
end
|
||||||
|
|
||||||
---The alias `table.merge` for EmmyLua
|
---The alias `table.merge` for EmmyLua
|
||||||
---@type fun(...):(NodeDefinition|ItemDefinition)
|
---@type fun(...):(NodeDefinition|ItemDefinition)
|
||||||
local defMerge = function(...)
|
local defMerge = function(...)
|
||||||
return table.merge(...)
|
return tableMerge(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param itemDef Item.ItemDefinition
|
---@param itemDef Item.ItemDefinition
|
||||||
---@return NodeDefinition|ItemDefinition
|
---@return NodeDefinition|ItemDefinition
|
||||||
local function itemDefToLuantiDef(itemDef)
|
local function itemDefToLuantiDef(itemDef)
|
||||||
itemDef = table.copy(itemDef)
|
local modName = Mod.getInfo().shortName
|
||||||
|
local S = core.get_translator(modName)
|
||||||
|
|
||||||
local s = itemDef.settings
|
local s = itemDef.settings
|
||||||
local c = itemDef.callbacks
|
local c = itemDef.callbacks
|
||||||
local description = s.title .. "\n" .. s.description
|
|
||||||
s.title, s.description = nil, description
|
|
||||||
|
|
||||||
local luantiDef = defMerge(s, c)
|
local title, description = s._title, s._description
|
||||||
|
|
||||||
|
if title == nil or title == '' then
|
||||||
|
title = string.match(s.name, '%S:(%S+)')..'-title'
|
||||||
|
end
|
||||||
|
|
||||||
|
if description == nil or description == '' then
|
||||||
|
description = string.match(s.name, '%S:(%S+)')..'-desc'
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(s._rarity ~= nil, 'The rarity of an item cannot be empty.')
|
||||||
|
|
||||||
|
if s._title_color ~= nil and s._description_color ~= nil then
|
||||||
|
if TITLE_RARITY_COLOR then
|
||||||
|
title = colorize(RARITY_COLORS[s._rarity], S(title))
|
||||||
|
else
|
||||||
|
title = colorize(s._title_color, S(title))
|
||||||
|
end
|
||||||
|
description = colorize(s._description_color, S(description))
|
||||||
|
else
|
||||||
|
error('Item description color parameters cannot be empty.')
|
||||||
|
end
|
||||||
|
|
||||||
|
local luantiDescription = addRarityToDesc(s._rarity, title..'\n '..description)
|
||||||
|
|
||||||
|
s._title = title
|
||||||
|
s._description = description
|
||||||
|
s.description = luantiDescription..core.get_background_escape_sequence('#110F13')
|
||||||
|
|
||||||
|
local luantiDef = defMerge(s, c, true)
|
||||||
|
|
||||||
return luantiDef
|
return luantiDef
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param luantiDef NodeDefinition|ItemDefinition
|
---@param luantiDef NodeDefinition
|
||||||
local function register3dItem(luantiDef)
|
local function registerNode(luantiDef)
|
||||||
local ld = luantiDef
|
local name = luantiDef.name
|
||||||
local name = ld.name
|
luantiDef.name = nil
|
||||||
ld.name = nil
|
|
||||||
core.register_node(':'..name, luantiDef)
|
core.register_node(':'..name, luantiDef)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param luantiDef ItemDefinition
|
---@param luantiDef ItemDefinition
|
||||||
local function registerFlatItem(luantiDef)
|
local function registerCraftitem(luantiDef)
|
||||||
local ld = luantiDef
|
local name = luantiDef.name
|
||||||
local name = ld.name
|
luantiDef.name = nil
|
||||||
ld.name = nil
|
|
||||||
core.register_craftitem(':'..name, luantiDef)
|
core.register_craftitem(':'..name, luantiDef)
|
||||||
end
|
end
|
||||||
|
|
||||||
---The alias `table.merge` for EmmyLua
|
---@param luantiDef ItemDefinition
|
||||||
---@type fun(...):(Item.ItemDefinition)
|
local function registerTool(luantiDef)
|
||||||
local defMerge = function(...)
|
local name = luantiDef.name
|
||||||
return table.merge(...)
|
luantiDef.name = nil
|
||||||
|
|
||||||
|
core.register_tool(':'..name, luantiDef)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param itemDef Item.ItemDefinition
|
---@param itemDef Item.ItemDefinition|Node.NodeDefinition
|
||||||
---@return Item
|
---@return Item
|
||||||
function Item:new(itemDef)
|
function Item:new(itemDef)
|
||||||
---Adding default parameters
|
assert(itemDef.settings.description == nil,
|
||||||
itemDef = defMerge(itemDef, self.defaultDef, true)
|
'The item description was specified using `description`, not `_description`. '..
|
||||||
|
'Please note that the `description` field should not be used when registering an item. '..
|
||||||
|
'Use `_title` and `_description` instead.')
|
||||||
|
|
||||||
|
itemDef = tableCopy(itemDef)
|
||||||
|
|
||||||
|
-- Adding default parameterss
|
||||||
|
---@type Item.ItemDefinition
|
||||||
|
itemDef = tableMerge(itemDef, self.defaultDef, true)
|
||||||
|
local s = itemDef.settings
|
||||||
|
|
||||||
|
local shortModName = Mod.getInfo().shortName
|
||||||
|
|
||||||
|
if s._add_mod_name then
|
||||||
|
s.name = shortModName .. ':' .. s.name
|
||||||
|
end
|
||||||
|
|
||||||
---@type Item
|
---@type Item
|
||||||
local instance = setmetatable({}, {__index = self})
|
local instance = self:getInstance({})
|
||||||
|
|
||||||
local luantiDef = itemDefToLuantiDef(itemDef)
|
local luantiDef = itemDefToLuantiDef(itemDef)
|
||||||
local visual = itemDef.settings.visual
|
local type = itemDef.settings._type
|
||||||
|
|
||||||
if visual == nil then
|
if type == nil then
|
||||||
visual = VisualEnum.ITEM_2D
|
type = 'craftitem'
|
||||||
end
|
end
|
||||||
|
|
||||||
itemDef.settings.visual = nil
|
itemDef.settings._type = nil
|
||||||
|
|
||||||
if visual == VisualEnum.ITEM_3D then
|
if type == 'node' then
|
||||||
register3dItem(luantiDef)
|
registerNode(luantiDef)
|
||||||
|
elseif type == 'craftitem' then
|
||||||
|
registerCraftitem(luantiDef)
|
||||||
|
elseif type == 'tool' then
|
||||||
|
registerTool(luantiDef)
|
||||||
else
|
else
|
||||||
visual = VisualEnum.ITEM_2D
|
error(('Unknown item type: `%s`.'):format(type))
|
||||||
registerFlatItem(luantiDef)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function instance:getVisual()
|
function instance:getType()
|
||||||
return visual
|
return type
|
||||||
end
|
end
|
||||||
|
|
||||||
instance.itemDef = itemDef
|
instance.itemDef = itemDef
|
||||||
instance.luantiDef = luantiDef
|
instance.luantiDef = luantiDef
|
||||||
|
|
||||||
|
Smc.addRegisteredItem(shortModName, instance)
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -102,8 +216,10 @@ end
|
|||||||
---@param defaultDef Item.ItemDefinition
|
---@param defaultDef Item.ItemDefinition
|
||||||
---@return Item
|
---@return Item
|
||||||
function Item:getExtended(defaultDef)
|
function Item:getExtended(defaultDef)
|
||||||
|
defaultDef = tableMerge(defaultDef, self.defaultDef, true)
|
||||||
|
|
||||||
---@type Item
|
---@type Item
|
||||||
local ChildClass = Mod:getClassExtended(self, {
|
local ChildClass = self:extend({
|
||||||
defaultDef = defaultDef,
|
defaultDef = defaultDef,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -116,10 +232,12 @@ end
|
|||||||
function Item:getFactory(defaultDef)
|
function Item:getFactory(defaultDef)
|
||||||
if defaultDef == nil then
|
if defaultDef == nil then
|
||||||
defaultDef = self.defaultDef
|
defaultDef = self.defaultDef
|
||||||
|
else
|
||||||
|
defaultDef = tableMerge(self.defaultDef, defaultDef, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@type ItemFactory
|
---@type ItemFactory
|
||||||
local instance = Core.ItemFactory():new(self, defaultDef)
|
local instance = ItemFactory:new(self, defaultDef)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
---@class Item.Callbacks
|
---@class Item.Callbacks
|
||||||
local Callbacks = {}
|
local Callbacks = {}
|
||||||
|
|
||||||
-- TODO: Разобраться с дубликатами аннотаций полей. Приоритетно брать аннотации из util/ide-helper
|
|
||||||
|
|
||||||
---Called when the 'place' key was pressed with the item in hand
|
---Called when the 'place' key was pressed with the item in hand
|
||||||
---and pointing at a node.
|
---and pointing at a node.
|
||||||
---Shall place item and return the leftover itemstack
|
---Shall place item and return the leftover itemstack
|
||||||
@@ -66,4 +64,4 @@ Callbacks.on_use = nil
|
|||||||
---@type fun(itemstack:ItemStack, user:Player|ObjectRef|nil, node, digparams)?
|
---@type fun(itemstack:ItemStack, user:Player|ObjectRef|nil, node, digparams)?
|
||||||
Callbacks.after_use = nil
|
Callbacks.after_use = nil
|
||||||
|
|
||||||
return Callbacks
|
return Callbacks
|
||||||
|
|||||||
8
mods/CORE/Item/src/Item/Rarity.lua
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
---Rarity of items:
|
||||||
|
--- * `common`
|
||||||
|
--- * `uncommon`
|
||||||
|
--- * `distinguished`
|
||||||
|
--- * `eminent`
|
||||||
|
--- * `prime`
|
||||||
|
--- * `unique`
|
||||||
|
---@alias Item.Rarity 'common'|'uncommon'|'distinguished'|'eminent'|'prime'|'unique'
|
||||||
@@ -3,21 +3,42 @@ local Settings = {}
|
|||||||
|
|
||||||
---Technical name of the item. Must be unique and have the format `mod_name:item_name`
|
---Technical name of the item. Must be unique and have the format `mod_name:item_name`
|
||||||
---@type string
|
---@type string
|
||||||
Settings.name = ""
|
Settings.name = ''
|
||||||
|
|
||||||
---Item description. 1st line of item description.
|
|
||||||
---@type string?
|
|
||||||
Settings.title = nil
|
|
||||||
|
|
||||||
---Item description. The rest of item description.
|
|
||||||
---@type string?
|
---@type string?
|
||||||
Settings.description = nil
|
Settings.description = nil
|
||||||
|
|
||||||
---Visual node type.
|
---Whether to add the mod name to the settings.name. Default: `true`
|
||||||
---Values are specified via an enumeration `Item.VisualEnum`.
|
---@type boolean?
|
||||||
---@type Enum.EnumKey?
|
Settings._add_mod_name = nil
|
||||||
Settings.visual = ""
|
|
||||||
|
|
||||||
|
---Item description. 1st line of item description.
|
||||||
|
---@type string?
|
||||||
|
Settings._title = nil
|
||||||
|
|
||||||
|
---Item description. The rest of item description.
|
||||||
|
---@type string?
|
||||||
|
Settings._description = nil
|
||||||
|
|
||||||
|
---Item title color in description.
|
||||||
|
---
|
||||||
|
---Default: `#FFFFFF`
|
||||||
|
---@type ColorString?
|
||||||
|
Settings._title_color = nil
|
||||||
|
|
||||||
|
---Item description color in description.
|
||||||
|
---
|
||||||
|
---Default: `#C0C0C0`
|
||||||
|
---@type ColorString?
|
||||||
|
Settings._description_color = nil
|
||||||
|
|
||||||
|
---Visual node type.
|
||||||
|
---@type Item.Type?
|
||||||
|
Settings._type = nil
|
||||||
|
|
||||||
|
---Formal (lore) rarity of an item. Default `common`.
|
||||||
|
---@type Item.Rarity?
|
||||||
|
Settings._rarity = nil
|
||||||
|
|
||||||
|
|
||||||
---Table of groups-characteristics of Item.
|
---Table of groups-characteristics of Item.
|
||||||
@@ -27,6 +48,27 @@ Settings.visual = ""
|
|||||||
---@type table<string,number>?
|
---@type table<string,number>?
|
||||||
Settings.groups = nil
|
Settings.groups = nil
|
||||||
|
|
||||||
|
---The number of inventory slots that can be stored in an ItemStack.
|
||||||
|
---@type integer?
|
||||||
|
Settings._itemstack_inventory_size = nil
|
||||||
|
|
||||||
|
---The name of the texture that will be displayed as the background
|
||||||
|
---for the item when it is equipped on the player as a bag.
|
||||||
|
---@type string?
|
||||||
|
Settings._itemstack_inventory_background = nil
|
||||||
|
|
||||||
|
---Defines a list of group names that items
|
||||||
|
---with which can be placed in an item's inventory.
|
||||||
|
---
|
||||||
|
---If it is `nil`, then any items will be allowed except those disallowed in `_itemstack_inventory_disallowed_groups`.
|
||||||
|
---@type string[]?
|
||||||
|
Settings._itemstack_inventory_allowed_groups = nil
|
||||||
|
|
||||||
|
---Defines a list of group names that items
|
||||||
|
---with which cannot be placed in an item's inventory.
|
||||||
|
---@type string[]?
|
||||||
|
Settings._itemstack_inventory_disallowed_groups = nil
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
---Texture shown in the inventory GUI.
|
---Texture shown in the inventory GUI.
|
||||||
@@ -164,4 +206,4 @@ Settings.touch_interaction = nil
|
|||||||
Settings.sound = nil
|
Settings.sound = nil
|
||||||
|
|
||||||
|
|
||||||
return Settings
|
return Settings
|
||||||
|
|||||||
1
mods/CORE/Item/src/Item/Type.lua
Normal file
@@ -0,0 +1 @@
|
|||||||
|
---@alias Item.Type 'craftitem'|'node'|'tool'
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
---@class Item.VisualEnum : Enum
|
|
||||||
---@field ITEM_2D Enum.EnumKey
|
|
||||||
---@field ITEM_3D Enum.EnumKey
|
|
||||||
local VisualEnum = Core.Enum:getModClassInstance({
|
|
||||||
'ITEM_2D',
|
|
||||||
'ITEM_3D',
|
|
||||||
})
|
|
||||||
|
|
||||||
return VisualEnum
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
local ItemFactory = Mod:new()
|
local modInfo = Mod.getInfo('smc__core__item_factory')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
ItemFactory.Class = ItemFactory.require('ItemFactory')
|
return require('ItemFactory')
|
||||||
|
|
||||||
Api.addModToGlobalSpace(ItemFactory, 'Core.ItemFactory')
|
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
name = smc__core__item_factory
|
name = smc__core__item_factory
|
||||||
depends = smc__api__api_loader
|
depends = smc__system__system_loader
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
---@class ItemFactory
|
---@class ItemFactory
|
||||||
---@field defaultDef Item.ItemDefinition|Node.NodeDefinition
|
---@field defaultDef Item.ItemDefinition|Node.NodeDefinition
|
||||||
---@field itemClass Item|Node
|
---@field itemClass Item|Node
|
||||||
@@ -8,10 +10,10 @@ local ItemFactory = {}
|
|||||||
---@return ItemFactory
|
---@return ItemFactory
|
||||||
function ItemFactory:new(itemClass, defaultDef)
|
function ItemFactory:new(itemClass, defaultDef)
|
||||||
---@type ItemFactory
|
---@type ItemFactory
|
||||||
local instance = setmetatable({
|
local instance =getInstance(self, {
|
||||||
defaultDef = defaultDef,
|
defaultDef = defaultDef,
|
||||||
itemClass = itemClass,
|
itemClass = itemClass,
|
||||||
}, {__index = self})
|
})
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
@@ -22,23 +24,12 @@ local defMerge = function(...)
|
|||||||
return table.merge(...)
|
return table.merge(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param itemDefs Item.ItemDefinition[]|Node.NodeDefinition[] List of nodes with parameters unique to them.
|
---@param itemDefs Item.ItemDefinition[]|Node.NodeDefinition[] List of nodes with parameters unique to them.
|
||||||
---@param addModName boolean? Whether to add the mod name to the settings.name. Default: `true`
|
function ItemFactory:registerItems(itemDefs)
|
||||||
function ItemFactory:registerItems(itemDefs, addModName)
|
|
||||||
if addModName == nil then
|
|
||||||
addModName = true
|
|
||||||
end
|
|
||||||
|
|
||||||
local modName = Mod.getInfo().shortName
|
|
||||||
|
|
||||||
---@param nodeDef Item.ItemDefinition | Node.NodeDefinition
|
---@param nodeDef Item.ItemDefinition | Node.NodeDefinition
|
||||||
for _, itemDef in ipairs(itemDefs) do
|
for _, itemDef in ipairs(itemDefs) do
|
||||||
itemDef = defMerge(itemDef, self.defaultDef, true)
|
itemDef = defMerge(itemDef, self.defaultDef, true)
|
||||||
|
|
||||||
if addModName then
|
|
||||||
itemDef.settings.name = modName .. ':' .. itemDef.settings.name
|
|
||||||
end
|
|
||||||
|
|
||||||
self.itemClass:new(itemDef)
|
self.itemClass:new(itemDef)
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -50,34 +41,20 @@ local defMerge = function(...)
|
|||||||
return table.merge(...)
|
return table.merge(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param shortNodeDefs table Table of the form `{ {name, title, description, tiles}, {name, title, description, tiles}, ...}`
|
---@param shortNodeDefs [string, (string[]|TileDefinition[])?][] Format list {{name, tiles}, {name, tiles}, ...}
|
||||||
---@param addModName boolean? Whether to add the mod name to the settings.name. Default: `true`
|
function ItemFactory:registerNodesByShortDef(shortNodeDefs)
|
||||||
function ItemFactory:registerNodesByShortDef(shortNodeDefs, addModName)
|
|
||||||
if addModName == nil then
|
|
||||||
addModName = true
|
|
||||||
end
|
|
||||||
|
|
||||||
local modName = Mod.getInfo().shortName
|
|
||||||
|
|
||||||
for _, shortNodeDef in ipairs(shortNodeDefs) do
|
for _, shortNodeDef in ipairs(shortNodeDefs) do
|
||||||
---@type Node.NodeDefinition
|
---@type Node.NodeDefinition
|
||||||
local nodeDef = {
|
local nodeDef = {
|
||||||
settings = {
|
settings = {
|
||||||
name = shortNodeDef[1],
|
name = shortNodeDef[1],
|
||||||
title = shortNodeDef[2],
|
tiles = shortNodeDef[2]
|
||||||
description = shortNodeDef[3],
|
|
||||||
tiles = shortNodeDef[4]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeDef = defMerge(nodeDef, self.defaultDef, true)
|
nodeDef = defMerge(nodeDef, self.defaultDef, true)
|
||||||
|
|
||||||
if addModName then
|
|
||||||
nodeDef.settings.name = modName .. ':' .. nodeDef.settings.name
|
|
||||||
end
|
|
||||||
|
|
||||||
self.itemClass:new(nodeDef)
|
self.itemClass:new(nodeDef)
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
local MapGen = Mod:new()
|
local modInfo = Mod.getInfo('smc__core__map_gen')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
MapGen.Class = MapGen.require('MapGen')
|
return require('MapGen')
|
||||||
|
|
||||||
Api.addModToGlobalSpace(MapGen, 'Core.MapGen')
|
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
name = smc__core__map_gen
|
name = smc__core__map_gen
|
||||||
depends = smc__api__api_loader, smc__game__items__rocks, smc__game__items__liquids
|
depends = smc__system__system_loader
|
||||||
@@ -1,64 +1,138 @@
|
|||||||
|
local id = core.get_content_id
|
||||||
|
|
||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
---@class MapGen.Biome
|
---@class MapGen.Biome
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field tempPoint number
|
---@field tempPoint number
|
||||||
---@field humidityPoint number
|
---@field humidityPoint number
|
||||||
---@field minY number
|
---@field minY number
|
||||||
---@field maxY number
|
---@field maxY number
|
||||||
---@field groundNodes MapGen.Biome.GroundNodes
|
---@field groundNodes MapGen.Biome.GroundNodes
|
||||||
---@field soilHeight number
|
---@field groundNodesIDs MapGen.Biome.GroundNodesIDs
|
||||||
---@field generateRock fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)
|
---@field soilHeight number
|
||||||
---@field generateSoil fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)
|
---@field generateAir generateFunc
|
||||||
---@field generateBottom fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)
|
---@field generateCavernAir generateFunc
|
||||||
|
---@field generateWater generateFunc
|
||||||
|
---@field generateRock generateFunc
|
||||||
|
---@field generateTurf generateFunc
|
||||||
|
---@field generateSoil generateFunc
|
||||||
|
---@field generateBottom generateFunc
|
||||||
|
---@field postProcessing fun(self, mapGenerator:MapGen, factors:MapGen.factors, data:number[], data2:number[], index:number, x:number, y:number, z:number, nodeType:MapGen.nodeType, area:VoxelArea)?
|
||||||
local Biome = {}
|
local Biome = {}
|
||||||
|
|
||||||
|
---@alias generateFunc fun(self, mapGenerator:MapGen, factors:MapGen.factors, data:number[], data2:number[], index:number, x:number, y:number, z:number)
|
||||||
|
|
||||||
---Definition table for the `MapGen.Biome`.
|
---Definition table for the `MapGen.Biome`.
|
||||||
---
|
---
|
||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class MapGen.BiomeDef
|
---@class MapGen.BiomeDef
|
||||||
---@field tempPoint number
|
---@field tempPoint number
|
||||||
---@field humidityPoint number
|
---@field humidityPoint number
|
||||||
---@field minY number
|
---@field minY number
|
||||||
---@field maxY number
|
---@field maxY number
|
||||||
---@field groundNodes MapGen.Biome.GroundNodes
|
---@field groundNodes MapGen.Biome.GroundNodes
|
||||||
---@field soilHeight number
|
---@field soilHeight number
|
||||||
---@field generateRock fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)?
|
---@field generateAir generateFunc?
|
||||||
---@field generateSoil fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)?
|
---@field generateCavernAir generateFunc?
|
||||||
---@field generateBottom fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)?
|
---@field generateWater generateFunc?
|
||||||
|
---@field generateRock generateFunc?
|
||||||
|
---@field generateTurf generateFunc?
|
||||||
|
---@field generateSoil generateFunc?
|
||||||
|
---@field generateBottom generateFunc?
|
||||||
|
---@field postProcessing fun(self, mapGenerator:MapGen, factors:MapGen.factors, data:number[], data2:number[], index:number, x:number, y:number, z:number, nodeType:MapGen.nodeType, area:VoxelArea)?
|
||||||
|
|
||||||
-- Default generate functions
|
-- Default generate functions
|
||||||
|
|
||||||
|
---@param self MapGen.Biome
|
||||||
---@param mapGenerator MapGen
|
---@param mapGenerator MapGen
|
||||||
---@param biome MapGen.Biome
|
---@param factors MapGen.factors
|
||||||
---@param data number[]
|
---@param data number[]
|
||||||
|
---@param data2 number[]
|
||||||
---@param index number
|
---@param index number
|
||||||
---@param x number
|
---@param x number
|
||||||
---@param y number
|
---@param y number
|
||||||
---@param z number
|
---@param z number
|
||||||
local defaultGenerateRock = function(mapGenerator, biome, data, index, x, y, z)
|
local defaultGenerateAir = function(self, mapGenerator, factors, data, data2, index, x, y, z)
|
||||||
--TODO: минимальную логику генерации прописать
|
data[index] = mapGenerator.nodeIDs.air
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param self MapGen.Biome
|
||||||
---@param mapGenerator MapGen
|
---@param mapGenerator MapGen
|
||||||
---@param biome MapGen.Biome
|
---@param factors MapGen.factors
|
||||||
---@param data number[]
|
---@param data number[]
|
||||||
|
---@param data2 number[]
|
||||||
---@param index number
|
---@param index number
|
||||||
---@param x number
|
---@param x number
|
||||||
---@param y number
|
---@param y number
|
||||||
---@param z number
|
---@param z number
|
||||||
local defaultGenerateSoil = function(mapGenerator, biome, data, index, x, y, z)
|
local defaultGenerateCavernAir = function(self, mapGenerator, factors, data, data2, index, x, y, z)
|
||||||
--TODO: минимальную логику генерации прописать
|
self:generateAir(mapGenerator, factors, data, data2, index, x, y, z)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param self MapGen.Biome
|
||||||
---@param mapGenerator MapGen
|
---@param mapGenerator MapGen
|
||||||
---@param biome MapGen.Biome
|
---@param factors MapGen.factors
|
||||||
---@param data number[]
|
---@param data number[]
|
||||||
|
---@param data2 number[]
|
||||||
---@param index number
|
---@param index number
|
||||||
---@param x number
|
---@param x number
|
||||||
---@param y number
|
---@param y number
|
||||||
---@param z number
|
---@param z number
|
||||||
local defaultGenerateBottom = function(mapGenerator, biome, data, index, x, y, z)
|
local defaultGenerateWater = function(self, mapGenerator, factors, data, data2, index, x, y, z)
|
||||||
--TODO: минимальную логику генерации прописать
|
data[index] = mapGenerator.nodeIDs.water
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param self MapGen.Biome
|
||||||
|
---@param mapGenerator MapGen
|
||||||
|
---@param factors MapGen.factors
|
||||||
|
---@param data number[]
|
||||||
|
---@param data2 number[]
|
||||||
|
---@param index number
|
||||||
|
---@param x number
|
||||||
|
---@param y number
|
||||||
|
---@param z number
|
||||||
|
local defaultGenerateRock = function(self, mapGenerator, factors, data, data2, index, x, y, z)
|
||||||
|
data[index] = self.groundNodesIDs.rock
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param self MapGen.Biome
|
||||||
|
---@param mapGenerator MapGen
|
||||||
|
---@param factors MapGen.factors
|
||||||
|
---@param data number[]
|
||||||
|
---@param data2 number[]
|
||||||
|
---@param index number
|
||||||
|
---@param x number
|
||||||
|
---@param y number
|
||||||
|
---@param z number
|
||||||
|
local defaultGenerateTurf = function(self, mapGenerator, factors, data, data2, index, x, y, z)
|
||||||
|
data[index] = self.groundNodesIDs.turf
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param self MapGen.Biome
|
||||||
|
---@param mapGenerator MapGen
|
||||||
|
---@param factors MapGen.factors
|
||||||
|
---@param data number[]
|
||||||
|
---@param data2 number[]
|
||||||
|
---@param index number
|
||||||
|
---@param x number
|
||||||
|
---@param y number
|
||||||
|
---@param z number
|
||||||
|
local defaultGenerateSoil = function(self, mapGenerator, factors, data, data2, index, x, y, z)
|
||||||
|
data[index] = self.groundNodesIDs.soil
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param self MapGen.Biome
|
||||||
|
---@param mapGenerator MapGen
|
||||||
|
---@param factors MapGen.factors
|
||||||
|
---@param data number[]
|
||||||
|
---@param data2 number[]
|
||||||
|
---@param index number
|
||||||
|
---@param x number
|
||||||
|
---@param y number
|
||||||
|
---@param z number
|
||||||
|
local defaultGenerateBottom = function(self, mapGenerator, factors, data, data2, index, x, y, z)
|
||||||
|
data[index] = self.groundNodesIDs.bottom
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -66,34 +140,65 @@ end
|
|||||||
---@param def MapGen.BiomeDef
|
---@param def MapGen.BiomeDef
|
||||||
---@return MapGen.Biome
|
---@return MapGen.Biome
|
||||||
function Biome:new(name, def)
|
function Biome:new(name, def)
|
||||||
|
if def.generateAir == nil then
|
||||||
|
Logger.infoLog('MapGen.Biome: The `%s` biome does not have a specified `generateAir()` function. The default function is used.', name)
|
||||||
|
def.generateAir = defaultGenerateAir
|
||||||
|
end
|
||||||
|
|
||||||
|
if def.generateCavernAir == nil then
|
||||||
|
Logger.infoLog('MapGen.Biome: The `%s` biome does not have a specified `generateCavernAir()` function. The default function is used.', name)
|
||||||
|
def.generateCavernAir = defaultGenerateCavernAir
|
||||||
|
end
|
||||||
|
|
||||||
|
if def.generateWater == nil then
|
||||||
|
Logger.infoLog('MapGen.Biome: The `%s` biome does not have a specified `generateWater()` function. The default function is used.', name)
|
||||||
|
def.generateWater = defaultGenerateWater
|
||||||
|
end
|
||||||
|
|
||||||
if def.generateRock == nil then
|
if def.generateRock == nil then
|
||||||
Logger.warningLog('MapGen.Biome: The `%s` biome does not have a specified `generateRock()` function. The default function is used.', name)
|
Logger.infoLog('MapGen.Biome: The `%s` biome does not have a specified `generateRock()` function. The default function is used.', name)
|
||||||
def.generateRock = defaultGenerateRock
|
def.generateRock = defaultGenerateRock
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if def.generateTurf == nil then
|
||||||
|
Logger.infoLog('MapGen.Biome: The `%s` biome does not have a specified `generateTurf()` function. The default function is used.', name)
|
||||||
|
def.generateTurf = defaultGenerateTurf
|
||||||
|
end
|
||||||
|
|
||||||
if def.generateSoil == nil then
|
if def.generateSoil == nil then
|
||||||
Logger.warningLog('MapGen.Biome: The `%s` biome does not have a specified `generateSoil()` function. The default function is used.', name)
|
Logger.infoLog('MapGen.Biome: The `%s` biome does not have a specified `generateSoil()` function. The default function is used.', name)
|
||||||
def.generateSoil = defaultGenerateSoil
|
def.generateSoil = defaultGenerateSoil
|
||||||
end
|
end
|
||||||
|
|
||||||
if def.generateBottom == nil then
|
if def.generateBottom == nil then
|
||||||
Logger.warningLog('MapGen.Biome: The `%s` biome does not have a specified `generateBottom()` function. The default function is used.', name)
|
Logger.infoLog('MapGen.Biome: The `%s` biome does not have a specified `generateBottom()` function. The default function is used.', name)
|
||||||
def.generateBottom = defaultGenerateBottom
|
def.generateBottom = defaultGenerateBottom
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local groundNodesIDs = {}
|
||||||
|
-- Converting node names to IDs
|
||||||
|
for k, v in pairs(def.groundNodes) do
|
||||||
|
groundNodesIDs[k] = id(v)
|
||||||
|
end
|
||||||
|
|
||||||
---@type MapGen.Biome
|
---@type MapGen.Biome
|
||||||
local instance = setmetatable({
|
local instance = getInstance(self, {})
|
||||||
name = name,
|
instance.name = name
|
||||||
tempPoint = def.tempPoint,
|
instance.tempPoint = def.tempPoint
|
||||||
humidityPoint = def.humidityPoint,
|
instance.humidityPoint = def.humidityPoint
|
||||||
minY = def.minY,
|
instance.minY = def.minY
|
||||||
maxY = def.maxY,
|
instance.maxY = def.maxY
|
||||||
groundNodes = def.groundNodes,
|
instance.groundNodes = def.groundNodes
|
||||||
soilHeight = def.soilHeight,
|
instance.groundNodesIDs = groundNodesIDs
|
||||||
generateRock = def.generateRock or defaultGenerateRock,
|
instance.soilHeight = def.soilHeight
|
||||||
generateSoil = def.generateSoil or defaultGenerateSoil,
|
instance.generateAir = def.generateAir
|
||||||
generateBottom = def.generateBottom or defaultGenerateBottom,
|
instance.generateCavernAir = def.generateCavernAir
|
||||||
}, {__index = self})
|
instance.generateWater = def.generateWater
|
||||||
|
instance.generateRock = def.generateRock
|
||||||
|
instance.generateSoil = def.generateSoil
|
||||||
|
instance.generateTurf = def.generateTurf
|
||||||
|
instance.generateBottom = def.generateBottom
|
||||||
|
instance.postProcessing = def.postProcessing
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class MapGen.Biome.GroundNodes
|
---@class MapGen.Biome.GroundNodes
|
||||||
---@field turf string The soil covering the surface of the biome
|
---@field turf string The soil covering the surface of the biome
|
||||||
---@field soil string The soil beneath the turf. The height of this layer depends on `Biome.soilHeight`
|
---@field soil string The soil beneath the turf. The height of this layer depends on `Biome.soilHeight`
|
||||||
|
---@field rock string The rock found beneath the soil of a biome.
|
||||||
|
---@field bottom string Covering the bottom of reservoirs.
|
||||||
local GroundNodes = {}
|
local GroundNodes = {}
|
||||||
|
|
||||||
return GroundNodes
|
return GroundNodes
|
||||||
|
|||||||
11
mods/CORE/MapGen/src/MapGen/Biome/GroundNodesIds.lua
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
--- Only for EmmyLua
|
||||||
|
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class MapGen.Biome.GroundNodesIDs
|
||||||
|
---@field turf number The soil covering the surface of the biome
|
||||||
|
---@field soil number The soil beneath the turf. The height of this layer depends on `Biome.soilHeight`
|
||||||
|
---@field rock number The rock found beneath the soil of a biome.
|
||||||
|
---@field bottom number Covering the bottom of reservoirs.
|
||||||
|
local GroundNodes = {}
|
||||||
|
|
||||||
|
return GroundNodes
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
local mathMin, mathMax, mathAbs
|
local mathMin, mathMax, mathAbs, mathClamp
|
||||||
= math.min, math.max, math.abs
|
= math.min, math.max, math.abs, math.clamp
|
||||||
|
|
||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
---@class MapGen.Cavern
|
---@class MapGen.Cavern
|
||||||
---@field name string
|
---@field name string
|
||||||
|
---@field isRegional boolean
|
||||||
---@field minY number
|
---@field minY number
|
||||||
---@field maxY number
|
---@field maxY number
|
||||||
---@field smoothDistance number
|
---@field smoothDistance number
|
||||||
@@ -16,6 +19,7 @@ local Cavern = {}
|
|||||||
---
|
---
|
||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class MapGen.CavernDef
|
---@class MapGen.CavernDef
|
||||||
|
---@field isRegional boolean?
|
||||||
---@field minY number
|
---@field minY number
|
||||||
---@field maxY number
|
---@field maxY number
|
||||||
---@field noiseParams NoiseParams
|
---@field noiseParams NoiseParams
|
||||||
@@ -50,13 +54,14 @@ function Cavern:new(name, def)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@type MapGen.Cavern
|
---@type MapGen.Cavern
|
||||||
local instance = setmetatable({
|
local instance = getInstance(self, {
|
||||||
name = name,
|
name = name,
|
||||||
minY = def.minY,
|
minY = def.minY,
|
||||||
maxY = def.maxY,
|
maxY = def.maxY,
|
||||||
smoothDistance = def.smoothDistance,
|
smoothDistance = def.smoothDistance,
|
||||||
threshold = def.threshold,
|
threshold = def.threshold,
|
||||||
}, {__index = self})
|
isRegional = def.isRegional or false,
|
||||||
|
})
|
||||||
|
|
||||||
function instance:getNoise()
|
function instance:getNoise()
|
||||||
return _noise
|
return _noise
|
||||||
@@ -77,32 +82,49 @@ function Cavern:new(name, def)
|
|||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param y number
|
---@param distance number
|
||||||
---@param minY number
|
---@return number
|
||||||
---@param maxY number
|
function Cavern:calcCavernWeight(distance)
|
||||||
---@param smoothDistance number
|
return mathClamp(distance / self.smoothDistance, 0, 1)
|
||||||
---@return number
|
|
||||||
local function calcCavernWeight(y, smoothDistance, minY, maxY)
|
|
||||||
local maxDist = maxY - y
|
|
||||||
local minDist = y - minY
|
|
||||||
|
|
||||||
return mathMax(0, mathMin(maxDist / smoothDistance, minDist / smoothDistance, 1))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns `true` if there is a cavern at the specified coordinates.
|
---Returns `true` if there is a cavern at the specified coordinates.
|
||||||
---@param x number
|
---@param x integer
|
||||||
---@param y number
|
---@param y integer
|
||||||
---@param z number
|
---@param z integer
|
||||||
---@return boolean
|
---@param distance number
|
||||||
function Cavern:isCavern(x, y, z)
|
---@return boolean
|
||||||
|
function Cavern:isCavern(x, y, z, distance)
|
||||||
|
local minY, maxY = self.minY, self.maxY
|
||||||
|
|
||||||
|
if y >= maxY or y <= minY then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
distance = mathMin(maxY - y, y - minY, distance)
|
||||||
|
|
||||||
|
local noise = self:getNoise()
|
||||||
|
local weight = self:calcCavernWeight(distance)
|
||||||
|
|
||||||
|
return noise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns `true` if there is a cavern at the specified coordinates.
|
||||||
|
---@param x integer
|
||||||
|
---@param y integer
|
||||||
|
---@param z integer
|
||||||
|
---@return boolean
|
||||||
|
function Cavern:isLayeredCavern(x, y, z)
|
||||||
local minY, maxY = self.minY, self.maxY
|
local minY, maxY = self.minY, self.maxY
|
||||||
|
|
||||||
if y > maxY or y < minY then
|
if y > maxY or y < minY then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local distance = mathMin(maxY - y, y - minY)
|
||||||
|
|
||||||
local noise = self:getNoise()
|
local noise = self:getNoise()
|
||||||
local weight = calcCavernWeight(y, self.smoothDistance, minY, maxY)
|
local weight = self:calcCavernWeight(distance)
|
||||||
|
|
||||||
return noise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
return noise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
---**NOT IMPLEMENTED**
|
---**NOT IMPLEMENTED**
|
||||||
---@class MapGen.Decoration
|
---@class MapGen.Decoration
|
||||||
local Decoration = {}
|
local Decoration = {}
|
||||||
@@ -5,9 +7,9 @@ local Decoration = {}
|
|||||||
---@return MapGen.Decoration
|
---@return MapGen.Decoration
|
||||||
function Decoration:new()
|
function Decoration:new()
|
||||||
---@type MapGen.Decoration
|
---@type MapGen.Decoration
|
||||||
local instance = setmetatable({
|
local instance = getInstance(self, {
|
||||||
|
|
||||||
}, {__index = self})
|
})
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,19 +1,22 @@
|
|||||||
---@class MapGen.Triangulation.Edge
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
---@class MapGen.Edge
|
||||||
---@field p1 MapGen.Peak
|
---@field p1 MapGen.Peak
|
||||||
---@field p2 MapGen.Peak
|
---@field p2 MapGen.Peak
|
||||||
|
---@field vec vector
|
||||||
local Edge = {}
|
local Edge = {}
|
||||||
|
|
||||||
---@param p1 MapGen.Peak
|
---@param p1 MapGen.Peak
|
||||||
---@param p2 MapGen.Peak
|
---@param p2 MapGen.Peak
|
||||||
---@return MapGen.Triangulation.Edge
|
---@return MapGen.Edge
|
||||||
function Edge:new( p1, p2 )
|
function Edge:new( p1, p2 )
|
||||||
---@type MapGen.Triangulation.Edge
|
---@type MapGen.Edge
|
||||||
local instance = setmetatable({
|
local instance = getInstance(self, {
|
||||||
p1 = p1,
|
p1 = p1,
|
||||||
p2 = p2
|
p2 = p2,
|
||||||
|
vec = p2.pos - p1.pos
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
__index = self,
|
|
||||||
__tostring = self.toString,
|
__tostring = self.toString,
|
||||||
__eq = self.eq
|
__eq = self.eq
|
||||||
})
|
})
|
||||||
@@ -28,14 +31,14 @@ function Edge:toString()
|
|||||||
end
|
end
|
||||||
|
|
||||||
---Returns true if the edges are equivalent.
|
---Returns true if the edges are equivalent.
|
||||||
---@param other MapGen.Triangulation.Edge
|
---@param other MapGen.Edge
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Edge:eq(other)
|
function Edge:eq(other)
|
||||||
return self.p1 == other.p1 and self.p2 == other.p2
|
return self.p1 == other.p1 and self.p2 == other.p2
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns true if the edges are similar.
|
---Returns true if the edges are similar.
|
||||||
---@param other MapGen.Triangulation.Edge
|
---@param other MapGen.Edge
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Edge:same(other)
|
function Edge:same(other)
|
||||||
return (self.p1 == other.p1 and self.p2 == other.p2)
|
return (self.p1 == other.p1 and self.p2 == other.p2)
|
||||||
@@ -45,14 +48,14 @@ end
|
|||||||
---Returns the length of an edge.
|
---Returns the length of an edge.
|
||||||
---@return number
|
---@return number
|
||||||
function Edge:length()
|
function Edge:length()
|
||||||
return (self.p1.getPeakPos() - self.p2.getPeakPos()):length()
|
return (self.p1.pos - self.p2.pos):length()
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns the vector of the point located in the middle of the edge.
|
---Returns the vector of the point located in the middle of the edge.
|
||||||
---@return vector
|
---@return vector
|
||||||
function Edge:getMidPoint()
|
function Edge:getMidPoint()
|
||||||
local pos1 = self.p1:getPeakPos()
|
local pos1 = self.p1.pos
|
||||||
local pos2 = self.p2:getPeakPos()
|
local pos2 = self.p2.pos
|
||||||
|
|
||||||
return (pos1 + pos2 )/2
|
return (pos1 + pos2 )/2
|
||||||
end
|
end
|
||||||
@@ -7,18 +7,17 @@ local Peak = require('MapGen.Peak')
|
|||||||
---Allows you to create a `MapGen.Peak` without specifying a noise parameter.
|
---Allows you to create a `MapGen.Peak` without specifying a noise parameter.
|
||||||
---
|
---
|
||||||
---**WARNING: MUST NOT BE PARTICIPATED IN GENERATION**
|
---**WARNING: MUST NOT BE PARTICIPATED IN GENERATION**
|
||||||
---@class MapGen.Triangulation.FakePeak : MapGen.Peak
|
---@class MapGen.FakePeak : MapGen.Peak
|
||||||
---@field id number
|
---@field id number
|
||||||
---@field getPeakPos fun():vector
|
---@field getPeakPos fun():vector
|
||||||
local FakePeak = Mod:getClassExtended(Peak, {})
|
local FakePeak = Class.extend(Peak, {})
|
||||||
|
|
||||||
---Returns a fake `MapGen.Peak` that has no noise parameters.
|
---Returns a fake `MapGen.Peak` that has no noise parameters.
|
||||||
---Note that the initialized peak should not be used in generation.
|
---Note that the initialized peak should not be used in generation.
|
||||||
---@param peakPos vector
|
---@param peakPos MapVector
|
||||||
---@return MapGen.Peak
|
---@return MapGen.Peak
|
||||||
function FakePeak:new(peakPos)
|
function FakePeak:new(peakPos)
|
||||||
---@diagnostic disable-next-line: missing-fields, param-type-not-match
|
local instance = Peak:new({pos = peakPos, color = '#000000'})
|
||||||
local instance = Peak:new(peakPos, {}, 1)
|
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
@@ -1,63 +1,350 @@
|
|||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
local modInfo = Mod.getInfo('smc__core__map_gen')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
---@type MapGen.Peak
|
||||||
|
local Peak = require('MapGen.Peak')
|
||||||
|
---@type MapGen.Triangle
|
||||||
|
local Triangle = require('MapGen.Triangle')
|
||||||
|
|
||||||
---@class MapGen.Layer
|
---@class MapGen.Layer
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field minY number
|
---@field minY integer
|
||||||
---@field maxY number
|
---@field maxY integer
|
||||||
---@field peaksList MapGen.Peak[]
|
---@field minTemp integer
|
||||||
---@field biomesByName table<string, MapGen.Biome>
|
---@field maxTemp integer
|
||||||
---@field biomesList MapGen.Biome[]
|
---@field minHumidity integer
|
||||||
---@field biomesDiagram table
|
---@field maxHumidity integer
|
||||||
---@field cavernsByName table<string, MapGen.Cavern>
|
---@field waterLevel integer
|
||||||
---@field cavernsList MapGen.Cavern[]
|
---@field calcTemp fun(self:MapGen.Layer, value:number, height:number):number
|
||||||
---@field trianglesList MapGen.Triangle[]
|
---@field calcHumidity fun(self:MapGen.Layer, value:number, height:number):number
|
||||||
---@field tetrahedronsList MapGen.Tetrahedron[]
|
---@field peaksList MapGen.Peak[]
|
||||||
|
---@field peaksByPos table<string, MapGen.Peak>
|
||||||
|
---@field noisesByColor table<MapGen.Layer.Category, table<ColorString, ValueNoise>>
|
||||||
|
---@field noisesParamsByColor table<MapGen.Layer.Category, table<ColorString, NoiseParams>>
|
||||||
|
---@field biomesByName table<string, MapGen.Biome>
|
||||||
|
---@field biomesList MapGen.Biome[]
|
||||||
|
---@field biomesDiagram table
|
||||||
|
---@field biomesGorizontalScattering integer
|
||||||
|
---@field biomesVerticalScattering integer
|
||||||
|
---@field cavernsByName table<string, MapGen.Cavern>
|
||||||
|
---@field cavernsList MapGen.Cavern[]
|
||||||
|
---@field regionsByName table<string, MapGen.Region>
|
||||||
|
---@field trianglesList MapGen.Triangle[]
|
||||||
local Layer = {
|
local Layer = {
|
||||||
peaksList = {},
|
peaksList = {},
|
||||||
|
peaksByPos = {},
|
||||||
|
noisesByColor = {
|
||||||
|
landscape = {},
|
||||||
|
temp = {},
|
||||||
|
humidity = {},
|
||||||
|
},
|
||||||
|
noisesParamsByColor = {
|
||||||
|
landscape = {},
|
||||||
|
temp = {},
|
||||||
|
humidity = {},
|
||||||
|
},
|
||||||
biomesByName = {},
|
biomesByName = {},
|
||||||
biomesList = {},
|
biomesList = {},
|
||||||
biomesDiagram = {},
|
biomesDiagram = {},
|
||||||
cavernsByName = {},
|
cavernsByName = {},
|
||||||
cavernsList = {},
|
cavernsList = {},
|
||||||
|
regionsByName = {},
|
||||||
trianglesList = {},
|
trianglesList = {},
|
||||||
tetrahedronsList = {},
|
tetrahedronsList = {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---Definition table for the `MapGen.Layer`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class MapGen.LayerDef
|
||||||
|
---@field minY integer
|
||||||
|
---@field maxY integer
|
||||||
|
---@field minTemp integer
|
||||||
|
---@field maxTemp integer
|
||||||
|
---@field minHumidity integer
|
||||||
|
---@field maxHumidity integer
|
||||||
|
---@field waterLevel? integer
|
||||||
|
---@field calcTemp (fun(self:MapGen.Layer, value:number, height:number):number)?
|
||||||
|
---@field calcHumidity (fun(self:MapGen.Layer, value:number, height:number):number)?
|
||||||
|
---@field biomesGorizontalScattering integer?
|
||||||
|
---@field biomesVerticalScattering integer?
|
||||||
|
---@field triangulationLandscapeFile string
|
||||||
|
---@field triangulationTempFile string
|
||||||
|
---@field triangulationHumidityFile string
|
||||||
|
---@field landscapeMapScale integer
|
||||||
|
---@field tempMapScale integer
|
||||||
|
---@field humidityMapScale integer
|
||||||
|
|
||||||
|
---@param self MapGen.Layer
|
||||||
|
---@param value number
|
||||||
|
---@param height number
|
||||||
|
---@return number
|
||||||
|
local function defaultCalcFunc(self, value, height)
|
||||||
|
return value
|
||||||
|
end
|
||||||
|
|
||||||
|
---Checks `def` for compliance with `MapGen.PeakDef` and creates a new peak
|
||||||
|
---if a peak with such coordinates has not yet been registered or returns an existing one.
|
||||||
|
---
|
||||||
|
---**Note:** If the colors of the new peak and the existing one at the same coordinates
|
||||||
|
---are different, an error will be called.
|
||||||
|
---@param def MapGen.PeakDef
|
||||||
|
---@param scale integer
|
||||||
|
---@return MapGen.Peak
|
||||||
|
function Layer:registerPeak(def, scale)
|
||||||
|
assert(def.pos ~= nil)
|
||||||
|
assert(def.pos.x ~= nil)
|
||||||
|
assert(def.pos.z ~= nil)
|
||||||
|
assert(def.color ~= nil)
|
||||||
|
assert(type(def.color) == 'string')
|
||||||
|
|
||||||
|
def.pos.x = def.pos.x * scale
|
||||||
|
def.pos.z = def.pos.z * scale
|
||||||
|
def.pos.y = 0
|
||||||
|
|
||||||
|
local positionStr = core.pos_to_string(def.pos)
|
||||||
|
|
||||||
|
local peak = self.peaksByPos[positionStr]
|
||||||
|
|
||||||
|
if peak == nil then
|
||||||
|
peak = Peak:new(def)
|
||||||
|
self.peaksByPos[positionStr] = peak
|
||||||
|
elseif peak.color ~= def.color then
|
||||||
|
error('There cannot be two peaks with the same coordinates but different colors.')
|
||||||
|
end
|
||||||
|
|
||||||
|
return peak
|
||||||
|
end
|
||||||
|
|
||||||
|
function Layer:initTrianglesNeighbours()
|
||||||
|
local triangles = self.trianglesList
|
||||||
|
|
||||||
|
for _, triangle in pairs(triangles) do
|
||||||
|
for _, id in ipairs(triangle.neighboursIDs) do
|
||||||
|
table.insert(triangle.neighbours, triangles[id])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param fileName string
|
||||||
|
---@param category MapGen.Layer.Category
|
||||||
|
---@param scale integer
|
||||||
|
function Layer:getTriaglesFromFile(fileName, category, scale)
|
||||||
|
local file = io.open(core.get_modpath(core.get_current_modname())..'/maps/'..fileName, 'r')
|
||||||
|
assert(file ~= nil)
|
||||||
|
|
||||||
|
local json = core.parse_json(file:read('*a')) or {}
|
||||||
|
file:close()
|
||||||
|
|
||||||
|
local triangles = json.triangles
|
||||||
|
assert(triangles ~= nil)
|
||||||
|
|
||||||
|
for _, tr in ipairs(triangles) do
|
||||||
|
local p1 = self:registerPeak( tr.vertices[1], scale)
|
||||||
|
local p2 = self:registerPeak( tr.vertices[2], scale)
|
||||||
|
local p3 = self:registerPeak( tr.vertices[3], scale)
|
||||||
|
|
||||||
|
local triangle = Triangle:new(tr.id,p1, p2, p3, tr.adjacent_triangle_ids)
|
||||||
|
|
||||||
|
self:addTriangle(category, triangle)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param minY number
|
---@param def MapGen.LayerDef
|
||||||
---@param maxY number
|
|
||||||
---@return MapGen.Layer
|
---@return MapGen.Layer
|
||||||
function Layer:new(name, minY, maxY)
|
function Layer:new(name, def)
|
||||||
|
if def.calcTemp == nil then
|
||||||
|
Logger.infoLog('MapGen.Layer: The `%s` layer does not have a specified `calcTemp()` function. The default function is used.', name)
|
||||||
|
def.calcTemp = defaultCalcFunc
|
||||||
|
end
|
||||||
|
|
||||||
|
if def.calcHumidity == nil then
|
||||||
|
Logger.infoLog('MapGen.Layer: The `%s` layer does not have a specified `calcHumidity()` function. The default function is used.', name)
|
||||||
|
def.calcHumidity = defaultCalcFunc
|
||||||
|
end
|
||||||
|
|
||||||
---@type MapGen.Layer
|
---@type MapGen.Layer
|
||||||
local instance = setmetatable({
|
local instance = getInstance(self, {})
|
||||||
name = name,
|
instance.name = name
|
||||||
minY = minY,
|
instance.minY = def.minY
|
||||||
maxY = maxY,
|
instance.maxY = def.maxY
|
||||||
}, {__index = self})
|
instance.minTemp = def.minTemp
|
||||||
|
instance.maxTemp = def.maxTemp
|
||||||
|
instance.minHumidity = def.minHumidity
|
||||||
|
instance.maxHumidity = def.maxHumidity
|
||||||
|
instance.waterLevel = def.waterLevel or 0
|
||||||
|
instance.calcTemp = def.calcTemp
|
||||||
|
instance.calcHumidity = def.calcHumidity
|
||||||
|
instance.biomesGorizontalScattering = def.biomesGorizontalScattering or 0
|
||||||
|
instance.biomesVerticalScattering = def.biomesVerticalScattering or 0
|
||||||
|
|
||||||
|
instance:getTriaglesFromFile(
|
||||||
|
def.triangulationLandscapeFile,
|
||||||
|
'landscape',
|
||||||
|
def.landscapeMapScale)
|
||||||
|
instance:getTriaglesFromFile(
|
||||||
|
def.triangulationTempFile,
|
||||||
|
'temp',
|
||||||
|
def.tempMapScale)
|
||||||
|
instance:getTriaglesFromFile(
|
||||||
|
def.triangulationHumidityFile,
|
||||||
|
'humidity',
|
||||||
|
def.humidityMapScale)
|
||||||
|
|
||||||
|
instance:initTrianglesNeighbours()
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---A category of noise that affects specific map generation objects.
|
||||||
|
---@alias MapGen.Layer.Category 'landscape'|'temp'|'humidity'
|
||||||
|
|
||||||
|
---Adds a triangle to the `Layer.trianglesList`
|
||||||
|
---@param category MapGen.Layer.Category
|
||||||
|
---@param triangle MapGen.Triangle
|
||||||
|
function Layer:addTriangle(category, triangle)
|
||||||
|
local triangles = self.trianglesList
|
||||||
|
if triangles ~= nil then
|
||||||
|
triangles[triangle.id] = triangle
|
||||||
|
else
|
||||||
|
error(('There is no peak category named %s.'):format(category))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---Adds a peak to the `Layer.peaksList`
|
---Adds a peak to the `Layer.peaksList`
|
||||||
---@param peak MapGen.Peak
|
---@param category MapGen.Layer.Category
|
||||||
function Layer:addPeak(peak)
|
---@param peak MapGen.Peak
|
||||||
|
function Layer:addPeak(category, peak)
|
||||||
table.insert(self.peaksList, peak)
|
table.insert(self.peaksList, peak)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns `nil`.
|
||||||
|
---@param pos Position Coordinate of the desired peak.
|
||||||
|
---@return MapGen.Peak?
|
||||||
|
function Layer:getPeakByPos(pos)
|
||||||
|
local positionStr = core.pos_to_string(pos)
|
||||||
|
|
||||||
|
local peak = self.peaksByPos[positionStr]
|
||||||
|
|
||||||
|
return peak
|
||||||
|
end
|
||||||
|
|
||||||
|
---Adds a NoiseParams color to the `Layer.noisesParamsByColor`
|
||||||
|
---@param category MapGen.Layer.Category
|
||||||
|
---@param color ColorString
|
||||||
|
---@param noiseParams NoiseParams
|
||||||
|
function Layer:addNoiseParamsColor(category, color, noiseParams)
|
||||||
|
color = Smc.fixColorString(color)
|
||||||
|
|
||||||
|
local noisesParams = self.noisesParamsByColor
|
||||||
|
|
||||||
|
if category == 'landscape' then
|
||||||
|
assert(noisesParams.landscape[color] == nil, ('The color `%s` already exists.'):format(color))
|
||||||
|
noisesParams.landscape[color] = noiseParams
|
||||||
|
elseif category == 'temp' then
|
||||||
|
assert(noisesParams.temp[color] == nil, ('The color `%s` already exists.'):format(color))
|
||||||
|
noisesParams.temp[color] = noiseParams
|
||||||
|
elseif category == 'humidity' then
|
||||||
|
assert(noisesParams.humidity[color] == nil, ('The color `%s` already exists.'):format(color))
|
||||||
|
noisesParams.humidity[color] = noiseParams
|
||||||
|
else
|
||||||
|
error(('There is no color category named `%s`.'):format(category))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns noise corresponding to a color.
|
||||||
|
---@param category MapGen.Layer.Category
|
||||||
|
---@param color ColorString
|
||||||
|
---@return ValueNoise
|
||||||
|
function Layer:getNoiseByColor(category, color)
|
||||||
|
color = Smc.fixColorString(color)
|
||||||
|
|
||||||
|
local noises = self.noisesByColor
|
||||||
|
|
||||||
|
if category == 'landscape' then
|
||||||
|
assert(noises.landscape[color] ~= nil, ('`%s` color does not exist.'):format(color))
|
||||||
|
return noises.landscape[color]
|
||||||
|
elseif category == 'temp' then
|
||||||
|
assert(noises.temp[color] ~= nil, ('`%s` color does not exist.'):format(color))
|
||||||
|
return noises.temp[color]
|
||||||
|
elseif category == 'humidity' then
|
||||||
|
assert(noises.humidity[color] ~= nil, ('`%s` color does not exist.'):format(color))
|
||||||
|
return noises.humidity[color]
|
||||||
|
else
|
||||||
|
error(('There is no color category named `%s`.'):format(category))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---Adds a biome to the `Layer.biomesByName` & `Layer.biomesList`
|
---Adds a biome to the `Layer.biomesByName` & `Layer.biomesList`
|
||||||
---@param biomeName string
|
---@param biomeName string
|
||||||
---@param biome MapGen.Biome
|
---@param biome MapGen.Biome
|
||||||
function Layer:addBiome(biomeName, biome)
|
function Layer:addBiome(biomeName, biome)
|
||||||
|
assert(self.biomesByName[biomeName] == nil,
|
||||||
|
('A biome named `%s` already exists in the `%s` layer.'):format(
|
||||||
|
biomeName, self.name))
|
||||||
|
|
||||||
self.biomesByName[biomeName] = biome
|
self.biomesByName[biomeName] = biome
|
||||||
table.insert(self.biomesList, biome)
|
table.insert(self.biomesList, biome)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Returns a biome by name, if there is no such biome, it will raise an error.
|
||||||
|
---@param biomeName string
|
||||||
|
---@return MapGen.Biome
|
||||||
|
function Layer:getBiomeByName(biomeName)
|
||||||
|
local biome = self.biomesByName[biomeName]
|
||||||
|
assert(biome ~= nil, ('Biome named `%s` does not exist in the `%s` layer.'):format(
|
||||||
|
biomeName, self.name))
|
||||||
|
|
||||||
|
return biome
|
||||||
|
end
|
||||||
|
|
||||||
---Adds a cavern to the `Layer.cavernsByName` & `Layer.cavernsList`
|
---Adds a cavern to the `Layer.cavernsByName` & `Layer.cavernsList`
|
||||||
---@param cavernName string
|
---@param cavernName string
|
||||||
---@param cavern MapGen.Cavern
|
---@param cavern MapGen.Cavern
|
||||||
function Layer:addCavern(cavernName, cavern)
|
function Layer:addCavern(cavernName, cavern)
|
||||||
|
assert(self.cavernsByName[cavernName] == nil,
|
||||||
|
('A cavern named `%s` already exists in the `%s` layer.'):format(
|
||||||
|
cavernName, self.name))
|
||||||
|
|
||||||
self.cavernsByName[cavernName] = cavern
|
self.cavernsByName[cavernName] = cavern
|
||||||
table.insert(self.cavernsList, cavern)
|
table.insert(self.cavernsList, cavern)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Returns a cavern by name, if there is no such cavern, it will raise an error.
|
||||||
|
---@param cavernName string
|
||||||
|
---@return MapGen.Cavern
|
||||||
|
function Layer:getCavernByName(cavernName)
|
||||||
|
local cavern = self.cavernsByName[cavernName]
|
||||||
|
assert(cavern ~= nil, ('Cavern named `%s` does not exist in the `%s` layer.'):format(
|
||||||
|
cavernName, self.name))
|
||||||
|
|
||||||
|
return cavern
|
||||||
|
end
|
||||||
|
|
||||||
|
function Layer:addRegion(regionName, region)
|
||||||
|
assert(self.regionsByName[regionName] == nil,
|
||||||
|
('A region named `%s` already exists in the `%s` layer.'):format(
|
||||||
|
regionName, self.name))
|
||||||
|
|
||||||
|
self.regionsByName[regionName] = region
|
||||||
|
--table.insert(self.regionsList, region)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns a region by name, if there is no such region, it will raise an error.
|
||||||
|
---@param regionName string
|
||||||
|
---@return MapGen.Region
|
||||||
|
function Layer:getRegionByName(regionName)
|
||||||
|
local region = self.regionsByName[regionName]
|
||||||
|
assert(region ~= nil, ('Region named `%s` does not exist in the `%s` layer.'):format(
|
||||||
|
regionName, self.name))
|
||||||
|
|
||||||
|
return region
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param height number
|
---@param height number
|
||||||
---@param biomesList MapGen.Biome[]
|
---@param biomesList MapGen.Biome[]
|
||||||
---@return string[], MapGen.Biome[]
|
---@return string[], MapGen.Biome[]
|
||||||
@@ -77,6 +364,19 @@ local function getBiomesNamesByHeight(height, biomesList)
|
|||||||
return biomesNames, biomes
|
return biomesNames, biomes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Initializes noise for generation.
|
||||||
|
---
|
||||||
|
---Should only be called after the mapgen object has been loaded.
|
||||||
|
function Layer:initNoises()
|
||||||
|
local noises = self.noisesByColor
|
||||||
|
|
||||||
|
for category, noisesParams in pairs(self.noisesParamsByColor) do
|
||||||
|
for color, noiseParams in pairs(noisesParams) do
|
||||||
|
noises[category][color] = core.get_value_noise(noiseParams)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---Initialization of the biome diagram using the Voronoi method.
|
---Initialization of the biome diagram using the Voronoi method.
|
||||||
function Layer:initBiomesDiagram()
|
function Layer:initBiomesDiagram()
|
||||||
local diagram = self.biomesDiagram
|
local diagram = self.biomesDiagram
|
||||||
@@ -98,10 +398,10 @@ function Layer:initBiomesDiagram()
|
|||||||
end
|
end
|
||||||
|
|
||||||
pastBiomesNames = newBiomesNames
|
pastBiomesNames = newBiomesNames
|
||||||
for temp = 0, 100 do
|
for temp = self.minTemp, self.maxTemp do
|
||||||
diagram[height][temp] = {}
|
diagram[height][temp] = {}
|
||||||
|
|
||||||
for humidity = 0, 100 do
|
for humidity = self.minHumidity, self.maxHumidity do
|
||||||
local minDistance = math.huge
|
local minDistance = math.huge
|
||||||
local closestBiome = nil
|
local closestBiome = nil
|
||||||
|
|
||||||
|
|||||||
@@ -1,94 +1,56 @@
|
|||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
---@class MapGen.Peak
|
---@class MapGen.Peak
|
||||||
---@field id number
|
---@field id number
|
||||||
---@field getPeakPos fun():vector
|
---@field pos vector
|
||||||
---@field getMultinoiseParams fun():MapGen.Peak.MultinoiseParams
|
---@field color ColorString
|
||||||
---@field getMultinoise fun():MapGen.Peak.Multinoise
|
---@field getGroups fun(self):table<string, number>
|
||||||
---@field initMultinoise fun()
|
---@field addGroups fun(self, groups:table<string, number>)
|
||||||
---@field getGroups fun():table<string, number>
|
|
||||||
local Peak = {
|
local Peak = {
|
||||||
id = 0,
|
id = 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- --- Helpers functions ---
|
|
||||||
|
|
||||||
---@param multinoiseParams MapGen.Peak.MultinoiseParams
|
|
||||||
---@return MapGen.Peak.Multinoise
|
|
||||||
local function multinoiseParamsToMultinoise(multinoiseParams)
|
|
||||||
---@diagnostic disable-next-line: missing-fields
|
|
||||||
---@type MapGen.Peak.Multinoise
|
|
||||||
local multinoise = {}
|
|
||||||
|
|
||||||
for noiseName, noiseParams in pairs(multinoiseParams) do
|
|
||||||
multinoise[noiseName] = core.get_value_noise(noiseParams)
|
|
||||||
end
|
|
||||||
|
|
||||||
return multinoise
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- --- Class registration ---
|
-- --- Class registration ---
|
||||||
|
|
||||||
---@param peakPos vector
|
---Definition table for the `MapGen.Peak`.
|
||||||
---@param multinoiseParams MapGen.Peak.MultinoiseParams
|
---
|
||||||
---@param groups? table<string, number>
|
---**Only for EmmyLua.**
|
||||||
---@return MapGen.Peak
|
---@class MapGen.PeakDef
|
||||||
function Peak:new(peakPos, multinoiseParams, groups)
|
---@field pos MapVector
|
||||||
---@type vector
|
---@field color ColorString
|
||||||
local _peakPos = peakPos
|
---@field groups? table<string, number>
|
||||||
---@type MapGen.Peak.MultinoiseParams
|
|
||||||
local _multinoiseParams = multinoiseParams
|
|
||||||
|
|
||||||
|
---@param def MapGen.PeakDef
|
||||||
|
---@return MapGen.Peak
|
||||||
|
function Peak:new(def)
|
||||||
|
local _color = def.color
|
||||||
local _groups
|
local _groups
|
||||||
|
|
||||||
if groups == nil then
|
if def.groups == nil then
|
||||||
_groups = {}
|
_groups = {}
|
||||||
else
|
else
|
||||||
_groups = groups
|
_groups = def.groups
|
||||||
end
|
end
|
||||||
|
|
||||||
---@diagnostic disable-next-line: missing-fields
|
|
||||||
---@type MapGen.Peak.Multinoise
|
|
||||||
local _multinoise = {} -- note: must be empty until luanti mapgen objects are loaded.
|
|
||||||
|
|
||||||
---@type MapGen.Peak
|
---@type MapGen.Peak
|
||||||
local instance = setmetatable({},
|
local instance = getInstance(self,{},
|
||||||
{
|
{
|
||||||
__index = self,
|
|
||||||
__tostring = self.toString,
|
__tostring = self.toString,
|
||||||
__eq = self.eq
|
__eq = self.eq
|
||||||
})
|
})
|
||||||
|
instance.pos = vector.new(def.pos)
|
||||||
function instance:getPeakPos()
|
instance.color = def.color
|
||||||
return vector.new(_peakPos.x, _peakPos.y, _peakPos.z)
|
|
||||||
end
|
|
||||||
|
|
||||||
function instance:getMultinoiseParams()
|
|
||||||
return _multinoiseParams
|
|
||||||
end
|
|
||||||
|
|
||||||
function instance:getMultinoise()
|
|
||||||
if table.is_empty(_multinoise) then
|
|
||||||
error('Attempt to get `MapGen.Triangulation.FakePeak` noise (empty noise).')
|
|
||||||
end
|
|
||||||
|
|
||||||
return _multinoise
|
|
||||||
end
|
|
||||||
|
|
||||||
function instance:initMultinoise()
|
|
||||||
if not table.is_empty(_multinoise) then
|
|
||||||
Logger.warningLog('Multinoise has already been initialized. Re-initialization is not recommended.')
|
|
||||||
end
|
|
||||||
|
|
||||||
if table.is_empty(_multinoiseParams) then
|
|
||||||
error('Attempt to initialize `MapGen.Triangulation.FakePeak` noise (noise parameter empty).')
|
|
||||||
end
|
|
||||||
|
|
||||||
_multinoise = multinoiseParamsToMultinoise(_multinoiseParams)
|
|
||||||
end
|
|
||||||
|
|
||||||
function instance:getGroups()
|
function instance:getGroups()
|
||||||
return _groups
|
return table.copy_with_metatables(_groups)
|
||||||
|
end
|
||||||
|
|
||||||
|
function instance:addGroups(groups)
|
||||||
|
-- Groups that cannot be overwritten
|
||||||
|
groups.is2d = nil
|
||||||
|
groups.is3d = nil
|
||||||
|
|
||||||
|
_groups = table.merge(_groups, groups, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
@@ -97,7 +59,7 @@ end
|
|||||||
---Returns a string describing the object in a readable form.
|
---Returns a string describing the object in a readable form.
|
||||||
---@return string
|
---@return string
|
||||||
function Peak:toString()
|
function Peak:toString()
|
||||||
local pos = self:getPeakPos()
|
local pos = self.pos
|
||||||
return ('Peak (%s) x: %.2f y: %.2f z: %.2f'):format( self.id, pos.x, pos.y, pos.z)
|
return ('Peak (%s) x: %.2f y: %.2f z: %.2f'):format( self.id, pos.x, pos.y, pos.z)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -105,8 +67,8 @@ end
|
|||||||
---@param other MapGen.Peak
|
---@param other MapGen.Peak
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Peak:eq(other)
|
function Peak:eq(other)
|
||||||
local pos1 = self:getPeakPos()
|
local pos1 = self.pos
|
||||||
local pos2 = other:getPeakPos()
|
local pos2 = other.pos
|
||||||
|
|
||||||
return pos1.x == pos2.x and pos1.z == pos2.z
|
return pos1.x == pos2.x and pos1.z == pos2.z
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
---For EmmyLua only
|
|
||||||
|
|
||||||
---**For EmmyLua only**
|
|
||||||
---@class MapGen.Peak.Multinoise
|
|
||||||
---@field landscapeNoise ValueNoise
|
|
||||||
---@field tempNoise ValueNoise
|
|
||||||
---@field humidityNoise ValueNoise
|
|
||||||
local Multinoise = {}
|
|
||||||
|
|
||||||
return Multinoise
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
---For EmmyLua only
|
|
||||||
|
|
||||||
---**For EmmyLua only**
|
|
||||||
---@class MapGen.Peak.MultinoiseParams
|
|
||||||
---@field landscapeNoise NoiseParams
|
|
||||||
---@field tempNoise NoiseParams
|
|
||||||
---@field humidityNoise NoiseParams
|
|
||||||
local MultinoiseParams = {}
|
|
||||||
|
|
||||||
return MultinoiseParams
|
|
||||||
112
mods/CORE/MapGen/src/MapGen/Region.lua
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
local mathClamp, mathHuge, mathSqrt = math.clamp, math.huge, math.sqrt
|
||||||
|
|
||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
---@class MapGen.Region
|
||||||
|
---@field name string
|
||||||
|
---@field vertices MapVector[]
|
||||||
|
local Region = {}
|
||||||
|
|
||||||
|
---@class MapGen.RegionDef
|
||||||
|
---@field vertices MapVector[]
|
||||||
|
|
||||||
|
---@param def MapGen.RegionDef
|
||||||
|
---@return MapGen.Region
|
||||||
|
function Region:new(name, def)
|
||||||
|
local instance = getInstance(self, {
|
||||||
|
name = name,
|
||||||
|
vertices = def.vertices
|
||||||
|
})
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
---Calculate squared distance from point to line segment.
|
||||||
|
---@param px integer
|
||||||
|
---@param pz integer
|
||||||
|
---@param x1 integer
|
||||||
|
---@param z1 integer
|
||||||
|
---@param x2 integer
|
||||||
|
---@param z2 integer
|
||||||
|
---@return number
|
||||||
|
local function calcDistanceSq(px, pz, x1, z1, x2, z2)
|
||||||
|
local dx = x2 - x1
|
||||||
|
local dz = z2 - z1
|
||||||
|
local len_sq = dx * dx + dz * dz
|
||||||
|
|
||||||
|
-- Degenerate segment (a single point):
|
||||||
|
if len_sq == 0 then
|
||||||
|
return (px - x1)^2 + (pz - z1)^2
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Project point onto line, clamped to segment [0, 1]
|
||||||
|
local param = mathClamp(((px - x1) * dx + (pz - z1) * dz) / len_sq, 0, 1)
|
||||||
|
|
||||||
|
-- Closest point on segment
|
||||||
|
local closest_x = x1 + param * dx
|
||||||
|
local closest_z = z1 + param * dz
|
||||||
|
|
||||||
|
return (px - closest_x)^2 + (pz - closest_z)^2
|
||||||
|
end
|
||||||
|
|
||||||
|
---Checks if a point is inside a `MapGen.Region` (2D check using x and z coordinates).
|
||||||
|
---@param point MapVector
|
||||||
|
---@return boolean, number?
|
||||||
|
function Region:pointInRegion(point)
|
||||||
|
local vertices = self.vertices
|
||||||
|
local px, pz = point.x, point.z
|
||||||
|
|
||||||
|
local n = #vertices
|
||||||
|
if n < 3 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Calculate minimum squared distance to any edge
|
||||||
|
local minDistanceSq = mathHuge
|
||||||
|
for i = 1, n do
|
||||||
|
local j = (i % n) + 1
|
||||||
|
local v1, v2 = vertices[i], vertices[j]
|
||||||
|
|
||||||
|
if v1 == nil or v2 == nil then error() end
|
||||||
|
|
||||||
|
local dist_sq = calcDistanceSq(px, pz, v1.x, v1.z, v2.x, v2.z)
|
||||||
|
|
||||||
|
if dist_sq < minDistanceSq then
|
||||||
|
minDistanceSq = dist_sq
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local minDistance = mathSqrt(minDistanceSq)
|
||||||
|
|
||||||
|
-- Check if point is on edge (within small epsilon)
|
||||||
|
if minDistance <= 1e-9 then
|
||||||
|
return true, minDistance
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Ray casting algorithm: count edge crossings
|
||||||
|
local inside = false
|
||||||
|
|
||||||
|
for i = 1, n do
|
||||||
|
local j = (i % n) + 1
|
||||||
|
local vi, vj = vertices[i], vertices[j]
|
||||||
|
|
||||||
|
if vi == nil or vj == nil then error() end
|
||||||
|
|
||||||
|
local xi, zi = vi.x, vi.z
|
||||||
|
local xj, zj = vj.x, vj.z
|
||||||
|
|
||||||
|
-- Check if horizontal ray from point (going +x) crosses this edge
|
||||||
|
if ((zi > pz) ~= (zj > pz)) and
|
||||||
|
(px < (xj - xi) * (pz - zi) / (zj - zi) + xi) then
|
||||||
|
inside = not inside
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if inside then
|
||||||
|
return true, minDistance
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return Region
|
||||||
61
mods/CORE/MapGen/src/MapGen/StaticLayer.lua
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
local id = core.get_content_id
|
||||||
|
|
||||||
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
---@class MapGen.StaticLayer
|
||||||
|
---@field name string
|
||||||
|
---@field minY number
|
||||||
|
---@field maxY number
|
||||||
|
---@field staticNode string
|
||||||
|
---@field staticNodeID number
|
||||||
|
---@field generateStaticNode fun(self, mapGenerator:MapGen, data:number[], data2:number[], index:number, x:number, y:number, z:number)
|
||||||
|
local StaticLayer = {}
|
||||||
|
|
||||||
|
---Definition table for the `MapGen.StaticLayer`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class MapGen.StaticLayerDef
|
||||||
|
---@field minY number
|
||||||
|
---@field maxY number
|
||||||
|
---@field staticNode string
|
||||||
|
---@field generateStaticNode fun(self, mapGenerator:MapGen, data:number[], data2:number[], index:number, x:number, y:number, z:number)
|
||||||
|
|
||||||
|
---@param self MapGen.StaticLayer
|
||||||
|
---@param mapGenerator MapGen
|
||||||
|
---@param data number[]
|
||||||
|
---@param data2 number[]
|
||||||
|
---@param index number
|
||||||
|
---@param x number
|
||||||
|
---@param y number
|
||||||
|
---@param z number
|
||||||
|
local defaultGenerateStaticNode = function(self, mapGenerator, data, data2, index, x, y, z)
|
||||||
|
data[index] = self.staticNodeID
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param def MapGen.StaticLayerDef
|
||||||
|
---@return MapGen.StaticLayer
|
||||||
|
function StaticLayer:new(name, def)
|
||||||
|
assert(def.maxY > def.minY, 'maxY there should be more minY')
|
||||||
|
|
||||||
|
if def.generateStaticNode == nil then
|
||||||
|
Logger.infoLog('MapGen.StaticLayer: The `%s` static layer does not have a specified `generateStaticNode()` function. The default function is used.', name)
|
||||||
|
def.generateStaticNode = defaultGenerateStaticNode
|
||||||
|
end
|
||||||
|
|
||||||
|
local staticNodeID = id(def.staticNode)
|
||||||
|
|
||||||
|
---@type MapGen.StaticLayer
|
||||||
|
local instance = getInstance(self, {
|
||||||
|
name = name,
|
||||||
|
minY = def.minY,
|
||||||
|
maxY = def.maxY,
|
||||||
|
staticNode = def.staticNode,
|
||||||
|
staticNodeID = staticNodeID,
|
||||||
|
generateStaticNode = def.generateStaticNode
|
||||||
|
})
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
return StaticLayer
|
||||||