Jump to content
  • Advertisement
Sign in to follow this  
RLS0812

What a web I Weave !

This topic is 1425 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 At the start f this year I was working on a 2D game engine I was hoping to eventually turn into a network game (( any one know an easy to understand Java threaded socket tutorial ? )) ... the last thing I was working on before I moved on to something else was implementing player placed items on the map.

 That was 8 months ago .

 I decided to pick up the project again yesterday, and I have to hand it to myself, I can write some really good spaghetti code !

I had forgotten how tangled everything had become as I added features and fixed bugs.

 Here is the class that handles the map generation, and some how the player collision, and a few other things I forgotten about ( there are 19 classes, almost all of them are tangled ) . Note how I have a tendency to abuse non static classes as static ...

// note: add methods to add and remove from the maps
public class MapGen {
	Font font ;
	Screen scr;
	Graphics gr;
	InteractiveObject io;
	InteractiveObjectsINI eo;
	GameLoop gl;
	String output;
	InteractiveObject[][] iomap; // <-- generated items
	Items[][] iimap; // <-- player placed
	ProbSelect ps; // add in methods to handle ii items 
	int mapX;
	int mapY;
	int cordX;
    int cordY;
    Random rand;
    int offsetX;
    int offsetY;
    int tile_size;
    List <String> objects;
    List <Integer> xlist;
    List <Integer> ylist;
    List <Color> clist;
    
	public MapGen(int x, int y,InteractiveObjectsINI e, GameLoop g){
		 eo = e;
		 gl = g;
		 mapX = x;
		 mapY = y;
		 output = "";
		 cordX = 0;
		 cordY = 0;
		 rand = new Random();
		 io = new InteractiveObject();
		 iomap = new InteractiveObject[mapX][mapY];
		 iimap = new Items[mapX][mapY];
		 ps = new ProbSelect();
		 tile_size = 32;
		 offsetX = 10;  //height
		 offsetY = 10; // width
		 // This is going into a different class ... does *not* belong in the IntObj class
		 // based on x out of 10000
		 //TODO create different "group" class
		 InteractiveObject.tree_rarity = 100;
		 InteractiveObject.rock_rarity = 150;
		 InteractiveObject.mining_node_rarity = 25;
		 InteractiveObject.plant_rarity = 200;
		 font =  new Font("Times New Roman",Font.BOLD,tile_size);
		 scr = new Screen(this);
		

		 

	}
	
	public void generateMap(){
		// Trees
		addEnvObjectsToMap(io.getTreeRarity(),eo.getTrees() );
		// Rocks
		addEnvObjectsToMap(io.getRockRarity(),eo.getRocks() );
		// Mining Nodes
		addEnvObjectsToMap(io.getMiningNodeRarity(),eo.getMiningNodes() );
		// Plants
		addEnvObjectsToMap(io.getPlantRarity(),eo.getPlants() );
		
		for (cordX = 0 ; cordX < mapX; cordX ++){ // <-- iiMap builder ... needs expanded later
			for (cordY = 0; cordY < mapY ; cordY ++){
				if (iomap[cordX][cordY] == null){iimap[cordX][cordY] = null;} 
				}} // < for loops
		}
		
	public void outputMap(Player p){
		//generateMap();
		String [] temp = new String[2];
		output = "|";
		int poX1 = p.getPlayerX() - offsetX;
		int poY1 = p.getPlayerY() - offsetY;
		int poX2 = p.getPlayerX() + offsetX;
		int poY2 = p.getPlayerY() + offsetY;
		objects = new ArrayList<String>();
		xlist = new ArrayList<Integer>();
		ylist = new ArrayList<Integer>();
		clist = new ArrayList<Color>();
		if (poX1 < 0){poX1 = 0;}
		if (poY1 < 0){poY1 = 0;}
		if (poX2 > mapX){poX2 = mapX;}
		if (poY2 > mapY){poY2 = mapY;}
		
		for (cordX = poX1 ; cordX < poX2; cordX ++){
			for (cordY = poY1; cordY < poY2 ; cordY ++){
				if (p.getPlayerX() == cordX && p.getPlayerY() == cordY ){
					objects.add(p.getPlayerSprite() ) ;
					xlist.add( ((cordX - p.getPlayerX() ) + offsetX ) * tile_size  );
					ylist.add( ((cordY - p.getPlayerY() ) + offsetY ) * tile_size );
					clist.add(Color.BLACK );
					
				}
			
				else{
					if (iomap[cordX][cordY] != null){
						temp = iomap[cordX][cordY].getStates();
						objects.add(temp[iomap[cordX][cordY].getCurrentState()]) ;
						xlist.add( ((cordX - p.getPlayerX() ) + offsetX ) * tile_size  );
						ylist.add( ((cordY - p.getPlayerY() ) + offsetY ) * tile_size );
						clist.add(iomap[cordX][cordY].getColor() );
						
					}
				}
			}
		}
		scr.run();
 }
	
	public boolean chance(int x){
		int num = rand.nextInt(10000);
		if (num < x){ return true;}
		else {return false;}
	}
	
	public void addEnvObjectsToMap(int rar,List<InteractiveObject> items){
		
		for (cordX = 0 ; cordX < mapX; cordX ++){
			for (cordY = 0; cordY < mapY ; cordY ++){
				if (iomap[cordX][cordY] == null){
					if (chance(rar) ){
					iomap[cordX][cordY] = ps.selectItem(items).getCopy(); // <--- I hope to the gods this works !
					}} // < if loops
				else{} // < first if
				}} // < for loops
		}
	public void say(String x){
		System.out.println(x);
	}
	
	public int getMapX(){return mapX;}
	public int getMapY(){return mapY;}
	///
	/// Get Object On Map - lazy way
	///
	public InteractiveObject checkMapLoc(Player p){
		if (strBoo(p.getPlayerDirection(),"n") ){
			try{ return iomap[p.getPlayerX() -1 ][p.getPlayerY()];}
			catch (Exception e){return null;}
		}
		else if (strBoo(p.getPlayerDirection(),"s") ){
			try{return iomap[p.getPlayerX() + 1 ][p.getPlayerY()];}
			catch (Exception e){return null;}
		}
		else if (strBoo(p.getPlayerDirection(),"w") ){
			try{return iomap[p.getPlayerX()][p.getPlayerY() - 1 ];}
			catch (Exception e){return null;}
		}
		else if (strBoo(p.getPlayerDirection(),"e") ){
			try{return iomap[p.getPlayerX()][p.getPlayerY() + 1 ];}
			catch (Exception e){return null;}
		}
		else{return null;}
	}
	public Items checkPlacedLoc(Player p){
		if (strBoo(p.getPlayerDirection(),"n") ){
			try{ return iimap[p.getPlayerX() -1 ][p.getPlayerY()];}
			catch (Exception e){return null;}
		}
		else if (strBoo(p.getPlayerDirection(),"s") ){
			try{return iimap[p.getPlayerX() + 1 ][p.getPlayerY()];}
			catch (Exception e){return null;}
		}
		else if (strBoo(p.getPlayerDirection(),"w") ){
			try{return iimap[p.getPlayerX()][p.getPlayerY() - 1 ];}
			catch (Exception e){return null;}
		}
		else if (strBoo(p.getPlayerDirection(),"e") ){
			try{return iimap[p.getPlayerX()][p.getPlayerY() + 1 ];}
			catch (Exception e){return null;}
		}
		else{return null;}
	}
	
	public boolean strBoo(String s, String s1){
		if (s.equalsIgnoreCase(s1) ){
			return true;
		}
		else{
			return false;
		}
	}
	
	public void map_out (Graphics g){ // were all the magic happens
		g.setFont(font);
		int tmp = objects.size();
		
		for (int i = 0; i < tmp; i++){
			g.setColor(clist.get(i) );
			g.drawString(objects.get(i),ylist.get(i),xlist.get(i));
		}
	}
	
	public void addToMapObjects(InteractiveObject o){
		
	}
	
	public void addToMapItems(Items i){
		
	}

	}

Share this post


Link to post
Share on other sites
Advertisement


Tabs = bad. There is no justification for them (IMMHO).

 

Tabs to indent, spaces to align. Tabstops of 8 are hideous though, 4 is plenty in curly-brace languages, and you can get away with just 2 if you're not a braces-on-their-own-line programmer.

 

3 is heresy, of course, because all programmers know that powers-of-two are faster tongue.png

Share this post


Link to post
Share on other sites

Once you reduced the size of a file from 3.5MB to 2.5MB by replacing space indent with tab or simply appreciating the fact that you can set the tab width in your editor according to your own preference, you will start wondering why anyone in their right mind would even consider spamming spaces to indent stuff.

 

"But then it won't look the same in all editors". Yes. Exactly. I can tailor the readability to my personal preference without impacting anybody else.

 

"But then stuff on different lines might not be properly aligned anymore". Well. True. Except we're writing code, not ASCII art. So stop creating those neatly aligned variable declaration blocks (unless you really ARE still using an ancient C compiler that enforces bad programming practices).

 

Now, mixing tabs and spaces is just awful, horrible and completely ruins your formatting pretty much everywhere except in the editor of origin...

Share this post


Link to post
Share on other sites

Once you reduced the size of a file from 3.5MB to 2.5MB by replacing space indent with tab or simply appreciating the fact that you can set the tab width in your editor according to your own preference, you will start wondering why anyone in their right mind would even consider spamming spaces to indent stuff.

 

 

I'm wondering why anyone in their right mind would keep a 3.5MB source file around in the first place.

Share this post


Link to post
Share on other sites

Or 2.5MB for that matter, I just checked and the entire source code (not just a file) for my entire game is less than half that. Even the one monster file there (which should have been split long ago, but eh) doesn't get even close to 200KB, and it's in the several thousands of lines range.

 


"But then it won't look the same in all editors". Yes. Exactly. I can tailor the readability to my personal preference without impacting anybody else.

But then don't forget about the location of curly braces, where to put blank lines, etc. If you want to deal with that then your best option is to use something that rearranges the entire code in the first place (I know Code::Blocks has this, I assume Visual Studio does as well), just changing tab size is not going to cut it.

Share this post


Link to post
Share on other sites

Or you could just enforce running the code through an automatic formatter before each commit, which uses either spaces or tabs (or absolutely nothing, if your source files are so big that just parsing the whitespace has a measurable impact on performance (!)) at your convenience. That way the source code formatting is always consistent and people can use their own preferences when working locally. Right? Right?

 

Just kidding, spaces >>> tabs.

Edited by Bacterius

Share this post


Link to post
Share on other sites

Spaces for indentation are utter bullshit. Tabs are smaller, more flexible (can be adjusted as preferred) and have no disadvantages, except in crappy forum software that doesn't properly support tabs. Using tabs for alignment beyond the first non-whitespace character will break things though. Though aligning variables and other OCD bullshit is a waste of time anyway.

 

Tabs for indentation.

Single spaces for separation.

Anything else is just plain dumb.

Edited by l0calh05t

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!