matrix.bb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. ; ------------------------------------
  2. ; Name: Matrix Command Set
  3. ; Version: V1.0
  4. ; Author: Simon Harrison
  5. ; Email: [email protected]
  6. ; Website: http://www.si-design.co.uk
  7. ; Date: 18/09/01
  8. ; ------------------------------------
  9. Dim db_matrix(65535)
  10. Dim db_matrix_data(65535,5)
  11. Dim db_image(65535)
  12. ; ------------------
  13. ; Name: DB_LoadImage
  14. ; ------------------
  15. Function DB_LoadImage(filename$,image_no)
  16. db_image(image_no)=LoadTexture(filename$)
  17. End Function
  18. ; ----------------
  19. ; Name: MakeMatrix
  20. ; ----------------
  21. Function MakeMatrix(no,width#,depth#,xseg#,zseg#)
  22. db_matrix_data(no,0)=width#
  23. db_matrix_data(no,1)=depth#
  24. db_matrix_data(no,2)=xseg#
  25. db_matrix_data(no,3)=zseg#
  26. db_backdrop=True
  27. db_matrix(no)=CreateMesh()
  28. surf=CreateSurface(db_matrix(no))
  29. For z#=1 To depth# : z#=z#-1
  30. For x#=1 To width# : x#=x#-1
  31. vert0=AddVertex(surf,x#,0,z#)
  32. vert1=AddVertex(surf,x#,0,z#+(depth#/zseg#))
  33. vert2=AddVertex(surf,x#+(width#/xseg#),0,z#+(depth#/zseg#))
  34. vert3=AddVertex(surf,x#+(width#/xseg#),0,z#)
  35. tri0=AddTriangle(surf,vert0,vert1,vert2)
  36. tri1=AddTriangle(surf,vert0,vert2,vert3)
  37. VertexNormal surf,vert0,0,1,0
  38. VertexNormal surf,vert1,0,1,0
  39. VertexNormal surf,vert2,0,1,0
  40. VertexNormal surf,vert3,0,1,0
  41. x#=x#+(width#/xseg#)
  42. Next
  43. z#=z#+(depth#/zseg#)
  44. Next
  45. End Function
  46. ; --------------------
  47. ; Name: PositionMatrix
  48. ; --------------------
  49. Function PositionMatrix(no,x#,y#,z#)
  50. PositionEntity db_matrix(no),x#,y#,z#
  51. End Function
  52. ; --------------------------
  53. ; Name: PrepareMatrixTexture
  54. ; --------------------------
  55. Function PrepareMatrixTexture(matrix_no,image_no,across,down)
  56. db_matrix_data(matrix_no,4)=across
  57. db_matrix_data(matrix_no,5)=down
  58. brush=CreateBrush()
  59. BrushTexture brush,db_image(image_no)
  60. surf=GetSurface(db_matrix(matrix_no),1)
  61. PaintSurface surf,brush
  62. End Function
  63. ; ----------------
  64. ; Name: FillMatrix
  65. ; ----------------
  66. Function FillMatrix(matrix_no,height#,tile_no)
  67. tile_no=tile_no-1
  68. xseg=db_matrix_data(matrix_no,2)
  69. zseg=db_matrix_data(matrix_no,3)
  70. across=db_matrix_data(matrix_no,4)
  71. down=db_matrix_data(matrix_no,5)
  72. surf=GetSurface(db_matrix(matrix_no),1)
  73. cv=CountVertices(surf)
  74. ; Set height
  75. For z=0 To zseg
  76. For x=0 To xseg
  77. index=(x*4)+(z*(xseg*4))
  78. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index-1 : Else index=index-1
  79. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index-((xseg*4)+1) Else index=index-((xseg*4)+1)
  80. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index+3 : Else index=index+3
  81. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index)
  82. Next
  83. Next
  84. ; Set tiles
  85. For z=0 To zseg-1
  86. For x=0 To xseg-1
  87. index=(x*4)+(z*(xseg*4))
  88. useg#=(1.0/across)
  89. vseg#=(1.0/down)
  90. x_tile=tile_no Mod across
  91. y_tile=(down-1)-((tile_no-x_tile)/across)
  92. u#=useg#*x_tile
  93. v#=1-(vseg#*y_tile)
  94. VertexTexCoords surf,index,(u#)+0.002,(v#)-0.002
  95. VertexTexCoords surf,index+1,(u#)+0.002,(v#-vseg#)+0.002
  96. VertexTexCoords surf,index+2,(u#+useg#)-0.002,(v#-vseg#)+0.002
  97. VertexTexCoords surf,index+3,(u#+useg#)-0.002,(v#)-0.002
  98. Next
  99. Next
  100. End Function
  101. ; ---------------------
  102. ; Name: RandomizeMatrix
  103. ; ---------------------
  104. Function RandomizeMatrix(matrix_no,height_range#)
  105. xseg=db_matrix_data(matrix_no,2)
  106. zseg=db_matrix_data(matrix_no,3)
  107. surf=GetSurface(db_matrix(matrix_no),1)
  108. cv=CountVertices(surf)
  109. For z=0 To zseg
  110. For x=0 To xseg
  111. height#=Rnd(0,height_range#)
  112. index=(x*4)+(z*(xseg*4))
  113. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index-1 : Else index=index-1
  114. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index-((xseg*4)+1) : Else index=index-((xseg*4)+1)
  115. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index+3 : Else index=index+3
  116. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index)
  117. Next
  118. Next
  119. End Function
  120. ; ---------------------
  121. ; Name: SetMatrixHeight
  122. ; ---------------------
  123. Function SetMatrixHeight(matrix_no,x,z,height#)
  124. xseg=db_matrix_data(matrix_no,2)
  125. surf=GetSurface(db_matrix(matrix_no),1)
  126. cv=CountVertices(surf)
  127. index=(x*4)+(z*(xseg*4))
  128. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index-1 : Else index=index-1
  129. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index-((xseg*4)+1) : Else index=index-((xseg*4)+1)
  130. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index) : index=index+3 : Else index=index+3
  131. If index>=0 And index<cv Then VertexCoords surf,index,VertexX#(surf,index),height#,VertexZ#(surf,index)
  132. End Function
  133. ; ---------------------
  134. ; Name: SetMatrixNormal
  135. ; ---------------------
  136. Function SetMatrixNormal(matrix_no,x,z,nx#,ny#,nz#)
  137. xseg=db_matrix_data(matrix_no,2)
  138. surf=GetSurface(db_matrix(matrix_no),1)
  139. cv=CountVertices(surf)
  140. index=(x*4)+(z*(xseg*4))
  141. If index>=0 And index<cv Then VertexNormal surf,index,nx#,ny#,nz# : index=index-1 : Else index=index-1
  142. If index>=0 And index<cv Then VertexNormal surf,index,nx#,ny#,nz# : index=index-((xseg*4)+1) : Else index=index-((xseg*4)+1)
  143. If index>=0 And index<cv Then VertexNormal surf,index,nx#,ny#,nz# : index=index+3 : Else index=index+3
  144. If index>=0 And index<cv Then VertexNormal surf,index,nx#,ny#,nz#
  145. End Function
  146. ; -------------------
  147. ; Name: SetMatrixTile
  148. ; -------------------
  149. Function SetMatrixTile(matrix_no,x,z,tile_no)
  150. tile_no=tile_no-1
  151. xseg=db_matrix_data(matrix_no,2)
  152. across=db_matrix_data(matrix_no,4)
  153. down=db_matrix_data(matrix_no,5)
  154. surf=GetSurface(db_matrix(matrix_no),1)
  155. index=(x*4)+(z*(xseg*4))
  156. useg#=(1.0/across)
  157. vseg#=(1.0/down)
  158. x_tile=tile_no Mod across
  159. y_tile=(down-1)-((tile_no-x_tile)/across)
  160. u#=useg#*x_tile
  161. v#=1-(vseg#*y_tile)
  162. VertexTexCoords surf,index,(u#)+0.002,(v#)-0.002
  163. VertexTexCoords surf,index+1,(u#)+0.002,(v#-vseg#)+0.002
  164. VertexTexCoords surf,index+2,(u#+useg#)-0.002,(v#-vseg#)+0.002
  165. VertexTexCoords surf,index+3,(u#+useg#)-0.002,(v#)-0.002
  166. End Function
  167. ; -------------------
  168. ; Name: GhostMatrixOn
  169. ; -------------------
  170. Function GhostMatrixOn(no)
  171. EntityBlend db_matrix(no),3
  172. End Function
  173. ; --------------------
  174. ; Name: GhostMatrixOff
  175. ; --------------------
  176. Function GhostMatrixOff(no)
  177. EntityBlend db_matrix(no),0
  178. End Function
  179. ; ------------------
  180. ; Name: DeleteMatrix
  181. ; ------------------
  182. Function DeleteMatrix(no)
  183. FreeEntity db_matrix(no)
  184. db_matrix(no)=0
  185. db_matrix_data(no,0)=0
  186. db_matrix_data(no,1)=0
  187. db_matrix_data(no,2)=0
  188. db_matrix_data(no,3)=0
  189. db_matrix_data(no,4)=0
  190. db_matrix_data(no,5)=0
  191. End Function
  192. ; -------------------
  193. ; Name: MatrixExist()
  194. ; -------------------
  195. Function MatrixExist(no)
  196. If db_matrix(no)<>0 Then Return True Else Return False
  197. End Function
  198. ; ---------------------
  199. ; Name: MatrixPositionX
  200. ; ---------------------
  201. Function MatrixPositionX(no)
  202. EntityX#(db_matrix(no))
  203. End Function
  204. ; ---------------------
  205. ; Name: MatrixPositionY
  206. ; ---------------------
  207. Function MatrixPositionY(no)
  208. EntityY#(db_matrix(no))
  209. End Function
  210. ; ---------------------
  211. ; Name: MatrixPositionZ
  212. ; ---------------------
  213. Function MatrixPositionZ(no)
  214. EntityZ#(db_matrix(no))
  215. End Function
  216. ; ------------------------
  217. ; Name: GetMatrixHeight#()
  218. ; ------------------------
  219. Function GetMatrixHeight#(matrix_no,x,z)
  220. xseg=db_matrix_data(matrix_no,2)
  221. surf=GetSurface(db_matrix(matrix_no),1)
  222. cv=CountVertices(surf)
  223. index=(x*4)+(z*(xseg*4))
  224. If index>=0 And index<cv
  225. y#=VertexY#(surf,index)
  226. Else
  227. index=index-1
  228. If index>=0 And index<cv
  229. y#=VertexY#(surf,index)
  230. Else
  231. index=index-((xseg*4)+1)
  232. If index>=0 And index<cv
  233. y#=VertexY#(surf,index)
  234. Else
  235. index=index+3
  236. If index>=0 And index<cv
  237. y#=VertexY#(surf,index)
  238. EndIf
  239. EndIf
  240. EndIf
  241. EndIf
  242. Return y#
  243. End Function
  244. ; ------------------------
  245. ; Name: GetGroundHeight#()
  246. ; ------------------------
  247. Function GetGroundHeight#(matrix_no,x_pos#,z_pos#)
  248. width#=db_matrix_data(matrix_no,0)
  249. depth#=db_matrix_data(matrix_no,1)
  250. xseg#=db_matrix_data(matrix_no,2)
  251. zseg#=db_matrix_data(matrix_no,3)
  252. x#=x_pos#/(width#/xseg#)
  253. z#=z_pos#/(depth#/zseg#)
  254. ix=Floor#(x#) ;integer parts of x,z
  255. iz=Floor#(z#)
  256. fx#=x#-ix ;fractional remainders
  257. fz#=z#-iz
  258. surf=GetSurface(db_matrix(matrix_no),1)
  259. cv=CountVertices(surf)
  260. index=(ix*4)+(iz*(xseg#*4))
  261. If index>=0 And index<cv Then y00#=VertexY#(surf,index) : index=index+1 : Else index=index+1
  262. If index>=0 And index<cv Then y01#=VertexY#(surf,index) : index=index+1 : Else index=index+1
  263. If index>=0 And index<cv Then y11#=VertexY#(surf,index) : index=index+1 : Else index=index+1
  264. If index>=0 And index<cv Then y10#=VertexY#(surf,index)
  265. ya#=(y10-y00)*fx+y00
  266. yb#=(y11-y01)*fx+y01
  267. y#=(yb-ya)*fz+ya
  268. Return y#
  269. End Function
  270. ; -----------------------
  271. ; Name: MatrixTileCount()
  272. ; -----------------------
  273. Function MatrixTileCount()
  274. across=db_matrix_data(matrix_no,4)
  275. down=db_matrix_data(matrix_no,5)
  276. Return across*down
  277. End Function
  278. ; ------------------------
  279. ; Name: MatrixTilesExist()
  280. ; ------------------------
  281. Function MatrixTilesExist()
  282. across=db_matrix_data(matrix_no,4)
  283. down=db_matrix_data(matrix_no,5)
  284. If across*down<>0 Then Return True Else Return False
  285. End Function
  286. ; ----------------------------
  287. ; Name: MatrixWireframeState()
  288. ; ----------------------------
  289. Function MatrixWireframeState()
  290. Return 0
  291. End Function