-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGameAPI.cs
More file actions
297 lines (256 loc) · 11.5 KB
/
GameAPI.cs
File metadata and controls
297 lines (256 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using Microsoft.Scripting.Hosting;
using SkylinesPythonShared;
using SkylinesPythonShared.API;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace SkylinesRemotePython.API
{
[Singleton("game")]
[Doc("Contains basic set of functions for interaction with Cities:Skylines")]
public class GameAPI
{
internal ClientHandler client;
internal NetLogic _netLogic;
internal ScriptScope _scope;
internal GameAPI(ClientHandler client, ScriptScope scope)
{
this.client = client;
_netLogic = new NetLogic();
_scope = scope;
}
[Doc("Returns prop object from its id")]
public Prop get_prop(int id)
{
return ObjectStorage.Instance.Props.GetById((uint)id, true);
}
[Doc("Returns prop iterator (can be used only in for loop)")]
public CitiesObjectEnumerable<Prop, PropData> props => new CitiesObjectEnumerable<Prop, PropData>();
[Doc("Returns tree object from its id")]
public Tree get_tree(long id)
{
return ObjectStorage.Instance.Trees.GetById((uint)id, true);
}
[Doc("Returns tree iterator (can be used only in for loop)")]
public CitiesObjectEnumerable<Tree, TreeData> trees => new CitiesObjectEnumerable<Tree, TreeData>();
[Doc("Returns building object from its id")]
public Building get_building(int id)
{
return ObjectStorage.Instance.Buildings.GetById((uint)id, true);
}
[Doc("Returns building iterator (can be used only in for loop)")]
public CitiesObjectEnumerable<Building, BuildingData> buildings => new CitiesObjectEnumerable<Building, BuildingData>();
[Doc("Returns node object from its id")]
public Node get_node(int id) => ObjectStorage.Instance.Nodes.GetById((uint)id, true);
[Doc("Returns node iterator (can be used only in for loop)")]
public CitiesObjectEnumerable<Node, NetNodeData> nodes => new CitiesObjectEnumerable<Node, NetNodeData>();
[Doc("Returns segment object from its id")]
public Segment get_segment(int id)
{
return ObjectStorage.Instance.Segments.GetById((uint)id, true);
}
[Doc("Returns segment iterator (can be used only in for loop)")]
public CitiesObjectEnumerable<Segment, NetSegmentData> segments => new CitiesObjectEnumerable<Segment, NetSegmentData>();
[Doc("Creates prop")]
public Prop create_prop(IPositionable position, string prefab_name, double angle = 0)
{
var msg = new CreatePropMessage()
{
Position = position.position,
Type = prefab_name,
Angle = angle
};
Prop shell = ObjectStorage.Instance.Props.CreateShell();
client.RemoteCall(Contracts.CreateProp, msg, (ret, error) => {
if(error != null) {
shell.AssignData(null, error);
return null;
}
PropData data = (PropData)ret;
ObjectStorage.Instance.Props.AddDataToDictionary(data);
shell.AssignData(data);
return null;
});
return shell;
}
[Doc("Creates tree")]
public Tree create_tree(IPositionable position, string prefab_name)
{
var msg = new CreateTreeMessage() {
Position = position.position,
prefab_name = prefab_name
};
Tree shell = ObjectStorage.Instance.Trees.CreateShell();
client.RemoteCall(Contracts.CreateTree, msg, (ret, error) => {
if (error != null) {
shell.AssignData(null, error);
return null;
}
TreeData data = (TreeData)ret;
ObjectStorage.Instance.Trees.AddDataToDictionary(data);
shell.AssignData(data);
return null;
});
return shell;
}
[Doc("Creates building")]
public Building create_building(IPositionable position, string type, double angle = 0)
{
var msg = new CreateBuildingMessage() {
Position = position.position,
Type = type,
Angle = angle
};
Building shell = ObjectStorage.Instance.Buildings.CreateShell();
client.RemoteCall(Contracts.CreateBuilding, msg, (ret, error) => {
if (error != null) {
shell.AssignData(null, error);
return null;
}
BuildingData data = (BuildingData)ret;
ObjectStorage.Instance.Buildings.AddDataToDictionary(data);
shell.AssignData(data);
return null;
});
return shell;
}
[Doc("Creates node (eg. segment junction)")]
public Node create_node(IPositionable position, object prefab)
{
if(!(prefab is string) && !(prefab is NetPrefab)) {
throw new Exception("Prefab must be string or NetPrefab");
}
CreateNodeMessage msg = new CreateNodeMessage() {
Position = position.position,
Type = prefab is NetPrefab ? ((NetPrefab)prefab).name : (string)prefab
};
Node shell = ObjectStorage.Instance.Nodes.CreateShell();
client.RemoteCall(Contracts.CreateNode, msg, (ret, error) => {
if (error != null) {
shell.AssignData(null, error);
return null;
}
NetNodeData data = (NetNodeData)ret;
ObjectStorage.Instance.Nodes.AddDataToDictionary(data);
shell.AssignData(data);
return null;
});
return shell;
}
[Doc("Creates straight segment (road). Don't use this method, but CreateSegments(..)")]
public Segment create_segment(IPositionable startNode, IPositionable endNode, object type)
{
return _netLogic.CreateSegmentImpl(startNode, endNode, type, null, null, null);
}
[Doc("Creates segment (road). Don't use this method, but CreateSegments(..)")]
public Segment create_segment(IPositionable startNode, IPositionable endNode, object type, IPositionable control_point)
{
return _netLogic.CreateSegmentImpl(startNode, endNode, type, null, null, control_point);
}
[Doc("Creates segment (road). Don't use this method, but CreateSegments(..)")]
public Segment create_segment(IPositionable startNode, IPositionable endNode, object type, Vector start_dir, Vector end_dir)
{
return _netLogic.CreateSegmentImpl(startNode, endNode, type, start_dir, end_dir, null);
}
[Doc("Starts a road on a given point. Call path_to(..) on the returned object to build a road")]
public PathBuilder begin_path(IPositionable startNode, object options = null)
{
return PathBuilder.BeginPath(this, startNode, options);
}
[Doc("Creates set of straight segments (road). Returns array of created segments")]
public IList<Segment> create_segments(IPositionable startNode, IPositionable endNode, object type)
{
return _netLogic.CreateSegmentsImpl(startNode, endNode, type, null, null, null);
}
[Doc("Creates set of straight segments (road) with specified control point of the underlying bezier curve")]
public IList<Segment> create_segments(IPositionable startNode, IPositionable endNode, object type, IPositionable control_point)
{
return _netLogic.CreateSegmentsImpl(startNode, endNode, type, null, null, control_point);
}
[Doc("Creates set of straight segments (road) with specified direction vectors at the start and end position")]
public IList<Segment> create_segments(IPositionable startNode, IPositionable endNode, object type, Vector start_dir, Vector end_dir)
{
return _netLogic.CreateSegmentsImpl(startNode, endNode, type, start_dir, end_dir, null);
}
[Doc("Returns network prefab (used to build nodes and segments). Eg. 'Basic road'")]
public NetPrefab get_net_prefab(string name)
{
return ObjectStorage.Instance.NetPrefabs.GetById(name, true);
}
[Doc("Returns if name is a valid prefab (network, building, tree etc.)")]
public bool is_prefab(string name)
{
return client.SynchronousCall<bool>(Contracts.ExistsPrefab, name);
}
[Doc("Returns terrain height at a given point (height is Y coord)")]
public float terrain_height(IPositionable pos)
{
return client.SynchronousCall<float>(Contracts.GetTerrainHeight, pos.position);
}
[Doc("Returns terrain height inlucing water level at a given point")]
public float surface_level(IPositionable pos)
{
return client.SynchronousCall<float>(Contracts.GetWaterLevel, pos.position);
}
[Doc("Draws line on map. Returns handle which can be used to delete the line. Use clear() to delete all lines")]
public RenderableObjectHandle draw_vector(IPositionable vector, IPositionable origin, string color = "red", double length = 20, double size = 0.1)
{
return new RenderableObjectHandle(client.SynchronousCall<int>(Contracts.RenderVector, new RenderVectorMessage() {
vector = vector.position,
origin = origin.position,
color = color,
length = (float)length,
size = (float)size
}));
}
[Doc("Draws circle on map. Returns handle which can be used to delete the circle")]
public RenderableObjectHandle draw_circle(IPositionable position, double radius = 5, string color = "red")
{
return new RenderableObjectHandle(client.SynchronousCall<int>(Contracts.RenderCircle, new RenderCircleMessage() {
position = position.position,
radius = (float)radius,
color = color
}));
}
[Doc("Clears all lines drawn on map")]
public void clear()
{
client.SynchronousCall<object>(Contracts.RemoveRenderedObject, 0);
}
[Doc("Prints collection content")]
public void print_list(IEnumerable collection)
{
client.Print(PythonHelp.PrintList(collection));
}
public delegate void __HelpDeleg(object obj = null);
[Doc("Prints help for given object")]
public void help(object obj = null)
{
obj = obj ?? this;
string text = PythonHelp.GetHelp(obj);
client.Print(text);
}
[Doc("Dumps all available documentation in the output")]
public void help_all()
{
string text = PythonHelp.DumpDoc();
client.Print(text);
}
public void help_markdown()
{
string text = PythonHelp.DumpDoc(true);
client.Print(text);
}
[Doc("Prints all variables available in the global scope")]
public void list_globals()
{
string vars = string.Join(", ",_scope.GetVariableNames());
client.Print("Variables in global scope: " + vars + "\n");
}
public override string ToString()
{
return PythonHelp.GetHelp(this);
}
}
}