Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/contentcreation/pps/MirkoKaiser/OrxMod/CuboidSS/SpaceStation1.1.lua @ 5278

Last change on this file since 5278 was 5277, checked in by mkaiser, 16 years ago

The first space station is quite ready, with lua script

File size: 15.3 KB
Line 
1-- This lua script creates a totally random generated space station for the orxonox computer game!
2
3
4
5-- This prints xml code, which creates a MovableEntity, which I need to attach all the parts of the space station, if you want to move, rotate or displace the whole space station, this is the line you have to change.
6print("<MovableEntity scale=1 position=\"0,0,-1000\" velocity=\"0,0,0\" rotationaxis=\"0,0,1\" rotationrate=0>")
7
8
9
10-- Create a randomseed, so that the math.random() function is actually random.
11        math.randomseed(os.time())
12-- End create randomseed.
13
14
15
16-- Here you can define some global variables, with which you can modify the space station.
17        -- Define the maximal size of the space station, this is actually just for the grid, be sure that this value is enough big.
18        -- To actually define the size of the space station you have to change the values in the section Attach all bodyparts, be sure that that values are some griddimensions (at least the sSSize-(pDim)) smaller than sSSize.
19        sSSize=30
20        -- Define how many parts the space station have, this value has to be exact, so be sure to increment if you're adding a new part.
21        sSParts=4
22        -- Define how many body parts the space station have, this value has to be exact. Body part means a part, which has connections at least in two directions.
23        sSBodyParts=3
24        -- 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.
25        pDim=2
26        -- Define the length in x-direction of the space station which will be occupied by bodyparts.
27        xBPLength=3
28        -- Define the variation of the edges of your bodyparts in the x-direction.
29        xBPVar=1
30        -- Define the length in y-direction of the space station which will be occupied by bodyparts.
31        yBPLength=3
32        -- Define the variation of the edges of your bodyparts in the y-direction.
33        yBPVar=1
34        -- Define the length in the z-direction of the space station which will be occupied by bodyparts.
35        zBPLength=6
36        -- Define the variation of the edges of your bodyparts in the z-direction.
37        zBPVar=1
38        -- Define the scale of the space station.
39        sSScale=100
40        -- 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: integer*(gridDim-connectionSize), then integer tells you how many griddimensions your part is.
41        gridDim=2.25
42-- End define global parameters.
43
44
45
46-- This creates a 4-dimensional grid, which tells us if there is a part or not, and in which direction it has connections.
47-- The parameters x,y,z are the axis of the space station, which iterate to sSSize, the maximal size of the space station.
48-- The griddimension, this word I will use later, means that the distance of a point to the next point is 2,25 in the game, so the absolute x-axis is x*2,25*sSScale, and so on for the other dimensions y and z.
49-- grid[x][y][z][0] contains 0 if there is no part at the position (x,y,z), otherwise 1.
50-- 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, "-" if there is one in the negative x-direction, "+-" if there are in both x-directions.
51-- 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, "-" if there is one in the negative y-direction, "+-" if there are in both y-directions.
52-- 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, "-" if there is one in the negative z-direction, "+-" if there are in both z-directions.
53        grid = {}
54        for x=0,sSSize do
55                grid[x] = {}
56                for y=0,sSSize do
57                        grid[x][y]= {}
58                        for z=0,sSSize do
59                                grid[x][y][z]={}
60                                for i=0,3 do
61                                        grid[x][y][z][i]=0
62                                end
63                        end
64                end
65        end
66-- End create 4-dim grid.
67
68
69
70-- This creates an array which stores all the bodyparts, it's size is depending on the global values pDim and sSParts.
71-- The first parameter i, tells us how many parts fit into the array, so it iterates from 0 to sSParts-1, each part has his own value i.
72-- 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. 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 z-axis, then you have to use the coordinate point (0,0,1).
73-- 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. 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, "+-", "+", "-").
74        bodyParts={}
75        for i=0,sSParts-1 do
76                bodyParts[i]={}
77                for x=0,pDim do
78                        bodyParts[i][x]={}
79                        for y=0,pDim do
80                                bodyParts[i][x][y]={}
81                                for z=0,pDim do
82                                        bodyParts[i][x][y][z]={}
83                                        for k=0,3 do
84                                                bodyParts[i][x][y][z][k]=0
85                                        end
86                                        bodyParts[i][x][y][z][4]=""
87                                        bodyParts[i][x][y][z][5]=""
88                                end
89                        end
90                end
91        end
92       
93        -- 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.
94        -- At position bodyParts[i][x][y][z][4] you have to put the mesh name of your part.
95        -- At bodyParts[i][x][y][z][5] you can rotate your part, with pitch=angle, yaw=angle or roll=angle (x,y or z). Positive angle means in screw direction.
96       
97        -- Insert the CuboidBody, which is only one griddimension and can have connections in every direction.
98        bodyParts[0][0][0][0][4]="CuboidBody.mesh"
99        bodyParts[0][0][0][0][5]=""
100        bodyParts[0][0][0][0][0]=1
101        bodyParts[0][0][0][0][1]="+-"
102        bodyParts[0][0][0][0][2]="+-"
103        bodyParts[0][0][0][0][3]="+-"
104        -- End insert CuboidBody.
105
106        -- Insert the DoubleCuboidBody, which is two griddimensions long, and one wide and high and can have connections in every direction except in the middle.
107        bodyParts[1][0][0][0][4]="DoubleCuboidBody.mesh"
108        bodyParts[1][0][0][0][5]="pitch=-90"
109        bodyParts[1][0][0][0][0]=1
110        bodyParts[1][0][0][0][1]="+-"
111        bodyParts[1][0][0][0][2]="+-"
112        bodyParts[1][0][0][0][3]="-"
113        bodyParts[1][0][0][1][0]=1
114        bodyParts[1][0][0][1][1]="+-"
115        bodyParts[1][0][0][1][2]="+-"
116        bodyParts[1][0][0][1][3]="+"
117        -- End insert DoubleCuboidBody.
118
119        -- Insert the CuboidConnectionLong, which is a Bodypart indeed, it is three griddimensions long and one wide and high and can have only connections at griddimension 1 (except the side in direction of griddimension 2) and griddimension 3 (except the side in direction of griddimension 2).
120        bodyParts[2][0][0][0][4]="CuboidConnectionBody.mesh"
121        bodyParts[2][0][0][0][5]="pitch=-90"
122        bodyParts[2][0][0][0][0]=1
123        bodyParts[2][0][0][0][1]="+-"
124        bodyParts[2][0][0][0][2]="+-"
125        bodyParts[2][0][0][0][3]="-"
126        bodyParts[2][0][0][1][0]=1
127        bodyParts[2][0][0][1][1]=0
128        bodyParts[2][0][0][1][2]=0
129        bodyParts[2][0][0][1][3]=0
130        bodyParts[2][0][0][2][0]=1
131        bodyParts[2][0][0][2][1]="+-"
132        bodyParts[2][0][0][2][2]="+-"
133        bodyParts[2][0][0][2][3]="+"
134        -- End insert CuboidConnectionLong.
135
136        -- Insert the Thruster, which is one griddimension long, wide and high, it can only have a connection into the negative z-direction.
137        -- If you're space station has no thrusters, be sure to set thrusterIndex=false, but maybe you can use this also for other parts, see section Attach thrusters to learn how thrusers are attached at your space station.
138        thrusterIndex=3
139        bodyParts[thrusterIndex][0][0][0][4]="Thruster.mesh"
140        bodyParts[thrusterIndex][0][0][0][5]="pitch=-90"
141        bodyParts[thrusterIndex][0][0][0][0]=1
142        bodyParts[thrusterIndex][0][0][0][1]=0
143        bodyParts[thrusterIndex][0][0][0][2]=0
144        bodyParts[thrusterIndex][0][0][0][3]="-"
145        --End insert the Thruster.
146
147-- End create array bodyParts.
148
149
150
151-- This is xml code, which means now we attach some parts to the MovableEntity.
152print("<attached>")
153
154
155
156-- Attach all bodyparts.
157-- This needs some explanation: My grid can't have negative indexes, but if the space station should rotate around it's own middle, i have to put the space station around the point (0,0,0), which means about half of the station has negative coordinates, to solve this problem, i do the following. I start in the middle of my grid (sSSize/2), subtract (BPLength/2) and let it run up to sSSize/2+BPLength/2, so in the grid my bodyparts will be placed around the middle of my grid, with some randomly chosen variation for each coordinate, so my station isn't a cubus. To transform that into the game i simply subtract 1/2*sSSize, so the space station is around the point (0,0,0).
158        -- Define at which position in the x-direction you're space station will start.
159        x=math.random(math.floor(sSSize/2)-xBPLength/2,math.floor(sSSize/2)-xBPLength/2+xBPVar)
160        -- Define at which position in the x-direction you're space station will end.
161        xMax=math.random(math.floor(sSSize/2)+xBPLength/2,math.floor(sSSize/2)+xBPLength/2+xBPVar)
162        while x<xMax do
163                -- The same for the y- and z-direction.
164                y=math.random(math.floor(sSSize/2)-yBPLength/2,math.floor(sSSize/2)-yBPLength/2+yBPVar)
165                yMax=math.random(math.floor(sSSize/2)+yBPLength/2,math.floor(sSSize/2)+yBPLength/2+yBPVar)
166                while y<yMax do
167                        yMax=math.random(math.floor(sSSize/2)+yBPLength/2,math.floor(sSSize/2)+yBPLength/2+yBPVar)
168                        z=math.random(math.floor(sSSize/2)-zBPLength/2,math.floor(sSSize/2)-zBPLength/2+zBPVar)
169                        zMax=math.random(math.floor(sSSize/2)+zBPLength/2,math.floor(sSSize/2)+zBPLength/2+zBPVar)
170                        while z<zMax do
171                                -- This loop choses a bodypart, which fits at position (x,y,z), check=1 is to start the loop, if after the fifth time the part does still not fit we terminate the loop and set no part at postition (x,y,z).
172                                check=1
173                                counter=0
174                                while counter <5 and check==1 do
175                                        -- This choses randomly a bodyPartIndex, which is the index used for the parts in the array bodyParts.
176                                        tempBodyPartIndex=math.random(0,sSBodyParts-1)
177                                        -- Check whether the randomly chosen part fits at that position or not.
178                                        for i=0,pDim do
179                                                for j=0,pDim do
180                                                        for k=0,pDim do
181                                                                -- 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, which means that the part doesn't fit there, so we do the while loop again.
182                                                                if bodyParts[tempBodyPartIndex][i][j][k][0] == 1 and grid[x+i][y+j][z+k][0] == 1 then
183                                                                        check=0
184                                                                end
185                                                        end
186                                                end
187                                        end
188                                        -- If check == 1, this means that the part fits there, so we put it there and break the while true loop, to go on.
189                                        if check == 1 then
190                                                -- This is xml code which means at position (x*gridDim*sSScale) will be the randomly chosen part.
191                                                print("<Model position=\"") print((x-math.floor(1/2*sSSize))*gridDim*sSScale) print(",") print((y-math.floor(1/2*sSSize))*gridDim*sSScale) print(",") print((z-math.floor(1/2*sSSize))*gridDim*sSScale) print("\" scale=") print(sSScale) print(" mesh= \"") print(bodyParts[tempBodyPartIndex][0][0][0][4]) print("\"") print(bodyParts[tempBodyPartIndex][0][0][0][5]) print(" />")
192                                                -- This actualizes the grid array with the values of the array bodyParts at the position tempBodyPartIndex, which is our randomly chosen part.
193                                                for i=0,pDim do
194                                                        for j=0,pDim do
195                                                                for k=0,pDim do
196                                                                        if bodyParts[tempBodyPartIndex][i][j][k][0] == 1 then
197                                                                                for l=0,3 do
198                                                                                        grid[x+i][y+j][z+k][l] = bodyParts[tempBodyPartIndex][i][j][k][l]
199                                                                                end
200                                                                        end
201                                                                end
202                                                        end
203                                                end
204                                        end
205                                        counter=counter+1
206                                end
207                                z=z+1
208                        end
209                        y=y+1
210                end
211                x=x+1
212        end
213-- End attach all bodyparts.
214
215
216
217-- Attach thrusters, if there are some.
218        if thrusterIndex ~= false then
219                -- To attach thrusters we start at (0,0,1) and iterate through x and y as start points and then through z, where we go as long as there are parts, at the first position where isn't a part we set our thruster.
220                for x=0,sSSize do
221                        for y=0,sSSize do
222                                 for z=1,sSSize do
223                                        if grid[x][y][z-1][0] == 1 and grid[x][y][z][0] == 0 then
224                                                print("<Model position=\"") print((x-math.floor(1/2*sSSize))*gridDim*sSScale) print(",") print((y-math.floor(1/2*sSSize))*gridDim*sSScale) print(",") print((z-math.floor(1/2*sSSize))*gridDim*sSScale) print("\" scale=") print(sSScale) print(" mesh= \"") print(bodyParts[thrusterIndex][0][0][0][4]) print("\"") print(bodyParts[thrusterIndex][0][0][0][5]) print(" >")
225
226                                        print("<attached>")
227                                                print("<ParticleEmitter position=\"0,0,0\" source=\"Orxonox/fire3\" />")
228                                        print("</attached>")
229                                        print("</Model>")
230
231                                                -- This actualizes the array grid.
232                                                for i=0,pDim do
233                                                        for j=0,pDim do
234                                                                for k=0,pDim do
235                                                                        if bodyParts[tempBodyPartIndex][i][j][k][0] == 1 then
236                                                                                for l=0,3 do
237                                                                                        grid[x+i][y+j][z+k][l] = bodyParts[thrusterIndex][i][j][k][l]
238                                                                                end
239                                                                        end
240                                                                end
241                                                        end
242                                                end
243                                                -- This breaks out of the for z=0,sSSize loop, because we have set one thruster and for the z-axis that is all we want.
244                                                break
245                                        end
246                                end
247                        end
248                end
249        end
250-- End attach Thrusters.
251
252
253
254-- Attach cockpit, if there is one.
255       
256-- End attach cockpit.
257
258
259
260-- Attach all connectionparts.
261        -- This iterates through the whole grid array.
262        for x=0,sSSize-1 do
263                for y=0,sSSize-1 do
264                        for z=0,sSSize-1 do
265                                -- 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 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 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.
266                                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
267                                        -- This is xml code which prints the connection part, the +gridDim*sSScale/2 is because the connection is set exactly in the middle of two gridpoints.
268                                        print("<Model position=\"") print((x-math.floor(1/2*sSSize))*gridDim*sSScale+gridDim*sSScale/2) print(",") print((y-math.floor(1/2*sSSize))*gridDim*sSScale) print(",") print((z-math.floor(1/2*sSSize))*gridDim*sSScale) print("\" scale=") print(sSScale) print(" mesh=\"CuboidConnection.mesh\" roll=90 />")
269                                end
270                                -- The same as in the x-direction, but for the y-direction.
271                                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
272                                        print("<Model position=\"") print((x-math.floor(1/2*sSSize))*gridDim*sSScale) print(",") print((y-math.floor(1/2*sSSize))*gridDim*sSScale+gridDim*sSScale/2) print(",") print((z-math.floor(1/2*sSSize))*gridDim*sSScale) print("\" scale=") print(sSScale) print(" mesh=\"CuboidConnection.mesh\" />")
273                                end
274                                -- The same as in the x-direction, but for the z-direction.
275                                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
276                                        print("<Model position=\"") print((x-math.floor(1/2*sSSize))*gridDim*sSScale) print(",") print((y-math.floor(1/2*sSSize))*gridDim*sSScale) print(",") print((z-math.floor(1/2*sSSize))*gridDim*sSScale+gridDim*sSScale/2) print("\" scale=") print(sSScale) print(" mesh=\"CuboidConnection.mesh\" pitch=90 />")
277                                end
278                        end
279                end
280        end
281-- End attach all connectionparts.
282
283
284
285-- This is xml code, which ends the attachment and the MovableEntity.
286print("</attached>")
287print("</MovableEntity>")
288
Note: See TracBrowser for help on using the repository browser.