1 | -- This lua script creates a totally random generated space station for the orxonox computer game! |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | -- This function creates a randomly generated space station. |
---|
6 | -- The first three arguments (xPos,yPos,zPos) are the position of the space station. |
---|
7 | -- The next tree arguments (xVel,yVel,zVel) are the velocity of the space station. |
---|
8 | -- The arguments (xRotAxis,yRotAxis,zRotAxis) are the rotation axis, with respect to which the space ship will rotate. Each argument can be 0 or 1. |
---|
9 | -- The argument RotRate defines how fast the space station will rotate. |
---|
10 | -- The arguments (xRotOnce,yRotOnce,zRotOnce) define in which direction the space station will face, if they are all zero, the station will face into the negative |
---|
11 | -- z-direction, and the positive y-direction is the top side of the station. |
---|
12 | function createSpaceStation(xPos,yPos,zPos, xVel,yVel,zVel, xRotAxis,yRotAxis,zRotAxis, RotRate, xRotOnce,yRotOnce,zRotOnce) |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
17 | -- This prints xml code, which creates a MovableEntity, which I need to attach all the parts of the space station. |
---|
18 | print("<MovableEntity scale=1 position=\"") print(xPos) print(",") print(yPos) print(",") print(zPos) |
---|
19 | print("\" velocity=\"") print(xVel) print(",") print(yVel) print(",") print(zVel) |
---|
20 | print("\" rotationaxis=\"") print(xRotAxis) print(",") print(yRotAxis) print(",") print(zRotAxis) print("\" rotationrate=") print(RotRate) |
---|
21 | print(" pitch=") print(xRotOnce) print(" yaw=") print(yRotOnce) print(" roll=") print(zRotOnce) print(" >") |
---|
22 | -- End create Movable Entity. |
---|
23 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
28 | -- Create a randomseed, so that the math.random() function is actually random. |
---|
29 | math.randomseed(os.time()) |
---|
30 | -- End create randomseed. |
---|
31 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
36 | -- Here you can define some global variables, with which you can modify the space station. |
---|
37 | |
---|
38 | -- Define the maximal size of the space station, this is actually just for the grid, be sure that this value is big enough. |
---|
39 | sSSize=30 |
---|
40 | -- Define how many parts the space station has, this value has to be exact, so be sure to increment it if you're adding a new part. |
---|
41 | sSParts=9 |
---|
42 | -- Define how many body parts the space station has, this value has to be exact. Body part means a part, which has connections at least in two directions. |
---|
43 | sSBodyParts=3 |
---|
44 | -- Define how many side parts for the left side you have. |
---|
45 | leftSideParts=1 |
---|
46 | -- Define how many side parts for the right side you have. |
---|
47 | rightSideParts=1 |
---|
48 | -- Define how many top parts you have. |
---|
49 | topParts=2 |
---|
50 | -- Define how many connection parts you have. |
---|
51 | connParts=1 |
---|
52 | -- Define the maximal dimension of a single part, be sure this value is big enough, better it's too big, it's only a matter of efficiency. |
---|
53 | pDim=6 |
---|
54 | -- Define the length in x-direction of the space station which will be occupied by bodyparts. |
---|
55 | xBPLength=4 |
---|
56 | -- Define the variation of the edges of your bodyparts in the x-direction. |
---|
57 | xBPVar=1 |
---|
58 | -- Define the length in y-direction of the space station which will be occupied by bodyparts. |
---|
59 | yBPLength=2 |
---|
60 | -- Define the variation of the edges of your bodyparts in the y-direction. |
---|
61 | yBPVar=1 |
---|
62 | -- Define the length in the z-direction of the space station which will be occupied by bodyparts. |
---|
63 | zBPLength=6 |
---|
64 | -- Define the variation of the edges of your bodyparts in the z-direction. |
---|
65 | zBPVar=1 |
---|
66 | -- Define the scale of the space station. |
---|
67 | sSScale=100 |
---|
68 | -- Define the griddimension, be sure this value matches the size of a single space station part plus the size of a connection part, which means your parts must be: |
---|
69 | -- integer*(gridDim-connectionSize), then integer tells you how many griddimensions your part is. |
---|
70 | gridDim=2.25 |
---|
71 | -- End define global parameters. |
---|
72 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
77 | -- This creates a 4-dimensional grid, which tells us if there is a part or not, and in which direction it has connections. |
---|
78 | -- The parameters x,y,z are the axis of the space station, which iterate to sSSize, the maximal size of the space station. |
---|
79 | -- The griddimension, this word I will use later, means that the distance of a point to the next point is gridDim in the game, so the absolute x-axis is x*gridDim*sSScale, |
---|
80 | -- and so on for the other dimensions y and z. |
---|
81 | -- grid[x][y][z][0] contains 0 if there is no part at the position (x,y,z), otherwise 1. |
---|
82 | -- grid[x][y][z][1] contains 0 if there is no connection from (x,y,z) in x-direction, "+" if there is one in the positive x-direction, |
---|
83 | -- "-" if there is one in the negative x-direction, "+-" if there are in both x-directions. |
---|
84 | -- grid[x][y][z][2] contains 0 if there is no connection from (x,y,z) in y-direction, "+" if there is one in the positive y-direction, |
---|
85 | -- "-" if there is one in the negative y-direction, "+-" if there are in both y-directions. |
---|
86 | -- grid[x][y][z][3] contains 0 if there is no connection from (x,y,z) in z-direction, "+" if there is one in the positive z-direction, |
---|
87 | -- "-" if there is one in the negative z-direction, "+-" if there are in both z-directions. |
---|
88 | grid = {} |
---|
89 | for x=-math.floor(sSSize/2),math.floor(sSSize/2) do |
---|
90 | grid[x] = {} |
---|
91 | for y=-math.floor(sSSize/2),math.floor(sSSize/2) do |
---|
92 | grid[x][y]= {} |
---|
93 | for z=-math.floor(sSSize/2),math.floor(sSSize/2) do |
---|
94 | grid[x][y][z]={} |
---|
95 | for i=0,3 do |
---|
96 | grid[x][y][z][i]=0 |
---|
97 | end |
---|
98 | end |
---|
99 | end |
---|
100 | end |
---|
101 | -- End create 4-dim grid. |
---|
102 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
107 | -- This creates an array which stores all the parts, it's size is depending on the global values pDim and sSParts. |
---|
108 | -- The first parameter i, tells us how many parts fit into the array, so it iterates from 1 to sSParts, each part has his own value i. |
---|
109 | -- The second, third and fourth parameters are the relative coordinates of the part, you have to start at (0,0,0) and be sure you fill the array into the right direction. |
---|
110 | -- A short example: your part is 2 griddimensions long and you place it in the game, that the relative coordinate point is at (0,0,0) and the part lies in the positive |
---|
111 | -- z-axis, then you have to use the coordinate point (0,0,1). |
---|
112 | -- The fifth parameter is an array with size 4, at index=0, you have to set 1 if your part covers the gridpoint at (x,y,z), otherwise 0. |
---|
113 | -- At index=1,2,3 you define the possible connection directions (1 for x, 2 for y and 3 for z), be sure to use the notation from above (0, "+-", "+", "-"). |
---|
114 | bodyParts={} |
---|
115 | for i=1,sSParts do |
---|
116 | bodyParts[i]={} |
---|
117 | for x=-math.floor(pDim/2),math.floor(pDim/2) do |
---|
118 | bodyParts[i][x]={} |
---|
119 | for y=-math.floor(pDim/2),math.floor(pDim/2) do |
---|
120 | bodyParts[i][x][y]={} |
---|
121 | for z=-math.floor(pDim/2),math.floor(pDim/2) do |
---|
122 | bodyParts[i][x][y][z]={} |
---|
123 | for k=0,3 do |
---|
124 | bodyParts[i][x][y][z][k]=0 |
---|
125 | end |
---|
126 | end |
---|
127 | end |
---|
128 | end |
---|
129 | -- This contains the name of the mesh file. |
---|
130 | bodyParts[i][0][0][0][4]="" |
---|
131 | -- This contains the first possible rotation of your part, pitch=... yaw=... roll=... . |
---|
132 | bodyParts[i][0][0][0][5]="" |
---|
133 | -- This contains the second possible rotation of your part, pitch=... yaw=... roll=... . |
---|
134 | bodyParts[i][0][0][0][6]="" |
---|
135 | -- This contains the third possible rotation of your part, pitch=... yaw=... roll=... . |
---|
136 | bodyParts[i][0][0][0][7]="" |
---|
137 | -- Contains the movement rotation, rotationaxis=... rotationrate=... . |
---|
138 | bodyParts[i][0][0][0][8]="" |
---|
139 | -- Contains the attachment, if your part has an attachment, e.g. <ParticleEmitter .../>. |
---|
140 | bodyParts[i][0][0][0][9]="" |
---|
141 | end |
---|
142 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
147 | -- Here you can add a part to the space station, there are some examples here and how to describe your part is written above in the commentary. |
---|
148 | |
---|
149 | -- The part must be inserted so, that the center of reference is at position (0,0,0). |
---|
150 | -- At position bodyParts[i][0][0][0][4] you have to put the mesh name of your part. |
---|
151 | -- At bodyParts[i][0][0][0][5] you can rotate your part, with pitch=angle, yaw=angle or roll=angle (x,y or z). Positive angle means in screw direction. |
---|
152 | -- At bodyParts[i][0][0][0][6] you have another possibility to rotate your part in a different way, e.g. for left and right side parts. |
---|
153 | -- At bodyParts[i][0][0][0][7] you have a third possibility to rotate your part, e.g. connection parts must be rotated into 3 different ways. |
---|
154 | -- At bodyParts[i][0][0][0][8] you can rotate your part around his own axis, with rotationaxis="x,y,z" rotationrate=number. |
---|
155 | -- At bodyParts[i][0][0][0][9] you can attach something to your model, e.g. <ParticleEmitter .../>. |
---|
156 | |
---|
157 | -- Insert the CuboidBody, which is only one griddimension and can have connections in every direction. |
---|
158 | bodyParts[1][0][0][0][4]="CuboidBody.mesh" |
---|
159 | |
---|
160 | bodyParts[1][0][0][0][0]=1 |
---|
161 | bodyParts[1][0][0][0][1]="+-" |
---|
162 | bodyParts[1][0][0][0][2]="+-" |
---|
163 | bodyParts[1][0][0][0][3]="+-" |
---|
164 | -- End insert CuboidBody. |
---|
165 | |
---|
166 | -- Insert the DoubleCuboidBody, which is two griddimensions long, and one wide and high and can have connections in every direction except in the middle. |
---|
167 | bodyParts[2][0][0][0][4]="DoubleCuboidBody.mesh" |
---|
168 | bodyParts[2][0][0][0][5]="pitch=-90" |
---|
169 | |
---|
170 | bodyParts[2][0][0][0][0]=1 |
---|
171 | bodyParts[2][0][0][0][1]="+-" |
---|
172 | bodyParts[2][0][0][0][2]="+-" |
---|
173 | bodyParts[2][0][0][0][3]="-" |
---|
174 | |
---|
175 | bodyParts[2][0][0][1][0]=1 |
---|
176 | bodyParts[2][0][0][1][1]="+-" |
---|
177 | bodyParts[2][0][0][1][2]="+-" |
---|
178 | bodyParts[2][0][0][1][3]="+" |
---|
179 | -- End insert DoubleCuboidBody. |
---|
180 | |
---|
181 | -- Insert the CuboidConnectionBody, it is three griddimensions long and one wide and high and can have only connections at griddimension 1 |
---|
182 | -- (except the side in direction of griddimension 2) and griddimension 3 (except the side in direction of griddimension 2). |
---|
183 | bodyParts[3][0][0][0][4]="CuboidConnectionBody.mesh" |
---|
184 | bodyParts[3][0][0][0][5]="pitch=-90" |
---|
185 | |
---|
186 | bodyParts[3][0][0][0][0]=1 |
---|
187 | bodyParts[3][0][0][0][1]="+-" |
---|
188 | bodyParts[3][0][0][0][2]="+-" |
---|
189 | bodyParts[3][0][0][0][3]="-" |
---|
190 | |
---|
191 | bodyParts[3][0][0][1][0]=1 |
---|
192 | |
---|
193 | bodyParts[3][0][0][2][0]=1 |
---|
194 | bodyParts[3][0][0][2][1]="+-" |
---|
195 | bodyParts[3][0][0][2][2]="+-" |
---|
196 | bodyParts[3][0][0][2][3]="+" |
---|
197 | -- End insert CuboidConnectionBody. |
---|
198 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
199 | |
---|
200 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
201 | -- Insert the Thruster, which is one griddimension long, wide and high, it can only have a connection into the negative z-direction. |
---|
202 | -- If you're space station has no thrusters, be sure to set thrusterIndex=false, but maybe you can use this also for other parts, |
---|
203 | -- see section Attach thrusters to learn how thrusers are attached at your space station. |
---|
204 | thrusterIndex=4 |
---|
205 | bodyParts[thrusterIndex][0][0][0][4]="Thruster.mesh" |
---|
206 | bodyParts[thrusterIndex][0][0][0][5]="pitch=-90" |
---|
207 | bodyParts[thrusterIndex][0][0][0][9]="<ParticleEmitter position=\"0,0,0\" source=\"Orxonox/fire3\" />" |
---|
208 | |
---|
209 | bodyParts[thrusterIndex][0][0][0][0]=1 |
---|
210 | bodyParts[thrusterIndex][0][0][0][3]="-" |
---|
211 | --End insert the Thruster. |
---|
212 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
213 | |
---|
214 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
215 | -- Insert the Cockpit. If your space station has no cockpit, be sure to set cockpitIndex=false. |
---|
216 | -- The Cockpit is 3 x-griddimensions long, 3 y-griddimensions and 2 z-griddimensions, it can only have a connection in the positive z-direction. |
---|
217 | cockpitIndex=5 |
---|
218 | bodyParts[cockpitIndex][0][0][0][4]="SemiCircleCockpit.mesh" |
---|
219 | bodyParts[cockpitIndex][0][0][0][5]="pitch=-90 yaw=180" |
---|
220 | |
---|
221 | bodyParts[cockpitIndex][0][0][0][0]=1 |
---|
222 | bodyParts[cockpitIndex][0][0][0][3]="+" |
---|
223 | |
---|
224 | bodyParts[cockpitIndex][-1][0][0][0]=1 |
---|
225 | bodyParts[cockpitIndex][1][0][0][0]=1 |
---|
226 | bodyParts[cockpitIndex][0][-1][0][0]=1 |
---|
227 | bodyParts[cockpitIndex][0][1][0][0]=1 |
---|
228 | bodyParts[cockpitIndex][-1][-1][0][0]=1 |
---|
229 | bodyParts[cockpitIndex][1][-1][0][0]=1 |
---|
230 | bodyParts[cockpitIndex][-1][1][0][0]=1 |
---|
231 | bodyParts[cockpitIndex][1][1][0][0]=1 |
---|
232 | bodyParts[cockpitIndex][0][0][-1][0]=1 |
---|
233 | bodyParts[cockpitIndex][-1][0][-1][0]=1 |
---|
234 | bodyParts[cockpitIndex][1][0][-1][0]=1 |
---|
235 | bodyParts[cockpitIndex][0][-1][-1][0]=1 |
---|
236 | bodyParts[cockpitIndex][0][1][-1][0]=1 |
---|
237 | bodyParts[cockpitIndex][-1][-1][-1][0]=1 |
---|
238 | bodyParts[cockpitIndex][1][-1][-1][0]=1 |
---|
239 | bodyParts[cockpitIndex][-1][1][-1][0]=1 |
---|
240 | bodyParts[cockpitIndex][1][1][-1][0]=1 |
---|
241 | -- End insert Cockpit. |
---|
242 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
243 | |
---|
244 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
245 | -- Insert the side parts. |
---|
246 | -- If your space station has no left side parts, be sure to set leftsidePartsIndex[0]=false. |
---|
247 | -- If your space station has no right side parts, be sure to set rightsidePartsIndex[0]=false. |
---|
248 | leftSidePartsIndex={} |
---|
249 | leftSidePartsIndex[0]="" |
---|
250 | rightSidePartsIndex={} |
---|
251 | rightSidePartsIndex[0]="" |
---|
252 | |
---|
253 | -- Insert the solar panel, which i wanna use as left and right side part. |
---|
254 | leftSidePartsIndex[1]=6 |
---|
255 | rightSidePartsIndex[1]=leftSidePartsIndex[1] |
---|
256 | bodyParts[leftSidePartsIndex[1]][0][0][0][4]="SolarPanel.mesh" |
---|
257 | bodyParts[leftSidePartsIndex[1]][0][0][0][5]="roll=90 pitch="..math.random(0,180) |
---|
258 | bodyParts[rightSidePartsIndex[1]][0][0][0][6]="roll=-90 pitch="..math.random(0,180) |
---|
259 | bodyParts[rightSidePartsIndex[1]][0][0][0][8]="rotationaxis=\"1,0,0\" rotationrate=5" |
---|
260 | bodyParts[leftSidePartsIndex[1]][0][0][0][0]=1 |
---|
261 | bodyParts[leftSidePartsIndex[1]][0][0][1][0]=1 |
---|
262 | bodyParts[leftSidePartsIndex[1]][0][0][-1][0]=1 |
---|
263 | bodyParts[leftSidePartsIndex[1]][0][1][0][0]=1 |
---|
264 | bodyParts[leftSidePartsIndex[1]][0][1][1][0]=1 |
---|
265 | bodyParts[leftSidePartsIndex[1]][0][1][-1][0]=1 |
---|
266 | bodyParts[leftSidePartsIndex[1]][0][-1][0][0]=1 |
---|
267 | bodyParts[leftSidePartsIndex[1]][0][-1][1][0]=1 |
---|
268 | bodyParts[leftSidePartsIndex[1]][0][-1][-1][0]=1 |
---|
269 | -- End insert solar panel. |
---|
270 | |
---|
271 | -- End insert side parts. |
---|
272 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
273 | |
---|
274 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
275 | -- Insert the top parts. |
---|
276 | -- If you have no top parts, be sure to set topPartsIndex[0]=false |
---|
277 | topPartsIndex={} |
---|
278 | topPartsIndex[0]="" |
---|
279 | |
---|
280 | -- Insert the CuboidLandingZone. |
---|
281 | topPartsIndex[1]=7 |
---|
282 | bodyParts[topPartsIndex[1]][0][0][0][4]="CuboidLandingZone.mesh" |
---|
283 | bodyParts[topPartsIndex[1]][0][0][0][5]="pitch=-90" |
---|
284 | |
---|
285 | bodyParts[topPartsIndex[1]][0][0][0][0]=1 |
---|
286 | bodyParts[topPartsIndex[1]][0][0][0][2]="+-" |
---|
287 | bodyParts[topPartsIndex[1]][1][0][0][0]=1 |
---|
288 | bodyParts[topPartsIndex[1]][-1][0][0][0]=1 |
---|
289 | bodyParts[topPartsIndex[1]][0][0][1][0]=1 |
---|
290 | bodyParts[topPartsIndex[1]][1][0][1][0]=1 |
---|
291 | bodyParts[topPartsIndex[1]][-1][0][1][0]=1 |
---|
292 | bodyParts[topPartsIndex[1]][0][0][2][0]=1 |
---|
293 | bodyParts[topPartsIndex[1]][1][0][2][0]=1 |
---|
294 | bodyParts[topPartsIndex[1]][-1][0][2][0]=1 |
---|
295 | bodyParts[topPartsIndex[1]][0][0][3][0]=1 |
---|
296 | bodyParts[topPartsIndex[1]][1][0][3][0]=1 |
---|
297 | bodyParts[topPartsIndex[1]][-1][0][3][0]=1 |
---|
298 | -- End insert the CuboidLanding Zone. |
---|
299 | |
---|
300 | -- Insert the SatelliteDish. |
---|
301 | topPartsIndex[2]=8 |
---|
302 | bodyParts[topPartsIndex[2]][0][0][0][4]="SatelliteDish.mesh" |
---|
303 | bodyParts[topPartsIndex[2]][0][0][0][5]="pitch=-90" |
---|
304 | bodyParts[topPartsIndex[2]][0][0][0][8]="rotationaxis=\"0,1,0\" rotationrate=5" |
---|
305 | bodyParts[topPartsIndex[2]][0][0][0][0]=1 |
---|
306 | bodyParts[topPartsIndex[2]][0][0][1][0]=1 |
---|
307 | bodyParts[topPartsIndex[2]][0][0][-1][0]=1 |
---|
308 | bodyParts[topPartsIndex[2]][1][0][0][0]=1 |
---|
309 | bodyParts[topPartsIndex[2]][1][0][1][0]=1 |
---|
310 | bodyParts[topPartsIndex[2]][1][0][-1][0]=1 |
---|
311 | bodyParts[topPartsIndex[2]][-1][0][0][0]=1 |
---|
312 | bodyParts[topPartsIndex[2]][-1][0][1][0]=1 |
---|
313 | bodyParts[topPartsIndex[2]][-1][0][-1][0]=1 |
---|
314 | -- End insert the SatelliteDish. |
---|
315 | |
---|
316 | -- End insert the top parts. |
---|
317 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
318 | |
---|
319 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
320 | -- Insert the connectionparts, which are used to connect all the bodyparts. |
---|
321 | -- If you're spacestation has no connectionparts, be sure to set connPartsIndex[0]=false. |
---|
322 | connPartsIndex={} |
---|
323 | connPartsIndex[0]="" |
---|
324 | |
---|
325 | -- Insert the CuboidConnection. |
---|
326 | connPartsIndex[1]=9 |
---|
327 | bodyParts[connPartsIndex[1]][0][0][0][4]="CuboidConnection.mesh" |
---|
328 | bodyParts[connPartsIndex[1]][0][0][0][5]="roll=90" |
---|
329 | bodyParts[connPartsIndex[1]][0][0][0][6]="" |
---|
330 | bodyParts[connPartsIndex[1]][0][0][0][7]="pitch=90" |
---|
331 | -- End insert the CuboidConnection. |
---|
332 | |
---|
333 | -- End insert the connectionparts. |
---|
334 | |
---|
335 | -- End create array bodyParts. |
---|
336 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
337 | |
---|
338 | |
---|
339 | |
---|
340 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
341 | -- Here I define some functions which I will use later. |
---|
342 | |
---|
343 | --This function actualizes the grid, which I have to call always after I have added a new part to the space station. |
---|
344 | function actualizeGrid(Index,x,y,z) |
---|
345 | for i=math.floor(-pDim/2),math.floor(pDim/2) do |
---|
346 | for j=math.floor(-pDim/2),math.floor(pDim/2) do |
---|
347 | for k=math.floor(-pDim/2),math.floor(pDim/2) do |
---|
348 | if bodyParts[Index][i][j][k][0] == 1 then |
---|
349 | for l=0,3 do |
---|
350 | grid[x+i][y+j][z+k][l] = bodyParts[Index][i][j][k][l] |
---|
351 | end |
---|
352 | end |
---|
353 | end |
---|
354 | end |
---|
355 | end |
---|
356 | end |
---|
357 | -- End actualizeGrid. |
---|
358 | |
---|
359 | -- This function checks wheter a given parts fits at that position or not. |
---|
360 | -- If the part fits there it returns 1, otherwise 0. |
---|
361 | function checkPart(Index,x,y,z) |
---|
362 | check=1 |
---|
363 | for i=math.floor(-pDim/2),math.floor(pDim/2) do |
---|
364 | for j=math.floor(-pDim/2),math.floor(pDim/2) do |
---|
365 | for k=math.floor(-pDim/2),math.floor(pDim/2) do |
---|
366 | -- If the part occupies the position (i,j,k), the grid must be empty there ((x+i, y+j, z+k)==0), if not, check is zero, |
---|
367 | -- which means that the part doesn't fit there. |
---|
368 | if bodyParts[Index][i][j][k][0] == 1 and grid[x+i][y+j][z+k][0] == 1 then |
---|
369 | check=0 |
---|
370 | end |
---|
371 | end |
---|
372 | end |
---|
373 | end |
---|
374 | return check |
---|
375 | end |
---|
376 | -- End checkPart function. |
---|
377 | |
---|
378 | -- This function prints the model with tempPartIndex in the bodyParts array at position lx,ly,lz. |
---|
379 | -- If you need to rotate the model around his own axis, then you have to set movEntity true and define the details of the rotation in |
---|
380 | -- bodyParts[tempPartIndex][0][0][0][8]. |
---|
381 | -- If your model needs to be rotated like bodyParts[tempPartIndex][0][0][0][5], then side must be 1, for bodyParts[tempPartIndex][0][0][0][6] side must be 2, |
---|
382 | -- for bodyParts[tempPartIndex][0][0][0][7] side must be 3. |
---|
383 | function printModel(lx,ly,lz,tempPartIndex,movEntity,side) |
---|
384 | if movEntity == true then |
---|
385 | print("<MovableEntity scale=1 position=\"") print(lx*gridDim*sSScale) print(",") print(ly*gridDim*sSScale) print(",") print(lz*gridDim*sSScale) print("\" ") |
---|
386 | print(bodyParts[tempPartIndex][0][0][0][8]) print(">") |
---|
387 | print("<attached>") |
---|
388 | lx=0 ly=0 lz=0 |
---|
389 | end |
---|
390 | |
---|
391 | print("<Model position=\"") print(lx*gridDim*sSScale) print(",") print(ly*gridDim*sSScale) print(",") print(lz*gridDim*sSScale) |
---|
392 | print("\" scale=") print(sSScale) print(" mesh= \"") print(bodyParts[tempPartIndex][0][0][0][4]) print("\"") |
---|
393 | |
---|
394 | if side == 1 then |
---|
395 | print(bodyParts[tempPartIndex][0][0][0][5]) print(">") |
---|
396 | elseif side == 2 then |
---|
397 | print(bodyParts[tempPartIndex][0][0][0][6]) print(">") |
---|
398 | elseif side == 3 then |
---|
399 | print(bodyParts[tempPartIndex][0][0][0][7]) print(">") |
---|
400 | end |
---|
401 | |
---|
402 | print("<attached>") |
---|
403 | print(bodyParts[tempPartIndex][0][0][0][9]) |
---|
404 | print("</attached>") |
---|
405 | |
---|
406 | print("</Model>") |
---|
407 | |
---|
408 | if movEntity == true then |
---|
409 | print("</attached>") |
---|
410 | print("</MovableEntity>") |
---|
411 | end |
---|
412 | end |
---|
413 | -- End function printModel() |
---|
414 | |
---|
415 | -- End define functions. |
---|
416 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
417 | |
---|
418 | |
---|
419 | |
---|
420 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
421 | -- This is xml code, which means now we attach some parts to the MovableEntity. |
---|
422 | print("<attached>") |
---|
423 | -- End attach to the MovableEntity. |
---|
424 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
425 | |
---|
426 | |
---|
427 | |
---|
428 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
429 | -- Attach all bodyparts. |
---|
430 | -- Define at which position in the x-direction you're space station will start. |
---|
431 | x=math.random(-math.floor(xBPLength/2),-math.floor(xBPLength/2)+xBPVar) |
---|
432 | -- Define at which position in the x-direction you're space station will end. |
---|
433 | xMax=math.random(math.floor(xBPLength/2),math.floor(xBPLength/2)+xBPVar) |
---|
434 | while x<xMax do |
---|
435 | -- The same for the y- and z-direction. |
---|
436 | y=math.random(-math.floor(yBPLength/2),-math.floor(yBPLength/2)+yBPVar) |
---|
437 | yMax=math.random(math.floor(yBPLength/2),math.floor(yBPLength/2)+yBPVar) |
---|
438 | while y<yMax do |
---|
439 | yMax=math.random(math.floor(yBPLength/2),math.floor(yBPLength/2)+yBPVar) |
---|
440 | z=math.random(-math.floor(zBPLength/2),-math.floor(zBPLength/2)+zBPVar) |
---|
441 | zMax=math.random(math.floor(zBPLength/2),math.floor(zBPLength/2)+zBPVar) |
---|
442 | while z<zMax do |
---|
443 | -- This loop choses a bodypart, which fits at position (x,y,z). |
---|
444 | -- If after the fifth time the part does still not fit we terminate the loop and set no part at postition (x,y,z). |
---|
445 | partSet=0 |
---|
446 | counter=0 |
---|
447 | while counter<5 and partSet==0 do |
---|
448 | -- This choses randomly a bodyPartIndex, which is the index used for the parts in the array bodyParts. |
---|
449 | tempBodyPartIndex=math.random(1,sSBodyParts) |
---|
450 | check=checkPart(tempBodyPartIndex,x,y,z) |
---|
451 | -- If check == 1, this means that the part fits there, so we put it there and break the while true loop, to go on. |
---|
452 | if check == 1 then |
---|
453 | -- This prints the chosen part at position (x*gridDim*sSScale,y*gridDim*sSScale,z*gridDim*sSScale). |
---|
454 | printModel(x,y,z,tempBodyPartIndex,false,1) |
---|
455 | -- This actualizes the grid array with the values of the array bodyParts at the position tempBodyPartIndex, which is our randomly chosen part. |
---|
456 | actualizeGrid(tempBodyPartIndex,x,y,z) |
---|
457 | partSet=1 |
---|
458 | end |
---|
459 | counter=counter+1 |
---|
460 | end |
---|
461 | z=z+1 |
---|
462 | end |
---|
463 | y=y+1 |
---|
464 | end |
---|
465 | x=x+1 |
---|
466 | end |
---|
467 | -- End attach all bodyparts. |
---|
468 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
469 | |
---|
470 | |
---|
471 | |
---|
472 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
473 | -- Attach thrusters, if there are some. |
---|
474 | if thrusterIndex ~= false then |
---|
475 | -- To attach thrusters we start at (-sSSize/2,-sSSize/2,-sSSize/2+1) and iterate through x and y as start points and then through z, |
---|
476 | -- where we go as long as there are parts, at the first position where isn't a part we set our thruster. |
---|
477 | for x=math.floor(-sSSize/2),math.floor(sSSize/2) do |
---|
478 | for y=math.floor(-sSSize/2),math.floor(sSSize/2) do |
---|
479 | for z=math.floor(-sSSize/2)+1,math.floor(sSSize/2) do |
---|
480 | if grid[x][y][z-1][0] == 1 and grid[x][y][z][0] == 0 then |
---|
481 | -- This prints the thruster. |
---|
482 | printModel(x,y,z,thrusterIndex,false,1) |
---|
483 | -- This actualizes the grid array with the values of the array bodyParts at the position thrusterIndex. |
---|
484 | actualizeGrid(thrusterIndex,x,y,z) |
---|
485 | -- This breaks out of the for z=-sSSize/2+1,sSSize/2 loop, because we have set one thruster and for the z-axis that is all we want. |
---|
486 | break |
---|
487 | end |
---|
488 | end |
---|
489 | end |
---|
490 | end |
---|
491 | end |
---|
492 | -- End attach Thrusters. |
---|
493 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
494 | |
---|
495 | |
---|
496 | |
---|
497 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
498 | -- Attach cockpit, if there is one. |
---|
499 | function setCockpit() |
---|
500 | if grid[x][y][z][0] == 0 and grid[x][y][z+1][0] == 1 then |
---|
501 | -- This checks whether the cockpits fits at that position or not. |
---|
502 | check=checkPart(cockpitIndex,x,y,z) |
---|
503 | if check == 1 then |
---|
504 | -- This prints the cockpit. |
---|
505 | printModel(x,y,z,cockpitIndex,false,1) |
---|
506 | cockpitSet=1 |
---|
507 | -- This actualizes the grid array with the values of the array bodyParts at the position cockpitIndex. |
---|
508 | actualizeGrid(cockpitIndex,x,y,z) |
---|
509 | end |
---|
510 | end |
---|
511 | end |
---|
512 | |
---|
513 | if cockpitIndex ~= false then |
---|
514 | cockpitSet=0 |
---|
515 | z=math.floor(-sSSize/2) |
---|
516 | while z<=math.floor(sSSize/2)-1 and cockpitSet==0 do |
---|
517 | round=0 |
---|
518 | while round<=math.floor(sSSize/2)-1 and cockpitSet==0 do |
---|
519 | y=round |
---|
520 | x=-round |
---|
521 | while x<=round and cockpitSet==0 do |
---|
522 | setCockpit() |
---|
523 | x=x+1 |
---|
524 | end |
---|
525 | while y>=-round and cockpitSet==0 do |
---|
526 | setCockpit() |
---|
527 | y=y-1 |
---|
528 | end |
---|
529 | while x>-round and cockpitSet==0 do |
---|
530 | setCockpit() |
---|
531 | x=x-1 |
---|
532 | end |
---|
533 | while y<=round and cockpitSet==0 do |
---|
534 | setCockpit() |
---|
535 | y=y+1 |
---|
536 | end |
---|
537 | round=round+1 |
---|
538 | end |
---|
539 | z=z+1 |
---|
540 | end |
---|
541 | end |
---|
542 | -- End attach cockpit. |
---|
543 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
544 | |
---|
545 | |
---|
546 | |
---|
547 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
548 | -- Attach parts on the left side of the space station. |
---|
549 | function setLeftSidePart() |
---|
550 | if grid[x][y][z][0] == 0 and grid[x+1][y][z][0] == 1 and (grid[x+1][y][z][1] == "+-" or grid[x+1][y][z][1] == "-") then |
---|
551 | -- This checks whether the parts fits at position x,y,z or not. |
---|
552 | check=checkPart(tempSidePartIndex,x,y,z) |
---|
553 | if check == 1 then |
---|
554 | -- This prints the part. |
---|
555 | printModel(x,y,z,tempSidePartIndex,true,1) |
---|
556 | partSet=1 |
---|
557 | -- This actualizes the grid array with the values of the array bodyParts at the position tempSidePartIndex. |
---|
558 | actualizeGrid(tempSidePartIndex,x,y,z) |
---|
559 | end |
---|
560 | |
---|
561 | end |
---|
562 | end |
---|
563 | |
---|
564 | if leftSidePartsIndex[0] ~= false then |
---|
565 | for sPC=1,leftSideParts do |
---|
566 | tempSidePartIndex = leftSidePartsIndex[math.random(1,leftSideParts)] |
---|
567 | partSet=0 |
---|
568 | x=math.floor(-sSSize/2) |
---|
569 | while x<=math.floor(sSSize/2)-1 and partSet==0 do |
---|
570 | round=0 |
---|
571 | while round<=math.floor(sSSize/2)-1 and partSet==0 do |
---|
572 | y=round |
---|
573 | z=-round |
---|
574 | while z<=round and partSet==0 do |
---|
575 | setLeftSidePart() |
---|
576 | z=z+1 |
---|
577 | end |
---|
578 | while y>=-round and partSet==0 do |
---|
579 | setLeftSidePart() |
---|
580 | y=y-1 |
---|
581 | end |
---|
582 | while z>=-round and partSet==0 do |
---|
583 | setLeftSidePart() |
---|
584 | z=z-1 |
---|
585 | end |
---|
586 | while y<=round and partSet==0 do |
---|
587 | setLeftSidePart() |
---|
588 | y=y+1 |
---|
589 | end |
---|
590 | round=round+1 |
---|
591 | end |
---|
592 | x=x+1 |
---|
593 | end |
---|
594 | end |
---|
595 | end |
---|
596 | -- End attach left side parts. |
---|
597 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
598 | |
---|
599 | |
---|
600 | |
---|
601 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
602 | -- Attach parts on the right side of the space station. |
---|
603 | function setRightSidePart() |
---|
604 | if grid[x][y][z][0] == 0 and grid[x-1][y][z][0] == 1 and (grid[x-1][y][z][1] == "+-" or grid[x-1][y][z][1] == "+") then |
---|
605 | -- This checks whether the part fits or not. |
---|
606 | check=checkPart(tempSidePartIndex,x,y,z) |
---|
607 | if check == 1 then |
---|
608 | -- This prints the part. |
---|
609 | printModel(x,y,z,tempSidePartIndex,true,2) |
---|
610 | partSet=1 |
---|
611 | -- This actualizes the grid array with the values of the array bodyParts at the position tempSidePartIndex. |
---|
612 | actualizeGrid(tempSidePartIndex,x,y,z) |
---|
613 | end |
---|
614 | |
---|
615 | end |
---|
616 | end |
---|
617 | |
---|
618 | if rightSidePartsIndex[0] ~= false then |
---|
619 | for sPC=1,rightSideParts do |
---|
620 | tempSidePartIndex = rightSidePartsIndex[math.random(1,rightSideParts)] |
---|
621 | partSet=0 |
---|
622 | x=math.floor(sSSize/2) |
---|
623 | while x>=math.floor(-sSSize/2)+1 and partSet==0 do |
---|
624 | round=0 |
---|
625 | while round<=math.floor(sSSize/2)-1 and partSet==0 do |
---|
626 | y=round |
---|
627 | z=-round |
---|
628 | while z<=round and partSet==0 do |
---|
629 | setRightSidePart() |
---|
630 | z=z+1 |
---|
631 | end |
---|
632 | while y>=-round and partSet==0 do |
---|
633 | setRightSidePart() |
---|
634 | y=y-1 |
---|
635 | end |
---|
636 | while z>=-round and partSet==0 do |
---|
637 | setRightSidePart() |
---|
638 | z=z-1 |
---|
639 | end |
---|
640 | while y<=round and partSet==0 do |
---|
641 | setRightSidePart() |
---|
642 | y=y+1 |
---|
643 | end |
---|
644 | round=round+1 |
---|
645 | end |
---|
646 | x=x-1 |
---|
647 | end |
---|
648 | end |
---|
649 | end |
---|
650 | -- End attach right side parts. |
---|
651 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
652 | |
---|
653 | |
---|
654 | |
---|
655 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
656 | -- Attach parts on top of the space station. |
---|
657 | function setTopPart() |
---|
658 | if grid[x][y][z][0] == 0 and grid[x][y-1][z][0] == 1 and (grid[x][y-1][z][2] == "+-" or grid[x][y-1][z][2] == "+") then |
---|
659 | -- This checks whether the part fits or not. |
---|
660 | check=checkPart(tempTopPartIndex,x,y,z) |
---|
661 | if check == 1 then |
---|
662 | -- This prints the part. |
---|
663 | printModel(x,y,z,tempTopPartIndex,true,1) |
---|
664 | partSet=1 |
---|
665 | -- This actualizes the grid array with the values of the array bodyParts at the position tempTopPartIndex. |
---|
666 | actualizeGrid(tempTopPartIndex,x,y,z) |
---|
667 | end |
---|
668 | end |
---|
669 | end |
---|
670 | |
---|
671 | if topPartsIndex[0] ~= false then |
---|
672 | for sPC=1,topParts do |
---|
673 | tempTopPartIndex = topPartsIndex[sPC] |
---|
674 | partSet=0 |
---|
675 | y=math.floor(sSSize/2) |
---|
676 | while y>=math.floor(-sSSize/2)+1 and partSet==0 do |
---|
677 | round=0 |
---|
678 | while round<=math.floor(sSSize/2)-1 and partSet==0 do |
---|
679 | x=round |
---|
680 | z=-round |
---|
681 | while z<=round and partSet==0 do |
---|
682 | setTopPart() |
---|
683 | z=z+1 |
---|
684 | end |
---|
685 | while x>=-round and partSet==0 do |
---|
686 | setTopPart() |
---|
687 | x=x-1 |
---|
688 | end |
---|
689 | while z>=-round and partSet==0 do |
---|
690 | setTopPart() |
---|
691 | z=z-1 |
---|
692 | end |
---|
693 | while x<=round and partSet==0 do |
---|
694 | setTopPart() |
---|
695 | x=x+1 |
---|
696 | end |
---|
697 | round=round+1 |
---|
698 | end |
---|
699 | y=y-1 |
---|
700 | end |
---|
701 | end |
---|
702 | end |
---|
703 | -- End attach top parts. |
---|
704 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
705 | |
---|
706 | |
---|
707 | |
---|
708 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
709 | -- Attach all connectionparts. |
---|
710 | -- This iterates through the whole grid array. |
---|
711 | if connPartsIndex[0] ~= false then |
---|
712 | for x=math.floor(-sSSize/2),math.floor(sSSize/2)-1 do |
---|
713 | for y=math.floor(-sSSize/2),math.floor(sSSize/2)-1 do |
---|
714 | for z=math.floor(-sSSize/2),math.floor(sSSize/2)-1 do |
---|
715 | tempConnPartIndex=connPartsIndex[math.random(1,connParts)] |
---|
716 | -- This checks whether there has to be a connection part between (x,y,z) and (x+1,y,z) or not. First it checks if there is a part at (x,y,z) and |
---|
717 | -- then it checks if that part can have a connection into the positive x-direction, if it can, it checks if there is a part at (x+1,y,z) and |
---|
718 | -- if that part can have a connection into the negative x-direction, if both can, it prints the xml code to set a connection part. |
---|
719 | if grid[x][y][z][0]==1 and (grid[x][y][z][1]=="+" or grid[x][y][z][1]=="+-") and grid[x+1][y][z][0]==1 and (grid[x+1][y][z][1]=="-" or grid[x+1][y][z][1]=="+-") then |
---|
720 | -- This prints the connection part, the +1/2 is because the connection is set exactly in the middle of two gridpoints. |
---|
721 | printModel(x+1/2,y,z,tempConnPartIndex,false,1) |
---|
722 | end |
---|
723 | -- The same as in the x-direction, but for the y-direction. |
---|
724 | if grid[x][y][z][0]==1 and ( grid[x][y][z][2]=="+" or grid[x][y][z][2]=="+-" ) and grid[x][y+1][z][0]==1 and ( grid[x][y+1][z][2]=="-" or grid[x][y+1][z][2]=="+-" ) then |
---|
725 | printModel(x,y+1/2,z,tempConnPartIndex,false,2) |
---|
726 | end |
---|
727 | -- The same as in the x-direction, but for the z-direction. |
---|
728 | if grid[x][y][z][0]==1 and ( grid[x][y][z][3]=="+" or grid[x][y][z][3]=="+-" ) and grid[x][y][z+1][0]==1 and ( grid[x][y][z+1][3]=="-" or grid[x][y][z+1][3]=="+-" ) then |
---|
729 | printModel(x,y,z+1/2,tempConnPartIndex,false,3) |
---|
730 | end |
---|
731 | end |
---|
732 | end |
---|
733 | end |
---|
734 | end |
---|
735 | -- End attach all connectionparts. |
---|
736 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
737 | |
---|
738 | |
---|
739 | |
---|
740 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
741 | -- This is xml code, which ends the attachment and the MovableEntity. |
---|
742 | print("</attached>") |
---|
743 | print("</MovableEntity>") |
---|
744 | -- End ends attachment and MovableEntity. |
---|
745 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
746 | |
---|
747 | |
---|
748 | |
---|
749 | -- End the createSpaceStation function. |
---|
750 | end |
---|
751 | |
---|
752 | |
---|
753 | |
---|