Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Game Play & Game Design / Re: Never-ending Shuffled Sequences - When Random is too Random on: 2008-04-14 17:37:57
After reading your blog I came up with this idea in which you'd need double or float probabilities for perhaps extremely low chances of some items drops (like in diablo 2), my idea was that you add items with a percentage probability (0.0-1.0), but if you end up going past 1.0d it would still work.

It isn't true random but if we go by the premise, true random is not the goal anyway. Only thing is that I am not good with benchmarks, I bet this would be better for huge number of items with extremely low probabilities (as keeping an array spot for each chance would eat memory), but I have no idea which would be better for less extreme cases.

Coded it in notepad bear with that.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
ShuffleBag
{
   private java.util.ArrayList< Item > items;
   
   long drawPeriod;
   long drawsLeft;
   
   public class Item
   {
      public Item( String name, double percentage )
      {
         item = name;
         weigth = percentage;
         drawsLeft = 0;
         timesToDraw = 0;
         doneDrawn = true;
      }
      public void setTimesToDraw( long timesToDraw )
      {
         drawsLeft = timesToDraw + this.timesToDraw - drawsLeft; // timesToDraw - ( this.timesToDraw + drawsLeft );
         this.timesToDraw = timesToDraw;
         
         doneDrawn = ( drawsLeft <= 0 );
      }
      public drawn()
      {
         --drawsLeft;
         if( drawsLeft <= 0 ) doneDrawn = true;
      }
      public resetDraws()
      {
         drawsLeft = timesToDraw;
         doneDrawn = ( drawsLeft <= 0 );
      }
     
      String item;
      double weight;
      long drawsLeft;
      long timesToDraw;
      boolean doneDrawn;
   }
   
   addItem( String name, double percentage, boolean pack )
   {
      Item item = new Item( name, percentage );
     
      items.add( item );
      if( pack ) pack();
   }
   pack()
   {
      double totalWeigth = 0;
      double minWeight = Double.MAX_VALUE;
      for( Item i : items )
      {
         totalWeigth += i.getWeight();
         if( minWeight > i.getWeight() ) minWeight = i.getWeight();
      }
     
      double leftoverWeight = 0.0d;
      if( totalWeight < 1.0d )
      {
         leftoverWeight = 1.0d - totalWeight;
         totalWeight = 1.0d;
      };
     
      drawPeriod = 0;
      for( Item i : items )
      {
         long timesToDraw = Math.ceil( i.getWeight() / minWeight );
         i.setTimesToDraw( timesToDraw );
         drawPeriod += timesToDraw;
      }
     
      drawPeriod += Math.ceil( leftoverWeight / minWeight );
      drawsLeft = drawPeriod;
   }
   
   String drawNext()
   {
      boolean reset = false;
      if( drawsLeft <= 0 )
      {
         drawsLeft = drawPeriod;
         reset = true;
      };
     
      long rand = RNG.getLong( 0, drawsLeft );
     
      boolean found = false;
      String s = "";
      long k = 0;
      for( Item i : items )
      {
         if( reset ) i.resetDraws();
         if( found ) continue;
         if( i.isDoneDrawn() ) continue;
         
         k += i.getDrawsLeft();
         if( rand < k )
         {
            s = i.getItem();
            i.drawn();
            --drawsLeft;
            found = true;
            if( !reset ) break;
         }
      }
     
      return s;
   }
}



Hmm thinking about it it would still have the problem where you get several times a low or high random number, perhaps the item list could be shuffled from time to time so the check order changed.
2  Java Game APIs & Engines / Java 2D / Alternative to drawImage to draw a tile as a texture? on: 2008-04-13 02:20:23
Hello, I am new to Java 2d and have been building a program to learn it. I have been drawing to a JPanel overriding the paint method. First a solid color, then an image, then a section of the image (I hear its called a tile), now I was trying to draw that section as a "texture" on a arbitrary section of the panel, but there is no drawImage function for that, becuase if the rectangle area is bigger than the tile then the tile gets scaled to fit instead of replicated.

I googled and found "TexturePaint" but also a lot of people saying its way too slow. Is there a better function for this or would I have to draw it manually calling drawImage several times? (yawn)

Hmm, what If I wanted to draw a tile on a rectangle section that would allow lets say 10 tiles horizontally, but I only want to draw it twice, is manual drawing the only way for that case? ( i was thinking on a if before the drawing that would reduce the rectangle area in this case, so that the texture filling could be used for this too)

Something like drawImage( image, dest x1, dest y1, dest  x2, dest y2,  src image x1, src image y1, src image x2, src image y2, repeat-x, repeat-y  ), where repeat-x/y can be set to a finite or infinite value (although if it is just infinite it would work for me).


EDIT: Yay I managed to code it "manually", this works as I want, although I believe the app was just a bit slower than usual, so if there's a better way to do this please tell me:



Vector3D and Vector2D hold x,y and z, 3D uses doubles (for 3d apps) and 2D uses long (for 2d apps).
"renderPos" already has the "point" where I want to render.and "size" the rectagular area's size I want to render to, both define the rectangular area that has to be textured.

"material" = sprite = wrapper for the texture, the texture is a tile image.
getRectStart and getRectEnd have the x1,y1 x2,y2 coordinates in the texture of the tile I want to draw.

I implemented features that remind me of CSS: repeat-x/y and x/y-offset, repeat-x is more like "draw this many times in x", so a value of zero is taken as do not draw, and "infinite" times is Integer.MAX_VALUE.

offset is the texture offset relative to the top left corner of the rectangle area, it doesn't have "standard" behavior but I'm liking the current behavior.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
   public void RenderQuad( java.awt.Graphics2D g, app.math.Vector3D renderPos, app.math.Vector3D size )
   {
      int timesRepeatedOnX = 0;
      int timesRepeatedOnY = 0;
     
      if( material.getRepeatOnX() == 0 || material.getRepeatOnY() == 0 ) return;
     
      app.math.Vector2D x1 = new app.math.Vector2D();
      x1.x = (int) Math.round( renderPos.x + material.getXOffset() );
      x1.y = (int) Math.round( renderPos.y + material.getYOffset() );
      x1.z = 0;
     
      app.math.Vector2D x2 = new app.math.Vector2D();
      x2.x = x1.x + material.getTileWidth();
      x2.y = x1.y + material.getTileHeight();
      x2.z = 0;
     
      app.math.Vector2D finalx2 = new app.math.Vector2D();
      finalx2.x = (int)Math.round(renderPos.x + size.x);
      finalx2.y = (int)Math.round(renderPos.y + size.y);
      finalx2.z = 0;
     
      java.awt.Rectangle r = g.getClipBounds();
      g.setClip( (int) Math.round(renderPos.x), (int) Math.round(renderPos.y), (int)Math.round(size.x), (int)Math.round(size.y) );
     
      Texture t = getTextureToRender();
      while( true )
      {
         g.drawImage( t.get(), (int)x1.x, (int)x1.y, (int)x2.x, (int)x2.y, (int)getRectStart().x, (int)getRectStart().y, (int)getRectEnd().x, (int)getRectEnd().y, null );
         
         x1.x = x2.x;
         //x1.y = x2.y;
         //x1.z = 0;
         
         x2.x += material.getTileWidth();
         
         ++timesRepeatedOnX;
         if( x1.x >= finalx2.x || timesRepeatedOnX >= material.getRepeatOnX() )
         {
            x1.x = (int) Math.round( renderPos.x + material.getXOffset() );
            x1.y = x2.y;
            x2.x = x1.x + material.getTileWidth();
            x2.y += material.getTileHeight();
            //x2.z = 0;
           
            timesRepeatedOnX = 0;
            ++timesRepeatedOnY;
         }
         if( x1.y >= finalx2.y || timesRepeatedOnY >= material.getRepeatOnY() )
         {
            break;
         }
      }
     
      g.setClip(r);
   }
3  Games Center / Archived Projects / Re: Palomo ! on: 2006-04-06 20:33:29
Cool game, the only annoying thing was the planes appearing from the same side twice. Of course, that adds to the difficulty.
Jamison, press R to start again, google translation, babelfish, etc
4  Game Development / Networking & Multiplayer / Check my first NIO code on: 2005-01-22 21:36:58
I have read this tutorial several times,
http://grexengine.com/sections/externalgames/articles/Adam%20Martin-Java%20NIO%20Networking%20for%20Games-2.html
as well as others, and came up with this, I would like you to tell me if I am doing anything wrong as well as answering some points I still don't get (check comments).

What I think is having the User class hold the data (ArrayList<String> TxMessages;) to be sent to that user. Using a Hasmap<SelectionKey, User> to find out who's user the selectionkey belongs to I guess.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
public class NIOClass {

      public NIOCLass (int port) {
            ConnSelector = Selector.open();
            RxSelector = Selector.open();
            TxSelector = Selector.open();
            charset = Charset.forName("UTF-8");
            TxPort = port;
            RxPort = port;
            init();
      }

      public void init() {
            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.configureBlocking( false );

            ServerSocket ss = ssc.socket();
            InetSocketAddress address = new InetSocketAddress(RxPort);
            ss.bind(address);
           
            //SelectionKey key = ssc.register(RxSelector, SelectionKey.OP_READ);
            //So I register the same channel + port with different selectors depending on their behavior
            ssc.register(ConnSelector, SelectionKey.OP_ACCEPT);
            ssc.register(RxSelector, SelectionKey.OP_READ);
            ssc.register(TxSelector, SelectionKey.OP_WRITE);
      }
     
     
//Serve for at least <minimum_time> as stop as soon as possible after serving for <maximum_time>
      public void serve (int minimum_time, int maximum_time) {
            long currentTime = System.currentTimeMillis();
            long minTime = currentTime + minimum_time;
            long maxTime = currentTime + maximum_time;
           
            boolean keepGoing = true;
            while (keepGoing) {
                  ConnSelector.selectNow();
                  Set keys = ConnSelector.selectedKeys();
                  if(keys.isEmpty()) {
                        TxSelector.selectNow();
                        keys = TxSelector.selectedKeys();
                  };
                  if(keys.isEmpty()) {
                        RxSelector.selectNow();
                        keys = RxSelector.selectedKeys();
                  }
                  Iterator it = keys.iterator();
                  while (keepGoing) {
                        if(it.hasNext()) {
                              SelectionKey key = (SelectionKey)it.next();
                              it.remove();
                             
                              //processkey();
                              if(key.isAcceptable()) {
                                    /* Banned IPs?
                                    * Maximum # of connections per IP?
                                    */

                                    ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
                                    SocketChannel sc = ssc.accept();
                                   
                                    sc.configureBlocking(false);
                                    sc.socket().setTcpNoDelay(true); // will this work or bring problems, what happens if two messages arrive for the same socket before I read.

                                    //SelectionKey newKey = sc.register(RxSelector, SelectionKey.OP_READ|SelectionKey.OP_WRITE);
                                    keys.add(sc.register(RxSelector, SelectionKey.OP_READ)); // keys.add(), is it necessary?, I saw it somewhere else.
                                    sc.register(TxSelector, SelectionKey.OP_WRITE);
                                    //and add the SelectionKeys to the respective User class
                              } else if(key.isWritable()) {
                                    //get respective User class from a hashmap or whatever
                                    String message;
                                    //message = User.TxMessages.next();
                                    ByteBuffer bb = charset.encode(message);
                                   
                                    WritableByteChannel wbc = (WritableByteChannel)key.channel();
                                    SocketChannel sc;
                                    wbc.write(bb);
                              } else if(key.isReadable()) {
                                    ReadableByteChannel rbc = (ReadableByteChannel) key.channel();
                                   
                                    ByteBuffer bb = null;
                                    bb = (ByteBuffer) key.attachment();
                                    if(bb == null) {
                                          // attach InputBuffer to key
                                          bb = ByteBuffer.allocate(1024);
                                          key.attach(bb);
                                    } else {
                                          // If we already had an inputBuffer, we need to make sure it's ready for use (what does this do?)
                                          bb.limit(bb.capacity());
                                    }
                                   
                                    int numBytesRead = rbc.read(bb);
                                    if(numBytesRead < 0) {
                                          // This is an undocumented feature - it means the client has disconnected
                                          key.cancel();
                                          key.channel().close();
                                    } else {
                                          while( /*there_are_still_requests_in_the_buffer*/ true ) {
                                                // Process the first request
                                                // Remove the first request
                                          }
                                    }

                                    rbc.read(bb);                                    
                                    String text = charset.decode(bb).toString();
                                    //do something
                              }
                             
                             
                        };                        
                        currentTime = System.currentTimeMillis();
                        if(currentTime > minTime) keepGoing = false;
                        if(currentTime > maxTime) keepGoing = false;
                  }
            }
            return;
      }
     
      public void write(User user, String message) {
            //User.TxMessages.add(message);
      }

      private int TxPort;
      private int RxPort;
      private java.nio.channels.Selector ConnSelector;
      private java.nio.channels.Selector RxSelector;
      private java.nio.channels.Selector TxSelector;
      Charset charset;
}
5  Discussions / Community & Volunteer Projects / Re: Help with Java Game on: 2005-01-19 15:19:31
fixed, didn't check the links...
6  Discussions / Community & Volunteer Projects / Re: Help with Java Game on: 2005-01-16 16:20:56
Yeah you are right but I guess it is not against my moral. I mean... I am making an exact clone of a game, why wouldn't I use the game's sprites.

I am using localhost so UDP or TCP makes no difference, I am working on game logic not network since there's no client app. Whenever I start testing with remote computers then I will add a TCP class and use it though.

Thank you, I need the extra luck ;o
7  Discussions / Community & Volunteer Projects / Help with Java Game on: 2005-01-16 03:15:12
Well, since I posted this at GDNet I guess there's no reason I shouldn't post it here...

Hello. I want am coding a pokémon clone and I would like some help, mostly with the client.
This is an old game i want to finish. I first started this as a MUD on vbScript, but I am moving to Java so it can have graphics. Here are some old docs, I wrote them for some friends that were new to coding but wanted to help me so most of them are topics you already know... They gave up after reading them, that's why they don't have more pages.
Docs


Well, I am using Netbeans 4.0 and JDK 1.5.0_01. The game would be client/server over TCP (I am using UDP for now, since the testing is using localhost, may use NIO).
Right now I am coding the server's game logic, since it doesn't require graphics... I have the main classes and I am working on loading the initial data from the database.
Jebb is coding the client's graphics, if you want to join you can help him or code something else like the Battle Engine(!), AI or a Network layer based on UDP.

I am using this ER Diagram, not all the relations are present, only the ones that are important (otherwise it wouldn't be readable):

Direct link: http://img113.exs.cx/img113/2314/der5hx.png

The game would be 2D, so it can reuse the sprites from the original games (by clone I mean clone, not sure if that's the real meaning of a 'game's clone' though). The mechanics and game data is documented as well, so it is just about putting it all together.

The client will use LWJGL and Web Start for displaying graphics.

The sprites are sprites for a GBA, so they are small. The actual game-screen would be about 4 GBA game screens. The GUI would also contain a chat, for private messages and a general chat. This is a very bad example of a layout:

Direct Link: http://img110.exs.cx/img110/9749/layoutex0ba.jpg

Um I feel I am not showing much..., well, it has a site. It's actually a test ground for my css and php so it doesn't have information about this game but it looks good ^^
Since I keep changing the php and css I am not sure if it still works on IE so use Firefox, i guess other languages are not working as well, so stick to English.
Site

Well... PM me, email me (giaym@hotmail.com) or post here to tell me if I am not posting enough information (ie. You should finish that design doc, or post screenshots, or whatever...)
8  Games Center / 4K Game Competition - 2005 / Re: [j4k mk3] jm4k on: 2005-01-05 18:53:08
I cant* play it here, invalid display mode. Would you mind doing a 16bit colors version?
9  Games Center / 4K Game Competition - 2005 / Re: Lander 4K on: 2005-01-05 18:20:43
i dont like the collision detection, i lost one when i shouldnt have (the "transparent" zones shouldnt count), i also got a free win though Tongue
10  Game Development / Newbie & Debugging Questions / Re: another im lost thread? on: 2005-01-01 15:42:52
Thank you. I had this rpg working, but it was a VBScript MUD (sort of MiRC), the networking was already built so I didnt have to code that. It didnt have direct keyboard/mouse input, audio or graphics though, it was a chat room.

By quickly I mean not having to learn libraries I will never use with this game and in the future. I will check Xith3D.

But, what's better on networking, UDP or RMI (if they arent completely different). I understand RMI can call methods on remote machines, that means I can forget about TCP/UDP and instead sending messages I would call remote methods (??).

If that is possible that would simplify everything (I think), but despite I dont care if the game isn't fast I dont want it to lag the machine. Would that be too slow for a game? (RMI's overhead).
11  Game Development / Newbie & Debugging Questions / another im lost thread? on: 2004-12-31 03:41:11
Hello, my problem is What should i learn and use to create a client/server rpg world with turn based battles? using java of course...
My main concerns are 3d graphics (FSEM with Java3D?), audio (JOAL?) and networking (plain UDP?).

Now you know my problem you might want to read this if you need more information...

Well, it would be tile-based 2d, but i would like to move to 3d whenever i had the rest working (never?), so i would use java3d or similar for my 2d game. I dont exactly remember what kind of objects you can draw using java3d but in the worst case the images can be put on cubes... Using java java3d would also let me add 3d'ish attack effects.

So i was reading this book http://fivedots.coe.psu.ac.th/~ad/jg/index.html (buy it if you read it =/ ), to understand the basics and it was fine until i started reading like 6 different libraries that do the same thing (maybe they dont, but im new and confused...) Java3D, Xinith, JOGL, and others, plus the networking chapter's TCP, UDP, RMI, CORBA, JNI, etc

I was going to do this with just html, css and php but then i couldnt have a character moving on the screen without having to click an hyperlink, and PvP being nearly impossible.

Then i thought "i can add java for the animations", but soon after that i realized i didnt need php if i was going to use java, it could be an applet, which leads to "it could just be a regular fullscreen client application" and no need of browser.

Now, my priority is learning the right stuff, then comes finishing this game quickly, and last its resources usage. So i would like to know what is actually used on java games / what is best to use.
My main concerns are 3d graphics (FSEM with Java3D?), audio (JOAL?) and networking (plain UDP?).

How would the game be? You create an account, login, start a character (choose its name, class, etc), and start playing. The world would be 2d, tile-based with top-down view.
Inventory and data stuff would be stored on a database (SQLServer7 until i can use Postgre). Any user can only see up to X players in a room (like 8 per room/zone, they arent that big), and battles are turn based with 3d attack effects   so i give the 3d library a use (like a rotating cube with a purple translucent texture).

If you say eg "RMI for networking", please tell me what RMI is and why it is recommended over other approaches thank you ^^
Now i must sleep...
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (15 views)
2014-12-28 10:39:27

CopyableCougar4 (19 views)
2014-12-28 02:10:29

BurntPizza (24 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!