Sprite Behaviours in C

The Commander Keen Community Mod!
Post Reply
lemm
Posts: 554
Joined: Sun Jul 05, 2009 12:32 pm

Sprite Behaviours in C

Post by lemm »

Hello,

It is now possible to write sprite behaviours in C. No more going through the trouble assembly to program our sprites. For this mod at least, I hope that some people with C knowledge will use this, because I sure as hell don't want to write 15 sprites worth of behaviours in assembly =).


Anyways, here is a slightly less sloppy example of the default garg movement code in C:

Code: Select all

#include "keen1.h";


extern Sprite temp_sprite;
extern long int tick_count;
extern int sprite_sync;

extern int compute_sprite_delta(void);
extern void do_fall(void);
extern int get_random(void);
extern void think_4_garg_look(void);


void garg_move() {

	if (temp_sprite.vel_y == 0 && temp_sprite.vel_x > -220 && temp_sprite.vel_x < 220) {
		if (get_random() < sprite_sync) {
			temp_sprite.think_ptr = think_4_garg_look;
			temp_sprite.var_c = 0;
		}
	}
	
	if (temp_sprite.vel_x > 0)
		temp_sprite.frame = 0x40;
	else
		temp_sprite.frame = 0x42;

	temp_sprite.frame += (int)(tick_count >> 4 & 1);
	
	do_fall();
	int csd = compute_sprite_delta();
	
	if (csd & 4)
		temp_sprite.vel_x = 0xffc4;
		
	if (csd & 1)
		temp_sprite.vel_x = 0x3C;
	
	if (csd & 2)
		temp_sprite.var_c = 0;
	else {
		int l = temp_sprite.vel_x;
		int m = (l & 0xFFFF) ^ (l >> 16);
		m -= (l >> 16);
		
		if (m == 0xDC && temp_sprite.var_c == 0) {
		
			temp_sprite.var_c = 1;
			temp_sprite.vel_y = 0xFF38;	
			
		}
	}

	return;
}
EDIT:

Here is a bare bones package if you want to try out writing stuff in C. It's still missing a few header files and the help is incomplete, but it contains a walkthrough to guide you through the process of making the garg code. You don't need any other patches to do the walkthrough.

There is one .zip; just dump all the contents into a keen v1.31 directory with ck1patch and follow the instructions. You need winXP.

http://www.mediafire.com/?tnimtdzk1mt

EDIT 2:

Slightly more user friendly. There is no need for hex-editing of object files anymore. Instead, run fixobj <object.obj> 0x#### , where 0x#### is the starting offset of your function, and then link. (It's in the instructions.)

http://www.mediafire.com/?ancwbdgyyym
Last edited by lemm on Thu Mar 04, 2010 11:24 am, edited 4 times in total.
User avatar
Fleexy
Site Admin
Posts: 490
Joined: Fri Dec 12, 2008 1:33 am
Location: Bloogton Tower

Post by Fleexy »

Nice! However I don't know one character about C/C#/C++. Do you think you could rig up something like this for QuickBASIC, GML, or VBScript? Of these I am most experienced at QB. Of course I understand if it's impossible or too hard.
Draik
Posts: 117
Joined: Sat Jul 26, 2008 8:52 am
Contact:

Post by Draik »

Those other languages I would put at unlikely at best.

On another note, woo! C is much more manageable than assembly for me, so I might pitch in here.
lemm
Posts: 554
Joined: Sun Jul 05, 2009 12:32 pm

Post by lemm »

I couldn't do it for basic, but there are basic to c translators out there which might work if you are adamantly opposed to C syntax =)

Here's one: http://www.bcxgurus.com/
Mink
Posts: 192
Joined: Sat Nov 03, 2007 4:08 pm
Location: Providence, RI, US

Post by Mink »

Amazing, Lemm! The extent to which you've improved modding is just astounding. I'm looking forward to writing some enemy behaviors.
levellass
Posts: 3001
Joined: Wed Oct 11, 2006 12:03 pm
Location: Ngaruawahia New Zealand

Post by levellass »

I can do QB or a limited amount of Assembly, those are the only two languages I understand well enough.
lemm
Posts: 554
Joined: Sun Jul 05, 2009 12:32 pm

Post by lemm »

Bamp.


Check out the new link. You can start fooling around with stuff now. Let me know if you are successful.
pizza2004
Posts: 15
Joined: Tue Jul 07, 2009 9:06 pm

Post by pizza2004 »

This is awesome, I'm going to look into using this in Commander Genius. How hard would it be to do this with the physics?
gerstrong
Posts: 63
Joined: Sun Jan 25, 2009 3:21 pm

Post by gerstrong »

This code seems to me quite similar in Commander Genius. Well it has to be... Interesting in fact
User avatar
Fleexy
Site Admin
Posts: 490
Joined: Fri Dec 12, 2008 1:33 am
Location: Bloogton Tower

Post by Fleexy »

BOOMSHAKALAKABUMP

It's been a few years, and I am just now thinking that I could actually make some use of this. However, MediaFire - being awful - decided to drop those files. Could someone please put this back up?

Thanks, lemm!
lemm
Posts: 554
Joined: Sun Jul 05, 2009 12:32 pm

Post by lemm »

I can't find it on my computer.

If the PCKF Dropbox is still up, the CKCM uses the same system to generate a patch from C code, so you could try to work backwards from that.
Post Reply