fork download
  1. package world;
  2.  
  3. import exceptions.ChunkNotLoadedException;
  4. import gen.ChunkLoader;
  5. import gen.WorldBuilder;
  6.  
  7. import java.awt.Color;
  8. import java.awt.Graphics2D;
  9. import java.awt.Point;
  10. import java.awt.image.BufferedImage;
  11. import java.io.FileNotFoundException;
  12. import java.util.ArrayList;
  13. import java.util.concurrent.Phaser;
  14.  
  15. import main.GraphicsMain;
  16.  
  17. public class World implements ScreenComponent{
  18.  
  19. private volatile ArrayList<Chunk> chunks = new ArrayList<Chunk>();
  20. private volatile int chunksStability = 0; //+'ive = # threads accessing, -'ive = # threads editing
  21. private volatile Object chunkStabilityCountLock = new Object();
  22. private volatile Phaser chunkStabilityPhaser = new Phaser() {
  23. protected boolean onAdvance(int phase, int registeredParties) {
  24. synchronized(chunkStabilityCountLock)
  25. {
  26. if (registeredParties == 0)
  27. {
  28. chunksStability = 0;
  29. }
  30. else
  31. {
  32. chunksStability = Math.max(Math.min(chunksStability*-1, 1), -1);
  33. }
  34. }
  35. return false;
  36. }
  37. };
  38.  
  39. private WorldBuilder worldBuilder;
  40.  
  41. public World(String fileName) throws FileNotFoundException {
  42. worldBuilder = new WorldBuilder(fileName);
  43. }
  44.  
  45. public void load()
  46. {
  47. worldBuilder.loadWorld();
  48. }
  49.  
  50. public void update() {
  51. growVillages();
  52. lockEditChunks();
  53. for (Chunk c : chunks)
  54. {
  55. c.update();
  56. }
  57. unlockEditChunks();
  58.  
  59. ArrayList<Village> villages = getVillages();
  60. for (Village v : villages)
  61. {
  62. v.update();
  63. }
  64. }
  65.  
  66. public void growVillages()
  67. {
  68. ArrayList<Village> toGrow = getGrownVillages();
  69. for (Village v : toGrow)
  70. {
  71. boolean complete = true;
  72. int maxDX = (int)(v.getRelativeChunkCenter().getX());
  73. int maxDY = (int)(v.getRelativeChunkCenter().getY());
  74. int oldVillageChunkLength = v.getChunkSideLength(v.getSizeRank()-v.getGrowth());
  75. for (int dX = (int) (-1*v.getRelativeChunkCenter().getX()); dX <= maxDX; ++dX)
  76. {
  77. for (int dY = (int) (-1*v.getRelativeChunkCenter().getY()); dY <= maxDY; ++dY)
  78. {
  79. if (dX < oldVillageChunkLength*-1 || dX > oldVillageChunkLength || dY < oldVillageChunkLength*-1 || dY > oldVillageChunkLength)
  80. {
  81. Chunk cur;
  82. try {
  83. cur = getChunk(v.getX()+dX, v.getY()+dY);
  84. } catch (ChunkNotLoadedException e) {
  85. complete = false;
  86. continue;
  87. }
  88. //TODO decide what to do if there is already a village there
  89. cur.addVillage(v);
  90. }
  91. }
  92. }
  93. v.resetGrowth();
  94. }
  95. }
  96.  
  97. @Override
  98. public BufferedImage draw() { return draw(GraphicsMain.WIDTH/-2, GraphicsMain.HEIGHT/-2, GraphicsMain.WIDTH, GraphicsMain.HEIGHT); }
  99. public BufferedImage draw(int x, int y) { return draw(x, y, GraphicsMain.WIDTH, GraphicsMain.HEIGHT); }
  100. public BufferedImage draw(int x, int y, int width, int height)
  101. {
  102. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  103. Graphics2D gI = image.createGraphics();
  104.  
  105. drawChunks(x, y, width, height, gI);
  106. drawVillagers(x, y, gI);
  107. gI.dispose();
  108. return image;
  109. }
  110.  
  111. private void drawChunks(int x0, int y0, int width, int height, Graphics2D gI)
  112. {
  113. for (int x = (int) ((x0-.0)/Chunk.getPixelLength()-0.5); x <= Math.ceil(((double)x0+width)/Chunk.getPixelLength()); ++x)
  114. {
  115. for (int y = (int) ((y0-.0)/Chunk.getPixelLength()-0.5); y <= Math.ceil(((double)y0+height)/Chunk.getPixelLength()); ++y)
  116. {
  117. Chunk c;
  118. try {
  119. c = getChunk(x, y);
  120. } catch (ChunkNotLoadedException e) {
  121. continue;
  122. }
  123.  
  124. int cScreenX = (int)((x-0.5)*Chunk.getPixelLength()-x0), cScreenY = (int)((y-0.5)*Chunk.getPixelLength()-y0);
  125. if (!(cScreenX <= width && cScreenX+Chunk.getPixelLength() >= 0 && cScreenY <= width && cScreenY+Chunk.getPixelLength() >= 0)) //Check that shouldn't fail!
  126. {
  127. System.out.println("WHOOPS! Check src.world.World.drawChunks()!");
  128. }
  129. BufferedImage cI = c.draw();
  130. gI.drawImage(cI, cScreenX, cScreenY, Chunk.getPixelLength(), Chunk.getPixelLength(), null);
  131. }
  132. }
  133. }
  134.  
  135. private void drawVillagers(int screenX0, int screenY0, Graphics2D gI)
  136. {
  137. ArrayList<Village> villages = getVillages();
  138. for (Village v : villages)
  139. {
  140. for (Villager villager : v.getPopulation())
  141. {
  142. double vAbsX = villager.getRelativeX()+(v.getX()-screenX0+0.5)*Chunk.lengthOfChunk-0.5, vAbsY = villager.getRelativeY()+(v.getY()-screenY0+0.5)*Chunk.lengthOfChunk-0.5;
  143. //gI.drawImage(villager.draw(), (int)(vAbsX*Building.lengthOfBuilding), (int)(vAbsY*Building.lengthOfBuilding), null);
  144. }
  145. }
  146. }
  147.  
  148. private ArrayList<Village> getGrownVillages()
  149. {
  150. ArrayList<Village> villages = getVillages();
  151. ArrayList<Village> grownVillages = new ArrayList<Village>();
  152. for (int i = 0; i < villages.size(); ++i)
  153. {
  154. if (villages.get(i).getGrowth() != 0)
  155. {
  156. grownVillages.add(villages.get(i));
  157. }
  158. }
  159. return grownVillages;
  160. }
  161.  
  162. private ArrayList<Chunk> getGrownVillageChunks()
  163. {
  164. ArrayList<Chunk> grownVillageChunks = new ArrayList<Chunk>();
  165.  
  166. lockEditChunks();
  167. for (Chunk c : chunks)
  168. {
  169. if (c.hasVillage() && c.getVillage().getGrowth() != 0)
  170. {
  171. grownVillageChunks.add(c);
  172. }
  173. }
  174. unlockEditChunks();
  175. return grownVillageChunks;
  176. }
  177.  
  178. private ArrayList<Village> getVillages()
  179. {
  180. ArrayList<Village> villages = new ArrayList<Village>();
  181. // Iterator has issues when the ArrayList is modified.
  182. // if the draw thread tries to zoom out and load new chunks during the time this loop is iterating,
  183. // the thread will crash
  184. lockEditChunks();
  185. for (Chunk c : chunks)
  186. {
  187. if (c.hasVillage() && !villages.contains(c.getVillage()))
  188. {
  189. villages.add(c.getVillage());
  190. }
  191. }
  192. unlockEditChunks();
  193. return villages;
  194. }
  195.  
  196. private ArrayList<Chunk> getVillageChunks()
  197. {
  198. ArrayList<Chunk> villageChunks = new ArrayList<Chunk>();
  199.  
  200. lockEditChunks();
  201. for (Chunk c : chunks)
  202. {
  203. if (c.hasVillage())
  204. {
  205. villageChunks.add(c);
  206. }
  207. }
  208. unlockEditChunks();
  209. return villageChunks;
  210. }
  211.  
  212. public Chunk getChunk(int x, int y) throws ChunkNotLoadedException
  213. {
  214. lockEditChunks();
  215. for (Chunk c : chunks)
  216. {
  217. if (c.getX() == x && c.getY() == y)
  218. {
  219. unlockEditChunks();
  220. return c;
  221. }
  222. }
  223. unlockEditChunks();
  224.  
  225. worldBuilder.getChunkLoader().queueLoad(new Point(x, y));
  226.  
  227. throw new ChunkNotLoadedException();
  228. }
  229.  
  230. public void addChunk(Chunk C)
  231. {
  232. destabalizeChunks();
  233. chunks.add(C);
  234. stabalizeChunks();
  235. }
  236.  
  237.  
  238. /**
  239. * Prevents other threads from editing <b>World.chunks</b>.
  240. * Calling this will freeze the thread if another thread has called <b>World.destabalizeChunks()</b>
  241. * without calling <b>World.stabalizeChunks()</b>
  242. */
  243. /*
  244. * #UnArrivedParties = # threads currently requiring stability level
  245. * #ArrivedParties = # threads waiting for opposite stability level
  246. * #RegisteredParites == 0 = no one is accessing/modifying
  247. */
  248. public void lockEditChunks()
  249. {
  250. chunkStabilityPhaser.register();
  251. if (this.chunkStabilityPhaser.getUnarrivedParties() > 1 && this.chunksStability < 0) //number threads currently editing > 0
  252. {
  253. this.chunkStabilityPhaser.arriveAndAwaitAdvance(); //wait until threads editing finish
  254. }
  255. //SOLVED I THINK
  256. //#registered threads > 0 then happened at momentary blip before decrement of chunk stability by threads waiting to modify chunks
  257. //#retistered threads == 0 then no threads waiting
  258. //other thread performs above check, but for a chunk stability of > 0
  259. //continues past believing that no threads are waiting for stable access
  260. //then begins editing, and we have a problem
  261. //not only that but increment operator messes up
  262. synchronized(chunkStabilityCountLock)
  263. {
  264. ++this.chunksStability;
  265. }
  266. }
  267. public void unlockEditChunks()
  268. {
  269. chunkStabilityPhaser.arriveAndDeregister();
  270. }
  271.  
  272. /**
  273. * Prevents other threads requiring stability of <b>World.chunks</b> from continuing
  274. * Calling this will freeze the thread if another thread has called <b>World.lockEditChunks()</b>
  275. * without calling <b>World.unlockEditChunks()</b>
  276. */
  277. public void destabalizeChunks()
  278. {
  279. chunkStabilityPhaser.register();
  280. if (this.chunkStabilityPhaser.getUnarrivedParties() > 1 && this.chunksStability > 0) //number threads currently editing > 0
  281. {
  282. this.chunkStabilityPhaser.arriveAndAwaitAdvance(); //wait until threads editing finish
  283. }
  284.  
  285. synchronized(chunkStabilityCountLock)
  286. {
  287. --this.chunksStability;
  288. }
  289. }
  290.  
  291. public void stabalizeChunks()
  292. {
  293. chunkStabilityPhaser.arriveAndDeregister();
  294. }
  295. }
  296.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:17: error: class World is public, should be declared in a file named World.java
public class World implements ScreenComponent{
       ^
Main.java:3: error: package exceptions does not exist
import exceptions.ChunkNotLoadedException;
                 ^
Main.java:4: error: package gen does not exist
import gen.ChunkLoader;
          ^
Main.java:5: error: package gen does not exist
import gen.WorldBuilder;
          ^
Main.java:15: error: package main does not exist
import main.GraphicsMain;
           ^
Main.java:17: error: cannot find symbol
public class World implements ScreenComponent{
                              ^
  symbol: class ScreenComponent
Main.java:19: error: cannot find symbol
	private volatile ArrayList<Chunk> chunks = new ArrayList<Chunk>();
	                           ^
  symbol:   class Chunk
  location: class World
Main.java:39: error: cannot find symbol
	private WorldBuilder worldBuilder;
	        ^
  symbol:   class WorldBuilder
  location: class World
Main.java:148: error: cannot find symbol
	private ArrayList<Village> getGrownVillages()
	                  ^
  symbol:   class Village
  location: class World
Main.java:162: error: cannot find symbol
	private ArrayList<Chunk> getGrownVillageChunks()
	                  ^
  symbol:   class Chunk
  location: class World
Main.java:178: error: cannot find symbol
	private ArrayList<Village> getVillages()
	                  ^
  symbol:   class Village
  location: class World
Main.java:196: error: cannot find symbol
	private ArrayList<Chunk> getVillageChunks()
	                  ^
  symbol:   class Chunk
  location: class World
Main.java:212: error: cannot find symbol
	public Chunk getChunk(int x, int y) throws ChunkNotLoadedException
	       ^
  symbol:   class Chunk
  location: class World
Main.java:212: error: cannot find symbol
	public Chunk getChunk(int x, int y) throws ChunkNotLoadedException
	                                           ^
  symbol:   class ChunkNotLoadedException
  location: class World
Main.java:230: error: cannot find symbol
	public void addChunk(Chunk C)
	                     ^
  symbol:   class Chunk
  location: class World
Main.java:19: error: cannot find symbol
	private volatile ArrayList<Chunk> chunks = new ArrayList<Chunk>();
	                                                         ^
  symbol:   class Chunk
  location: class World
Main.java:42: error: cannot find symbol
		worldBuilder = new WorldBuilder(fileName);
		                   ^
  symbol:   class WorldBuilder
  location: class World
Main.java:53: error: cannot find symbol
		for (Chunk c : chunks)
		     ^
  symbol:   class Chunk
  location: class World
Main.java:59: error: cannot find symbol
		ArrayList<Village> villages = getVillages();
		          ^
  symbol:   class Village
  location: class World
Main.java:60: error: cannot find symbol
		for (Village v : villages)
		     ^
  symbol:   class Village
  location: class World
Main.java:68: error: cannot find symbol
		ArrayList<Village> toGrow = getGrownVillages();
		          ^
  symbol:   class Village
  location: class World
Main.java:69: error: cannot find symbol
		for (Village v : toGrow)
		     ^
  symbol:   class Village
  location: class World
Main.java:81: error: cannot find symbol
						Chunk cur;
						^
  symbol:   class Chunk
  location: class World
Main.java:84: error: cannot find symbol
						} catch (ChunkNotLoadedException e) {
						         ^
  symbol:   class ChunkNotLoadedException
  location: class World
Main.java:97: error: method does not override or implement a method from a supertype
	@Override
	^
Main.java:98: error: cannot find symbol
	public BufferedImage draw() { return draw(GraphicsMain.WIDTH/-2, GraphicsMain.HEIGHT/-2, GraphicsMain.WIDTH, GraphicsMain.HEIGHT); }
	                                          ^
  symbol:   variable GraphicsMain
  location: class World
Main.java:98: error: cannot find symbol
	public BufferedImage draw() { return draw(GraphicsMain.WIDTH/-2, GraphicsMain.HEIGHT/-2, GraphicsMain.WIDTH, GraphicsMain.HEIGHT); }
	                                                                 ^
  symbol:   variable GraphicsMain
  location: class World
Main.java:98: error: cannot find symbol
	public BufferedImage draw() { return draw(GraphicsMain.WIDTH/-2, GraphicsMain.HEIGHT/-2, GraphicsMain.WIDTH, GraphicsMain.HEIGHT); }
	                                                                                         ^
  symbol:   variable GraphicsMain
  location: class World
Main.java:98: error: cannot find symbol
	public BufferedImage draw() { return draw(GraphicsMain.WIDTH/-2, GraphicsMain.HEIGHT/-2, GraphicsMain.WIDTH, GraphicsMain.HEIGHT); }
	                                                                                                             ^
  symbol:   variable GraphicsMain
  location: class World
Main.java:99: error: cannot find symbol
	public BufferedImage draw(int x, int y) { return draw(x, y, GraphicsMain.WIDTH, GraphicsMain.HEIGHT); }
	                                                            ^
  symbol:   variable GraphicsMain
  location: class World
Main.java:99: error: cannot find symbol
	public BufferedImage draw(int x, int y) { return draw(x, y, GraphicsMain.WIDTH, GraphicsMain.HEIGHT); }
	                                                                                ^
  symbol:   variable GraphicsMain
  location: class World
Main.java:113: error: cannot find symbol
		for (int x = (int) ((x0-.0)/Chunk.getPixelLength()-0.5); x <= Math.ceil(((double)x0+width)/Chunk.getPixelLength()); ++x)
		                            ^
  symbol:   variable Chunk
  location: class World
Main.java:113: error: cannot find symbol
		for (int x = (int) ((x0-.0)/Chunk.getPixelLength()-0.5); x <= Math.ceil(((double)x0+width)/Chunk.getPixelLength()); ++x)
		                                                                                           ^
  symbol:   variable Chunk
  location: class World
Main.java:115: error: cannot find symbol
			for (int y = (int) ((y0-.0)/Chunk.getPixelLength()-0.5); y <= Math.ceil(((double)y0+height)/Chunk.getPixelLength()); ++y)
			                            ^
  symbol:   variable Chunk
  location: class World
Main.java:115: error: cannot find symbol
			for (int y = (int) ((y0-.0)/Chunk.getPixelLength()-0.5); y <= Math.ceil(((double)y0+height)/Chunk.getPixelLength()); ++y)
			                                                                                            ^
  symbol:   variable Chunk
  location: class World
Main.java:117: error: cannot find symbol
				Chunk c;
				^
  symbol:   class Chunk
  location: class World
Main.java:120: error: cannot find symbol
				} catch (ChunkNotLoadedException e) {
				         ^
  symbol:   class ChunkNotLoadedException
  location: class World
Main.java:124: error: cannot find symbol
				int cScreenX = (int)((x-0.5)*Chunk.getPixelLength()-x0), cScreenY = (int)((y-0.5)*Chunk.getPixelLength()-y0);
				                             ^
  symbol:   variable Chunk
  location: class World
Main.java:124: error: cannot find symbol
				int cScreenX = (int)((x-0.5)*Chunk.getPixelLength()-x0), cScreenY = (int)((y-0.5)*Chunk.getPixelLength()-y0);
				                                                                                  ^
  symbol:   variable Chunk
  location: class World
Main.java:125: error: cannot find symbol
				if (!(cScreenX <= width && cScreenX+Chunk.getPixelLength() >= 0 && cScreenY <= width && cScreenY+Chunk.getPixelLength() >= 0)) //Check that shouldn't fail!
				                                    ^
  symbol:   variable Chunk
  location: class World
Main.java:125: error: cannot find symbol
				if (!(cScreenX <= width && cScreenX+Chunk.getPixelLength() >= 0 && cScreenY <= width && cScreenY+Chunk.getPixelLength() >= 0)) //Check that shouldn't fail!
				                                                                                                 ^
  symbol:   variable Chunk
  location: class World
Main.java:130: error: cannot find symbol
				gI.drawImage(cI, cScreenX, cScreenY, Chunk.getPixelLength(), Chunk.getPixelLength(), null);
				                                     ^
  symbol:   variable Chunk
  location: class World
Main.java:130: error: cannot find symbol
				gI.drawImage(cI, cScreenX, cScreenY, Chunk.getPixelLength(), Chunk.getPixelLength(), null);
				                                                             ^
  symbol:   variable Chunk
  location: class World
Main.java:137: error: cannot find symbol
		ArrayList<Village> villages = getVillages();
		          ^
  symbol:   class Village
  location: class World
Main.java:138: error: cannot find symbol
		for (Village v : villages)
		     ^
  symbol:   class Village
  location: class World
Main.java:140: error: cannot find symbol
			for (Villager villager : v.getPopulation())
			     ^
  symbol:   class Villager
  location: class World
Main.java:142: error: cannot find symbol
				double vAbsX = villager.getRelativeX()+(v.getX()-screenX0+0.5)*Chunk.lengthOfChunk-0.5, vAbsY = villager.getRelativeY()+(v.getY()-screenY0+0.5)*Chunk.lengthOfChunk-0.5;
				                                                               ^
  symbol:   variable Chunk
  location: class World
Main.java:142: error: cannot find symbol
				double vAbsX = villager.getRelativeX()+(v.getX()-screenX0+0.5)*Chunk.lengthOfChunk-0.5, vAbsY = villager.getRelativeY()+(v.getY()-screenY0+0.5)*Chunk.lengthOfChunk-0.5;
				                                                                                                                                                ^
  symbol:   variable Chunk
  location: class World
Main.java:150: error: cannot find symbol
		ArrayList<Village> villages = getVillages();
		          ^
  symbol:   class Village
  location: class World
Main.java:151: error: cannot find symbol
		ArrayList<Village> grownVillages = new ArrayList<Village>();
		          ^
  symbol:   class Village
  location: class World
Main.java:151: error: cannot find symbol
		ArrayList<Village> grownVillages = new ArrayList<Village>();
		                                                 ^
  symbol:   class Village
  location: class World
Main.java:164: error: cannot find symbol
		ArrayList<Chunk> grownVillageChunks = new ArrayList<Chunk>();
		          ^
  symbol:   class Chunk
  location: class World
Main.java:164: error: cannot find symbol
		ArrayList<Chunk> grownVillageChunks = new ArrayList<Chunk>();
		                                                    ^
  symbol:   class Chunk
  location: class World
Main.java:167: error: cannot find symbol
		for (Chunk c : chunks)
		     ^
  symbol:   class Chunk
  location: class World
Main.java:180: error: cannot find symbol
		ArrayList<Village> villages = new ArrayList<Village>();
		          ^
  symbol:   class Village
  location: class World
Main.java:180: error: cannot find symbol
		ArrayList<Village> villages = new ArrayList<Village>();
		                                            ^
  symbol:   class Village
  location: class World
Main.java:185: error: cannot find symbol
		for (Chunk c : chunks)
		     ^
  symbol:   class Chunk
  location: class World
Main.java:198: error: cannot find symbol
		ArrayList<Chunk> villageChunks = new ArrayList<Chunk>();
		          ^
  symbol:   class Chunk
  location: class World
Main.java:198: error: cannot find symbol
		ArrayList<Chunk> villageChunks = new ArrayList<Chunk>();
		                                               ^
  symbol:   class Chunk
  location: class World
Main.java:201: error: cannot find symbol
		for (Chunk c : chunks)
		     ^
  symbol:   class Chunk
  location: class World
Main.java:215: error: cannot find symbol
		for (Chunk c : chunks)
		     ^
  symbol:   class Chunk
  location: class World
Main.java:227: error: cannot find symbol
		throw new ChunkNotLoadedException();
		          ^
  symbol:   class ChunkNotLoadedException
  location: class World
62 errors
stdout
Standard output is empty