Ticket #1449: misc.js

File misc.js, 9.8 KB (added by FeXoR, 12 years ago)

misc.js (changed the default Iberian starting entity placement)

Line 
1/////////////////////////////////////////////////////////////////////////////////////////
2// passageMaker
3//
4// Function for creating shallow water between two given points by changing the heiight of all tiles in
5// the path with height less than or equal to "maxheight" to "height"
6//
7// x1,z1: Starting point of path
8// x2,z2: Ending point of path
9// width: Width of the shallow
10// maxheight: Maximum height that it changes
11// height: Height of the shallow
12// smooth: smooth elevation in borders
13// tileclass: (Optianal) - Adds those tiles to the class given
14// terrain: (Optional) - Changes the texture of the elevated land
15//
16/////////////////////////////////////////////////////////////////////////////////////////
17
18function passageMaker(x1, z1, x2, z2, width, maxheight, height, smooth, tileclass, terrain)
19{
20 var mapSize = g_Map.size;
21 for (var ix = 0; ix < mapSize; ix++)
22 {
23 for (var iz = 0; iz < mapSize; iz++)
24 {
25 var a = z1-z2;
26 var b = x2-x1;
27 var c = (z1*(x1-x2))-(x1*(z1-z2));
28 var dis = abs(a*ix + b*iz + c)/sqrt(a*a + b*b);
29 var k = (a*ix + b*iz + c)/(a*a + b*b);
30 var my = iz-(b*k);
31 var inline = 0;
32 if (b == 0)
33 {
34 if ((iz <= Math.max(z1,z2))&&(Math.min(z1,z2)))
35 {
36 inline = 1;
37 }
38 }
39 else
40 {
41 if ((my <= Math.max(z1,z2))&&(my >= Math.min(z1,z2)))
42 {
43 inline = 1;
44 }
45 }
46 if ((dis <= width)&&(inline))
47 {
48 if(g_Map.getHeight(ix, iz) <= maxheight)
49 {
50 if (dis >= width - smooth)
51 {
52 g_Map.setHeight(ix, iz, ((width - dis)*(height)+(g_Map.getHeight(ix, iz))*(dis + smooth - width))/(smooth)/2);
53 }
54 else
55 {
56 g_Map.setHeight(ix, iz, height);
57 }
58 if (tileclass !== undefined)
59 {
60 addToClass(ix, iz, tileclass);
61 }
62 if (terrain !== undefined)
63 {
64 placeTerrain(ix, iz, terrain);
65 }
66 }
67 }
68 }
69 }
70}
71//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72//rndRiver is a fuction that creates random values useful for making a jagged river.
73//
74//it works the same as sin or cos function. the only difference is that it's period is 1 instead of 2*pi
75//it needs the "seed" parameter to use it to make random curves that don't get broken.
76//seed must be created using randFloat(). or else it won't work
77//
78// f: Input: Same as angle in a sine function
79// seed: Random Seed: Best to implement is to use randFloat()
80//
81//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82
83function rndRiver(f, seed)
84{
85 var rndRq = seed;
86 var rndRw = rndRq;
87 var rndRe = 0;
88 var rndRr = f-floor(f);
89 var rndRa = 0;
90 for (var rndRx=0; rndRx<=floor(f); rndRx++)
91 {
92 rndRw = 10*(rndRw-floor(rndRw));
93 }
94 if (rndRx%2==0)
95 {
96 var rndRs = -1;
97 }
98 else
99 {
100 var rndRs = 1;
101 }
102 rndRe = (floor(rndRw))%5;
103 if (rndRe==0)
104 {
105 rndRa = (rndRs)*2.3*(rndRr)*(rndRr-1)*(rndRr-0.5)*(rndRr-0.5);
106 }
107 else if (rndRe==1)
108 {
109 rndRa = (rndRs)*2.6*(rndRr)*(rndRr-1)*(rndRr-0.3)*(rndRr-0.7);
110 }
111 else if (rndRe==2)
112 {
113 rndRa = (rndRs)*22*(rndRr)*(rndRr-1)*(rndRr-0.2)*(rndRr-0.3)*(rndRr-0.3)*(rndRr-0.8);
114 }
115 else if (rndRe==3)
116 {
117 rndRa = (rndRs)*180*(rndRr)*(rndRr-1)*(rndRr-0.2)*(rndRr-0.2)*(rndRr-0.4)*(rndRr-0.6)*(rndRr-0.6)*(rndRr-0.8);
118 }
119 else if (rndRe==4)
120 {
121 rndRa = (rndRs)*2.6*(rndRr)*(rndRr-1)*(rndRr-0.5)*(rndRr-0.7);
122 }
123 return rndRa;
124}
125
126
127/////////////////////////////////////////////////////////////////////////////////////////
128// createStartingPlayerEntities
129//
130// Creates the starting player entities
131// fx&fz: position of player base
132// playerid: id of player
133// civEntities: use getStartingEntities(id-1) fo this one
134// BUILDING_ANGlE: angle of main base building
135//
136///////////////////////////////////////////////////////////////////////////////////////////
137function createStartingPlayerEntities(fx, fz, playerid, civEntities, BUILDING_ANGlE)
138{
139 var uDist = 6;
140 var uSpace = 2;
141 placeObject(fx, fz, civEntities[0].Template, playerid, BUILDING_ANGlE);
142 for (var j = 1; j < civEntities.length; ++j)
143 {
144 var uAngle = BUILDING_ANGlE - PI * (2-j) / 2;
145 var count = (civEntities[j].Count !== undefined ? civEntities[j].Count : 1);
146 for (var numberofentities = 0; numberofentities < count; numberofentities++)
147 {
148 var ux = fx + uDist * cos(uAngle) + numberofentities * uSpace * cos(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * cos(uAngle + PI/2));
149 var uz = fz + uDist * sin(uAngle) + numberofentities * uSpace * sin(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * sin(uAngle + PI/2));
150 placeObject(ux, uz, civEntities[j].Template, playerid, uAngle);
151 }
152 }
153}
154
155//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156// placeCivDefaultEntities
157//
158// Creates the default starting player entities depending on the players civ
159// fx&fy: position of player base
160// playerid: id of player
161// angle: angle of main base building, optional, default is BUILDING_ANGlE
162// kwargs: Takes some optional keyword arguments to tweek things
163// 'iberWall': may be false, 'walls' (default) or 'towers'. Determines the defensive structures Iberians get as civ bonus
164//
165//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
166function placeCivDefaultEntities(fx, fz, playerid, angle, kwargs)
167{
168 // Unpack kwargs
169 kwargs = (kwargs || {});
170 var iberWall = 'walls';
171 if ('iberWall' in kwargs)
172 iberWall = kwargs['iberWall'];
173 // Place default civ starting entities
174 var civ = g_MapSettings.PlayerData[playerid-1].Civ;
175 var civEntities = getStartingEntities(playerid-1);
176 var uDist = 6;
177 var uSpace = 2;
178 placeObject(fx, fz, civEntities[0].Template, playerid, angle);
179 for (var j = 1; j < civEntities.length; ++j)
180 {
181 var uAngle = angle - PI * (2-j) / 2;
182 var count = (civEntities[j].Count !== undefined ? civEntities[j].Count : 1);
183 for (var numberofentities = 0; numberofentities < count; numberofentities++)
184 {
185 var ux = fx + uDist * cos(uAngle) + numberofentities * uSpace * cos(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * cos(uAngle + PI/2));
186 var uz = fz + uDist * sin(uAngle) + numberofentities * uSpace * sin(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * sin(uAngle + PI/2));
187 placeObject(ux, uz, civEntities[j].Template, playerid, uAngle);
188 }
189 }
190 // Add defensive structiures for Iberians as their civ bonus
191 if (civ == 'iber' && iberWall != false)
192 {
193 if (iberWall == 'towers')
194 placePolygonalWall(fx, fz, 15, ['entry'], 'tower', civ, playerid, angle, 7);
195 else
196 {
197 placeGenericFortress(fx, fz, 20/*radius*/, playerid);
198 }
199 }
200}
201
202
203/////////////////////////////////////////////////////////////////////////////////////////
204// paintTerrainBasedOnHeight
205//
206// paints the tiles which have a height between minheight and maxheight with the given terrain
207// minheight: minimum height of the tile
208// maxheight: maximum height of the tile
209// mode: accepts 4 values. 0 means the it will select tiles with height more than minheight and less than maxheight.
210// 1 means it selects tiles with height more than or equal to minheight and less than max height. 2 means more than
211// minheight and less than or equal to maxheight. 3 means more than or equal to minheight and less than or equal to maxheight
212// terrain: intended terrain texture
213//
214///////////////////////////////////////////////////////////////////////////////////////////
215
216function paintTerrainBasedOnHeight(minheight, maxheight, mode, terrain)
217{
218 var mSize = g_Map.size;
219 for (var qx = 0; qx < mSize; qx++)
220 {
221 for (var qz = 0; qz < mSize; qz++)
222 {
223 if (mode == 0)
224 {
225 if ((g_Map.getHeight(qx, qz) > minheight)&&(g_Map.getHeight(qx, qz) < maxheight))
226 {
227 placeTerrain(qx, qz, terrain);
228 }
229 }
230 else if (mode == 1)
231 {
232 if ((g_Map.getHeight(qx, qz) >= minheight)&&(g_Map.getHeight(qx, qz) < maxheight))
233 {
234 placeTerrain(qx, qz, terrain);
235 }
236 }
237 else if (mode == 2)
238 {
239 if ((g_Map.getHeight(qx, qz) > minheight)&&(g_Map.getHeight(qx, qz) <= maxheight))
240 {
241 placeTerrain(qx, qz, terrain);
242 }
243 }
244 else if (mode == 3)
245 {
246 if ((g_Map.getHeight(qx, qz) >= minheight)&&(g_Map.getHeight(qx, qz) <= maxheight))
247 {
248 placeTerrain(qx, qz, terrain);
249 }
250 }
251 }
252 }
253}
254
255/////////////////////////////////////////////////////////////////////////////////////////
256// paintTileClassBasedOnHeight
257//
258// paints the tiles which have a height between minheight and maxheight with the given tile class
259// minheight: minimum height of the tile
260// maxheight: maximum height of the tile
261// mode: accepts 4 values. 0 means the it will select tiles with height more than minheight and less than maxheight.
262// 1 means it selects tiles with height more than or equal to minheight and less than max height. 2 means more than
263// minheight and less than or equal to maxheight. 3 means more than or equal to minheight and less than or equal to maxheight
264// tileclass: intended tile class
265//
266///////////////////////////////////////////////////////////////////////////////////////////
267
268function paintTileClassBasedOnHeight(minheight, maxheight, mode, tileclass)
269{
270 var mSize = g_Map.size;
271 for (var qx = 0; qx < mSize; qx++)
272 {
273 for (var qz = 0; qz < mSize; qz++)
274 {
275 if (mode == 0)
276 {
277 if ((g_Map.getHeight(qx, qz) > minheight)&&(g_Map.getHeight(qx, qz) < maxheight))
278 {
279 addToClass(qx, qz, tileclass);
280 }
281 }
282 else if (mode == 1)
283 {
284 if ((g_Map.getHeight(qx, qz) >= minheight)&&(g_Map.getHeight(qx, qz) < maxheight))
285 {
286 addToClass(qx, qz, tileclass);
287 }
288 }
289 else if (mode == 2)
290 {
291 if ((g_Map.getHeight(qx, qz) > minheight)&&(g_Map.getHeight(qx, qz) <= maxheight))
292 {
293 addToClass(qx, qz, tileclass);
294 }
295 }
296 else if (mode == 3)
297 {
298 if ((g_Map.getHeight(qx, qz) >= minheight)&&(g_Map.getHeight(qx, qz) <= maxheight))
299 {
300 addToClass(qx, qz, tileclass);
301 }
302 }
303 }
304 }
305}