Archive

Author Archive

Scorched Earth clone

November 6th, 2009 Sune Rievers No comments

I’ve just finished a tutorial from www.riemers.net. The tutorial was about learning 2D programming in XNA, by creating a (very simplified) clone of Scorched Earth (a long time favorite of mine from a distant time :) ).

I’ve added a few features, namely:

  • Menu screen with number of players selection
  • Restart game by pressing escape
  • Xbox360 version :D
  • Vibration in Xbox360 mode
  • Optimized collision detection by checking X-values of objects before creating matrices
  • Original Scorch death/win messages
  • Computer players with multiple types of AI
  • Numerous small tweaks, including kill count, stats and support for multiple rockets

Download is available here: rievers.dk

Categories: .net, c#, xna Tags: , , , , ,

Bachelor project done

August 11th, 2009 Sune Rievers No comments

My bachelor defense, which marks the conclusion of my bachelor project, is finally over :)

Attached are my report and the slides from the presentation.

Btw, I got a 10 grade, so I’m very happy :D

Categories: diku, latex Tags: , , , ,

Dump of MS-SQL Data

June 24th, 2009 Sune Rievers No comments

Found this today: http://vyaskn.tripod.com/code.htm#inserts

Narayana Vyas Kondreddi has created a stored procedure, that dumps all data from a MS-SQL database. It works like a charm! I think this is one of the few features where MySQL is actually ahead of MS-SQL, via the mysqldump tool. This makes backup and server transitions a breeze, when dealing with MySQL, and now also MS-SQL (sort of). Only inserts are supported, and there is no checking for existing data.

The script is really easy to use, after creating the SP, you run this sql query: EXEC sp_generate_inserts 'titles' to dump all data from the ‘titles’ table. It also allows some customization, and reducing the amount of exported data, eg using sql where-statements.

To export all data from all tables in the database, run:

SELECT 'EXEC sp_generate_inserts ' +
'[' + name + ']' +
',@owner = ' +
'[' + RTRIM(USER_NAME(uid)) + '],' +
'@ommit_images = 1, @disable_constraints = 1'
FROM sysobjects
WHERE type = 'U' AND
OBJECTPROPERTY(id,'ismsshipped') = 0

The entire script is located here for MS-SQL 2005.

You would probably also need to script the db_owner user (when login is already created on the server):

CREATE USER [mydatabase] FOR LOGIN [myuser] WITH DEFAULT_SCHEMA=[dbo]
GO

EXEC sp_addrolemember 'db_owner', 'myuser'
GO
Categories: sql Tags: , , , , , ,

Recursion in T-SQL

June 19th, 2009 Sune Rievers No comments

Today I had to deal with a child-parent table with a list of user groups. These groups are nested, and persons in a subgroup should have access to all information that users of parent groups have.

I tried to do this by using a function or stored procedure, but none of these were optimal solutions. In the end, I stumbled across the WITH statement in T-SQL.

From this blog and this, I learned that it is actually pretty easy to extract the list of children, given a parent id (or vice versa).


USE tempdb;
GO

DECLARE @t TABLE (
Code INT NOT NULL UNIQUE CLUSTERED,
[Name] VARCHAR(25),
Parent INT NULL
);

INSERT INTO @t VALUES(1,      'Name1',   NULL);
INSERT INTO @t VALUES(2,      'Name2',   1);
INSERT INTO @t VALUES(3,      'Name3',   NULL);
INSERT INTO @t VALUES(4,      'Name4',   2);
INSERT INTO @t VALUES(5,      'Name5',   3);

WITH hierarchy
AS
(
SELECT Code, Parent, [Name], 0 AS lvl, CAST('\' + LTRIM(Code) + '\' AS VARCHAR(MAX)) AS [path]
FROM @t
WHERE Parent IS NULL

UNION ALL

SELECT c.Code, c.Parent, c.[Name], p.lvl + 1 AS lvl, p.[path] + CAST(LTRIM(c.Code) + '\' AS VARCHAR(MAX)) AS [path]
FROM @t AS c INNER JOIN hierarchy AS p ON c.Parent = p.Code
)
SELECT (REPLICATE(SPACE(1), lvl * 4)) + [Name]
FROM hierarchy
ORDER BY [path];
GO

Above code is from http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/865bcee0-dbe0-4a4e-98ab-82ae6c802df9

This is actually pretty sweet! With proper indexes and database structure, this runs almost as fast as when joining on the top level (root parent id)! :)

The code for the view ended up like this:


CREATE view [dbo].[GroupLevels]

as

WITH hierarchy
AS
(
SELECT departmentId, parentDepId, departmentDesc
FROM UserGroups
WHERE parentDepId IS NULL

UNION ALL

SELECT c.departmentId, c.parentDepId, c.departmentDesc
FROM UserGroups AS c INNER JOIN hierarchy AS p ON c.parentDepId = p.departmentId
)
SELECT departmentId, parentDepId
FROM hierarchy

Now I can use this simple join statement in order to join on sub groups as well (GroupLevels is my view):


SELECT * FROM
ACCESS A,
USERS U
INNER JOIN Membership M ON M.Group IN
   (SELECT departmentId FROM GroupLevels g
        WHERE g.parentDepId = A.intGroupID)
AND M.account = U.account
AND A.level = 1
Categories: sql Tags: , , ,

Source code in LaTeX

June 10th, 2009 Sune Rievers No comments

I’m about to hand in my Bachelor’s Thesis, and needed to include some source code in the report. Previously, I’ve used the following macro in the preamble:

\usepackage{moreverb}

\def\sourcetabsize{4}
\newenvironment{sourcestyle}{\begin{scriptsize}}{\end{scriptsize}}
\def\sourceinput#1{\par\begin{sourcestyle}\verbatimtabinput[\sourcetabsize]{#1}\end{sourcestyle}\par}

And then included the source files using:


\begin{figure}[h]
\sourceinput{helloworld.c}
\caption{hello world c code}
\end{figure}

This has some drawbacks, namely:

  • The appearence is sort of restriced, unless you want to manually rewrite the macro (which quickly grows out of hand)
  • Source code is limited to one page (unless you include them without the figure)
  • This seems a bit like a hack

After some googling, I found out that there is actually a package called listings, which does this in a more proper way :)

This should go in the preamble:


\usepackage{listings}
\lstset{tabsize=2,breaklines=true,breakatwhitespace=true,basicstyle=\scriptsize\tt,morecomment=[l]{//},
postbreak=\mbox{{\tiny$\hookrightarrow$}}}

More options for lstset are available in the pdf, or at this blog.

To include a source code file, type this anywhere you want it included:


\subsection*{hello world c code}
\lstinputlisting{helloworld.c}

So, don’t put stuff in figures, unless you really need to! Now I will just have to rewrite my references to my late figures…

Categories: latex Tags:

Change ownership of tables in MS-SQL

June 3rd, 2009 Sune Rievers No comments

I recently had to task of changing all tables on a MS-SQL database to a new owner (well, dbo), and then I stumbled across this blog post, quoted here:

Normally you use sp_changeobjectowner(SQL Server 2000 SP3)

Sample:
Remenber to have the right permissions. In this sample I’m running as dbo and i have logged by using sa

use Northwind
EXECUTE sp_changeobjectowner 'EmployeeTerritory', 'guest'

To take back this, the parameters change a little:

use Northwind
EXECUTE sp_changeobjectowner 'guest.EmployeeTerritory', 'dbo'

Well you must do it for all tables, but using an undocumented Stored Procedure called sp_MSforeachtable you can do this in one line of code:


use Northwind
sp_MSforeachtable @command1="sp_changeobjectowner '?', 'dbo'"

Another undocumented Stored Procedure is sp_MSforeachdb which could be used for change the owner of all Databases

All views and stored procedures must however still be changed manually, but this is far easier, as they don’t contain unscriptable data (why oh why, MS-SQL, don’t you have a dump feature like MySQL has had for years??).

Grats and thanks to Jose Blanco Muradas for finding this :)

Categories: sql Tags: , , , ,

New server

May 27th, 2009 Sune Rievers No comments

I’m soon getting a Shuttle X27D, to use as a home server/media center. I realize that the embedded Intel 950 graphics is very low-fi, so I wonder if it will be up for it… Anyway, it would be nice to have X on the tv, as I liked to keep my servers headless.

A display page, for instance tailing a few logfiles, running top etc. would definitely be a treat :)

I’m thinking about running Ubuntu server on this box, as it is more of a development and file server than a production server (and a bastion host)… Well, I’ll still have to see how Ubuntu fits into my setup!

Categories: Uncategorized Tags: , ,

Grooveshark plugin

May 27th, 2009 Sune Rievers No comments

Just found this great plugin for Wordpress: Grooveshark for Wordpress

It lets you embed music directly into your blog… nice stuff!

Example here:


Categories: music Tags: , , ,

Useful bash commands

May 14th, 2009 Sune Rievers No comments

This is a list of some very useful bash commands, along with selected parameters. I always google for this stuff when I need it, so I thought I would keep a list of my own :)

Find and delete empty (0 bytes) files

find . -type f -size 0k -exec rm {} \;

Find and delete files with a given name (for instance Thumbs.db)

find . -name "Thumbs.db" -exec rm {} \;

Move all files from subfolders to current folder
find . -type f -exec mv {} . \;

Iterate over files with a given extension

for file in `find . -name *.txt`; do
echo $file
done

Follow a growing log file

tail -f /var/log/messages

Multiple files could be listed like this, eg.
tail -f /var/log/messages /var/log/dmesg

Backup using tar (here everything in /etc)

tar -Pczf /mnt/backup/backup/etc.tar.gz /etc/*

Count number of words in LaTeX file

detex file.tex | wc -c

Find duplicate files

# Check sizes first and only checksum files of the same size
find . ! -empty -type f -printf "%s " -exec ls -dQ {} \; | \
sort -n | uniq -D -w 1 | \
cut -d" " -f2- | \
xargs md5sum | sort | \
uniq -w32 -d --all-repeated=separate | \
cut -c35-

Categories: bash Tags: ,

Latex test

May 14th, 2009 Sune Rievers No comments

weee \LaTeX!!

e^{\i \pi} + 1 = 0
Categories: latex Tags: