Jump to content

  • Log In with Google      Sign In   
  • Create Account

Shippou

Member Since 24 Nov 2011
Offline Last Active Today, 12:27 AM

Topics I've Started

What Causes Port Mismatches ?

14 October 2014 - 06:07 PM

I am playing around with an object server , and have run onto an issue with the port

 My client sends information on port 9999.

My server listens to port 9999.

 

 However, whenever I receive an incoming connection,

System.out.println("" + socket.getRemoteSocketAddress() );

gives me an incrementing port number !

 

/127.0.0.1:56304
You said: Moo
/127.0.0.1:56305
You said: Baa
/127.0.0.1:56306
You said: Quack

Do you know what is causing this, and how to fix it ?

 

***

Client

***
 

public class Client {
	
	int port;
	String url;
	Socket socket;
	ObjectInputStream input;
	ObjectOutputStream output;
	
	public Client(String address,int po){url = address; port = po;}
	

	public void out(Object obj){
		try{
			socket = new Socket (url,port);
			output = new ObjectOutputStream (socket.getOutputStream());
			output.writeObject(obj);
			socket.close();
		}
		catch(Exception e){
			System.out.println("Client Crash\n" + e);
		}
	}
}

***

Server

***
 

public class Server implements Runnable {
	int port;
	ServerSocket ssocket;
	Socket socket ;
	ObjectInputStream input;
	Object dump;
	TestObj to;
	
	public Server(int por){port = por;}
	
	@Override
	public void run() {
		try{
			ssocket = new ServerSocket(port);
			
			while (true){
			socket = ssocket.accept();
			System.out.println("" + socket.getRemoteSocketAddress() );
			input = new ObjectInputStream(socket.getInputStream());
			dump = input.readObject() ;
			socket.close();
			to = (TestObj) dump;
			to.say();
				}

			 } 
		catch (Exception e){
			System.out.println("Server Crash\n" + e);
		}
		
	}
}

Can I Make This Code Faster Without Over Complicating It ?

14 October 2014 - 02:51 PM

 I have been working on test code for a client/server to implement into an existing project.

 My question is: could I improve the code to make it run faster, WITHOUT overcomplicated things ?

 Once I implement this into my existing code, it will probably have to handle between 10 to 20 updates per second ( several different object types possible. )

 

***

MAIN

***

public class Main {

	public static void main (String[] args){
		TestObj to = new TestObj();
		Client client = new Client();
		Server server = new Server();
		Thread t = new Thread(server,"");
		t.start();
		
		to.set("Moo");
		client.out(to);
		to.set("Baa");
		client.out(to);
		to.set("Quack");
		client.out(to);
	}
}

***

TestObj ( only used for this code test )

***

public class TestObj implements Serializable {
	String s;
	
	public void set(String str){
		s = str;
	}

	public void say(){
		System.out.println("You said: " + s);
	}
}

***

Client

***

public class Client {

	Socket socket;
	ObjectInputStream input;
	ObjectOutputStream output;

	
	public void out(Object obj){
		try{
			socket = new Socket ("127.0.0.1",1660);
			output = new ObjectOutputStream (socket.getOutputStream());
			output.writeObject(obj);
		}
		catch(Exception e){
			System.out.println("Client Crash\n" + e);
		}
	}
}

***

Server

***

public class Server implements Runnable {
	ServerSocket ssocket;
	Socket socket;
	ObjectInputStream input;
	Object dump;
	TestObj to;
	
	@Override
	public void run() {
		try{
			ssocket = new ServerSocket(1660);
			
			while (true){
			socket = ssocket.accept();
			input = new ObjectInputStream(socket.getInputStream());
			dump = input.readObject() ;
			socket.close();
			// if (dump instanceof TestObject){}
			to = (TestObj) dump;
			to.say();
			} 
			
		} 
		catch (Exception e){
			System.out.println("Server Crash\n" + e);
		}
		
	}
}

What a web I Weave !

13 October 2014 - 10:22 PM

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

	}


Offsetting 3D Points Based On Rotation + Position Of Camera

11 August 2014 - 10:58 AM

 Please keep in mind that I graduated High School in 1998, and I am very rusty on advanced math !

 

 I have set up a basic Object that contains all my point data ( X Y Z ) for my 3D cube. Whenever it renders in the world, it does not rotate with the "camera" . ( It does move on X Y )

 

What are the SIMPLIFIED math formulas that are required to offset the points based on the camera's movement and rotation ?

 What are the SIMPLIFIED math formulas required for scaling as the camera approaches and retreats  ?


How To Make Combat Formulas Work Better ?

14 July 2014 - 11:59 AM

 I have been running combat simulations on and off for the last couple days, and I have been having issues making every thing "work" together to create a smooth running system.

 Here is what I have, maybe someone can figure out a better system than what I have.

( this is real time combat )

 

Combat Level - derived from several of the base skills ( haven't figured out a good way to calculate this yet )

HP - health ... regenerates based on combat level

MP - magic ... regenerates based on combat level

RP - religion ... regenerates based on combat level

Strength - how hard something can hit

Accuracy - percent added to damage

Agility - percentage chance of dodging some or all of an attack

Defense - amount of damage that can be absorbed

Armor - percentage of damage that can be absorbed

Luck - positive or negative random effect

 

* A difference of 3 or more combat levels between the combatants will change percentage of damage dealt and damage taken

* The level of MP & RP is the attack and defence level.

 

 

 Hit Given =  (Strength * (Accuracy bonus + luck) ) * combat level difference penalty or bonus percentage

 Hit Taken = (Hit Given - ( (Defense * armor bonus) * (agility bonus + luck) ) ) * combat level difference penalty or bonus percentage


PARTNERS