# 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.

## 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 !

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
// Rocks
// Mining Nodes
// Plants

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 ){
xlist.add( ((cordX - p.getPlayerX() ) + offsetX ) * tile_size  );
ylist.add( ((cordY - p.getPlayerY() ) + offsetY ) * tile_size );

}

else{
if (iomap[cordX][cordY] != null){
temp = iomap[cordX][cordY].getStates();
xlist.add( ((cordX - p.getPlayerX() ) + offsetX ) * tile_size  );
ylist.add( ((cordY - p.getPlayerY() ) + offsetY ) * tile_size );

}
}
}
}
scr.run();
}

public boolean chance(int x){
int num = rand.nextInt(10000);
if (num < x){ return true;}
else {return false;}
}

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));
}
}

}

}

}



##### Share on other sites

https://github.com/EsotericSoftware/kryonet

Kryonet is what all the kids are using these days.  I have no experience with it myself, but it may be a good place to start.

##### Share on other sites

Is the inconsistent indent a forum artifact?

##### Share on other sites
No, OP's

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

##### Share on other sites

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

##### 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 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 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 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 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

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007203

×