Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/contentcreation/pps/MirkoKaiser/CuboidSS/Version2/levels/CuboidSpaceStation.lua @ 5331

Last change on this file since 5331 was 5329, checked in by mkaiser, 16 years ago

Some changes.

File size: 36.3 KB
RevLine 
[5329]1----------------------------------------------------------------------------------------------------
2-- This lua script creates a totally random generated space station for the orxonox computer game!--
3-- (c) Wallah 2008, published under GPL licence!                                                                                                  --
4----------------------------------------------------------------------------------------------------
5
6--------------------------------------------------------------------------------------------------------------------------------------------------------------------
7-- IMPORTANT: If you need more parameters, do the following: copy the actual function (just the headline and the end statement) to the end of the file,                   --
8-- like I did with createSpaceStation() and let that function call the new function where you can modify the parameters. For all parameters which the old function--
9-- doesn't have you just give the standard default values, which I have defined. This is to make sure, that anyone else who uses the old function can still use it--
10-- the same way he/she always did. If you want a function with less parameters, just create a new one at the end of the file and call this function with some     --
11-- default values. REMEMBER: Function overloading is not possible, be sure to call your function differently from others already existing ones.                                   --
12--------------------------------------------------------------------------------------------------------------------------------------------------------------------
13
14-- This function creates a randomly generated space station.
15-- The first argument ranSeed, must be false, or a positive Integer, if it is false your space station is always chosen randomly, if you give an integer,
16--      your space station will be generated randomly, but once you have the space station it will always be the same.
17-- The argument xLength defines how large the space station will be into the x-direction.
18-- The argument xVar defines how much the space station will vary at the ends in x-direction, this is so that the station is no cube.
19-- The argument yLength is the same as xLength, but for the y-direction.
20-- The argument yVar is the same as xLength, but for the y-direction.
21-- The argument zLength is the same as xLength, but for the z-direction.
22-- The argument zVar is the same as xLength, but for the z-direction.
23-- The argument sSScale scales the station proportionally in all directions.
24function createSpaceStationPar(ranSeed, xLength, xVar, yLength, yVar, zLength, zVar, sSScale)
25
26
27
28----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
29-- This prints xml code, which creates a MovableEntity, which I need to attach all the parts of the space station.
30print("<MovableEntity scale=1 position=\"0,0,0\" >")
31-- End create Movable Entity.
32----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
33
34
35
36----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
37-- Create a randomseed, so that the math.random() function is actually random.
38if ranSeed == false then
39        math.randomseed(os.time())
40else
41        math.randomseed(ranSeed)
42end
43-- End create randomseed.
44----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
45
46
47
48----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
49-- Here you can define some global variables, with which you can modify the space station.
50
51-- Define the maximal size of the grid, this value is based on how large you define the area of bodyparts plus 20 gridpoints for attachparts.
52sSSize=30
53if xLength>=yLength and xLength>=zLength then
54        sSSize=xLength+20
55elseif yLength>=xLength and yLength>=zLength then
56        sSSize=yLength+20
57elseif zLength>=xLength and zLength>=yLength then
58        sSSize=zLength+20
59end
60-- 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.
61sSParts=9
62-- 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.
63sSBodyParts=3
64-- Define how many frontParts you have.
65frontParts=1
66-- Define how many backParts you have.
67backParts=1
68-- Define how many side parts for the left side you have.
69leftSideParts=1
70-- Define how many side parts for the right side you have.
71rightSideParts=1
72-- Define how many top parts you have.
73topParts=2
74-- Define how many connection parts you have.
75connParts=1
76-- 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.
77-- It should be at least 1 bigger than the biggest part needs, because I guarantee that it works with odd numbers, to do so I use the math.floor function.
78pDim=6
79-- 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:
80--      integer*(gridDim-connectionSize), then integer tells you how many griddimensions your part is.
81gridDim=2.25
82-- End define global parameters.
83----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
84
85
86
87----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
88-- This creates a 4-dimensional grid, which tells us if there is a part or not, and in which direction it has connections.
89-- The parameters x,y,z are the axis of the space station, which iterate to sSSize, the maximal size of the space station.
90-- 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,
91--      and so on for the other dimensions y and z.
92-- grid[x][y][z][0] contains 0 if there is no part at the position (x,y,z), otherwise 1.
93-- 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,
94--      "-" if there is one in the negative x-direction, "+-" if there are in both x-directions.
95-- 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,
96--      "-" if there is one in the negative y-direction, "+-" if there are in both y-directions.
97-- 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,
98--      "-" if there is one in the negative z-direction, "+-" if there are in both z-directions.
99grid = {}
100for x=-math.floor(sSSize/2),math.floor(sSSize/2) do
101        grid[x] = {}
102        for y=-math.floor(sSSize/2),math.floor(sSSize/2) do
103                grid[x][y]= {}
104                for z=-math.floor(sSSize/2),math.floor(sSSize/2) do
105                        grid[x][y][z]={}
106                        for i=0,3 do
107                                grid[x][y][z][i]=0
108                        end
109                end
110        end
111end
112-- End create 4-dim grid.
113----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
114
115
116
117----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
118-- This creates an array which stores all the parts, it's size is depending on the global values pDim and sSParts.
119-- 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.
120-- 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.
121--      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
122--      z-axis, then you have to use the coordinate point (0,0,1).
123-- 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.
124--      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, "+-", "+", "-").
125bodyParts={}
126for i=1,sSParts do
127        bodyParts[i]={}
128        for x=-math.floor(pDim/2),math.floor(pDim/2) do
129                bodyParts[i][x]={}
130                for y=-math.floor(pDim/2),math.floor(pDim/2) do
131                        bodyParts[i][x][y]={}
132                        for z=-math.floor(pDim/2),math.floor(pDim/2) do
133                                bodyParts[i][x][y][z]={}
134                                for k=0,3 do
135                                        bodyParts[i][x][y][z][k]=0
136                                end
137                        end
138                end
139        end
140        -- This contains the name of the mesh file.
141        bodyParts[i][0][0][0][4]=""
142        -- This contains the first possible rotation of your part, pitch=... yaw=... roll=... .
143        bodyParts[i][0][0][0][5]=""
144        -- This contains the second possible rotation of your part, pitch=... yaw=... roll=... .
145        bodyParts[i][0][0][0][6]=""
146        -- This contains the third possible rotation of your part, pitch=... yaw=... roll=... .
147        bodyParts[i][0][0][0][7]=""
148        -- Contains the movement rotation, rotationaxis=... rotationrate=... .
149        bodyParts[i][0][0][0][8]=""
150        -- Contains the attachment, if your part has an attachment, e.g. <ParticleEmitter .../>.
151        bodyParts[i][0][0][0][9]=""
152        -- Contains how many of this part you want to attach to your space station.
153        bodyParts[i][0][0][0][10]=1
154end
155----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
156
157
158
159----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
160-- 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.
161
162-- The part must be inserted so, that the center of reference is at position (0,0,0).
163-- At position bodyParts[i][0][0][0][4] you have to put the mesh name of your part.
164-- 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.
165-- 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.
166-- 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.
167-- At bodyParts[i][0][0][0][8] you can rotate your part around his own axis, with rotationaxis="x,y,z" rotationrate=number.
168-- At bodyParts[i][0][0][0][9] you can attach something to your model, e.g. <ParticleEmitter .../>.
169
170-- Insert the CuboidBody, which is only one griddimension and can have connections in every direction.
171bodyParts[1][0][0][0][4]="CuboidBody.mesh"
172
173bodyParts[1][0][0][0][0]=1
174bodyParts[1][0][0][0][1]="+-"
175bodyParts[1][0][0][0][2]="+-"
176bodyParts[1][0][0][0][3]="+-"
177-- End insert CuboidBody.
178
179-- Insert the DoubleCuboidBody, which is two griddimensions long, and one wide and high and can have connections in every direction except in the middle.
180bodyParts[2][0][0][0][4]="DoubleCuboidBody.mesh"
181bodyParts[2][0][0][0][5]="pitch=-90"
182
183bodyParts[2][0][0][0][0]=1
184bodyParts[2][0][0][0][1]="+-"
185bodyParts[2][0][0][0][2]="+-"
186bodyParts[2][0][0][0][3]="-"
187
188bodyParts[2][0][0][1][0]=1
189bodyParts[2][0][0][1][1]="+-"
190bodyParts[2][0][0][1][2]="+-"
191bodyParts[2][0][0][1][3]="+"
192-- End insert DoubleCuboidBody.
193
194-- Insert the CuboidConnectionBody, it is three griddimensions long and one wide and high and can have only connections at griddimension 1
195--      (except the side in direction of griddimension 2) and griddimension 3 (except the side in direction of griddimension 2).
196bodyParts[3][0][0][0][4]="CuboidConnectionBody.mesh"
197bodyParts[3][0][0][0][5]="pitch=-90"
198
199bodyParts[3][0][0][0][0]=1
200bodyParts[3][0][0][0][1]="+-"
201bodyParts[3][0][0][0][2]="+-"
202bodyParts[3][0][0][0][3]="-"
203
204bodyParts[3][0][0][1][0]=1
205
206bodyParts[3][0][0][2][0]=1
207bodyParts[3][0][0][2][1]="+-"
208bodyParts[3][0][0][2][2]="+-"
209bodyParts[3][0][0][2][3]="+"
210-- End insert CuboidConnectionBody.
211----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
212
213----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
214-- Insert the back parts.
215-- If you're space station has no back parts, be sure to set backPartsIndex[0]=false.
216backPartsIndex={}
217backPartsIndex[0]=""
218
219-- Insert the thruster, which is one griddimension long, wide and high, it can only have a connection into the negative z-direction.
220backPartsIndex[1]=4
221bodyParts[backPartsIndex[1]][0][0][0][4]="Thruster.mesh"
222bodyParts[backPartsIndex[1]][0][0][0][5]="pitch=-90"
223bodyParts[backPartsIndex[1]][0][0][0][9]="<ParticleEmitter position=\"0,0,0\" source=\"Orxonox/fire3\" />"
224bodyParts[backPartsIndex[1]][0][0][0][10]=5
225
226bodyParts[backPartsIndex[1]][0][0][0][0]=1
227bodyParts[backPartsIndex[1]][0][0][0][3]="-"
228-- End insert the thruster.
229
230-- End insert the back parts.
231----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
232
233----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
234-- Insert the front parts. If your space station has no front parts, be sure to set frontPartsIndex[0]=false.
235frontPartsIndex={}
236frontPartsIndex[0]=""
237
238-- The SemiCircleCockpit is 3 x-griddimensions long, 3 y-griddimensions and 2 z-griddimensions, it can only have a connection in the positive z-direction.
239frontPartsIndex[1]=5
240bodyParts[frontPartsIndex[1]][0][0][0][4]="SemiCircleCockpit.mesh"
241bodyParts[frontPartsIndex[1]][0][0][0][5]="pitch=-90 yaw=180"
242bodyParts[frontPartsIndex[1]][0][0][0][9]="<BlinkingBillboard position=\"0,-0.1,2.2\" material=\"Examples/Flare\" colour=\"1.0, 1.0, 1.0\" amplitude=0.01 frequency=0.5 quadratic=1 /> <BlinkingBillboard position=\"0.5,-0.1,2.2\" material=\"Examples/Flare\" colour=\"1.0, 1.0, 1.0\" amplitude=0.01 frequency=0.5 phase=120 quadratic=1 /> <BlinkingBillboard position=\"-0.5,-0.1,2.2\" material=\"Examples/Flare\" colour=\"1.0, 1.0, 1.0\" amplitude=0.01 frequency=0.5 phase=240 quadratic=1 />"
243
244bodyParts[frontPartsIndex[1]][0][0][0][0]=1
245bodyParts[frontPartsIndex[1]][0][0][0][3]="+"
246
247bodyParts[frontPartsIndex[1]][-1][0][0][0]=1
248bodyParts[frontPartsIndex[1]][1][0][0][0]=1
249bodyParts[frontPartsIndex[1]][0][-1][0][0]=1
250bodyParts[frontPartsIndex[1]][0][1][0][0]=1
251bodyParts[frontPartsIndex[1]][-1][-1][0][0]=1
252bodyParts[frontPartsIndex[1]][1][-1][0][0]=1
253bodyParts[frontPartsIndex[1]][-1][1][0][0]=1
254bodyParts[frontPartsIndex[1]][1][1][0][0]=1
255bodyParts[frontPartsIndex[1]][0][0][-1][0]=1
256bodyParts[frontPartsIndex[1]][-1][0][-1][0]=1
257bodyParts[frontPartsIndex[1]][1][0][-1][0]=1
258bodyParts[frontPartsIndex[1]][0][-1][-1][0]=1
259bodyParts[frontPartsIndex[1]][0][1][-1][0]=1
260bodyParts[frontPartsIndex[1]][-1][-1][-1][0]=1
261bodyParts[frontPartsIndex[1]][1][-1][-1][0]=1
262bodyParts[frontPartsIndex[1]][-1][1][-1][0]=1
263bodyParts[frontPartsIndex[1]][1][1][-1][0]=1
264-- End insert SemiCircleCockpit.
265
266-- End insert the front parts.
267----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
268
269----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
270-- Insert the side parts.
271-- If your space station has no left side parts, be sure to set leftsidePartsIndex[0]=false.
272-- If your space station has no right side parts, be sure to set rightsidePartsIndex[0]=false.
273leftSidePartsIndex={}
274leftSidePartsIndex[0]=""
275rightSidePartsIndex={}
276rightSidePartsIndex[0]=""
277
278-- Insert the solar panel, which i wanna use as left and right side part.
279leftSidePartsIndex[1]=6
280rightSidePartsIndex[1]=leftSidePartsIndex[1]
281bodyParts[leftSidePartsIndex[1]][0][0][0][4]="SolarPanel.mesh"
282bodyParts[leftSidePartsIndex[1]][0][0][0][5]="roll=90 pitch="..math.random(0,180)
283bodyParts[rightSidePartsIndex[1]][0][0][0][6]="roll=-90 pitch="..math.random(0,180)
284bodyParts[rightSidePartsIndex[1]][0][0][0][8]="rotationaxis=\"1,0,0\" rotationrate=2"
285bodyParts[leftSidePartsIndex[1]][0][0][0][0]=1
286bodyParts[leftSidePartsIndex[1]][0][0][1][0]=1
287bodyParts[leftSidePartsIndex[1]][0][0][-1][0]=1
288bodyParts[leftSidePartsIndex[1]][0][1][0][0]=1
289bodyParts[leftSidePartsIndex[1]][0][1][1][0]=1
290bodyParts[leftSidePartsIndex[1]][0][1][-1][0]=1
291bodyParts[leftSidePartsIndex[1]][0][-1][0][0]=1
292bodyParts[leftSidePartsIndex[1]][0][-1][1][0]=1
293bodyParts[leftSidePartsIndex[1]][0][-1][-1][0]=1
294-- End insert solar panel.
295
296-- End insert side parts.
297----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
298
299----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
300-- Insert the top parts.
301-- If you have no top parts, be sure to set topPartsIndex[0]=false
302topPartsIndex={}
303topPartsIndex[0]=""
304
305-- Insert the CuboidLandingZone.
306topPartsIndex[1]=7
307bodyParts[topPartsIndex[1]][0][0][0][4]="CuboidLandingZone.mesh"
308bodyParts[topPartsIndex[1]][0][0][0][5]="pitch=-90"
309
310bodyParts[topPartsIndex[1]][0][0][0][0]=1
311bodyParts[topPartsIndex[1]][0][0][0][2]="+-"
312bodyParts[topPartsIndex[1]][1][0][0][0]=1
313bodyParts[topPartsIndex[1]][-1][0][0][0]=1
314bodyParts[topPartsIndex[1]][0][0][1][0]=1
315bodyParts[topPartsIndex[1]][1][0][1][0]=1
316bodyParts[topPartsIndex[1]][-1][0][1][0]=1
317bodyParts[topPartsIndex[1]][0][0][2][0]=1
318bodyParts[topPartsIndex[1]][1][0][2][0]=1
319bodyParts[topPartsIndex[1]][-1][0][2][0]=1
320bodyParts[topPartsIndex[1]][0][0][3][0]=1
321bodyParts[topPartsIndex[1]][1][0][3][0]=1
322bodyParts[topPartsIndex[1]][-1][0][3][0]=1
323-- End insert the CuboidLandingZone.
324
325-- Insert the SatelliteDish.
326topPartsIndex[2]=8
327bodyParts[topPartsIndex[2]][0][0][0][4]="SatelliteDish.mesh"
328bodyParts[topPartsIndex[2]][0][0][0][5]="pitch=-90"
329bodyParts[topPartsIndex[2]][0][0][0][8]="rotationaxis=\"0,1,0\" rotationrate=5"
330
331bodyParts[topPartsIndex[2]][0][0][0][0]=1
332bodyParts[topPartsIndex[2]][0][0][1][0]=1
333bodyParts[topPartsIndex[2]][0][0][-1][0]=1
334bodyParts[topPartsIndex[2]][1][0][0][0]=1
335bodyParts[topPartsIndex[2]][1][0][1][0]=1
336bodyParts[topPartsIndex[2]][1][0][-1][0]=1
337bodyParts[topPartsIndex[2]][-1][0][0][0]=1
338bodyParts[topPartsIndex[2]][-1][0][1][0]=1
339bodyParts[topPartsIndex[2]][-1][0][-1][0]=1
340-- End insert the SatelliteDish.
341
342-- End insert the top parts.
343----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
344
345----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
346-- Insert the connection parts, which are used to connect all the bodyparts.
347-- If you're spacestation has no connection parts, be sure to set connPartsIndex[0]=false.
348connPartsIndex={}
349connPartsIndex[0]=""
350
351-- Insert the CuboidConnection.
352connPartsIndex[1]=9
353bodyParts[connPartsIndex[1]][0][0][0][4]="CuboidConnection.mesh"
354bodyParts[connPartsIndex[1]][0][0][0][5]="roll=90"
355bodyParts[connPartsIndex[1]][0][0][0][6]=""
356bodyParts[connPartsIndex[1]][0][0][0][7]="pitch=90"
357-- End insert the CuboidConnection.
358
359-- End insert the connection parts.
360
361-- End create array bodyParts.
362----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
363
364
365
366----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
367-- Here I define some functions which I will use later.
368
369--This function actualizes the grid, which I have to call always after I have added a new part to the space station.
370function actualizeGrid(Index,x,y,z)
371        for i=math.floor(-pDim/2)+1,math.floor(pDim/2) do
372                for j=math.floor(-pDim/2)+1,math.floor(pDim/2) do
373                        for k=math.floor(-pDim/2)+1,math.floor(pDim/2) do
374                                if bodyParts[Index][i][j][k][0] == 1 then
375                                        for l=0,3 do
376                                                grid[x+i][y+j][z+k][l] = bodyParts[Index][i][j][k][l]
377                                        end
378                                end
379                        end
380                end
381        end
382end
383-- End actualizeGrid.
384
385-- This function checks wheter a given parts fits at that position or not.
386-- If the part fits there it returns 1, otherwise 0.
387function checkPart(Index,x,y,z)
388        check=1
389        for i=math.floor(-pDim/2)+1,math.floor(pDim/2) do
390                for j=math.floor(-pDim/2)+1,math.floor(pDim/2) do
391                        for k=math.floor(-pDim/2)+1,math.floor(pDim/2) do
392                                -- 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,
393                                --      which means that the part doesn't fit there.
394                                if bodyParts[Index][i][j][k][0] == 1 and grid[x+i][y+j][z+k][0] == 1 then
395                                        check=0
396                                end
397                        end
398                end
399        end
400        return check
401end
402-- End checkPart function.
403
404-- This function prints the model with tempPartIndex in the bodyParts array at position lx,ly,lz.
405-- 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
406--      bodyParts[tempPartIndex][0][0][0][8].
407-- 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,
408--      for bodyParts[tempPartIndex][0][0][0][7] side must be 3.
409function printModel(lx,ly,lz,tempPartIndex,movEntity,side)
410        if movEntity == true then
411                print("<MovableEntity scale=1 position=\"") print(lx*gridDim*sSScale) print(",") print(ly*gridDim*sSScale) print(",") print(lz*gridDim*sSScale) print("\" ")
412                print(bodyParts[tempPartIndex][0][0][0][8]) print(">")
413                print("<attached>")
414                lx=0 ly=0 lz=0
415        end
416
417        print("<Model position=\"") print(lx*gridDim*sSScale) print(",") print(ly*gridDim*sSScale) print(",") print(lz*gridDim*sSScale)
418        print("\" scale=") print(sSScale) print(" mesh= \"") print(bodyParts[tempPartIndex][0][0][0][4]) print("\"")
419
420                if side == 1 then
421                        print(bodyParts[tempPartIndex][0][0][0][5]) print(">")
422                elseif side == 2 then
423                        print(bodyParts[tempPartIndex][0][0][0][6]) print(">")
424                elseif side == 3 then
425                        print(bodyParts[tempPartIndex][0][0][0][7]) print(">")
426                end
427
428                print("<attached>")
429                        print(bodyParts[tempPartIndex][0][0][0][9])
430                print("</attached>")
431
432        print("</Model>")
433
434        if movEntity == true then
435                print("</attached>")
436                print("</MovableEntity>")
437        end
438end
439-- End function printModel().
440
441-- This function sets a part to a side of the space station.
442-- The arguments lx,ly,lz are the coordinates of the grid, where you want to set the part.
443-- The arguments xAxis,yAxis,zAxis can be 0 or 1, but only one argument out of the three can be 1. This means two of them must always be zero. You have to set xAxis to one,
444--      if your part is attached to a side, which faces into the x-direction (negative or positive, this is later specified with Dir), that means the x-Axis is a normal vector
445--      of the side to which you want to attach the part. The same for yAxis and zAxis.
446-- The argument Dir must be 1 if your side, where you want to attach the part, faces into the positive direction, -1 if the side faces into the negative direction. The side
447--      faces into the positive direction means, that the side of the side where the part will be attached is directed into the direction of the positive direction of the
448--      corresponding axis.
449-- The argument index is the index of the part for the bodyParts array.
450-- The argument printMovEnt must be false if your part doesn't need to be attached to an extra MovableEntity. If your part must be attached to an extra MovableEntity
451--      this argument must be true. The extra MovableEntity is used to rotate the part around his own axis, or something like that.
452-- The argument printSide is like the argument side of the printModel() function. It defines how your part will be rotated. Read the commentary there.
453-- The function returns 0 if the part couldn't be set, because it did not fit there or there was no side to attach the part. It returns 1 if the part is successfully set.
454function setPart(lx,ly,lz,xAxis,yAxis,zAxis,Dir,index,printMovEnt,printSide)
455
456        partSet=0
457        -- For the bodyParts array I use 1 as x-, 2 as y- and 3 as z-Axis for the definition in which directions a part can have connections.
458        coord=1*xAxis+2*yAxis+3*zAxis
459        -- If I try to attach the part from the positive direction to the side of the space station, the part of the station (where I want to attach the new part) must have
460        --      a connection into the positive direction. Otherwise I look from the negative side and so the part of the station must have a connection into the negative direction.
461        if Dir==1 then
462                conn="+"
463        elseif Dir==-1 then
464                conn="-"
465        end
466        -- I look from the direction defined through Dir, and here I check, whether I have reached a side of the space station, which means at position lx,ly,lz is nothing and
467        --      at the next position is a part which can have a connection into the direction from where I look.
468        if grid[lx][ly][lz][0] == 0 and grid[lx+(-1*xAxis*Dir)][ly+(-1*yAxis*Dir)][lz+(-1*zAxis*Dir)][0] == 1 and (grid[lx+(-1*xAxis*Dir)][ly+(-1*yAxis*Dir)][lz+(-1*zAxis*Dir)][coord]=="+-" or grid[lx+(-1*xAxis*Dir)][ly+(-1*yAxis*Dir)][lz+(-1*zAxis*Dir)][coord]==conn) then
469                -- This checks whether the part fits at that position or not.
470                check=checkPart(index,lx,ly,lz)
471                if check == 1 then
472                        -- This prints the part.
473                        printModel(lx,ly,lz,index,printMovEnt,printSide)
474                        partSet=1
475                        -- This actualizes the grid array with the values of the array bodyParts at the position index.
476                        actualizeGrid(index,lx,ly,lz)
477                end
478        end
479        return partSet
480end
481-- End function setPart().
482
483-- This function sets a part to a side of the space station. It is called spiralSet, because it starts in the middle of the side and goes out in a form of a spiral.
484-- The argument xAxis,yAxis,zAxis,Dir,printMovEnt,printSide are the same as the arguments from the setPart() function, please read the commentary there.
485-- The argument index here must be an array, where you define the index for your part for the bodyParts array. The first used index is 1 and goes up to parts.
486-- The argument parts is the number of different parts which you want to attach to a side.
487function spiralSet(xAxis,yAxis,zAxis,Dir,index,parts,printMovEnt,printSide)
488        if index[0] ~= false then
489                -- The array vector contains the actual position where you try to set the part. vector[0],vector[1] and vector[3] contains the x,y,z-coordinate.
490                vector={}
491                -- This must be done, because there are different sides from where I try to attach a part.
492                coord1=1*yAxis+2*zAxis
493                coord2=(coord1+1)%3
494                coord3=(coord2+1)%3
495
496                for pc=1,parts do
497                        tempIndex = index[pc]
498                        for eachPart=1,bodyParts[tempIndex][0][0][0][10] do
499                                partSet=0
500                                vector[coord1]=math.floor(Dir*sSSize/2)-2*Dir
501                                while vector[coord1]~=math.floor(-1*Dir*sSSize/2)+2*Dir and partSet==0 do
502                                        round=0
503                                        while round<=math.floor(sSSize/2)-2 and partSet==0 do
504                                                vector[coord2]=round
505                                                vector[coord3]=-round
506                                                while vector[coord3]<=round and partSet==0 do
507                                                        partSet=setPart(vector[0],vector[1],vector[2],xAxis,yAxis,zAxis,Dir,tempIndex,printMovEnt,printSide)
508                                                        vector[coord3]=vector[coord3]+1
509                                                end
510                                                while vector[coord2]>=-round and partSet==0 do
511                                                        partSet=setPart(vector[0],vector[1],vector[2],xAxis,yAxis,zAxis,Dir,tempIndex,printMovEnt,printSide)
512                                                        vector[coord2]=vector[coord2]-1
513                                                end
514                                                while vector[coord3]>-round and partSet==0 do
515                                                        partSet=setPart(vector[0],vector[1],vector[2],xAxis,yAxis,zAxis,Dir,tempIndex,printMovEnt,printSide)
516                                                        vector[coord3]=vector[coord3]-1
517                                                end
518                                                while vector[coord2]<=round and partSet==0 do
519                                                        partSet=setPart(vector[0],vector[1],vector[2],xAxis,yAxis,zAxis,Dir,tempIndex,printMovEnt,printSide)
520                                                        vector[coord2]=vector[coord2]+1
521                                                end
522                                                round=round+1
523                                        end
524                                        vector[coord1]=vector[coord1]-Dir
525                                end
526                        end
527                end
528        end
529end
530-- End function spiralSet().
531
532-- End define functions.
533----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
534
535
536
537----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
538-- This is xml code, which means now we attach some parts to the MovableEntity.
539print("<attached>")
540-- End attach to the MovableEntity.
541----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
542
543
544
545----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
546-- Attach all bodyparts.
547-- Define at which position in the x-direction you're space station will start.
548x=math.random(-math.floor(xLength/2),-math.floor(xLength/2)+xVar)
549-- Define at which position in the x-direction you're space station will end.
550xMax=math.random(math.floor(xLength/2),math.floor(xLength/2)+xVar)
551while x<xMax do
552        -- The same for the y- and z-direction.
553        y=math.random(-math.floor(yLength/2),-math.floor(yLength/2)+yVar)
554        yMax=math.random(math.floor(yLength/2),math.floor(yLength/2)+yVar)
555        while y<yMax do
556                yMax=math.random(math.floor(yLength/2),math.floor(yLength/2)+yVar)
557                z=math.random(-math.floor(zLength/2),-math.floor(zLength/2)+zVar)
558                zMax=math.random(math.floor(zLength/2),math.floor(zLength/2)+zVar)
559                while z<zMax do
560                        -- This loop choses a bodypart, which fits at position (x,y,z).
561                        -- If after the fifth time the part does still not fit we terminate the loop and set no part at postition (x,y,z).
562                        partSet=0
563                        counter=0
564                        while counter<5 and partSet==0 do
565                                -- This choses randomly a bodyPartIndex, which is the index used for the parts in the array bodyParts.
566                                tempBodyPartIndex=math.random(1,sSBodyParts)
567                                check=checkPart(tempBodyPartIndex,x,y,z)
568                                -- If check == 1, this means that the part fits there, so we put it there and break the while true loop, to go on.
569                                if check == 1 then
570                                        -- This prints the chosen part at position (x*gridDim*sSScale,y*gridDim*sSScale,z*gridDim*sSScale).
571                                        printModel(x,y,z,tempBodyPartIndex,false,1)
572                                        -- This actualizes the grid array with the values of the array bodyParts at the position tempBodyPartIndex, which is our randomly chosen part.
573                                        actualizeGrid(tempBodyPartIndex,x,y,z)
574                                        partSet=1
575                                end
576                                counter=counter+1
577                        end
578                        z=z+1
579                end
580                y=y+1
581        end
582        x=x+1
583end
584-- End attach all bodyparts.
585----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
586
587
588
589----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
590-- Attach backParts, if there are some.
591        spiralSet(0,0,1,1,backPartsIndex,backParts,false,1)
592-- End attach backParts.
593----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
594
595----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
596-- Attach frontParts, if there are.
597        spiralSet(0,0,1,-1,frontPartsIndex,frontParts,false,1)
598-- End attach frontParts.
599----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
600
601----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
602-- Attach parts on the left side of the space station.
603        spiralSet(1,0,0,-1,leftSidePartsIndex,leftSideParts,true,1)
604-- End attach left side parts.
605----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
606
607----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
608-- Attach parts on the right side of the space station.
609        spiralSet(1,0,0,1,rightSidePartsIndex,rightSideParts,true,2)
610-- End attach right side parts.
611----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
612
613----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
614-- Attach parts on top of the space station.
615        spiralSet(0,1,0,1,topPartsIndex,topParts,true,1)
616-- End attach top parts.
617----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
618
619
620
621----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
622-- Attach all connectionparts.
623-- This iterates through the whole grid array.
624if connPartsIndex[0] ~= false then
625        for x=math.floor(-sSSize/2)+2,math.floor(sSSize/2)-2 do
626                for y=math.floor(-sSSize/2)+2,math.floor(sSSize/2)-2 do
627                        for z=math.floor(-sSSize/2)+2,math.floor(sSSize/2)-2 do
628                                tempConnPartIndex=connPartsIndex[math.random(1,connParts)]
629                                -- 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
630                                --      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
631                                --      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.
632                                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
633                                        -- This prints the connection part, the +1/2 is because the connection is set exactly in the middle of two gridpoints.
634                                        printModel(x+1/2,y,z,tempConnPartIndex,false,1)
635                                end
636                                -- The same as in the x-direction, but for the y-direction.
637                                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
638                                        printModel(x,y+1/2,z,tempConnPartIndex,false,2)
639                                end
640                                -- The same as in the x-direction, but for the z-direction.
641                                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
642                                        printModel(x,y,z+1/2,tempConnPartIndex,false,3)
643                                end
644                        end
645                end
646        end
647end
648-- End attach all connectionparts.
649----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
650
651
652
653----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
654-- This is xml code, which ends the attachment and the MovableEntity.
655print("</attached>")
656print("</MovableEntity>")
657-- End ends attachment and MovableEntity.
658----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
659
660
661
662end
663-- End createSpaceStationPar() function.
664
665
666
667-- This function is for the lazy guys, which do not care how the space station looks like, so I use some good standard values.
668function createSpaceStation()
669        createSpaceStationPar(false,4,1,2,1,6,1,100)
670end
671-- End createSpaceStaion() function.
672
673
674
Note: See TracBrowser for help on using the repository browser.