Monday 14 September 2015

Making Audio go private

Today I have been working on getting my Audio system implemented, mostly in order to keep the Activity Lifecycle happy- ie ensuring that media is paused, resumed and released appropriately. The initial implementation I made was an Audio class which was built up of static methods, each able to do a specific function, and if necessary call each other to use pre-existing functionality. The class worked, and is in a small project not a bad model, but I have been reading up on ways to setup a game View and it seems that the Audio class needs to be less public.

Honestly, my first reason for making methods static was that I read that using getters and setters was potentially heavy on resources, but in further reading I have found that for my audio needs this won't be an issue.

The main root of my game framework is now a base activity which is able to create and switch between views, the views themselves after a little experimentation are now extensions of the SurfaceView, and are able to take a pre-initialised Audio instance as a parameter. What this does is allow the main activity to set up the audio environment in one place, passing in itself for context rather than having the Audio being setup in the beginning of a new "screen".

The audio environment is now also much more encapsulated, and as such should only be passed to one screen and any reference will be lost as that screen dies. The activity itself can also ensure that the correct settings are in place for the audio-stream, should the app framework be used for other app types.

The project I have been working on is a soundboard which emulates the character select screen of Mortal Kombat 3, a childhood favourite of mine. So far I have been able to load the 15 game characters names and corresponding "introduction" speech files from the game, and I have it set up to play a random one from this list and display the name on each touch event.
The names themselves exist in an array which is organised in the order that the game presents them on the screen, however it may be necessary at a later date to have them sorted alphabetically- which we will do if needed. One string array (in the strings.xml resource) holds these names, and another array holds the filenames for the audio files in the same order. The reason I have kept the two seperate instead of trying to parse one array into an audio filename, is that I am able to localize the name strings and they will still line up with the sound files whose names are written in english. While the game characters names are the same in any language, it may not be the same in character based languages, and if I ever work on supporting those it would be great to not have to rewrite my audio file loading.

There are probably many other ways to link the names in an array to specific sound files but for now this seems to be the simplest and works well for this project.

The next step will be to load in a music track to loop in the background and then I can begin working on graphics!

Monday 7 September 2015

MediaPlayer experiments

When working with the media player there are some little things I have found that you need to keep an eye out for. The first is that the MediaPlayer object is quite happy to continue playing your media track after you have closed the app itself. This is an example of how important it is to release all resources and to monitor which ones persist in the memory of the device! While bumming around on the android developer website, I started looking into the billing API and found another example in the Google Payments "Binding" when an in-app purchase is attempted. When an app closes, if that binding is not released, it can use up system resources and degrade performance.

My experiments with the media player began with finding a question on the learnandroid subreddit, which asked about pausing media with a touchevent. From my reading I could see that the poster had not prepared the media track after stopping it, something which is unique to that circumstance but not the pause method. The poster also had a bit of confusion about the touch event itself and where to trigger the media playing. After posting a quick suggestion I decided that I should try to implement a music player in a test app too to get to grips with the little things that might be confusing for a beginner.

Following on from my blank screen SurfaceView test, I used VLC to convert a ripped mp3 into the OGGVorbis format with reduced sound quality (to save on memory) and added that into a new assets folder. From my previous experiemnts with Soundpool (An app with pikachu on it that says pikachu when you touch the screen, and plays a computer booting up sfx from pokemon firered on launch), I knew that sound files can be retrieved using the AssetManager in android. I made the first mistake in not putting the new directory in the right place in the file structure, but that didn't become apparent until it was fully set up.

My first set up was to have all of the media player implemented in the SurfaceView class itself, which is a Runnable in a class called Screen. The implementation included instantiating an asset manager, trying to get the file i wanted an AssetFileDescriptor which I could pass into a new instance of MediaPlayer. The process itself was all set up and the data source set and media started, but the app itself didn't work. After adding some Log notes and an exception message for the try-catch block I found that the asset manager wasn't retrieving the files in the folder, and looked back to my original pikachu app to find the structure difference. 

Once the file was able to load and play I ran the app and lo and behold, the music played, (Nine Inchnails- Satellites for those who need to know!). I was elated, then I closed the app and hmm.. still music.
I knew this had something to do with releasing the resources and the activity life cycle so I started to look there for ways to control my music. I started with onStop, onPause and onResume methods inside my SurfaceView which were called from the main activity, and implemented checks for if the media player was valid and playing etc, and stopped the music and restarted it as I expected. This did not work. The app was happy enough to stop the music but not to restart it when the app was loaded up again. I understood that the app was saved in temporary memory when the app was minimized or obscured, so the app was going through the onPause method, but I didn't realize the onStop method was being called also!

Going back I was able to debug my app and place appropriate boolean flags to control the music playing. In the onPause method, the music is (if it is not already) paused, and on resume the music is (if not null such as on initial bootup of the app) started again. The call to onStop was moved into an if block which checked that the app was not being closed fully (isFinishing()) and if so, stopped playback and also released the media player.
With my experiment finally performing the way I wanted it to, I was finally able to begin abstracting the audio into a seperate class. I created an Audio class that could handle music using static methods, which would mean it is accessible throughout my app. Instead of using a default constructor to set up the Audio environment, I opted instead to use static methods that only initialize variables if they are going to be used. I currently have a method that is needed to pass in a context for use in attaining the assets, but later on I will access this statically from a core class that handles the game.

The Audio class also has onPause, onResume and onStop methods which can be called to ensure that any active media, be it sound FX from the soundPool or music from MediaPlayer are paused and resumed correctly, and destroyed on closing the app.

I am really quite happy with the progress I am making in understanding these (albeit simple) aspects of app development. I want to be able to say not only that I understand the theory, but that I can put code on paper to describe the processes and show that I understand the way these systems work together on a more holistic level.

The code for my Audio and Screen app are linked below for viewing.

MainActivity Class
Audio Class
SurfaceView Class

Thursday 27 August 2015

Return to Game Development

Recently I have moved away from the Udacity course and tried to get back on track with the game development that I have wanted to do all along. I have returned my studies to Beginning Android Games, an Apress book I got a while back. When I first started trying to learn from this book I found the information to be far too heavy, I was not experienced enough with the Android OS to get it. Now that I have a better understanding, the pieces are coming together much easier.

While working through the examples, I have been playing around with the code and finding out the OTHER methods that some of the classes do. While I had made a game on Android following a "For Dummies" book in the past, this time I actually understand the process, the effect that the Activity Life Cycle has, and can see where the ideas I have fit into the code.

To help me keep notes and have an "in my own words" reference, I have been typing up  notes into Google Keep, an app I have had on my phone for a long time and rarely used. Being able to type quickly has always prevented me from note-taking on my phone. I am a terrible touch screen typist! Using the in-browser version of Google Keep has been a lifesaver- actual keyboard typing, and sync to my phone for on the go note revision. I have found myself memorizing key concepts quicker with these notes, and can now comfortably say that an extremely basic framework in a regular View can flow pretty quickly from my fingers. I do enjoy making notes and even updating them to make them more precise, but I can't always be at my laptop- for this reason I have contemplated buying a bluetooth for a while. (I have planned to buy a tablet for myself for quite a while now and really wanted a keyboard for it- so I have been watching Amazon and Bestbuy for a deal daily!) I finally ordered myself a keyboard last night, and hopefully that gives me more opportunities to be productive- either in writing up ideas, taking notes or coding when I am not limited by the amount of time I can comfortably sit at the computer.

As an example of the notes I have been keeping, here is my first entry!


Soundpool is a RAM based storage for small soundFX files, ideally in .ogg format and shorter than about 5 seconds. Setting up a SoundPool requires the parameters indicating how many sounds can play simultaneously (20 is a good default), which sound stream is being used (AudioManager.STREAM_MUSIC for media of any type), and a final unused parameter, default 0.

SoundPool sp = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
The SoundPool default constructor has been deprecated with the release of Lollipop, and now utilises a Builder for instantiating a SoundPool object.

SoundPool sp = SoundPool.Builder()  
.setUsage(audioAttributes)  
.setStream()  
.build();

 To load a soundFX file to the Pool, you have to get an AssetFileDescriptor from the Asset file.

AssetManager assetManager = context.getAssets();
AssetFileDescriptor asset1 = assetManager.openFD("dir/filename.extension");

When adding the descriptor to the soundpool, it will return an integer which identifies the sound loaded, which can be used as a parameter later. It is vital therefore that any files loaded be done so in an integer expression. the load method itself will take in the AssetFileDescriptor and a sound priority int as parameters. The sound priority is currently unused as is default as 1 (similar I guess to the weight attribute in xml layouts, having them all at 1 gives uniform importance).

int asset1IdNum = sp.load(asset1, 1);

This asset can later be played using the SoundPool instance, by referencing the identifier for the resource, as well as some other parameters.

sp.play(identifierInt, float leftChannelVolume, float rightChannelVolume, int priority, int looping, float playbackSpeed)

The channelVolume parameters allow a sound to be set on one ear or the other. The priority again is unused and is defaulted to 0. Looping is often not a good idea for a sound effect and is thus set to 0, and playback speed will be 1 by default, less for slow play and more for fast playback.
When all use of the SoundPool is done, it is best practice to release the resources directly by using either specific calls to soundPool.unload(identifierInt) or to all resources using soundPool.release();


I have made notes on MusicStream, working with the View and onDraw() method (invalidating as a method of continuous graphics rendering), setting the application to keep the screen on, go fullscreen and hide the notification bar and working in SurfaceView and Threads.

I'm hoping that my notes will not only enable me to retain information better, but also that they may help me to guide others if they ask me for advice on starting on a similar path!

There are notes I have to include after todays coding to describe how to set a listener for loading files, as I wanted to have a specific sound effect play as my testing app opens (computer booting up sound effect from a monster catching game you may have heard of!)

Friday 21 August 2015

Update to R2-D2 print

Now with second layer for black. The registration I used worked out really well. The first attempt at a second layer that I did last time I was printing didn't line up well at all and I was super worried, luckily it worked out!

Monday 27 July 2015

Checkboxes and scrollviews

The Udacity course took a swing into the Java side with explanations of variables and accessing the Views so as to manipulate them more programmatically. This to me was a welcome change of pace because I already have a fair understanding of Java. While XML has been a fun way to approach design, I'm happier when I'm making more unique code. The introduction to logic in this series has been with the use of a checkbox. Checking the state of the box also led to some details on inheritance. For the most part this is in explaining how to get a specific type of View by its ID rather than just the default View. What I found interesting was my ideas of how to handle the quantity picker and 0 or overly high values being selected. While at first I thought my  ode did a simple but effective job at preventing the variable from decreasing, I realized later that an if/else block actually gave me a much more elegant solution.

My original code simply had the following:

public void increment() {
     quantity++;

     if(quantity > 100){
          quantity = 100;
     }

     updateDisplay();
}

Not too pretty but it does the job needed. The tutorial suggests adding a Toast if the user attempts to increase the quantity above the limit (here set at 100). This idea seemed fun so I worked that into my code and came to the realization that if the incrementer is only ever going to respond to single digit changes, we could instead just never increment past the upper limit. My code instead now looks like this!

public void increment() {
     if(quantity == 100){
          Toast.makeText(this, "You cannot order more coffees",
          Toast.LENGTH_SHORT).show();
     } else {
          quantity++;
     }

     updateDisplay();
}

It is these little changes to the code that really give me a sense of satisfaction in my own ability. The code that the tutorial uses is a little more heavy but also does the task, but seeing my own implementation and its efficiency is one of those little wins.

The other aspect of the tutorials I have been introduced to is the use of a scrollview as the root view in the xml file. This is a welcome lesson as I have been attempting to move the Java utility I made for my work into an android setting (more as a test of my ability to port an existing app than anything else) and realized I quickly ran out of space on the screen if I had text at a decent size. While I am happy to read a book or website in tiny text, its important that if my app is to be used for quick sessions to check data, the display be a size that utilizes as much of the screen as possible.

Next steps are going to be looking at Intents basics and localization. Since I don't speak any foreign languages, I will be playing around with specific locations of English, such as En-GB and EN-US so that I can get accustomed to adding localization as I go, but also so I can dick around with stereotypical dialects :P

Sunday 12 July 2015

Nerdy Relief Print

Just a proof for a  two color print I'm working on of R2-D2. Ink on newsprint

Review: Android Development for Beginners on Udacity

My self-learned programming education has always had some tangents, times when I wasn't sure that my current method of learning was not efficient. Lately my progress in Android has been stunted by a lack of the fundamentals that the book I was learning from assumed. My email inbox has also been barraged by a bunch of online learning platforms with all kinds of promotions, and from this I discovered the Udacity course Android Development for Beginners.

I started this course mid week and I have to say I'm hooked. The style of learning is very methodical, and definitely shows that the directors have considered their target audience. While I already have fair amount of familiarity with java, the Android platform has so many quirks that my progress was a little jagged. Going back to basics was a scary proposition, both because it meant having to sift through some basics that will seems ridiculously easy, but also because I didn't want to find out that I missed some super important things.

Either way, I jumped in and committed myself to thinking "no matter what, take every lesson as though you are a newbie". With the mindset of a newbie, it was much easier to listen to some of the explanations of important concepts that seem easy to me now.

I admit to not being very familiar with XML; I know that its a markup language and I have familiarity with HTML so it didn't seem too foreign. In all honesty, my belief going into the course was that XML for android was a cop-out, the easy drag and drop method of putting together an app. I wanted the Java, I wanted to learn how to do things the hard way because I thought that's what a real programmer would do. The flaw in my thinking was revealed right away when I saw how much coding went into the XML, and that the drag and drop option was being skipped over entirely for a real coding approach.

The first lesson does not even touch an IDE, but instead puts the learner in a position to test their knowledge using a custom browser IDE with a preview pane. The experience here was liberating as I didn't feel like I had to learn a whole new IDE and spend six hours just getting it installed before I even got to write some code. Having a custom work space in-browser meant I didn't have to worry about saving files, strange error messages or settings windows. The lessons give a sample piece of code to either analyse, edit, or extend, and this led to some great time had just moving things around.

The lessons also emphasize the importance of searching through documentation for new concepts and really expanding your own knowledge past that of the syllabus. Once the first lesson is done you finally start to work with the IDE, in this case Android Studio. Since Eclipse is not going to be updated as much with Android development, and the course on Udacity is led by Google professionals, it only makes sense to make the switch to Android Studio.

The first practice lesson actually leads to an app which simply displays a message over a birthday themed image, but gets you to practice all of the concepts covered so far. My app looked as follows.

While its not in fact Toby's birthday until December, he was next to me and was a suitable replacement for the lessons "Ben"(no offence Ben). The app focused on using a relative layout to position the Views around the screen and overlapping an image. Overall the lesson did not take long but gave a strong foundation both in putting together a project, and also in running it on my android device.

From there the next lesson stepped it up a notch and worked through creating an app that simulates a coffee ordering interface, nicknamed "Just Java". The app added interactivity with Java and taught me a lot about using the resources system to save strings and images for use later. With a little extra time on my hands while the instructors explained the basic concepts of variables, I managed to jazz up my app a little to be the final product listed here.

While the app itself does nothing beyond using a picker to increment a number and update a price field, I was able to learn how to work with currency Number Formatting, how to grab an XML element and work with it in the Java Activity, and also how to nest layouts to achieve the look and spacing I desired. Admittedly, some of these may have been easier for me than beginners as I already have a working knowledge of Data Types, OOP, Inheritance and Polymorphism, all of which are for now are just taken as truth for the students in the course.

Overall I am thoroughly impressed with the course so far and hope to get more done as soon as possible, my ideas for apps are coming through more frequently and with a larger range of inspirations and I hope to share some of the finished products here soon!

Monday 29 June 2015

Android and Java

My main track of learning has been in Java, and as such I have of course found an interest in learning to work with the Android platform. My own smartphone is an Android, we own a tablet which is Android and I see myself continuing to make purchases of this type in the future. I got a couple of Android game programming books for Christmas and one of them is too simple (copy what we wrote and don't ask questions), the other assumes a lot more knowledge and has taken me a while to catch up to.

Before learning Java more comprehensibly through the Head First Java book, I studied via a web series from developer John Purcell. The course is available on Udemy and I highly recommend it for anyone looking to get a grip on Java while seeing someone type and run code into the Eclipse IDE.

There is also an Android course available by John Purcell which I hope to take in once I have a handle of the basics. My experience so far has been that studying from one source and then getting that second look from another really helps to solidify my understanding. Seeing one example of a new concept compared to another is often the deciding factor on whether I "get it" or need to go to bed because I am too tired.

My current understanding of Android is of the Manifest and how to add activities, the use of a few basic View's, some simple touch handling and basic drawing to a Canvas. I am working on an activity that takes in multiple touches (pointers) and assigns them to a new or incomplete pair. For each pointer, if it belongs to a pair but has no other pointer, it draws a circle, and for complete pairs (both pointers still touching the screen) it draws smaller circles and a line to connect them. My code for this has gone through a few iterations as I figure out the pointers for each MotionEvent that is fired and handle it, but the drawing aspect has me stuck right now. My program may just be over complicated (using a class instance for each PointerPair) and I may have to go back to simple arrays to keep track of each pointer and its relationship. for now though my code looks like this.

There are a few Log.d debugging lines in there for when I test on my phone. It took a while for me to realize that my code was not functioning and cloned my initial PointerPair instance to all slots of the array because I didn't exit the for loop when I should have. I understand my code isn't pretty and probably makes some ridiculous optimization choices but its main objective is to help me learn so I'm not too precious yet :P

(I used hilite.me for code formatting)

  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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package ***********;

import java.util.ArrayList;
import java.util.Random;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class DrawLinesTest extends Activity implements OnTouchListener{
 private final int MAX_PAIRS = 5;
 PointerPair[] pairs = new PointerPair[MAX_PAIRS];
 Random rng;
 PointerPair openPair = null;
 
 ArrayList<PointerPair> drawables = new ArrayList<PointerPair>();
 
 
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  RenderView v = new RenderView(this);
  v.setOnTouchListener(this);
  setContentView(v);
  
  rng = new Random();
 }
 
 public void onResume(){
  super.onResume();
  for(int i = 0; i < pairs.length; i++){
   pairs[i] = null;
  }
 }
 
 @TargetApi(Build.VERSION_CODES.FROYO)
 public boolean onTouch(View v, MotionEvent e){
  //Log.d("PointerPair", "Screen Touched");
  int action = e.getActionMasked();
  int pointerIndex = e.getActionIndex();
  int pointerId = e.getPointerId(pointerIndex);
  
  switch(action){
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_POINTER_DOWN:
   if(openPair != null){
    openPair.setPointer2Id(pointerId);
    openPair.setCoords(1, e.getX(), e.getY());
    Log.d("PointerPair", "Pairs: An pointer partner has been found! ");
    openPair = null;
   } else {
    boolean pairCreated = false;
    for(int i = 0; i < pairs.length; i++){
     if(!pairCreated){
      if(pairs[i] == null){
       Log.d("PointerPair", "Pairs: Creating new pair at index " + i);
       pairs[i] = new PointerPair(pointerId, e.getX(), e.getY());
       drawables.add(pairs[i]);
       Log.d("PointerPair", "Pairs: An pointer needs a partner! ");
       openPair = pairs[i];
       pairCreated = true;
      }
     }
    }
   }
   break;
  case MotionEvent.ACTION_MOVE:
   for(int i = 0; i < pairs.length; i++){
    if(pairs[i] != null){
     if(pointerId == pairs[i].pointer1Id){
      pairs[i].setCoords(0, e.getX(), e.getY());
     } else if(pointerId == pairs[i].pointer2Id){
      Log.d("PointerPair", "Painting - updating second pointer coords");
      pairs[i].setCoords(1, e.getX(), e.getY());
     }
    } 
   }
   break;
  case MotionEvent.ACTION_POINTER_UP:
   for(int i = 0; i < pairs.length; i++){
    if(pairs[i] != null){
     if(pointerId == pairs[i].pointer1Id){
      //Log.d("PointerPair", "Pairs: Lifted Pointer matches pointer 1 in pair ID:" + i);
      pairs[i].pointer1Id = -1;
      
     } else if(pointerId == pairs[i].pointer2Id){
      //Log.d("PointerPair", "Pairs: Lifted Pointer matches pointer 2 in pair ID:" + i);
      pairs[i].pointer2Id = -1;
     }
    } 
   }
   break;
  case MotionEvent.ACTION_UP:
   for(int i = 0; i < pairs.length; i++){
    pairs[i] = null;
    drawables.clear();
   }
   openPair = null;
   break;
  }
  
  cleanPairs();
  v.invalidate();
  return true;
 }
 
 private void cleanPairs(){
  for(int j = 0; j < pairs.length; j++){
   if(pairs[j] != null){
    pairs[j].update();
   }
  }
  
  // Clean out dead pairs
  for(int i= 0; i < pairs.length; i++){
   if(pairs[i] != null){
    if(pairs[i].pairDead()){
     drawables.remove(pairs[i]);
     //Log.d("PointerPair", "Pairs: Nulling a pair at position" + i);
     pairs[i] = null;
    }
   }
  }
 }
 
 class PointerPair{
  private int pointer1Id, pointer2Id;
  private boolean needsPair, isPointer1Dead, isPointer2Dead, isPairDead;
  private int p1x, p1y, p2x, p2y;
  Paint paint;
  
  
  public PointerPair(int pointer1Id){
   this.pointer1Id = pointer1Id;
   pointer2Id = -1;
   needsPair = true;
   paint = new Paint();
   paint.setARGB(255, rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
  }
  
  public PointerPair(int pointer1Id, float x, float y){
   this(pointer1Id);
   setCoords(0, x, y);
  }
  
  public void setCoords(int pointerIndex, float x, float y){
   if(pointerIndex == 0){
    p1x = (int) x;
    p1y = (int) y;
   } else if(pointerIndex == 1){
    p2x = (int) x;
    p2y = (int) y;
   }
  }
  
  public void update(){
   if(pointer1Id == -1){
    isPointer1Dead = true;
   }
   if (pointer2Id == -1){
    isPointer2Dead = true;
   }
   
   if(isPointer1Dead && isPointer2Dead){
    isPairDead = true;
   } else if (isPointer1Dead && needsPair){
    isPairDead = true;
   }
  }
  
  public boolean needsPair(){
   return needsPair;
  }
  
  public void setPointer2Id(int pointer2Id){
   this.pointer2Id = pointer2Id;
   needsPair = false;
  }
  
  public boolean pairDead(){
   return isPairDead;
  }
  
  public Paint getPaint(){
   return paint;
  }
 }
 
 class RenderView extends View{
  Paint paint;
  
  public void onDraw(Canvas canvas){
   
   clear(canvas);
   for(int i = 0; i < pairs.length; i++){
    for(PointerPair p: drawables){
     Paint paint1 = p.getPaint();
     
     if(p.pointer1Id != -1 && p.pointer2Id != -1){
      Log.d("PointerPairPaint", "Painting a pair, 2 circles baby!s");
      canvas.drawCircle(p.p1x, p.p1y, getWidth() / 15, paint1);
      canvas.drawCircle(p.p2x, p.p2y, getWidth() / 15, paint1);
      return;
     } 
     
     if(p.needsPair() || p.isPointer2Dead){
      Log.d("PointerPairPaint", "Painting lonely first pointer, " + p.p1x + ", " + p.p1y);
      canvas.drawCircle(p.p1x, p.p1y, getWidth() / 10, paint1);
      return;
     } else if(p.isPointer1Dead){
      Log.d("PointerPairPaint", "Painting lonely second pointer, " + p.p2x + ", " + p.p2y);
      canvas.drawCircle(p.p2x, p.p2y, getWidth() / 10, paint1);
      return;
     }
    }
   }
   //invalidate();
  }
  
  public RenderView(Context context){
   super(context);
   paint = new Paint();
  }
  
  private void clear(Canvas c){
   paint.setColor(Color.GRAY);
   c.drawRect(0, 0, getWidth() - 1, getHeight() - 1, paint);
  }
  
 }
 

}

Sunday 28 June 2015

3 Way Battle Simulation

So in my previous post I made reference to a simulation of battles between Mages, Rangers and Warriors. Just thought I'd post it here for anyone to look through and play with! The Barracks.java class is the runnable, and will output a .rep file (simple text document, open with Notepad) with the breakdown of the battle. It wouldn't be too hard to add more info to the report. you can tweak the attack power and defense or any of the other stats for each of the classes.

Mob Class (Parent for the three rpg-classes)

 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
package saving;

import java.io.Serializable;

@SuppressWarnings("serial")
public class Mob implements Serializable{
 
 int maxHealth;
 int health;
 String name;
 int attack;
 double defence;
 double accuracy;
 double dodge;
 String type;
 private String battleReport = null;
 private String attackReport = null;
 
 private boolean isAlive;
 
 public Mob(){
  isAlive = true;
 }
 
 public void takeDamage(int damage){
  int absorb = (int) (damage * defence);
  if(damage - absorb >= 1){   
   health -= (damage - absorb);
  }
  
  battleReport = name +  " absorbs " + absorb + " damage.\n" + name + " takes " + (damage - absorb) + " damage\nHealth remaining: " + health + "\n";
  
  if(health <= 0) {
   health = 0;
   isAlive = false;
  }
 }
 
 public void attack(Mob enemy){
  attackReport = null;
  battleReport = null;
  enemy.attackReport = null;
  enemy.battleReport = null;
  attackReport = (name + " is attacking " + enemy.name);
  if(Math.random() > accuracy){
   // Missed
   attackReport += "\nAttack missed. End of attack.";
   enemy.battleReport = enemy.name + " is unharmed\n";
   return;
  }
  
  if(Math.random() < enemy.dodge){
   // Dodged
   attackReport += "\n" + enemy.name + " dodged the attack. End of attack.\n";
   enemy.battleReport = enemy.name + " is unharmed\n";
   return;
  }
  
  int attackDamage = attack;
  if ((type.equals("Orc")  && enemy.type.equals("Elf")) 
    || (type.equals("Human") && enemy.type.equals("Orc"))
    || (type.equals("Elf") &&  enemy.type.equals("Human"))){
   attackDamage *= 1.2;
   attackDamage -= Math.random() * attackDamage;
  } else if((type.equals("Orc") && enemy.type.equals("Human")) 
    || (type.equals("Human") && enemy.type.equals("Elf"))
    || (type.equals("Elf") && enemy.type.equals("Orc"))){
   attackDamage *= 0.8;
   attackDamage -= Math.random() * attackDamage + 1;
  }
  enemy.takeDamage(attackDamage);
 }
 
 public void heal(int hpUp){
  health += hpUp;
  if(health > maxHealth) health = maxHealth;
 }
 
 public String toString(){
  return "MOB: " + this.hashCode() + "\nHealth: " + health +"\nAlive: " + isAlive; 
 }
 
 public boolean isAlive(){
  return isAlive;
 }
 
 public String getBattleReport(){
  return battleReport;
 }
 
 public String getAttackReport(){
  return attackReport;
 }

}

Main Class - Barracks.java

Edit the COMBATANTS constant to change how many mobs are in each army, note, it will take a lot longer to run if you go too high!

1000 combatants (3,000 total) took approx 0.5 seconds
10,000 combatants (30,000 total) took approx 5 seconds
100,000 combatants (300,000 total) took approx 62 seconds
1,000,000 Could not run, Out of Memory.


  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
package saving;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Barracks {

 private final int COMBATANTS = 500; // exceed 100,000 at your own risk.
 private int skirmishes;
 
 @SuppressWarnings("rawtypes")
 private ArrayList<ArrayList> mobs;
 
 public static void main(String[] args) {
  Barracks b = new Barracks();
  b.go();
 }
 
 public void go(){
  long startTime = System.currentTimeMillis();
  System.out.println("Starting simulation: Number of Combatants per army = " + COMBATANTS);
  // Armies of Mobs
  ArrayList<Orc> orcs = new ArrayList<Orc>();
  ArrayList<Elf> elves = new ArrayList<Elf>();
  ArrayList<Human> humans = new ArrayList<Human>();
  
  // Populate armies
  for(int i = 0; i < COMBATANTS; i++){
   orcs.add(new Orc("Warrior" + i));
   elves.add(new Elf("Ranger" + i));
   humans.add(new Human("Mage" + i));
  }

  // Armies added to array list
  mobs = new ArrayList<>();
  mobs.add(orcs);
  mobs.add(elves);
  mobs.add(humans);
  
  try {
   // Create text report
   FileWriter fw = new FileWriter("BattleReport.rep");
   BufferedWriter bw = new BufferedWriter(fw);
   // Report title
   bw.write("Battle between " + COMBATANTS + " orcs, elves and Humans:");
   
   // Main simulation loop
   while(!orcs.isEmpty() && !elves.isEmpty() && !humans.isEmpty()){
    // While all armies have combatants, select to opposite faction mobs
    int class1 = (int) (Math.random() * 3);
    int mobNum = (int) (Math.random() * mobs.get(class1).size());
    Mob fighter1 = (Mob) mobs.get(class1).get(mobNum);
    
    // Prevent same faction skirmish
    int class2 = (int) (Math.random() * 3);
    while(class2 == class1){
     class2 = (int) (Math.random() * 3);
    }
    int mob2Num = (int)(Math.random() * mobs.get(class2).size());    
    Mob fighter2 = (Mob) mobs.get(class2).get(mob2Num);
    
    // Actual skirmish
    fighter1.attack(fighter2);
    // Get report on actions taken
    bw.write("\n" + fighter1.getAttackReport());
    // Get report on hits taken and damage mitigation
    bw.write("\n" + fighter2.getBattleReport());
    // If damage is taken, increment skirmishes counter
    if(fighter2.getBattleReport().contains("takes")){
     skirmishes++;
    }
    if(!((Mob) mobs.get(class1).get(mobNum)).isAlive()){
     mobs.get(class1).remove(mobNum);
    } else if(!((Mob) mobs.get(class2).get(mob2Num)).isAlive()){
     mobs.get(class2).remove(mob2Num);
    }
    
   }
   long delta = System.currentTimeMillis() - startTime;
   double elapsedTime = delta / 1000.0;
   System.out.println("Ending simulation: Number of Skirmishes = " + skirmishes + "\nTime taken: " + elapsedTime + "sec");
   
   // Write out summary of battles
   bw.write(battleReport());
   bw.close();
   
  }catch (IOException e){
   
  }
  
 }
 
 public String battleReport(){
  String report = null;
  // Find defeated army and display remaining enemies and total skirmishes
  if(mobs.get(0).isEmpty()){
   report = "\nOrcs are defeated" +
   "\n" + mobs.get(1).size() + " Elves remain" +
   "\n" + mobs.get(2).size() + " Humans remain";
  } else if (mobs.get(1).isEmpty()){
   report = "\nElves are defeated" +
     "\n" + mobs.get(0).size() + " Orcs remain" +
     "\n" + mobs.get(2).size() + " Humans remain";
  }else if (mobs.get(2).isEmpty()){
   report = "\nHumans are defeated" +
     "\n" + mobs.get(0).size() + " Orcs remain" +
     "\n" + mobs.get(1).size() + " Elves remain";
  }

  return (report + "\n" + skirmishes + " total Skirmishes");
 }
}

Elf.java - The Ranger class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package saving;

@SuppressWarnings("serial")
public class Elf extends Mob{
 
 public Elf(String name){
  super();
  this.name = name;
  maxHealth = 180;
  health = maxHealth;
  type = "Elf";
  
  attack = 16;
  defence = 0.15;
  accuracy = 0.95;
  dodge = 0.06;
 }
}

Orc.java - The Warrior class


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package saving;

@SuppressWarnings("serial")
public class Orc extends Mob{
 
 public Orc(String name){
  super();
  this.name = name;
  maxHealth = 150;
  health = maxHealth;
  type = "Orc";
  
  attack = 20;
  defence = 0.30;
  accuracy = 0.85;
  dodge = 0.1;
 }
}

Mage.java - The Mage class


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package saving;

@SuppressWarnings("serial")
public class Human extends Mob{

 public Human(String name){
  super();
  this.name = name;
  maxHealth = 140;
  health = maxHealth;
  type = "Human";
  
  attack = 30;
  defence = 0.12;
  accuracy = 0.90;
  dodge = 0.05;
 }
 
}