Vim: Replace tabs with spaces

When working in other peoples code I almost always notice mixed tabs and spaces. Like many programmers, this makes my skin crawl since it usually causes formatting to look horrible or incomprehensible.

First I'll double check that there are mixed tabs and spaces by highlighting the tabs.
/\t


If I see some whitespace highlighted and some not highlighted I'll look at the indentation and figure out how many spaces each tab should be, then do a replace. This will perform a replace on the entire file (%s) for all tabs (\t) into four spaces (\ \ \ \ ), the g at the end will allow the match to be done multiple times per line:
:%s/\t/\ \ \ \ /g


If your tab expand matches the file, you can also use the vim built-in retab:
:%retab

Taking Stuff Apart: Data-Doc Data Switch

Not much to this one. The case is a nice size for a project box, which is why it was picked up in the first place. The mess of wires inside is impressive. As usual click the pictures for more (and in higher resolution).

Data-Doc Data Switch - Front

strcpy or fun with pointers, a while loop and cstrings

ps1 and ps2 are pointers to a character array, what does this do?
while (*ps2++=*ps1++);

It is my favorite example of bizarre looking C code which utilizes pointers, pointer increments, unary operation precedence, C-string null termination and the evaluation of an assignment. Breaking it down a little "*ps2++=*ps1++" means the same thing as this:
*ps2 = *ps1;
ps2++;
ps1++;

Since ps1 and ps2 are character pointers the first line dereferences each and sets the value of ps1 to the memory pointed to by ps2. The second and third lines just increment each pointer. Both are pointing to character arrays, so they now point to the next character in the array.

The final important detail is that a cstring ends with a null terminator. So eventually the loop will get to the strings end and assign "'\0' = '\0'", which evaluates to '\0', which is false. Thus ending the loop.

Here's a full example:


Here it is in the form of strcpy (from wikipedia):

Linux Automated Backup

I've been wanting to setup some sort of automatic backup on my personal data for quite some time, but have always settled on manually copying important documents to a second hard drive. Well thanks to THIS nice guide to setting up rsync and cron I now have it setup.

Short guide shorter:

1) Create a bash script with the following (note there are only two lines below, the command is long):

#!/bin/bash
sudo rsync -av --progress --delete --log-file=<location to put log file>/$(date +%Y%m%d)_rsync.log <directory to backup> <directory to put backed up directory in>

2) Run "sudo crontab -e" and enter the following line to have your backup done every morning at 4am:
0 4 * * * /root/rsync-shell.sh

Showing Gnome Workspace Names

One of my favourite things in Linux is having multiple workspaces. Something I didn't know until recently is that you could name your workspaces, and with a simple trick have the name displayed on your top or bottom panel.

New Project Smell

Took the first steps on a new project, an automatic cat feeder. The plan is to have it completed by Thanksgiving so that the cats have something to eat while I'm out of town. That being said, I cut the pieces for the box out of some 1/4" MDF and have a lot of work to do.

Automatic Cat Feeder - Pieces

Taking Stuff Apart: 8mm Projector

More treasures from the landfill, this is something I took apart a year or two ago. The only reason its still kicking around is because of the motor, which will be re-purposed in another project someday. There's nothing left to identify what model it is, anyone know?

There isn't much left for the front. Everything interesting has been ripped out including an old bulb, lens and a switch. The bulb's reflector was actually used in another project. In the top corners are a couple spokes which were probably used to attach the reels, film would go through the slot in the bottom right where the bulb and lens used to be.
8mm projector 1

OpenGL in SDL on Ubuntu

Supposedly SDL is better supported than GLUT for cross-platform OpenGL. There's even a Nintendo DS and Wii port for it!

SDL is right in the Ubuntu repository, so it is just as easy to install as GLUT was:
sudo aptitude install libsdl-dev

To test it out, this time I went to NeHe Lesson 2. There is a port for Linux/SDL.

The same GL header files are needed, but this time SDL is needed as well:
#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL.h"

It looks like there are a couple more flags needed, the part at the end is SDL, I'm not sure why it looks that way.
gcc lesson02.c -lGL -lGLU `sdl-config --cflags --libs`

GLUT on Ubuntu: Basics

To install GLUT:
sudo aptitude install freeglut3 freeglut3-dev

This will install the OpenGL header files to /usr/include/GL/
And the library objects to /usr/lib/libglut.a

With all that taken care of now you just need a quick sample app to see if it worked, I googled "glut hello world" and this was the second result.

I needed to add "#include ". Also notice that GLUT is included like "#include ". I then renamed it to helloworld.cpp and was able to compile as follows:
g++ helloworld.cpp -lglut

That's it, now you're ready to figure out something cool to make.

Crappy Volume Control

My dad was kind enough to provide me with a nice db systems amplifier, but the thing didn't have any volume control. I found my source (a PS3) to be lacking in range settings so I needed to come up with some sort of pre-amp. What you see below is the result. Nothing more than a dual potentiometer stuck inside an MDF box, well almost a box, I never built the top or rear of the thing.

Crappy Volume - Side Crappy Volume - Top

DVD Rip for PS3 through Mediatomb

For episodic content I like to rip my DVD's so that I can play them off my mediacenter (MediaTomb enabled PS3) one after another without worrying about disks. For Linux I use an application called "DVD::Rip" which makes this process fairly simple:

Job control on UNIX systems

I've been using control-z and fg to stop / resume programs from the UNIX terminal for as long as I've been using that environment. The problem is that I've only used those two commands so my usual workflow has always been "stack based". Meaning I have always used fg to resume the most recent program that I stopped.

Long story short, there is another command called jobs which enumerates all the programs which have been stopped. At this point you can use fg # to resume whichever job you want.

What a concept.

I found this web page had a nice description of all these commands:
http://acs.ucsd.edu/info/jobctrl.shtml

Sim Ant

I found a copy of Sim Ant at the local landfill today. Complete with the original box, 4x 5.25" floppy disks, 2x 3.5" floppy disks and the manual, with a whopping 178 pages.

One of the requirements on the box actually says "Requires hard drive". Click the photo for some more.

The Whole package

Taking Stuff Apart: Realistic Modulette-8


When I saw this fine piece of electronics at the landfill I knew it was special. Not only did it have a plethora of nice looking knobs and various shiny areas, but it also had a wooden enclosure. Although stamped steel cases are nice, its a little impersonal, not like this finely crafted device. I had already thrown it in the trunk before I even noticed - the thing was an 8-track player.

Click below for more pictures.

MediaTomb + PS3

I like to stream audio and video content from my Ubuntu box to my tv and I have a PS3. The PS3 has Universal Plug and Play support. I tried a handful of UPNP servers including Vuze, GeexBox, uShare and MediaTomb. I stopped at MediaTomb because it worked for all my needs and had a snazzy web ui for the server.

There is a problem however. MediaTomb's development version has better PS3 support and some extra features which are nice (video thumbnails, dvd iso navigation, watched markers, probably others).

Building MediaTomb from source was a huge hassle the first time I did it due to ffmpeg and its various dependencies. When I built it yesterday however I found THIS guide. Which was almost perfect, I needed to modify the "nut" Makefile to compile with -fPIC, and ffmpeg has moved from libamr to libopencore-amr, so I needed to install the opencore libraries from source as well. Other than that its a GREAT guide and now I have a MediaTomb 0.12.0 build.

Ants

I haven't had a lot of time to work on my Ant application thanks to a new job, but I have high hopes that I will start working on it again soon. I've been running the last iteration of the program a couple times a day, cursing the ants for not following the trails the way I expect them to.

To get back up to speed with DS / Devkitpro development I started working on a simple Logging program that I've wanted to make for a while. Essentially it will take down point of sale information for things like Gas Station, Restaurant and Shopping, then allow display / calculations based on the information.

In other news, HERE(youtube video) is a Nintendo DS project my brother has started in the past couple months. He has a rom file available here: ROM FILE

Vim Window Size

I have a couple entries in my vimrc configuration that I need:

map - <C-W>-
map = <C-W>+


When you have multiple windows open you can change their size with "Control-w" and '+' or '-' (note that you need to be holding SHIFT to get a '+' sign). This will change the window size one line at a time. If your extremely suave and can count line numbers in your head you can prepend the number of lines you would like to move and the keyboard shortcut isn't all that bad.

I can't count the lines in my head, so I use the above macro. Since I work with multiple windows constantly I have the increase and decrease size bound to '-' and '=', I use equal so that I don't have to press SHIFT for one but not the other.

goto in PHP 5.3

It appears that the goto statement is being added to PHP 5.3. Considering its bad reputation the move seems like a strange feature to be adding. Especially since one of the main benefit of a goto, exception handling, appears to have been introduced in PHP 5.

Hopefully all the web programmers out there will tread cautiously with their new power.

Taking Stuff Apart: Nikko NA-550

From time to time I come across an old piece of electronics that I want to harvest some parts from. Stepping motors from floppy disk drives, Solenoids from zip drives, small DC motors from CD drives to name a few of my favorites. Audio Amplifiers in particular are my very favorit. They have lots of interesting and hard to find pieces.





Without further adieu, here's a before shot of this vintage 80's "Nikko NA-550 Integrated Amplifier" that I picked up at a landfill a few weeks ago. For anyone curious, the power button was jammed in the off position, which is probably what earned it a spot at the dump.



Got a bunch of nice metal knobs, some dB meters, some very nice Alps selector switches, some Alps potentiometers, a 2.5A breaker and some other misc parts.

Ant AI

I've been toiling away trying to come up with an algorithm which will make ants move in a believable manner while only using a very limited view (the 4 adjacent tiles which they can move to). This was much more difficult than I was expecting, it sounds too easy to say "the ants wander around randomly, then once they found food they could head home following the chemicals they left behind". Implementing it like that not only doesn't work, but the ants end up congregating around the ant hill and don't accomplish much of anything.

The final algorithm is a set of rules that look like this:
1. If carrying food, call go home AI.
2. If carrying food and in base, drop food (or wander until there is a spot to drop the food), set chemical output to low.
3. If not on surface, wander until on surface.
4. If on surface, check for food, pickup, change chemical output to high.
5. If on surface and no food, try to follow a chemical trail.
6. If no trail to follow search for food by looking for a spot with low (or no) chemicals.

The most successful part of this algorithm is step #6, the ants really spread out and find food spawned in a random location quickly.

The other interesting part, which isn't in the steps above, has to do with how the ants apply their pheromone to the tiles. I'm not sure how it is done in nature, but I have it set so if the trail is cold and the ant doesn't want other ants to follow it (i.e. not carrying food) it deposits only a small chemical trail, essentially marking that someone has been there. On the other hand if it does want other ants to follow it (i.e. carrying food), it will deposit a high amount.

To glue everything together in a natural manner, the chemical levels "decay" over time.

The biggest surprise of all this how complex the chemical trail following algorithm became. In an ideal world the chemical trail would be a high resolution heat-map style implementation which would allow the direction they are moving in to be clear. With my low resolution chemical trail, if there is a block of several high-chemical tiles all clumped up the ants start getting put into cycles, so I have to add special cases to resolve that. I'm not sure how interesting those special cases are so I'll leave them out for now.

I now have lots of variables to tweak (chemical output low, chemical output high, chemical decay rate, decay amount, max chemical per tile) several algorithms and functions which could use tweaking (chemicalOutput, trail following). All in all I'm very happy with the result given the constraints, and will hopefully have a demo out soon.

First Demo

My DS application has come a long way in the past few weeks, I think it is finally time to present the fruits of my labor. I made a video to show off the player movement, world movement, camera scrolling and some of the simple AI / world effects I have going. This is only the beginning though! Everything is now in place for me to build more cool things. There are a couple more things I would like to show off in the near term which I'll post here and on the YouTube channel so keep an eye out!



If you would like the demo shoot me an e-mail. Its not at the point where I'd like to publicly distribute it however.

Video Conversion - mencoder + ffmpeg

This is just a quick post. I am working on creating a video for YouTube to show off the current state of my project (see my prior post). Several challenges came up during this process, this is how I handled them:

1) I needed a desktop recording program to record the emulator.
This was easy, I installed a program called "gtk-recordmydesktop" and was able to figure everything out with no instructions. On Linux (Ubuntu) this is in the package manager so it was as easy as "sudo apt-get install gtk-recordmydesktop"

2) The resulting video is a non-standard resolution and format, in this case my video file is out.ogv with 272 x 432 resolution. My video editor wants a dv file with 4:3 aspect ratio and YouTube wants 640 x 480 resolution. Getting this to the necessary aspect ratio / resultion / file type required re-encoding / padding / scaling with mencoder, and a transcode with ffmpeg.

I found this command to do a straight conversion:
mencoder out.ogv -ovc xvid -xvidencopts pass=1 -o output.avi

The file was not the correct aspect ratio however, to get that I add expand=-304:0:152:0 to pad the video:
mencoder out-2.ogv -vf expand=-304:0:152:0 -ovc xvid -xvidencopts pass=1 -o output-padded.avi

This is great, but I still need 640 x 480 resolution, so I add scale=640:480 and have the final result:
mencoder out-2.ogv -vf expand=-304:0:152:0,scale=640:480 -ovc xvid -xvidencopts pass=1 -o output-padded-scaled.avi

All thats left now is to convert to DV with ffmpeg:
ffmpeg -threads 2 -i output-padded-scaled.avi -s pal -r pal -aspect 4:3 -ac 2 -ar 48000 -y output-padded-scaled.dv

3) Now to get back to editing my video for YouTube!

Small addendum:
I used my cell phone to record the audio, which saves in AMR format, which is proprietary and not easy to convert. I managed to get it with the following commands:
amrnb-decoder Input.amr audio.sw
sox -r 8000 audio.sw audio.wav


I used references:
google
http://forum.doom9.org/archive/index.php/t-1
31659.html
http://www.kinodv.org/dcforum/dcforum?az=show_topic&forum=103&topic_id=609
http://aldeby.org/blog/index.php/encodedecode-3gpp-amr-files.html

Side Tracked

I have a new addiction which has put all my other projects on hold. Nintendo DS Homebrew. A couple years ago I took a look at the homebrew development tools and libraries available for the DS and Wii, but didn't get much further than building and tweaking the example programs. Recently I took another look at the Nintendo DS homebrew devkit, devkitPRO. After a few hours I had myself a nice PONG clone, complete with touchpad control. Well, it was so easy to put together that I started working on something bigger.

That was all two weeks ago, and my bigger project is coming along nicely (5000 lines strong). At the core my "game" is an ant simulator, complete with randomly generated underground tunnels, a surface world with food spawns and multiple possible underground <-> surface links and a queen that lays eggs and soon breeders to move the eggs away from the queen, soldiers to fight spiders, spiders, etc. I'm nearing the point where I can stop with the ant simulation portion of the project and start with the game portion. The Nintendo DS can currently handle simple AI movement for thousands of ants without a hitch, but drawing more than 50-100 on the same screen becomes problematic.

I have several ideas kicking around including things like create a colony of 1,000 ants, ants-vs-humans and ant colony vs. ant colony. But those are still being designed.

I made a high level checklist for things I'd like the projects to have at a high level, and am making good progress on it. The asterisk indicates that the bullet has been completed:

*1. Create the 2D underground world.
* - 2d
* - destructable, player
* - basic types:
* -Dirt
* -Empty
* -Surface
* -Barrier

2. Create basics
* - Player
* - Ants
* - Basic AI
* - Camera following
- Player control
* - D-Pad
* - Touch, simple (broke this after I made the surface.
- Touch, dijkstra

*3. Create surface world
* - Link to underground
* - Objects (Rocks, Food), carryable by Player / Ants
* - Object spawns (food on surface).

4. Basic ant functions
* - hp / death
- extended statistics (not sure what these are)
* - Food / Eating
* - Pickup / Drop logic (i.e. stacks of food)
* - Queen / Egg production
- Egg / Hatch (i.e. Game loop that checks underground for growing eggs.)
- Collision detection
- Grid, patch knows when ant is on it
- Two ants per patch, if friendly + enemy ants, fight.

5. A.I.
- Ant action AI's:
* -Wander
* -Queen, egg production
-Forage
-Attack
-Defend
-???
- Base design
* -simple/random
-more interesting?

6. Gameplay,
- Enemies (Pincer pit, spider, foot, lawnmower)
- Workers / Breeders / Soldiers
- Scores
- Ant combat.
- Win conditions
- levels

7. Polish
- Terrain themes (outdoors, patio, kitchen, leg, etc)
- Object models:
-dirt
-barriers
-???
- Sound Effects
- Background Music

Automated Checksums

I'm creating some checksums today for a folder full of large files.

This is the method, which I found on the ubuntu forums. Essentially md5sum is called on each file with 'find -exec' :
find -type f -exec md5sum "{}" \; > md5sum.txt

' find ' returns the contents of the directory, ' -type f ' ensures only regular files are returned, I'm not sure what the ' "{}" \;' part is for, finally I added '> md5sum.txt' to send the output to a file.

This call can take a very long time depending on the size of files it is being called on, so I checked up on it by counting the lines in md5sum.txt with the following command:
cat md5sum.txt | nl | tail -1 | cut -f 1

What this does is list the entire md5sum.txt file, count each line with 'nl', output only the last line (aka first from the end) with 'tail -1' then return only the first field of that line with 'cut -f 1'

Finally, once md5sum.txt has been generated the files can be checked using the ' -c ' flag:
md5sum -c md5sum.txt