Advertisement
kajacx

ring rotator

May 2nd, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. # kajacx_rotator.crpl
  2. # Created on: 5/2/2019 10:58:05 PM
  3. # ------------------------------------------
  4.  
  5. $MaxRange:20 # maximum range ever use
  6.  
  7. $Range:20 # range for this specific rotator
  8. $Interval:3 # rotate every N frames
  9. $RotationSpeed:0.08726646259 # rotatble by X radians (0.08726646259 = 5 degrees)
  10. $CenterX:67
  11. $CenterY:48
  12.  
  13. $DebugImages:1
  14. $ImageSquare:"Custom0pp"
  15.  
  16. once
  17. ShowTraceLog
  18. ClearTraceLog
  19. #HideTraceLog
  20.  
  21. "Start" Trace
  22. @InitializeRings
  23. "Init done" Trace
  24. <-CenterX <-CenterY <-Range @SaveTerrain ->Terrain
  25. "Save done" Trace
  26.  
  27. <-Interval SetTimer0
  28. 0 ->angle
  29.  
  30.  
  31. Self RemoveImages
  32. <-DebugImages if
  33. :DrawDebugImages
  34. endif
  35. endonce
  36.  
  37. GetTimer0 eq0 if
  38. <-angle <-RotationSpeed add ->angle
  39. "Pasting at angle" <-angle Trace2
  40. <-CenterX <-CenterY <-Range <-Terrain <-angle @PasteTerrain
  41. <-Interval SetTimer0
  42. endif
  43.  
  44. :InitializeRings # [ - ] call at start or when MaxRange changes
  45. # Rings - index is length from start
  46. <-MaxRange 1 add CreateListStartingSize ->RingsX
  47. <-MaxRange 1 add CreateListStartingSize ->RingsY
  48. <-MaxRange 1 add 0 do
  49. <-RingsX I CreateList SetListElement
  50. <-RingsY I CreateList SetListElement
  51. loop
  52.  
  53. "Rings done" Trace
  54.  
  55. #assign cells into rings
  56. <-MaxRange 1 add <-MaxRange neg do # J = Y
  57. <-MaxRange 1 add <-MaxRange neg do # I = X
  58. I J 0 0 Distance 0 Round asint ->IR_Distance
  59. <-IR_Distance <-MaxRange lte if
  60. <-RingsX <-IR_Distance GetListElement I AppendToList
  61. <-RingsY <-IR_Distance GetListElement J AppendToList
  62. endif
  63. loop
  64. loop
  65.  
  66. "assign done" Trace
  67. 1 Delay # quick and dirty way t oavoid the 1000000 overflow
  68.  
  69. #sort cells in rings by their angle
  70. 0 ->DEBUG_Comparisons
  71.  
  72. <-MaxRange 1 add 0 do
  73. <-RingsX I GetListElement ->IR_RsX
  74. <-RingsY I GetListElement ->IR_RsY
  75. <-IR_RsX GetListCount 0 do # use insertion sort
  76.  
  77. # find smallest element in the remaining list
  78. 0 ->IR_LowIndex
  79. 1000000 ->IR_LowValue
  80. <-IR_RsX GetListCount I do
  81. <-IR_RsY I GetListElement <-IR_RsX I GetListElement atan2 6.28318530718 add 6.28318530718 mod ->IR_Value
  82. <-IR_Value <-IR_LowValue lt if
  83. <-IR_Value ->IR_LowValue
  84. I ->IR_LowIndex
  85. endif
  86. <-DEBUG_Comparisons 1 add ->DEBUG_Comparisons
  87. loop
  88.  
  89. #now switch values around
  90. <-IR_RsX I GetListElement ->IR_OldX
  91. <-IR_RsY I GetListElement ->IR_OldY
  92.  
  93. <-IR_RsX I SetListElement(<-IR_RsX <-IR_LowIndex GetListElement)
  94. <-IR_RsY I SetListElement(<-IR_RsY <-IR_LowIndex GetListElement)
  95.  
  96. <-IR_RsX <-IR_LowIndex SetListElement(<-IR_OldX)
  97. <-IR_RsY <-IR_LowIndex SetListElement(<-IR_OldY)
  98. loop
  99. ### This takes way to many isstructions, it would be better to precompute in a separate program and paste the completed data
  100. "Processed" I "ring, comparisions:" <-DEBUG_Comparisons Trace4
  101. 1 Delay
  102. loop
  103.  
  104. "sort done" Trace
  105.  
  106. # aaand done
  107.  
  108. :SaveTerrain # [ X Y Range - List] saves terrain to a list
  109. ->ST_Range
  110. ->ST_Y
  111. ->ST_X
  112.  
  113. CreateList ->ST_Terrain
  114. <-ST_Range 1 add 0 do # go over avaliable rings
  115. <-RingsX I GetListElement ->ST_RsX
  116. <-RingsY I GetListElement ->ST_RsY
  117. <-ST_RsX GetListCount 0 do
  118. # save the terrain height in order of the rings
  119. <-ST_Terrain # keep the list
  120. <-ST_RsX I GetListElement <-ST_X add # x coord
  121. <-ST_RsY I GetListElement <-ST_Y add # x coord
  122. GetTerrain
  123. AppendToList # save to ST_Terrain
  124. loop
  125. loop
  126.  
  127.  
  128. #<-ST_Range 2 mul 1 add ->ST_Offset
  129. #
  130. #<-ST_Offset <-ST_Offset mul CreateListStartingSize ->ST_Terrain
  131. #
  132. #<-ST_Range 1 add <-ST_Range neg do # J = Y
  133. # <-ST_Range 1 add <-ST_Range neg do # I = X
  134. # #I J 0 0 Distance 0 Round asint ->ST_Distance
  135. # #<-ST_Distance <-ST_Range lte if
  136. # # save terrain always - space will be waster but it is better than the alternative
  137. # # or is it? maybe it would be more efficient to store terrain in the same order as the rings...
  138. # <-ST_Terrain J <-ST_Offset mul I add I J GetTerrain SetListElement
  139. # #endif
  140. # loop
  141. #loop
  142.  
  143. <-ST_Terrain return
  144.  
  145. :PasteTerrain # [ X Y Range List Angle - ] pastes the terrain into the world, at the given CCW angle in radians
  146. ->PT_Angle
  147. ->PT_Terrain
  148. ->PT_Range
  149. ->PT_Y
  150. ->PT_X
  151.  
  152. <-PT_Terrain Trace
  153.  
  154. 0 ->PT_Index # index from the terrain list, in order of the rings
  155.  
  156. <-PT_Range 1 add 0 do # go over avaliable rings
  157. <-RingsX I GetListElement ->PT_RsX
  158. <-RingsY I GetListElement ->PT_RsY
  159. <-PT_RsX GetListCount ->PT_Count
  160. <-PT_Angle 6.28318530718 div <-PT_Count mul 0 Round asint ->PT_Offset
  161. "Ring" I "Offset" <-PT_Offset Trace4
  162. <-PT_Count 0 do
  163. # at I+Offset position, set terrain from I, that way it will be a counter-clockwise rotation
  164. I <-PT_Offset add <-PT_Count mod ->PT_Target
  165. <-PT_RsX <-PT_Target GetListElement <-PT_X add #x target coordinate
  166. <-PT_RsY <-PT_Target GetListElement <-PT_Y add #y target coordinate
  167. <-PT_Terrain <-PT_Index GetListElement # saved terrain heights are ordered by the ring order already
  168. #Trace3
  169. SetTerrain
  170. <-PT_Index 1 add ->PT_Index
  171. loop
  172. loop
  173.  
  174. # aaand done?
  175.  
  176.  
  177. :DrawDebugImages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement