Use for i=1,# rather than ipairs for speed

See http://springrts.com/wiki/Lua_Performance#TEST_9:_for-loops for timing details.