cm_terrain.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. #include "../server/exe_headers.h"
  2. #include "cm_local.h"
  3. #include "cm_patch.h"
  4. #include "cm_landscape.h"
  5. #include "../game/genericparser2.h"
  6. #include "cm_randomterrain.h"
  7. #ifdef _WIN32
  8. #pragma optimize("p", on)
  9. #endif
  10. void R_LoadDataImage ( const char *name, byte **pic, int *width, int *height);
  11. void R_InvertImage ( byte *data, int width, int height, int depth);
  12. void R_Resample ( byte *source, int swidth, int sheight, byte *dest, int dwidth, int dheight, int components);
  13. //#define _SMOOTH_TERXEL_BRUSH
  14. #ifdef _SMOOTH_TERXEL_BRUSH
  15. #define BRUSH_SIDES_PER_TERXEL 8
  16. #else
  17. #define BRUSH_SIDES_PER_TERXEL 5
  18. #endif
  19. void CCMLandScape::SetShaders(int height, CCMShader *shader)
  20. {
  21. int i;
  22. for(i = height; shader && (i < HEIGHT_RESOLUTION); i++)
  23. {
  24. if(!mHeightDetails[i].GetSurfaceFlags())
  25. {
  26. mHeightDetails[i].SetFlags(shader->contentFlags, shader->surfaceFlags);
  27. }
  28. }
  29. }
  30. void CCMLandScape::LoadTerrainDef(const char *td)
  31. {
  32. char terrainDef[MAX_QPATH];
  33. CGenericParser2 parse;
  34. CGPGroup *basegroup, *classes, *items;
  35. Com_sprintf(terrainDef, MAX_QPATH, "ext_data/RMG/%s.terrain", Info_ValueForKey(td, "terrainDef"));
  36. Com_DPrintf("CM_Terrain: Loading and parsing terrainDef %s.....\n", Info_ValueForKey(td, "terrainDef"));
  37. if(!Com_ParseTextFile(terrainDef, parse))
  38. {
  39. Com_sprintf(terrainDef, MAX_QPATH, "ext_data/arioche/%s.terrain", Info_ValueForKey(td, "terrainDef"));
  40. if(!Com_ParseTextFile(terrainDef, parse))
  41. {
  42. Com_Printf("Could not open %s\n", terrainDef);
  43. return;
  44. }
  45. }
  46. // The whole file....
  47. basegroup = parse.GetBaseParseGroup();
  48. // The root { } struct
  49. classes = basegroup->GetSubGroups();
  50. while(classes)
  51. {
  52. items = classes->GetSubGroups();
  53. while(items)
  54. {
  55. if(!stricmp(items->GetName(), "altitudetexture"))
  56. {
  57. int height;
  58. const char *shaderName;
  59. CCMShader *shader;
  60. // Height must exist - the rest are optional
  61. height = atol(items->FindPairValue("height", "0"));
  62. // Shader for this height
  63. shaderName = items->FindPairValue("shader", "");
  64. if(strlen(shaderName))
  65. {
  66. shader = CM_GetShaderInfo(shaderName);
  67. if(shader)
  68. {
  69. SetShaders(height, shader);
  70. }
  71. }
  72. }
  73. else if(!stricmp(items->GetName(), "water"))
  74. {
  75. const char *shaderName;
  76. CCMShader *shader;
  77. // Grab the height of the water
  78. mBaseWaterHeight = atol(items->FindPairValue("height", "0"));
  79. SetRealWaterHeight(mBaseWaterHeight);
  80. // Grab the material of the water
  81. shaderName = items->FindPairValue("shader", "");
  82. shader = CM_GetShaderInfo(shaderName);
  83. if(shader)
  84. {
  85. mWaterContents = shader->contentFlags;
  86. mWaterSurfaceFlags = shader->surfaceFlags;
  87. }
  88. }
  89. items = (CGPGroup *)items->GetNext();
  90. }
  91. classes = (CGPGroup *)classes->GetNext();
  92. }
  93. Com_ParseTextFileDestroy(parse);
  94. }
  95. CCMPatch::~CCMPatch(void)
  96. {
  97. }
  98. CCMLandScape::CCMLandScape(const char *configstring, bool server)
  99. {
  100. int numPatches, numBrushesPerPatch, size;// seed;
  101. char heightMap[MAX_QPATH];
  102. // char *ptr;
  103. holdrand = 0x89abcdef;
  104. // Clear out the height details
  105. memset(mHeightDetails, 0, sizeof(CCMHeightDetails) * HEIGHT_RESOLUTION);
  106. mBaseWaterHeight = 0;
  107. mWaterHeight = 0.0f;
  108. // When constructed, referenced once
  109. mRefCount = 1;
  110. // Extract the relevant data from the config string
  111. Com_sprintf(heightMap, MAX_QPATH, "%s", Info_ValueForKey(configstring, "heightMap"));
  112. numPatches = atol(Info_ValueForKey(configstring, "numPatches"));
  113. mTerxels = atol(Info_ValueForKey(configstring, "terxels"));
  114. mHasPhysics = !!atol(Info_ValueForKey(configstring, "physics"));
  115. //seed = strtoul(Info_ValueForKey(configstring, "seed"), &ptr, 10);
  116. mBounds[0][0] = (float)atof(Info_ValueForKey(configstring, "minx"));
  117. mBounds[0][1] = (float)atof(Info_ValueForKey(configstring, "miny"));
  118. mBounds[0][2] = (float)atof(Info_ValueForKey(configstring, "minz"));
  119. mBounds[1][0] = (float)atof(Info_ValueForKey(configstring, "maxx"));
  120. mBounds[1][1] = (float)atof(Info_ValueForKey(configstring, "maxy"));
  121. mBounds[1][2] = (float)atof(Info_ValueForKey(configstring, "maxz"));
  122. // Calculate size of the brush
  123. VectorSubtract(mBounds[1], mBounds[0], mSize);
  124. // Work out the dimensions of the brush in blocks - the object is to make the blocks as square as possible
  125. mBlockWidth = Round(sqrtf(numPatches * mSize[0] / mSize[1]));
  126. mBlockHeight = Round(sqrtf(numPatches * mSize[1] / mSize[0]));
  127. // ...which lets us get the size of the heightmap
  128. mWidth = mBlockWidth * mTerxels;
  129. mHeight = mBlockHeight * mTerxels;
  130. mHeightMap = (byte *)Z_Malloc(GetRealArea(), TAG_CM_TERRAIN, qfalse);
  131. mFlattenMap = 0; //only needed on random terrains
  132. if(strlen(heightMap))
  133. {
  134. byte *imageData;
  135. int iWidth, iHeight;
  136. Com_DPrintf("CM_Terrain: Loading heightmap %s.....\n", heightMap);
  137. R_LoadDataImage(heightMap, &imageData, &iWidth, &iHeight);
  138. mRandomTerrain = 0;
  139. if(imageData)
  140. {
  141. if(strstr(heightMap, "random_"))
  142. {
  143. mFlattenMap = (byte *)Z_Malloc(GetRealArea(), TAG_CM_TERRAIN, qfalse);
  144. memset ( mFlattenMap, 0, GetRealArea() );// Zero means unused.
  145. mRandomTerrain = CreateRandomTerrain ( configstring, this, mHeightMap, GetRealWidth(), GetRealHeight());
  146. }
  147. else
  148. {
  149. // Flip to make the same as GenSurf
  150. R_InvertImage(imageData, iWidth, iHeight, 1);
  151. R_Resample(imageData, iWidth, iHeight, mHeightMap, GetRealWidth(), GetRealHeight(), 1);
  152. }
  153. Z_Free(imageData);
  154. }
  155. }
  156. else
  157. {
  158. Com_Error(ERR_FATAL, "Terrain has no heightmap specified\n");
  159. }
  160. // Work out the dimensions of the terxel - should be almost square
  161. mTerxelSize[0] = mSize[0] / mWidth;
  162. mTerxelSize[1] = mSize[1] / mHeight;
  163. mTerxelSize[2] = mSize[2] / 255.0f;
  164. // Work out the patchsize
  165. mPatchSize[0] = mSize[0] / mBlockWidth;
  166. mPatchSize[1] = mSize[1] / mBlockHeight;
  167. mPatchSize[2] = 1.0f;
  168. mPatchScalarSize = VectorLength(mPatchSize);
  169. // Loads in the water height and properties
  170. // Gets the shader properties for the blended shaders
  171. LoadTerrainDef(configstring);
  172. Com_DPrintf("CM_Terrain: Creating patches.....\n");
  173. mPatches = (CCMPatch *)Z_Malloc(sizeof(CCMPatch) * GetBlockCount(), TAG_CM_TERRAIN, qfalse);
  174. numBrushesPerPatch = mTerxels * mTerxels * 2;
  175. size = (numBrushesPerPatch * sizeof(cbrush_t)) + (numBrushesPerPatch * BRUSH_SIDES_PER_TERXEL * 2 * (sizeof(cbrushside_t) + sizeof(cplane_t)));
  176. mPatchBrushData = (byte *)Z_Malloc(size * GetBlockCount(), TAG_CM_TERRAIN, qfalse);
  177. // Initialize all terrain patches
  178. UpdatePatches();
  179. }
  180. // Initialise a plane from 3 coords
  181. void CCMPatch::InitPlane(struct cbrushside_s *side, cplane_t *plane, vec3_t p0, vec3_t p1, vec3_t p2)
  182. {
  183. vec3_t dx, dy;
  184. VectorSubtract(p1, p0, dx);
  185. VectorSubtract(p2, p0, dy);
  186. CrossProduct(dx, dy, plane->normal);
  187. VectorNormalize(plane->normal);
  188. plane->dist = DotProduct(p0, plane->normal);
  189. plane->type = PlaneTypeForNormal(plane->normal);
  190. SetPlaneSignbits(plane);
  191. #ifdef _XBOX
  192. // MATT! - does this work?
  193. cmg.planes[side->planeNum.GetValue()] = *plane;
  194. #else
  195. side->plane = plane;
  196. #endif
  197. }
  198. // Create the planes required for collision detection
  199. // 2 brushes per terxel - each brush has 5 sides and 5 planes
  200. void* CCMPatch::GetAdjacentBrushY ( int x, int y )
  201. {
  202. int yo1 = y % owner->GetTerxels();
  203. int yo2 = (y-1) % owner->GetTerxels();
  204. int xo = x % owner->GetTerxels();
  205. CCMPatch* patch;
  206. // Different patch
  207. if ( yo2 > yo1 )
  208. {
  209. patch = owner->GetPatch ( x / owner->GetTerxels(), (y-1) / owner->GetTerxels() );
  210. }
  211. else
  212. {
  213. patch = this;
  214. }
  215. cbrush_t *brush;
  216. brush = patch->mPatchBrushData;
  217. brush += ((yo2 * owner->GetTerxels ( ) + xo) * 2);
  218. brush ++;
  219. return brush;
  220. }
  221. void* CCMPatch::GetAdjacentBrushX ( int x, int y )
  222. {
  223. int xo1 = x % owner->GetTerxels();
  224. int xo2 = (x-1) % owner->GetTerxels();
  225. int yo = y % owner->GetTerxels();
  226. CCMPatch* patch;
  227. // Different patch
  228. if ( xo2 > xo1 )
  229. {
  230. patch = owner->GetPatch ( (x-1) / owner->GetTerxels(), y / owner->GetTerxels() );
  231. }
  232. else
  233. {
  234. patch = this;
  235. }
  236. cbrush_t *brush;
  237. brush = patch->mPatchBrushData;
  238. brush += ((yo * owner->GetTerxels ( ) + xo2) * 2);
  239. if ( ! ((x+y) & 1) )
  240. {
  241. brush ++;
  242. }
  243. return brush;
  244. }
  245. void CCMPatch::CreatePatchPlaneData(void)
  246. {
  247. #ifndef PRE_RELEASE_DEMO
  248. int realWidth;
  249. int x, y, i, j;
  250. #if 0
  251. int n;
  252. #endif
  253. cbrush_t *brush;
  254. cbrushside_t *side;
  255. cplane_t *plane;
  256. vec3_t *coords;
  257. vec3_t localCoords[8];
  258. mNumBrushes = owner->GetTerxels() * owner->GetTerxels() * 2;
  259. realWidth = owner->GetRealWidth();
  260. coords = owner->GetCoords();
  261. brush = mPatchBrushData;
  262. side = (cbrushside_t *)(mPatchBrushData + mNumBrushes);
  263. plane = (cplane_t *)(side + (mNumBrushes * BRUSH_SIDES_PER_TERXEL * 2));
  264. for(y = mHy; y < mHy + owner->GetTerxels(); y++)
  265. {
  266. for(x = mHx; x < mHx + owner->GetTerxels(); x++)
  267. {
  268. int offsets[4];
  269. if ( (x+y)&1 )
  270. {
  271. offsets[0] = (y * realWidth) + x; // TL
  272. offsets[1] = (y * realWidth) + x + 1; // TR
  273. offsets[2] = ((y + 1) * realWidth) + x; // BL
  274. offsets[3] = ((y + 1) * realWidth) + x + 1; // BR
  275. }
  276. else
  277. {
  278. offsets[2] = (y * realWidth) + x; // TL
  279. offsets[0] = (y * realWidth) + x + 1; // TR
  280. offsets[3] = ((y + 1) * realWidth) + x; // BL
  281. offsets[1] = ((y + 1) * realWidth) + x + 1; // BR
  282. }
  283. for(i = 0; i < 4; i++)
  284. {
  285. VectorCopy(coords[offsets[i]], localCoords[i]);
  286. VectorCopy(coords[offsets[i]], localCoords[i + 4]);
  287. // Set z of base of brush to bottom of landscape brush
  288. localCoords[i + 4][2] = owner->GetMins()[2];
  289. }
  290. // Set the bounds of the terxel
  291. VectorSet(brush[0].bounds[0], MAX_WORLD_COORD, MAX_WORLD_COORD, MAX_WORLD_COORD);
  292. VectorSet(brush[0].bounds[1], MIN_WORLD_COORD, MIN_WORLD_COORD, MIN_WORLD_COORD);
  293. for(i = 0; i < 8; i++)
  294. {
  295. for(j = 0; j < 3; j++)
  296. {
  297. // mins
  298. if(localCoords[i][j] < brush[0].bounds[0][j])
  299. {
  300. brush[0].bounds[0][j] = localCoords[i][j];
  301. }
  302. // maxs
  303. if(localCoords[i][j] > brush[0].bounds[1][j])
  304. {
  305. brush[0].bounds[1][j] = localCoords[i][j];
  306. }
  307. }
  308. }
  309. VectorDec(brush[0].bounds[0]);
  310. VectorInc(brush[0].bounds[1]);
  311. VectorCopy(brush[0].bounds[0], brush[1].bounds[0]);
  312. VectorCopy(brush[0].bounds[1], brush[1].bounds[1]);
  313. brush[0].contents = mContentFlags;
  314. brush[1].contents = mContentFlags;
  315. #ifndef _SMOOTH_TERXEL_BRUSH
  316. // Set up sides of the brushes
  317. brush[0].numsides = 5;
  318. brush[0].sides = side;
  319. brush[1].numsides = 5;
  320. brush[1].sides = side + 5;
  321. for ( i = 0; i < 8 ; i ++ )
  322. {
  323. localCoords[i][0] = (int)localCoords[i][0];
  324. localCoords[i][1] = (int)localCoords[i][1];
  325. localCoords[i][2] = (int)localCoords[i][2];
  326. }
  327. // Create the planes of the 2 triangles that make up the tops of the brushes
  328. InitPlane(side + 0, plane + 0, localCoords[0], localCoords[1], localCoords[2]);
  329. InitPlane(side + 5, plane + 5, localCoords[3], localCoords[2], localCoords[1]);
  330. // Create the bottom face of the brushes
  331. InitPlane(side + 1, plane + 1, localCoords[6], localCoords[5], localCoords[4]);
  332. InitPlane(side + 6, plane + 6, localCoords[5], localCoords[6], localCoords[7]);
  333. // Create the 3 vertical faces
  334. InitPlane(side + 2, plane + 2, localCoords[0], localCoords[2], localCoords[4]);
  335. InitPlane(side + 7, plane + 7, localCoords[3], localCoords[1], localCoords[7]);
  336. InitPlane(side + 3, plane + 3, localCoords[0], localCoords[4], localCoords[1]);
  337. InitPlane(side + 8, plane + 8, localCoords[3], localCoords[7], localCoords[2]);
  338. InitPlane(side + 4, plane + 4, localCoords[2], localCoords[1], localCoords[6]);
  339. InitPlane(side + 9, plane + 9, localCoords[5], localCoords[1], localCoords[6]);
  340. // Increment to next terxel
  341. brush += 2;
  342. side += 10;
  343. plane += 10;
  344. #else
  345. // Set up sides of the brushes
  346. brush[0].numsides = 5;
  347. brush[0].sides = side;
  348. brush[1].numsides = 5;
  349. brush[1].sides = side + 8;
  350. // Create the planes of the 2 triangles that make up the tops of the brushes
  351. InitPlane(side + 0, plane + 0, localCoords[0], localCoords[1], localCoords[2]);
  352. InitPlane(side + 8, plane + 8, localCoords[3], localCoords[2], localCoords[1]);
  353. // Create the bottom face of the brushes
  354. InitPlane(side + 1, plane + 1, localCoords[4], localCoords[6], localCoords[5]);
  355. InitPlane(side + 9, plane + 9, localCoords[7], localCoords[5], localCoords[6]);
  356. // Create the 3 vertical faces
  357. InitPlane(side + 2, plane + 2, localCoords[0], localCoords[2], localCoords[4]);
  358. InitPlane(side + 10, plane + 10, localCoords[3], localCoords[1], localCoords[7]);
  359. InitPlane(side + 3, plane + 3, localCoords[0], localCoords[4], localCoords[1]);
  360. InitPlane(side + 11, plane + 11, localCoords[3], localCoords[7], localCoords[2]);
  361. InitPlane(side + 4, plane + 4, localCoords[2], localCoords[1], localCoords[6]);
  362. InitPlane(side + 12, plane + 12, localCoords[5], localCoords[1], localCoords[6]);
  363. float V = DotProduct ( (plane + 8)->normal, localCoords[0] ) - (plane + 8)->dist;
  364. if ( V < 0 )
  365. {
  366. InitPlane ( brush[0].sides + brush[0].numsides, plane + brush[0].numsides, localCoords[3], localCoords[2], localCoords[1]);
  367. brush[0].numsides++;
  368. InitPlane ( brush[1].sides + brush[1].numsides, plane + 8 + brush[1].numsides, localCoords[0], localCoords[1], localCoords[2]);
  369. brush[1].numsides++;
  370. }
  371. // Determine if we need to smooth the brush transition from the brush above us
  372. if ( y > 0 && y < owner->GetPatchHeight ( ) - 1 )
  373. {
  374. cbrush_t* abovebrush = (cbrush_t*)GetAdjacentBrushY ( x, y );
  375. #ifdef _XBOX
  376. cplane_t* aboveplane = &cmg.planes[abovebrush->sides->planeNum.GetValue()];
  377. #else
  378. cplane_t* aboveplane = abovebrush->sides->plane;
  379. #endif
  380. V = DotProduct ( aboveplane->normal, ((y+x)&1)?(localCoords[2]):(localCoords[1]) ) - aboveplane->dist;
  381. if ( V < 0 )
  382. {
  383. memcpy ( brush[0].sides + brush[0].numsides, abovebrush->sides, sizeof(cbrushside_t) );
  384. brush[0].numsides++;
  385. memcpy ( abovebrush->sides + abovebrush->numsides, side + 0, sizeof(cbrushside_t) );
  386. abovebrush->numsides++;
  387. }
  388. }
  389. // Determine if we need to smooth the brush transition from the brush to the left of us
  390. if ( x > 0 && x < owner->GetPatchWidth ( ) - 1 )
  391. {
  392. cbrush_t* abovebrush = (cbrush_t*)GetAdjacentBrushX ( x, y );
  393. #ifdef _XBOX
  394. cplane_t* aboveplane = &cmg.planes[abovebrush->sides->planeNum.GetValue()];
  395. #else
  396. cplane_t* aboveplane = abovebrush->sides->plane;
  397. #endif
  398. V = DotProduct ( aboveplane->normal, localCoords[1] ) - aboveplane->dist;
  399. if ( V < 0 )
  400. {
  401. if ( (x+y)&1 )
  402. {
  403. memcpy ( brush[0].sides + brush[0].numsides, abovebrush->sides, sizeof(cbrushside_t) );
  404. brush[0].numsides++;
  405. memcpy ( abovebrush->sides + abovebrush->numsides, side + 0, sizeof(cbrushside_t) );
  406. abovebrush->numsides++;
  407. }
  408. else
  409. {
  410. memcpy ( brush[1].sides + brush[1].numsides, abovebrush->sides, sizeof(cbrushside_t) );
  411. brush[1].numsides++;
  412. memcpy ( abovebrush->sides + abovebrush->numsides, side + 8, sizeof(cbrushside_t) );
  413. abovebrush->numsides++;
  414. }
  415. }
  416. }
  417. // Increment to next terxel
  418. brush += 2;
  419. side += 16;
  420. plane += 16;
  421. #endif
  422. }
  423. }
  424. #endif // PRE_RELEASE_DEMO
  425. }
  426. void CCMPatch::Init(CCMLandScape *ls, int heightX, int heightY, vec3_t world, byte *hMap, byte *patchBrushData)
  427. {
  428. #ifndef PRE_RELEASE_DEMO
  429. int min, max, x, y, height;
  430. // Set owning landscape
  431. owner = ls;
  432. // Store the base of the top left corner
  433. VectorCopy(world, mWorldCoords);
  434. // Store pointer to first byte of the height data for this patch.
  435. mHx = heightX;
  436. mHy = heightY;
  437. mHeightMap = hMap + ((heightY * owner->GetRealWidth()) + heightX);
  438. // Calculate the bounds for culling
  439. // Use the dimensions 1 terxel outside the patch to allow for sloping of edge terxels
  440. min = 256;
  441. max = -1;
  442. for(y = heightY - 1; y < heightY + owner->GetTerxels() + 1; y++)
  443. {
  444. if(y >= 0)
  445. {
  446. for(x = heightX - 1; x < heightX + owner->GetTerxels() + 1; x++)
  447. {
  448. if(x >= 0)
  449. {
  450. height = hMap[(y * owner->GetRealWidth()) + x];
  451. if(height > max)
  452. {
  453. max = height;
  454. }
  455. if(height < min)
  456. {
  457. min = height;
  458. }
  459. }
  460. }
  461. }
  462. }
  463. // Mins
  464. mBounds[0][0] = world[0];
  465. mBounds[0][1] = world[1];
  466. mBounds[0][2] = world[2] + (min * owner->GetTerxelSize()[2]);
  467. // Maxs
  468. mBounds[1][0] = world[0] + (owner->GetPatchSize()[0]);
  469. mBounds[1][1] = world[1] + (owner->GetPatchSize()[1]);
  470. mBounds[1][2] = world[2] + (max * owner->GetTerxelSize()[2]);
  471. // Corner heights
  472. mCornerHeights[0] = mHeightMap[0];
  473. mCornerHeights[1] = mHeightMap[owner->GetTerxels()];
  474. mCornerHeights[2] = mHeightMap[owner->GetTerxels() * owner->GetRealWidth()];
  475. mCornerHeights[3] = mHeightMap[(owner->GetTerxels() * owner->GetRealWidth()) + owner->GetTerxels()];
  476. // Set the surfaceFlags using average height (may want a more complex algo here)
  477. mSurfaceFlags = owner->GetSurfaceFlags((min + max) >> 1);
  478. mContentFlags = owner->GetContentFlags((min + max) >> 1);
  479. // Set base of brush data from big array
  480. mPatchBrushData = (cbrush_t *)patchBrushData;
  481. CreatePatchPlaneData();
  482. #endif // PRE_RELEASE_DEMO
  483. }
  484. CCMPatch *CCMLandScape::GetPatch(int x, int y)
  485. {
  486. return(mPatches + ((y * mBlockWidth) + x));
  487. }
  488. void CCMLandScape::PatchCollide(struct traceWork_s *tw, trace_t &trace, const vec3_t start, const vec3_t end, int checkcount)
  489. {
  490. vec3pair_t tBounds;
  491. // Convert to valid bounding box
  492. CM_CalcExtents(start, end, tw, tBounds);
  493. // if (com_newtrace->integer)
  494. if (1)
  495. {
  496. float slope, offset;
  497. float startPatchLoc, endPatchLoc, startPos, endPos;
  498. float patchDirection = 1;
  499. float checkDirection = 1;
  500. int countPatches, count;
  501. CCMPatch *patch;
  502. float fraction = trace.fraction;
  503. if (fabs(end[0]-start[0]) >= fabs(fabs(end[1]-start[1])))
  504. { // x travels more than y
  505. // calculate line slope and offset
  506. if (end[0] - start[0])
  507. {
  508. slope = (end[1] - start[1]) / (end[0] - start[0]);
  509. }
  510. else
  511. {
  512. slope = 0;
  513. }
  514. offset = start[1] - (start[0] * slope);
  515. // find the starting
  516. startPatchLoc = floor((start[0] - mBounds[0][0]) / mPatchSize[0]);
  517. endPatchLoc = floor((end[0] - mBounds[0][0]) / mPatchSize[0]);
  518. if (startPatchLoc <= endPatchLoc)
  519. { // moving along slope in a positive direction
  520. endPatchLoc++;
  521. startPatchLoc--;
  522. countPatches = endPatchLoc - startPatchLoc + 1;
  523. }
  524. else
  525. { // moving along slope in a negative direction
  526. endPatchLoc--;
  527. startPatchLoc++;
  528. patchDirection = -1;
  529. countPatches = startPatchLoc - endPatchLoc + 1;
  530. }
  531. if (slope < 0.0)
  532. {
  533. checkDirection = -1;
  534. }
  535. // first calculate the real world location
  536. startPos = ((startPatchLoc * mPatchSize[0] + mBounds[0][0]) * slope) + offset;
  537. // calculate it back into patch coords
  538. startPos = floor((startPos - mBounds[0][1] + tw->size[0][1]) / mPatchSize[1]);
  539. do
  540. {
  541. if (startPatchLoc >= 0 && startPatchLoc < mBlockWidth)
  542. { // valid location
  543. // first calculate the real world location
  544. endPos = (((startPatchLoc+patchDirection) * mPatchSize[0] + mBounds[0][0]) * slope) + offset;
  545. // calculate it back into patch coords
  546. endPos = floor((endPos - mBounds[0][1] + tw->size[1][1]) / mPatchSize[1]);
  547. if (checkDirection < 0)
  548. {
  549. startPos++;
  550. endPos--;
  551. }
  552. else
  553. {
  554. startPos--;
  555. endPos++;
  556. }
  557. count = fabs(endPos - startPos) + 1;
  558. while(count)
  559. {
  560. if (startPos >= 0 && startPos < mBlockHeight)
  561. { // valid location
  562. patch = GetPatch(startPatchLoc, startPos);
  563. // Collide with every patch to find the minimum fraction
  564. CM_HandlePatchCollision(tw, trace, tBounds[0], tBounds[1], patch, checkcount);
  565. if (trace.fraction <= 0.0)
  566. {
  567. return;
  568. }
  569. }
  570. startPos += checkDirection;
  571. count--;
  572. }
  573. if (trace.fraction < fraction)
  574. {
  575. return;
  576. }
  577. }
  578. // move to the next spot
  579. // we still stay one behind, to get the opposite edge of the terrain patch
  580. startPos = ((startPatchLoc * mPatchSize[0] + mBounds[0][0]) * slope) + offset;
  581. startPatchLoc += patchDirection;
  582. // first calculate the real world location
  583. // calculate it back into patch coords
  584. startPos = floor((startPos - mBounds[0][1] + tw->size[0][1]) / mPatchSize[1]);
  585. countPatches--;
  586. }
  587. while (countPatches);
  588. }
  589. else
  590. {
  591. // calculate line slope and offset
  592. slope = (end[0] - start[0]) / (end[1] - start[1]);
  593. offset = start[0] - (start[1] * slope);
  594. // find the starting
  595. startPatchLoc = floor((start[1] - mBounds[0][1]) / mPatchSize[1]);
  596. endPatchLoc = floor((end[1] - mBounds[0][1]) / mPatchSize[1]);
  597. if (startPatchLoc <= endPatchLoc)
  598. { // moving along slope in a positive direction
  599. endPatchLoc++;
  600. startPatchLoc--;
  601. countPatches = endPatchLoc - startPatchLoc + 1;
  602. }
  603. else
  604. { // moving along slope in a negative direction
  605. endPatchLoc--;
  606. startPatchLoc++;
  607. patchDirection = -1;
  608. countPatches = startPatchLoc - endPatchLoc + 1;
  609. }
  610. if (slope < 0.0)
  611. {
  612. checkDirection = -1;
  613. }
  614. // first calculate the real world location
  615. startPos = ((startPatchLoc * mPatchSize[1] + mBounds[0][1]) * slope) + offset;
  616. // calculate it back into patch coords
  617. startPos = floor((startPos - mBounds[0][0] + tw->size[0][0]) / mPatchSize[0]);
  618. do
  619. {
  620. if (startPatchLoc >= 0 && startPatchLoc < mBlockHeight)
  621. { // valid location
  622. // first calculate the real world location
  623. endPos = (((startPatchLoc+patchDirection) * mPatchSize[1] + mBounds[0][1]) * slope) + offset;
  624. // calculate it back into patch coords
  625. endPos = floor((endPos - mBounds[0][0] + tw->size[1][0]) / mPatchSize[0]);
  626. if (checkDirection < 0)
  627. {
  628. startPos++;
  629. endPos--;
  630. }
  631. else
  632. {
  633. startPos--;
  634. endPos++;
  635. }
  636. count = fabs(endPos - startPos) + 1;
  637. while(count)
  638. {
  639. if (startPos >= 0 && startPos < mBlockWidth)
  640. { // valid location
  641. patch = GetPatch(startPos, startPatchLoc);
  642. // Collide with every patch to find the minimum fraction
  643. CM_HandlePatchCollision(tw, trace, tBounds[0], tBounds[1], patch, checkcount);
  644. if (trace.fraction <= 0.0)
  645. {
  646. return;
  647. }
  648. }
  649. startPos += checkDirection;
  650. count--;
  651. }
  652. if (trace.fraction < fraction)
  653. {
  654. return;
  655. }
  656. }
  657. // move to the next spot
  658. // we still stay one behind, to get the opposite edge of the terrain patch
  659. startPos = ((startPatchLoc * mPatchSize[1] + mBounds[0][1]) * slope) + offset;
  660. startPatchLoc += patchDirection;
  661. // first calculate the real world location
  662. // calculate it back into patch coords
  663. startPos = floor((startPos - mBounds[0][0] + tw->size[0][0]) / mPatchSize[0]);
  664. countPatches--;
  665. }
  666. while (countPatches);
  667. }
  668. }
  669. else
  670. {
  671. int x, y;
  672. vec3_t tWork;
  673. vec3_t pStart, pEnd;
  674. int minx, maxx, miny, maxy;
  675. CCMPatch *patch;
  676. // Work out and grab the relevant patches
  677. VectorSubtract(tBounds[0], mBounds[0], tWork);
  678. VectorInverseScaleVector(tWork, mPatchSize, pStart);
  679. VectorSubtract(tBounds[1], mBounds[0], tWork);
  680. VectorInverseScaleVector(tWork, mPatchSize, pEnd);
  681. minx = Com_Clamp(0, mBlockWidth - 1, floorf(pStart[0]));
  682. maxx = Com_Clamp(0, mBlockWidth - 1, ceilf(pEnd[0]));
  683. miny = Com_Clamp(0, mBlockHeight - 1, floorf(pStart[1]));
  684. maxy = Com_Clamp(0, mBlockHeight - 1, ceilf(pEnd[1]));
  685. // generic box collide with each one
  686. for(y = miny; y <= maxy; y++)
  687. {
  688. for(x = minx; x <= maxx; x++)
  689. {
  690. patch = GetPatch(x, y);
  691. // Collide with every patch to find the minimum fraction
  692. CM_HandlePatchCollision(tw, trace, tBounds[0], tBounds[1], patch, checkcount);
  693. if (trace.fraction <= 0.0)
  694. {
  695. break;
  696. }
  697. }
  698. }
  699. }
  700. }
  701. float CCMLandScape::WaterCollide(const vec3_t begin, const vec3_t end, float fraction) const
  702. {
  703. // Check for completely above water
  704. if((begin[2] > mWaterHeight) && (end[2] > mWaterHeight))
  705. {
  706. return(fraction);
  707. }
  708. // Check for completely below water
  709. if((begin[2] < mWaterHeight) && (end[2] < mWaterHeight))
  710. {
  711. return(fraction);
  712. }
  713. // Check for starting in water and leaving
  714. if(begin[2] < mWaterHeight - SURFACE_CLIP_EPSILON)
  715. {
  716. fraction = ((mWaterHeight - SURFACE_CLIP_EPSILON) - begin[2]) / (end[2] - begin[2]);
  717. return(fraction);
  718. }
  719. // Now the trace must be entering the water
  720. if(begin[2] > mWaterHeight + SURFACE_CLIP_EPSILON)
  721. {
  722. fraction = (begin[2] - (mWaterHeight + SURFACE_CLIP_EPSILON)) / (begin[2] - end[2]);
  723. }
  724. return(fraction);
  725. }
  726. void CCMLandScape::GetTerxelLocalCoords ( int x, int y, vec3_t localCoords[8] )
  727. {
  728. int realWidth;
  729. vec3_t* coords;
  730. int offsets[4];
  731. int i;
  732. coords = GetCoords ( );
  733. realWidth = GetRealWidth ( );
  734. if ( (x+y)&1 )
  735. {
  736. offsets[0] = (y * realWidth) + x; // TL
  737. offsets[1] = (y * realWidth) + x + 1; // TR
  738. offsets[2] = ((y + 1) * realWidth) + x; // BL
  739. offsets[3] = ((y + 1) * realWidth) + x + 1; // BR
  740. }
  741. else
  742. {
  743. offsets[2] = (y * realWidth) + x; // TL
  744. offsets[0] = (y * realWidth) + x + 1; // TR
  745. offsets[3] = ((y + 1) * realWidth) + x; // BL
  746. offsets[1] = ((y + 1) * realWidth) + x + 1; // BR
  747. }
  748. for( i = 0; i < 4; i++ )
  749. {
  750. VectorCopy(coords[offsets[i]], localCoords[i]);
  751. VectorCopy(coords[offsets[i]], localCoords[i + 4]);
  752. // Set z of base of brush to bottom of landscape brush
  753. localCoords[i + 4][2] = GetMins()[2];
  754. }
  755. }
  756. void CCMLandScape::UpdatePatches(void)
  757. {
  758. CCMPatch *patch;
  759. int x, y, ix, iy, numBrushesPerPatch;
  760. vec3_t world;
  761. int size;
  762. /* for(y=0;y<GetRealHeight();y++)
  763. {
  764. for(x=0;x<GetRealWidth();x++)
  765. {
  766. Com_Printf("(%d,%d) = %u\n", x, y, (unsigned)mHeightMap[y*GetRealWidth() + x]);
  767. }
  768. }
  769. */
  770. // Calculate real world coordinates from the heightmap
  771. CalcRealCoords();
  772. numBrushesPerPatch = mTerxels * mTerxels * 2;
  773. size = (numBrushesPerPatch * sizeof(cbrush_t)) + (numBrushesPerPatch * BRUSH_SIDES_PER_TERXEL * 2 * (sizeof(cbrushside_t) + sizeof(cplane_t)));
  774. patch = mPatches;
  775. for(y = 0, iy = 0; y < mHeight; y += mTerxels, iy++)
  776. {
  777. for(x = 0, ix = 0; x < mWidth; x += mTerxels, ix++, patch++)
  778. {
  779. VectorSet(world, mBounds[0][0] + (x * mTerxelSize[0]), mBounds[0][1] + (y * mTerxelSize[1]), mBounds[0][2]);
  780. patch->Init(this, x, y, world, mHeightMap, mPatchBrushData + (size * (ix + (iy * mBlockWidth))));
  781. }
  782. }
  783. /*
  784. for ( y = mTerxels; y < mHeight - mTerxels; y ++ )
  785. {
  786. for ( x = mTerxels; x < mWidth - mTerxels; x ++ )
  787. {
  788. int xo = x % mTerxels;
  789. int yo = y % mTerxels;
  790. int xor = (x + 1) % mTerxels;
  791. int yob = (y + 1) % mTerxels;
  792. CCMPatch* patch = mPatches + (mWidth / mTerxels) * y + (x / mTerxels);
  793. CCMPatch* rpatch = mPatches + (mWidth / mTerxels) * y + ((x+1) / mTerxels);
  794. CCMPatch* bpatch = mPatches + (mWidth / mTerxels) * (y + 1) + (x / mTerxels);
  795. int offsets[4];
  796. vec3_t localCoords[8];
  797. vec3_t localCoordsR[8];
  798. vec3_t localCoordsL[8];
  799. GetTerxelLocalCoords ( x, y, localCoords );
  800. GetTerxelLocalCoords ( x + 1, y, localCoordsR );
  801. GetTerxelLocalCoords ( x, y + 1, localCoordsB );
  802. brush = patch->GetCollisionData ( );;
  803. side = (cbrushside_t *)(mPatchBrushData + patch->GetNumBrushes ( ) );
  804. plane = (cplane_t *)(side + (mNumBrushes * BRUSH_SIDES_PER_TERXEL * 2));
  805. float V = DotProduct ( (plane + 8)->normal, localCoords[0] ) + plane->dist;
  806. if ( V < 0 )
  807. {
  808. InitPlane ( brush[0].sides + brush[0].numsides, plane + brush[0].numsides, localCoords[3], localCoords[2], localCoords[1]);
  809. brush[0].numsides++;
  810. InitPlane ( brush[1].sides + brush[1].numsides, plane + 8 + brush[1].numsides, localCoords[0], localCoords[1], localCoords[2]);
  811. brush[1].numsides++;
  812. }
  813. }
  814. }
  815. */
  816. // Cleanup coord array
  817. Z_Free(mCoords);
  818. }
  819. void CCMLandScape::CalcRealCoords(void)
  820. {
  821. int x, y;
  822. mCoords = (vec3_t *)Z_Malloc(sizeof(vec3_t) * GetRealWidth() * GetRealHeight(), TAG_CM_TERRAIN, qfalse);
  823. // Work out the real world coordinates of each heightmap entry
  824. for(y = 0; y < GetRealHeight(); y++)
  825. {
  826. for(x = 0; x < GetRealWidth(); x++)
  827. {
  828. ivec3_t icoords;
  829. int offset;
  830. offset = (y * GetRealWidth()) + x;
  831. VectorSet(icoords, x, y, mHeightMap[offset]);
  832. VectorScaleVectorAdd(GetMins(), icoords, GetTerxelSize(), mCoords[offset]);
  833. }
  834. }
  835. }
  836. void CCMLandScape::TerrainPatchIterate(void (*IterateFunc)( CCMPatch *, void * ), void *userdata) const
  837. {
  838. int i;
  839. CCMPatch *patch;
  840. patch = mPatches;
  841. for(i = 0; i < GetBlockCount(); i++, patch++)
  842. {
  843. IterateFunc(patch, userdata);
  844. }
  845. }
  846. #define LERP(t, a, b) (((b)-(a))*(t) + (a))
  847. float CCMLandScape::GetWorldHeight(vec3_t origin, const vec3pair_t bounds, bool aboveGround) const
  848. {
  849. vec3_t work;
  850. int minx, maxx, miny, maxy;
  851. int TL, TR, BL, BR;
  852. int final;
  853. VectorSubtract(origin, mBounds[0], work);
  854. VectorInverseScaleVector(work, mTerxelSize, work);
  855. // Presume the bases of all misc models are less than 1 terxel square
  856. minx = Com_Clamp(0, GetWidth(), (int)floorf(work[0]));
  857. maxx = Com_Clamp(0, GetWidth(), (int)ceilf(work[0]));
  858. miny = Com_Clamp(0, GetHeight(), (int)floorf(work[1]));
  859. maxy = Com_Clamp(0, GetHeight(), (int)ceilf(work[1]));
  860. TL = mHeightMap[(miny * GetRealWidth()) + minx];
  861. TR = mHeightMap[(miny * GetRealWidth()) + maxx];
  862. BL = mHeightMap[(maxy * GetRealWidth()) + minx];
  863. BR = mHeightMap[(maxy * GetRealWidth()) + maxx];
  864. if(aboveGround)
  865. {
  866. // int max1, max2;
  867. // max1 = maximum(TL, TR);
  868. // max2 = maximum(BL, BR);
  869. // final = maximum(max1, max2);
  870. float h1, h2;
  871. float tx, ty;
  872. tx = (work[0] - minx)/((float)(maxx-minx));
  873. ty = (work[1] - miny)/((float)(maxy-miny));
  874. h1 = LERP(tx, TL, TR);
  875. h2 = LERP(tx, BL, BR);
  876. final = LERP(ty, h1, h2);
  877. }
  878. else
  879. {
  880. int min1, min2;
  881. min1 = minimum(TL, TR);
  882. min2 = minimum(BL, BR);
  883. final = minimum(min1, min2);
  884. }
  885. origin[2] = (final * mTerxelSize[2]) + mBounds[0][2];
  886. // compute slope at this spot
  887. if (maxx == minx)
  888. maxx = Com_Clamp(0, GetWidth(), minx+1);
  889. if (maxy == miny)
  890. maxy = Com_Clamp(0, GetHeight(), miny+1);
  891. BR = mHeightMap[(maxy * GetRealWidth()) + maxx];
  892. // rise over run
  893. return (fabs((float)(BR - TL)) * mTerxelSize[2]) / mTerxelSize[0];
  894. }
  895. void CM_CircularIterate(byte *data, int width, int height, int xo, int yo, int insideRadius, int outsideRadius, int *user, void (*callback)(byte *, float, int *))
  896. {
  897. int x, y, offset;
  898. byte *work;
  899. for(y = -outsideRadius; y < outsideRadius + 1; y++)
  900. {
  901. if(y + yo >= 0 && y + yo < height)
  902. {
  903. offset = sqrtf((outsideRadius * outsideRadius) - (y * y));
  904. for(x = -offset; x < offset + 1; x++)
  905. {
  906. if(x + xo >= 0 && x + xo < width)
  907. {
  908. float radius = sqrt((float)(x*x+y*y));
  909. if ( radius >= insideRadius )
  910. {
  911. work = data + (x + xo) + ((y + yo) * width);
  912. callback( work, (radius - (float)insideRadius) / (float)(outsideRadius - insideRadius), user);
  913. }
  914. }
  915. }
  916. }
  917. }
  918. }
  919. void CM_ForceHeight( byte *work, float lerp, int *user)
  920. {
  921. *work = (byte)Com_Clamp(0, 255, (int)*user);
  922. }
  923. void CM_GetAverage( byte *work, float lerp, int *user)
  924. {
  925. user[0] += *work;
  926. user[1]++;
  927. }
  928. void CM_Smooth ( byte* work, float lerp, int *user )
  929. {
  930. float smooth = sin ( M_PI/2*3 + (1.0f-lerp) * (M_PI / 2) ) + 1.0f;
  931. // float smooth = (1.0f - lerp);
  932. *work = *work + (int)((float)(*user - *work) * smooth);
  933. }
  934. void CM_MakeAverage( byte *work, float lerp, int *user)
  935. {
  936. int height, diff;
  937. height = (int)*work;
  938. diff = *user - height;
  939. if(abs(diff) > 3)
  940. {
  941. diff >>= 2;
  942. }
  943. height += diff;
  944. *work = (byte)Com_Clamp(0, 255, height);
  945. }
  946. void CCMLandScape::SaveArea(CArea *area)
  947. {
  948. mAreas.push_back(area);
  949. }
  950. void CCMLandScape::CarveLine ( vec3_t start, vec3_t end, int depth, int width )
  951. {
  952. int x, x1, x2, deltax;
  953. int y, y1, y2, deltay;
  954. int xinc1, xinc2;
  955. int yinc1, yinc2;
  956. int den, num;
  957. int count, add;
  958. int i;
  959. float heightStart;
  960. float heightEnd;
  961. float heightStep;
  962. x1 = (int) start[0];
  963. y1 = (int) start[1];
  964. x2 = (int) end[0];
  965. y2 = (int) end[1];
  966. deltax = abs(x2 - x1);
  967. deltay = abs(y2 - y1);
  968. x = x1;
  969. y = y1;
  970. // The x-values are increasing
  971. if (x2 >= x1)
  972. {
  973. xinc1 = 1;
  974. xinc2 = 1;
  975. }
  976. // The x-values are decreasing
  977. else
  978. {
  979. xinc1 = -1;
  980. xinc2 = -1;
  981. }
  982. // The y-values are increasing
  983. if (y2 >= y1)
  984. {
  985. yinc1 = 1;
  986. yinc2 = 1;
  987. }
  988. // The y-values are decreasing
  989. else
  990. {
  991. yinc1 = -1;
  992. yinc2 = -1;
  993. }
  994. if (deltax >= deltay) // There is at least one x-value for every y-value
  995. {
  996. xinc1 = 0; // Don't change the x when numerator >= denominator
  997. yinc2 = 0; // Don't change the y for every iteration
  998. den = deltax;
  999. num = deltax / 2;
  1000. add = deltay;
  1001. count = deltax; // There are more x-values than y-values
  1002. }
  1003. else // There is at least one y-value for every x-value
  1004. {
  1005. xinc2 = 0; // Don't change the x for every iteration
  1006. yinc1 = 0; // Don't change the y when numerator >= denominator
  1007. den = deltay;
  1008. num = deltay / 2;
  1009. add = deltax;
  1010. count = deltay; // There are more y-values than x-values
  1011. }
  1012. vec3_t pt;
  1013. vec3_t bounds[2] = {{-1,-1,-1},{1,1,1}};
  1014. pt[0] = start[0];
  1015. pt[1] = start[1];
  1016. GetWorldHeight ( pt, bounds, false );
  1017. heightStart = pt[2];
  1018. pt[0] = end[0];
  1019. pt[1] = end[1];
  1020. GetWorldHeight ( pt, bounds, false );
  1021. heightEnd = pt[2];
  1022. heightStep = (heightEnd-heightStart) / count;
  1023. for ( i = 0; i <= count; i++ )
  1024. {
  1025. // Flatten the current location
  1026. CArea area;
  1027. pt[0] = x;
  1028. pt[1] = y;
  1029. area.Init ( pt, width / 2 + (irand(0, width/2)) );
  1030. FlattenArea ( &area, heightStart + (heightStep * i) - (depth/2 - (irand(0, depth/2))), false, true, true );
  1031. // Increase the numerator by the top of the fraction
  1032. num += add;
  1033. if (num >= den)
  1034. {
  1035. // Calculate the new numerator value
  1036. num -= den;
  1037. // Change the x and y as appropriate
  1038. x += xinc1;
  1039. y += yinc1;
  1040. }
  1041. // Change the x and y as appropriate
  1042. x += xinc2;
  1043. y += yinc2;
  1044. }
  1045. }
  1046. void CCMLandScape::CarveBezierCurve ( int numCtlPoints, vec3_t* ctlPoints, int steps, int depth, int size )
  1047. {
  1048. int i;
  1049. int choose;
  1050. int n;
  1051. float u;
  1052. float t;
  1053. float tt;
  1054. float t1;
  1055. float step;
  1056. vec3_t pt;
  1057. vec3_t lastpt;
  1058. vec3_t b[10];
  1059. n = numCtlPoints - 1;
  1060. choose = 1;
  1061. for ( i = 1; i <= n; i ++ )
  1062. {
  1063. if ( i == 1 )
  1064. choose = n;
  1065. else
  1066. choose = choose * (n-i+1) / i;
  1067. (*(ctlPoints+i))[0] *= choose;
  1068. (*(ctlPoints+i))[1] *= choose;
  1069. }
  1070. step = 1.0f / (float)steps;
  1071. for ( choose = 0, t = step; t < 1; t += step, choose++ )
  1072. {
  1073. b[0][0] = (*(ctlPoints+0))[0];
  1074. b[0][1] = (*(ctlPoints+0))[1];
  1075. for ( u = t, i = 1; i <= n; i ++ )
  1076. {
  1077. b[i][0] = (*(ctlPoints+i))[0] * u;
  1078. b[i][1] = (*(ctlPoints+i))[1] * u;
  1079. u = u * t;
  1080. }
  1081. pt[0] = b[n][0];
  1082. pt[1] = b[n][1];
  1083. t1 = 1 - t;
  1084. tt = t1;
  1085. for ( i = n - 1; i >= 0; i -- )
  1086. {
  1087. pt[0] += b[i][0] * tt;
  1088. pt[1] += b[i][1] * tt;
  1089. tt = tt * t1;
  1090. }
  1091. if ( choose != 0 )
  1092. {
  1093. CarveLine ( lastpt, pt, depth, size );
  1094. }
  1095. // Save this point for next time around
  1096. lastpt[0] = pt[0];
  1097. lastpt[1] = pt[1];
  1098. }
  1099. }
  1100. void CCMLandScape::FlattenArea(CArea *area, int height, bool save, bool forceHeight, bool smooth )
  1101. {
  1102. vec3_t temp;
  1103. ivec3_t icoords;
  1104. int radius;
  1105. int height2;
  1106. if(save)
  1107. {
  1108. SaveArea(area);
  1109. // mAreas.push_back(*area);
  1110. }
  1111. // Work out coords in the heightmap
  1112. VectorSubtract(area->GetPosition(), mBounds[0], temp);
  1113. icoords[0] = temp[0] / (mBounds[1][0] - mBounds[0][0]) * (float)GetRealWidth ( );
  1114. icoords[1] = temp[1] / (mBounds[1][1] - mBounds[0][1]) * (float)GetRealHeight ( );
  1115. // VectorInverseScaleVector(temp, mTerxelSize, icoords);
  1116. // round up, we'd rather have a little more area flattened than have less then what was requested
  1117. radius = (int)ceilf( (area->GetRadius() / mTerxelSize[1]) );
  1118. // Work out the average height of the surrounding terrain
  1119. height2 = height;
  1120. if(height < 0)
  1121. {
  1122. ivec3_t info;
  1123. info[0] = 0;
  1124. info[1] = 0;
  1125. CM_CircularIterate(mHeightMap, GetRealWidth(), GetRealHeight(), icoords[0], icoords[1], 0, radius, info, CM_GetAverage);
  1126. if(info[1])
  1127. {
  1128. height = info[0] / info[1];
  1129. }
  1130. }
  1131. else
  1132. {
  1133. height = height & 0x7F;
  1134. }
  1135. if ( smooth )
  1136. {
  1137. CM_CircularIterate(mHeightMap, GetRealWidth(), GetRealHeight(), icoords[0], icoords[1], radius, radius * 3, &height, CM_Smooth);
  1138. }
  1139. if ( forceHeight )
  1140. {
  1141. CM_CircularIterate(mHeightMap, GetRealWidth(), GetRealHeight(), icoords[0], icoords[1], 0, radius + 1, &height, CM_ForceHeight );
  1142. assert (mFlattenMap);
  1143. CM_CircularIterate(mFlattenMap, GetRealWidth(), GetRealHeight(), icoords[0], icoords[1], 0, radius + 1, &height2, CM_ForceHeight );
  1144. }
  1145. else if ( smooth )
  1146. {
  1147. CM_CircularIterate(mHeightMap, GetRealWidth(), GetRealHeight(), icoords[0], icoords[1], 0, radius, &height, CM_Smooth);
  1148. }
  1149. }
  1150. void CM_BelowLevel(byte *data, float lerp, int *info)
  1151. {
  1152. info[1]++;
  1153. if(*data < info[2])
  1154. {
  1155. info[0]++;
  1156. }
  1157. }
  1158. float CCMLandScape::FractionBelowLevel(CArea *area, int height)
  1159. {
  1160. vec3_t temp;
  1161. ivec3_t icoords, info;
  1162. int count;
  1163. float level;
  1164. // Work out coords in the heightmap
  1165. VectorSubtract(area->GetPosition(), mBounds[0], temp);
  1166. VectorInverseScaleVector(temp, mTerxelSize, icoords);
  1167. // Work out radius of area in heightmap entries
  1168. count = area->GetRadius() / mTerxelSize[1];
  1169. info[0] = 0;
  1170. info[1] = 0;
  1171. info[2] = height;
  1172. if(height < 0)
  1173. {
  1174. info[2] = mBaseWaterHeight;
  1175. }
  1176. CM_CircularIterate(mHeightMap, GetRealWidth(), GetRealHeight(), icoords[0], icoords[1], 0, count, info, CM_BelowLevel);
  1177. level = 0.0f;
  1178. if(info[1])
  1179. {
  1180. level = (float)info[0] / info[1];
  1181. }
  1182. return(level);
  1183. }
  1184. CArea *CCMLandScape::GetFirstArea(void)
  1185. {
  1186. if(!mAreas.size())
  1187. {
  1188. return(NULL);
  1189. }
  1190. mAreasIt = mAreas.begin();
  1191. return (*mAreasIt);
  1192. }
  1193. CArea *CCMLandScape::GetFirstObjectiveArea(void)
  1194. {
  1195. if(!mAreas.size())
  1196. {
  1197. return(NULL);
  1198. }
  1199. mAreasIt = mAreas.begin();
  1200. while (mAreasIt != mAreas.end())
  1201. {
  1202. // run through the areas to find the player area
  1203. if((*mAreasIt)->GetType() == AT_OBJECTIVE)
  1204. {
  1205. return (*mAreasIt);
  1206. }
  1207. mAreasIt++;
  1208. }
  1209. return(NULL);
  1210. }
  1211. CArea *CCMLandScape::GetPlayerArea(void)
  1212. { // do me
  1213. if(!mAreas.size())
  1214. {
  1215. return(NULL);
  1216. }
  1217. mAreasIt = mAreas.begin();
  1218. while (mAreasIt != mAreas.end())
  1219. {
  1220. // run through the areas to find the player area
  1221. if((*mAreasIt)->GetType() == AT_PLAYER)
  1222. {
  1223. return (*mAreasIt);
  1224. }
  1225. mAreasIt++;
  1226. }
  1227. return(NULL);
  1228. }
  1229. CArea *CCMLandScape::GetNextArea(void)
  1230. {
  1231. mAreasIt++;
  1232. if(mAreasIt == mAreas.end())
  1233. {
  1234. return(NULL);
  1235. }
  1236. return (*mAreasIt);
  1237. }
  1238. CArea *CCMLandScape::GetNextObjectiveArea(void)
  1239. {
  1240. mAreasIt++;
  1241. while (mAreasIt != mAreas.end())
  1242. {
  1243. // run through the areas to find the player area
  1244. if((*mAreasIt)->GetType() == AT_OBJECTIVE)
  1245. {
  1246. return (*mAreasIt);
  1247. }
  1248. mAreasIt++;
  1249. }
  1250. return(NULL);
  1251. }
  1252. bool CCMLandScape::AreaCollision(CArea *area, int *areaTypes, int areaTypeCount)
  1253. {
  1254. CArea *areas;
  1255. int i;
  1256. float segment;
  1257. bool collision;
  1258. areas = GetFirstArea();
  1259. while(areas)
  1260. {
  1261. collision = false;
  1262. if(area->GetVillageID() == areas->GetVillageID())
  1263. {
  1264. // Check for being too close angularly
  1265. if(area->GetAngleDiff() && areas->GetAngleDiff())
  1266. {
  1267. segment = areas->GetAngle() - area->GetAngle();
  1268. if(segment < M_PI)
  1269. {
  1270. segment += (float)(2 * M_PI);
  1271. }
  1272. if(segment > M_PI)
  1273. {
  1274. segment -= (float)(2 * M_PI);
  1275. }
  1276. if(fabsf(segment) < areas->GetAngleDiff() + area->GetAngleDiff())
  1277. {
  1278. collision = true;
  1279. }
  1280. }
  1281. }
  1282. // Check for buildings being too close together
  1283. if(Distance(areas->GetPosition(), area->GetPosition()) < areas->GetRadius() + area->GetRadius())
  1284. {
  1285. collision = true;
  1286. }
  1287. if(collision)
  1288. {
  1289. // If no area type list was specified then all areas are fair game
  1290. if ( !areaTypes )
  1291. {
  1292. return true;
  1293. }
  1294. for(i = 0; i < areaTypeCount; i++)
  1295. {
  1296. if(areas->GetType() == areaTypes[i])
  1297. {
  1298. return(true);
  1299. }
  1300. }
  1301. }
  1302. areas = GetNextArea();
  1303. }
  1304. return(false);
  1305. }
  1306. void CCMLandScape::rand_seed(int seed)
  1307. {
  1308. holdrand = seed;
  1309. Com_Printf("rand_seed = %d\n", holdrand);
  1310. }
  1311. float CCMLandScape::flrand(float min, float max)
  1312. {
  1313. float result;
  1314. assert((max - min) < 32768);
  1315. holdrand = (holdrand * 214013L) + 2531011L;
  1316. result = (float)(holdrand >> 17); // 0 - 32767 range
  1317. result = ((result * (max - min)) / 32768.0F) + min;
  1318. // Com_Printf("flrand: Seed = %d\n", holdrand);
  1319. return(result);
  1320. }
  1321. int CCMLandScape::irand(int min, int max)
  1322. {
  1323. int result;
  1324. assert((max - min) < 32768);
  1325. max++;
  1326. holdrand = (holdrand * 214013L) + 2531011L;
  1327. result = holdrand >> 17;
  1328. result = ((result * (max - min)) >> 15) + min;
  1329. // Com_Printf("irand: Seed = %d\n", holdrand);
  1330. return(result);
  1331. }
  1332. CCMLandScape::~CCMLandScape(void)
  1333. {
  1334. if(mHeightMap)
  1335. {
  1336. Z_Free(mHeightMap);
  1337. mHeightMap = NULL;
  1338. }
  1339. if(mFlattenMap)
  1340. {
  1341. Z_Free(mFlattenMap);
  1342. mFlattenMap = NULL;
  1343. }
  1344. if(mPatchBrushData)
  1345. {
  1346. Z_Free(mPatchBrushData);
  1347. mPatchBrushData = NULL;
  1348. }
  1349. if(mPatches)
  1350. {
  1351. Z_Free(mPatches);
  1352. mPatches = NULL;
  1353. }
  1354. if (mRandomTerrain)
  1355. {
  1356. delete mRandomTerrain;
  1357. }
  1358. for(mAreasIt=mAreas.begin(); mAreasIt != mAreas.end(); mAreasIt++)
  1359. {
  1360. delete (*mAreasIt);
  1361. }
  1362. mAreas.clear();
  1363. }
  1364. class CCMLandScape *CM_InitTerrain(const char *configstring, thandle_t terrainId, bool server)
  1365. {
  1366. CCMLandScape *ls;
  1367. ls = new CCMLandScape(configstring, server);
  1368. ls->SetTerrainId(terrainId);
  1369. return(ls);
  1370. }
  1371. void CM_TerrainPatchIterate(const class CCMLandScape *landscape, void (*IterateFunc)( CCMPatch *, void * ), void *userdata)
  1372. {
  1373. landscape->TerrainPatchIterate(IterateFunc, userdata);
  1374. }
  1375. float CM_GetWorldHeight(const CCMLandScape *landscape, vec3_t origin, const vec3pair_t bounds, bool aboveGround)
  1376. {
  1377. return landscape->GetWorldHeight(origin, bounds, aboveGround);
  1378. }
  1379. void CM_FlattenArea(CCMLandScape *landscape, CArea *area, int height, bool save, bool forceHeight, bool smooth )
  1380. {
  1381. landscape->FlattenArea(area, height, save, forceHeight, smooth );
  1382. }
  1383. void CM_CarveBezierCurve(CCMLandScape *landscape, int numCtls, vec3_t* ctls, int steps, int depth, int size )
  1384. {
  1385. landscape->CarveBezierCurve(numCtls, ctls, steps, depth, size );
  1386. }
  1387. void CM_SaveArea(CCMLandScape *landscape, CArea *area)
  1388. {
  1389. landscape->SaveArea(area);
  1390. }
  1391. float CM_FractionBelowLevel(CCMLandScape *landscape, CArea *area, int height)
  1392. {
  1393. return(landscape->FractionBelowLevel(area, height));
  1394. }
  1395. bool CM_AreaCollision(class CCMLandScape *landscape, class CArea *area, int *areaTypes, int areaTypeCount)
  1396. {
  1397. return(landscape->AreaCollision(area, areaTypes, areaTypeCount));
  1398. }
  1399. CArea *CM_GetFirstArea(CCMLandScape *landscape)
  1400. {
  1401. return(landscape->GetFirstArea());
  1402. }
  1403. CArea *CM_GetFirstObjectiveArea(CCMLandScape *landscape)
  1404. {
  1405. return(landscape->GetFirstObjectiveArea());
  1406. }
  1407. CArea *CM_GetPlayerArea(CCMLandScape *landscape)
  1408. {
  1409. return(landscape->GetPlayerArea());
  1410. }
  1411. CArea *CM_GetNextArea(CCMLandScape *landscape)
  1412. {
  1413. return(landscape->GetNextArea());
  1414. }
  1415. CArea *CM_GetNextObjectiveArea(CCMLandScape *landscape)
  1416. {
  1417. return(landscape->GetNextObjectiveArea());
  1418. }
  1419. CRandomTerrain *CreateRandomTerrain(const char *config, CCMLandScape *landscape, byte *heightmap, int width, int height)
  1420. {
  1421. CRandomTerrain *RandomTerrain = 0;
  1422. #ifndef PRE_RELEASE_DEMO
  1423. char *ptr;
  1424. unsigned long seed;
  1425. seed = strtoul(Info_ValueForKey(config, "seed"), &ptr, 10);
  1426. landscape->rand_seed(seed);
  1427. RandomTerrain = new CRandomTerrain;
  1428. RandomTerrain->Init(landscape, heightmap, width, height);
  1429. #endif // #ifndef PRE_RELEASE_DEMO
  1430. /*
  1431. RandomTerrain->CreatePath(0, -1, 0, 9, 0.1, 0.5, 0.5, 0.5, 0.05, 0.08, 0.31, 0.1, 3);
  1432. RandomTerrain->CreatePath(1, 0, 0, 6, 0.5, 0.5, 0.9, 0.1, 0.08, 0.1, 0.31, 0.1, 0.9);
  1433. RandomTerrain->CreatePath(2, 0, 0, 6, 0.5, 0.5, 0.9, 0.9, 0.08, 0.1, 0.31, 0.1, 0.9);
  1434. RandomTerrain->Generate();
  1435. */
  1436. return RandomTerrain;
  1437. }
  1438. // end
  1439. #ifdef _WIN32
  1440. #pragma optimize("p", off)
  1441. #endif