factorio-types 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,7 @@ Intended to be used with [Typescript to Lua](https://github.com/TypeScriptToLua/
10
10
 
11
11
  ## Configuration
12
12
 
13
- This library includes [tsconfig.base.json](./tsconfig.base.json) in the root of the package with recommended configuration. This can be used as follows
13
+ This library includes [tsconfig.base.json](https://github.com/sguest/factorio-types/blob/master/tsconfig.base.json) in the root of the package with recommended configuration. This can be used as follows
14
14
 
15
15
  `tsconfig.json`:
16
16
 
@@ -21,24 +21,96 @@ This library includes [tsconfig.base.json](./tsconfig.base.json) in the root of
21
21
  }
22
22
  ```
23
23
 
24
+ ## Stages
25
+
26
+ Factorio mods and the api go through [three distinct stages](https://lua-api.factorio.com/latest/). Types are organized into namespaces `runtime`, `prototype`, and `settings` representing the types to be used in each stage. These types, and therefore their corresponding namespaces, are a compile-time-only feature as lua does not have types, however using a type from the wrong namespace during the wrong stage is likely to result in a runtime error as you will be attempting to access something Factorio does not let you access at that time.
27
+
28
+ Note there is some overlap between the types for the Settings and Prototype phases since both regularly provide prototype data to `data.extend()`, and in fact the settings-phase prototypes extend `prototype.PrototypeBase`. However, you should only be providing types from the `settings` namespace during the settings phase, and from the `prototype` namespace during the prototype phase.
29
+
24
30
  ## Examples
25
31
 
26
32
  A very minimal proof-of-concept showing basic toolchain setup can be found [Here](https://github.com/sguest/factorio-fire-armor-typescript)
27
33
 
28
34
  A slightly more in-depth and realistic mode can be found [Here](https://github.com/sguest/basic-seablock)
29
35
 
30
- ## Noise library
36
+ ## Primitives
31
37
 
32
- A note on using the `noise` library from within Factorio: Several mods (typically those that deal with terrain generation) will `require('noise')`. While this seems to work, this is not an officially documented part of the Factorio modding API and is not expressed in these types. If you want to use it, you'll want to add the following to your configuration
38
+ Primitive types like `uint8` or `nil` are preserved from the docs to better represent expected data formats, and are aliased as typescript types, so `nil` is `null`, and the various numeric formats such as `uint8`, `float`, etc are aliases for `number`.
33
39
 
34
- `tsconfig.json`:
40
+ Note that since typescript has a single `number` type, the compiler will **not** prevent things like passing a `float` to a method that expects `uint8` because these are both just `number` types under the hood.
41
+
42
+ ## Lualib
43
+
44
+ Factorio makes various lua functions available to mods via [LuaLib](https://github.com/wube/factorio-data/tree/master/core/lualib)
45
+
46
+ Unlike the main parts of the factorio API, these do not exist as ambient globals but instead must be imported
47
+
48
+ ```typescript
49
+ import * as noise from "noise";
50
+ let x = noise.var_get('x');
51
+ ```
52
+
53
+ Also unlike the main factorio API, these types are not documented in a machine-readable way and therefore are not generated here along with the main API types. Instead, they are hand-written as needed and as a result are incomplete and more likely to be wrong. See the [existing declarations](https://github.com/sguest/factorio-types/tree/master/src/lualib).
54
+
55
+ If there are types of lualib that you need but do not exist, either open an issue asking for it, or write the type definitions and open a pull request. If you would like to continue working with these types while they do not exist in `factorio-types`, you can add the import path to `tstl.noResolvePaths` in tsconfig.json, and then `require()` the paths, which will be typed as `any`.
56
+
57
+ `tsconfig.json`
35
58
 
36
59
  ```json
37
60
  {
38
61
  "tstl": {
39
- "noResolvePaths": ["noise"]
62
+ "noResolvePaths": ["math2d"]
40
63
  }
41
64
  }
42
65
  ```
43
66
 
44
- You can then optionally write your own custom type definitions for these types, or leave them as `any`
67
+ `some-file.ts`
68
+
69
+ ```typescript
70
+ const math2d = require('math2d');
71
+ ```
72
+
73
+ ### Noise
74
+
75
+ The `noise` module has mostly-complete type definitions. A couple notes on usage due to the limitations of typescript
76
+
77
+ The `noise.var` function is named `noise.var_get` since `var` is a reserved word in typscript. It will be emitted as `noise.var` thanks to a [@customName compiler annotation](https://typescripttolua.github.io/docs/advanced/compiler-annotations/#customname).
78
+
79
+ typescript
80
+
81
+ ```ts
82
+ import * as noise from "noise";
83
+ let x = noise.var_get('x');
84
+ ```
85
+
86
+ output lua
87
+
88
+ ```lua
89
+ local noise = require("noise")
90
+ local x = noise.var("x")
91
+ ```
92
+
93
+ Noise values have operator overloads for common arithmetic operations, however Typescript does not support operator overloading. These are implemented as [Operator map types](https://typescripttolua.github.io/docs/advanced/language-extensions#operator-map-types) in the noise library, which will emit as regular operators in lua. Available operators are named following lua conventions for overloaded operators without the preceding double-underscore:
94
+
95
+ - Addition: `noise.add`
96
+ - Subraction: `noise.sub`
97
+ - Multiplication: `noise.mul`
98
+ - Division: `noise.div`
99
+ - Unary Negation: `noise.unm`
100
+ - Exponentiation: `noise.pow`
101
+
102
+ typescript
103
+
104
+ ```ts
105
+ let x = noise.var_get('x');
106
+ let y = noise.var_get('y');
107
+ let sum = noise.add(x, y);
108
+ ```
109
+
110
+ Output lua
111
+
112
+ ```lua
113
+ local x = noise.var("x")
114
+ local y = noise.var("y")
115
+ local sum = x + y;
116
+ ```