Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. const f32 MAX_BUILD_LENGTH = 4.0f;
  2.  
  3. shared class BlockCursor
  4. {
  5. Vec2f tileAimPos;
  6. bool cursorClose;
  7. bool buildable;
  8. bool supported;
  9. bool hasReqs;
  10. // for gui
  11. bool rayBlocked;
  12. bool buildableAtPos;
  13. Vec2f rayBlockedPos;
  14. bool blockActive;
  15. bool blobActive;
  16. bool sameTileOnBack;
  17. CBitStream missing;
  18.  
  19. BlockCursor()
  20. {
  21. blobActive = blockActive = buildableAtPos = rayBlocked = hasReqs = supported = buildable = cursorClose = sameTileOnBack = false;
  22. }
  23. };
  24.  
  25. void AddCursor(CBlob@ this)
  26. {
  27. if (!this.exists("blockCursor"))
  28. {
  29. BlockCursor bc;
  30. this.set("blockCursor", bc);
  31. }
  32. }
  33.  
  34. bool canPlaceNextTo(CMap@ map, const Tile &in tile)
  35. {
  36. return tile.support > 0;
  37. }
  38.  
  39. bool isBuildableAtPos(CBlob@ this, Vec2f p, TileType buildTile, CBlob @blob, bool &out sameTile)
  40. {
  41. f32 radius = 0.0f;
  42. CMap@ map = this.getMap();
  43. sameTile = false;
  44.  
  45. if (blob is null) // BLOCKS
  46. {
  47. radius = map.tilesize;
  48. }
  49. else // BLOB
  50. {
  51. radius = blob.getRadius();
  52. }
  53.  
  54. //check height + edge proximity
  55. if (p.y < 2 * map.tilesize ||
  56. p.x < 2 * map.tilesize ||
  57. p.x > (map.tilemapwidth - 2.0f)*map.tilesize)
  58. {
  59. return false;
  60. }
  61.  
  62. // tilemap check
  63. const bool buildSolid = (map.isTileSolid(buildTile) || (blob !is null && blob.isCollidable()));
  64. Vec2f tilespace = map.getTileSpacePosition(p);
  65. const int offset = map.getTileOffsetFromTileSpace(tilespace);
  66. Tile backtile = map.getTile(offset);
  67. Tile left = map.getTile(offset - 1);
  68. Tile right = map.getTile(offset + 1);
  69. Tile up = map.getTile(offset - map.tilemapwidth);
  70. Tile down = map.getTile(offset + map.tilemapwidth);
  71.  
  72. if (buildTile > 0 && buildTile < 255 && blob is null && buildTile == map.getTile(offset).type)
  73. {
  74. sameTile = true;
  75. return false;
  76. }
  77.  
  78. if ((buildTile == CMap::tile_wood && backtile.type >= CMap::tile_wood_d1 && backtile.type <= CMap::tile_wood_d0) ||
  79. (buildTile == CMap::tile_castle && backtile.type >= CMap::tile_castle_d1 && backtile.type <= CMap::tile_castle_d0))
  80. {
  81. //repair like tiles
  82. }
  83. else if (backtile.type == CMap::tile_wood && buildTile == CMap::tile_castle)
  84. {
  85. // can build stone on wood, do nothing
  86. }
  87. else if (buildTile == CMap::tile_wood_back && backtile.type == CMap::tile_castle_back)
  88. {
  89. //cant build wood on stone background
  90. return false;
  91. }
  92. else if (map.isTileSolid(backtile) || map.hasTileSolidBlobs(backtile))
  93. {
  94. if (!buildSolid && !map.hasTileSolidBlobsNoPlatform(backtile) && !map.isTileSolid(backtile))
  95. {
  96. //skip onwards, platforms don't block backwall
  97. }
  98. else
  99. {
  100. return false;
  101. }
  102. }
  103.  
  104. //printf("c");
  105. bool canPlaceOnBackground = ((blob is null) || (blob.getShape().getConsts().support > 0)); // if this is a blob it has to do support - so spikes cant be placed on back
  106.  
  107. if (
  108. (!canPlaceOnBackground || !map.isTileBackgroundNonEmpty(backtile)) && // can put against background
  109. !( // can put sticking next to something
  110. canPlaceNextTo(map, left) || (canPlaceOnBackground && map.isTileBackgroundNonEmpty(left)) ||
  111. canPlaceNextTo(map, right) || (canPlaceOnBackground && map.isTileBackgroundNonEmpty(right)) ||
  112. canPlaceNextTo(map, up) || (canPlaceOnBackground && map.isTileBackgroundNonEmpty(up)) ||
  113. canPlaceNextTo(map, down) || (canPlaceOnBackground && map.isTileBackgroundNonEmpty(down))
  114. )
  115. )
  116. {
  117. return false;
  118. }
  119. // no blocking actors?
  120. // printf("d");
  121. if (blob is null || !blob.hasTag("ignore blocking actors"))
  122. {
  123. bool isLadder = false;
  124. bool isSpikes = false;
  125. if (blob !is null)
  126. {
  127. const string bname = blob.getName();
  128. isLadder = bname == "ladder";
  129. isSpikes = bname == "spikes";
  130. }
  131.  
  132. Vec2f middle = p;
  133.  
  134. if (!isLadder && (buildSolid || isSpikes) && map.getSectorAtPosition(middle, "no build") !is null)
  135. {
  136. return false;
  137. }
  138.  
  139. //if (blob is null)
  140. //middle += Vec2f(map.tilesize*0.5f, map.tilesize*0.5f);
  141.  
  142. const string name = blob !is null ? blob.getName() : "";
  143. CBlob@[] blobsInRadius;
  144. if (map.getBlobsInRadius(middle, buildSolid ? map.tilesize : 0.0f, @blobsInRadius))
  145. {
  146. for (uint i = 0; i < blobsInRadius.length; i++)
  147. {
  148. CBlob @b = blobsInRadius[i];
  149. if (!b.isAttached() && b !is blob)
  150. {
  151. if (blob !is null || buildSolid)
  152. {
  153. if (b is this && isSpikes) continue;
  154.  
  155. Vec2f bpos = b.getPosition();
  156.  
  157. const string bname = b.getName();
  158.  
  159. bool cantBuild = isBlocking(b);
  160.  
  161. // cant place on any other blob
  162. if (cantBuild &&
  163. !b.hasTag("dead") &&
  164. !b.hasTag("material") &&
  165. !b.hasTag("projectile") &&
  166. bname != "bush")
  167. {
  168. Vec2f tl, br;
  169. b.getShape().getBoundingRect(tl, br);
  170.  
  171. Vec2f halftile = Vec2f(map.tilesize, map.tilesize) / 2.0f;
  172. Vec2f _tl = middle - halftile;
  173. Vec2f _br = middle + halftile;
  174.  
  175. if (br.x > _tl.x && br.y > _tl.y && _br.x > tl.x && _br.y > tl.y)
  176. {
  177. return false;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185.  
  186. return true;
  187. }
  188.  
  189. bool isBlocking(CBlob@ blob)
  190. {
  191. string name = blob.getName();
  192. if (name == "heart" || name == "log" || name == "food" || name == "fishy" || name == "steak" || name == "grain")
  193. return false;
  194.  
  195. return blob.isCollidable() || blob.getShape().isStatic();
  196. }
  197.  
  198. void SetTileAimpos(CBlob@ this, BlockCursor@ bc)
  199. {
  200. // calculate tile mouse pos
  201. Vec2f pos = this.getPosition();
  202. Vec2f aimpos = this.getAimPos();
  203. Vec2f mouseNorm = aimpos - pos;
  204. f32 mouseLen = mouseNorm.Length();
  205. const f32 maxLen = MAX_BUILD_LENGTH;
  206. mouseNorm /= mouseLen;
  207.  
  208. if (mouseLen > maxLen * getMap().tilesize)
  209. {
  210. f32 d = maxLen * getMap().tilesize;
  211. Vec2f p = pos + Vec2f(d * mouseNorm.x, d * mouseNorm.y);
  212. p = getMap().getTileSpacePosition(p);
  213. bc.tileAimPos = getMap().getTileWorldPosition(p);
  214. }
  215. else
  216. {
  217. bc.tileAimPos = getMap().getTileSpacePosition(aimpos);
  218. bc.tileAimPos = getMap().getTileWorldPosition(bc.tileAimPos);
  219. }
  220.  
  221. bc.cursorClose = (mouseLen < getMaxBuildDistance(this));
  222. }
  223.  
  224. f32 getMaxBuildDistance(CBlob@ this)
  225. {
  226. return (MAX_BUILD_LENGTH + 0.51f) * getMap().tilesize;
  227. }
  228.  
  229. void SetupBuildDelay(CBlob@ this)
  230. {
  231. this.set_u32("build time", getGameTime());
  232. this.set_u32("build delay", 7); // move this to builder init
  233. }
  234.  
  235. bool isBuildDelayed(CBlob@ this)
  236. {
  237. return (getGameTime() <= this.get_u32("build time"));
  238. }
  239.  
  240. void SetBuildDelay(CBlob@ this)
  241. {
  242. SetBuildDelay(this, this.get_u32("build delay"));
  243. }
  244.  
  245. void SetBuildDelay(CBlob@ this, uint time)
  246. {
  247. this.set_u32("build time", getGameTime() + time);
  248. }
  249.  
  250. bool isBuildRayBlocked(Vec2f pos, Vec2f target, Vec2f &out point)
  251. {
  252. CMap@ map = getMap();
  253.  
  254. Vec2f vector = target - pos;
  255. vector.Normalize();
  256. target -= vector * map.tilesize;
  257.  
  258. f32 halfsize = map.tilesize * 0.5f;
  259.  
  260. return map.rayCastSolid(pos + Vec2f(0, halfsize), target, point) &&
  261. map.rayCastSolid(pos + Vec2f(halfsize, 0), target, point) &&
  262. map.rayCastSolid(pos + Vec2f(0, -halfsize), target, point) &&
  263. map.rayCastSolid(pos + Vec2f(-halfsize, 0), target, point);
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement