sword/fist fighting a.i code....

Posted By: Blink

sword/fist fighting a.i code.... - 08/01/08 15:24

ok...i was working on converting/combining two a5 scripts to a6 that were contributions from another site. i was able to get it running with no errors, but my dilemma is, when the main character approaches the enemy model, the enemy turns away, and then follows the player model around. i dont understand what's wrong. i am no coder, so can one of the great code masters please look at this and tell me whats wrong? i would like to contribute this to the community so us coding noobs can have a easy a.i. to play with.

Code:
//sword/////
////////////////////////////////////////////////////////////

 

var video_mode = 7; // 800x600

var video_depth = 32; // 16 bit mode

 

var player_distance; // moves the player

var enemy_distance; // moves the enemy

 

define sword_base = skill12; // using skill12, 13, 14 to store the sword_base coords

define sword_tip = skill15; // using skill15, 16, 17 to store the sword_tip coords

define healthpoints = skill18; // just another name for skill18

font standard_font = <ackfont.pcx>, 6, 9; // panel font



//////////////////////////////////////////////////////////////

 

bmap spark_map = <spkpart.pcx>;// for spark particles

bmap blood_map = <bloodspray.pcx>; // for blood particles

 

//////////////////////////////////////////////////////////////

 

sound beep_sound = msg.wav;

sound sword_snd = knife.wav;

 

//////////////////////////////////////////////////////////////

 

function particle_sparks();

function particle_blood();

function fade_particle();

 

//////////////////////////////////////////////////////////////

 

string sword_combat_wmb =  testswordlvl.wmb ;

string health_str = "STRENGTH: ";

 

///////////////////////////////////////////////////////////////

 

entity* enemy; // useful only if you want to display its health

 

///////////////////////////////////////////////////////////////

 

panel health_panel // displays the numbers

{

pos_x = 0;

pos_y = 0;

digits =120, 575, 4, standard_font, 1, player.healthpoints;

flags = refresh, visible;

}

text health_text // displays the text

{

pos_x = 30;

pos_y = 550;

font = standard_font;

string = health_str;

flags = visible;

}

 

////////////////////////////////////////////////////////////////

 

function main()

{

level_load (sword_combat_wmb);

wait (2); // wait for the level to be loaded

clip_size = 0; // show all the triangles for all the models

on_d = null; // disable the debug panel (key "D") because it is used for movement

fps_max = 40; // lock the frame rate to 40 fps

}

 

action player_fight // attached to the player

{

player = me; // I'm the player

player.healthpoints = 100; // and I have 100 health points

while (player.healthpoints > 0) // as long as I'm alive

{

camera.x = player.x - 200 * cos(player.pan); // 200 = distance between the player and the camera

camera.y = player.y - 200 * sin(player.pan); // same value here

camera.z = player.z + 50; // above the player

camera.pan = player.pan; // looks in the same direction with the player

camera.tilt = -10; // look down at the player

 

my.pan +=0 * (key_a - key_d) * time_step - 20 * mouse_force.x * time_step; // rotates with the keys A and D or with the mouse

 

 

 

player_distance.x = 10 * (key_w - key_s) * time_step; // moves forward / backward with W / S

 

player_distance.y = 5 * (key_a - key_d) * time_step; // move side to side

player_distance.z = 0;

 

if ((key_w == 1) || (key_s == 1)) || ((key_a == 1) || (key_d == 1)) // the player is walking

{

ent_animate("walk", my.skill20); // play walk frames animation

my.skill20 += 4 * time_step; // "walk" animation speed

if (my.skill20 > 100) {my.skill20 = 0;} // loop animation

}

 

else // the player is standing

{

ent_animate("stand", my.skill21); // play stand frames animation

my.skill21 += 2 * time_step; // "stand" animation speed

if (my.skill21 > 100) {my.skill21 = 0;} // loop animation

}

 

ent_move(player_distance, nullvector);

 

if (mouse_left == 1)// if we press the left mouse button

{

my.skill22 = 0; // reset "attack" frames

while (my.skill22 < 100)

{

ent_vertex(my.sword_tip, 601); // sword tip vertex coords - get the value in Med

ent_vertex(my.sword_base, 5); // sword base vertex coords - get the value in Med

trace_mode = ignore_me + ignore_passable; // ignore me (the player) and all the entities that are passable

trace (my.sword_base, my.sword_tip); // trace between these sword positions

if (result != 0) // the player has hit something

{

effect (particle_sparks, 10, target, normal);

if (you != null) {you.healthpoints -= 6 * time_step;} // if it hasn't hit a wall, decrease health

ent_playsound (my, sword_snd, 50); // sword sound

}

ent_animate("attack", my.skill22); // play attack frames animation

my.skill22 += 17 * time_step; // "attack" animation speed

wait (1);

}

while (mouse_left == 1) {wait (1);} // can't use autofire on a sword

}

wait (1);

}

while (my.skill23 < 90) // the player is dead

{

ent_animate("death", my.skill23); // play death frames animation

my.skill23 += 3 * time_step; // "death" animation speed

wait (1);

}

my.passable = on; // the corpse can't be hit by the enemy sword from now on

}

 

action enemy_fight // attached to the enemy

{

enemy = me; // I'm the enemy

enemy.healthpoints = 20; // and I have 100 healthpoints

while (my.healthpoints > 0) // as long as I'm alive

{

if (vec_dist (my.x, player.x) < 600 && player.healthpoints > 0) // the player approaches the enemy

{

vec_set(temp, player.x);

vec_sub(temp, my.x);

vec_to_angle(my.pan, temp);

my.tilt = 0; // I'm a maniac

enemy_distance.x = 5 * time_step;

enemy_distance.y = 0;

enemy_distance.z = 0;

ent_move(enemy_distance, nullvector);

ent_animate("walk", my.skill19); // play walk frames animation

my.skill19 += 10 * time_step; // "walk" animation speed

if (my.skill19 > 100) {my.skill19 = 0;} // loop animation

if (vec_dist (my.x, player.x) < 50) // if the player comes closer than 50 quants attack him

{

my.skill20 = 0; // reset "attack" frames

while (my.skill20 < 100)

{

ent_vertex(my.sword_tip, 2124); // sword tip vertex coords - get the value in Med

ent_vertex(my.sword_base, 517); // sword base vertex coords - get the value in Med

trace_mode = ignore_me + ignore_passable;

trace (my.sword_base, my.sword_tip); // trace between these sword positions

if (result != 0) // hit something, could be the player or another enemy

{

effect (particle_blood, 2, target, normal);

if (you != null) {you.healthpoints -= 0.5 * time_step;}

ent_playsound (my, sword_snd, 50); // sword sound

}

ent_animate("attack", my.skill20); // play attack frames animation

my.skill20 += 5 * time_step; // "attack" animation speed

wait (1);

}

wait (-6); // slows down the enemy and reduces the number of traces per second

}

}

else // the player is farther than 200 quants away

{

ent_animate("stand", my.skill21); // play stand frames animation

my.skill21 += 2 * time_step; // "stand" animation speed

if (my.skill21 > 100) {my.skill21 = 0;} // loop animation

}

wait (1);

}

while (my.skill22 < 80) // the enemy is dead

{

ent_animate("death", my.skill22); // play death frames animation

my.skill22 += 1 * time_step; // "death" animation speed

wait (1);

}

my.passable = on; // the corpse can't be hit by the sword from now on

}

 

 

 

function particle_sparks()

{

temp.x = random(2) - 1;

temp.y = random(2) - 1;

temp.z = random(1) - 1.5;

vec_add (my.vel_x, temp);

my.alpha = 30 + random(50);

my.bmap = spark_map;

my.size = 5;

my.flare = on;

my.bright = on;

my.move = on;

my.lifespan = 20;

my.function = fade_particle;

my.gravity =0;

my.beam = on;

my.streak = on;

 

}

 

 

function particle_blood()

{

temp.x = random(2) - 1;

temp.y = random(2) - 1;

temp.z = random(1) - 1.5;

vec_add (my.vel_x, temp);

my.alpha = 70 + random(30);

my.bmap = blood_map;

my.size = 6;

my.flare = on;

my.bright = on;

my.move = on;

my.lifespan = 20;

my.function = fade_particle;

}

 

function fade_particle()

{

my.alpha -= 5 * time_step;

if (my.alpha < 0) {my.lifespan = 0;}

}
 



Thanks in advance, Blink.
Posted By: TigerTao

Re: sword/fist fighting a.i code.... - 08/01/08 20:56

So the enemy turns away from you and then moves backwards to the player?

Could be you have your enemy model facing in the wrong x axis in MED.

Just as an experiment, after vec_to _angle put this in

my.pan += 180;

and change enemy_distance.x from 5 to -5

Thats not a fix by the way.
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/02/08 15:21

didnt work, thanks Tiger...

anyone else have any ideas?
Posted By: TigerTao

Re: sword/fist fighting a.i code.... - 08/02/08 16:39

Well I tested your code and my enemy is facing the player and moving to him.

The only thing I had to add was a wait in the enemy code until the player was created, I only took out the animation code and particle code.

Heres the whittled down code I used

Code:
var video_mode = 7; // 800x600
var video_depth = 32; // 16 bit mode
var player_distance; // moves the player
var enemy_distance; // moves the enemy

define healthpoints = skill18; // just another name for skill18

entity* enemy; // useful only if you want to display its health


function main()

{
	level_load("level.wmb");

	wait (2); // wait for the level to be loaded

	clip_size = 0; // show all the triangles for all the models
	on_d = null; // disable the debug panel (key "D") because it is used for movement
	fps_max = 40; // lock the frame rate to 40 fps
}



action player_fight // attached to the player

{
	player = me; // I'm the player

	player.healthpoints = 100; // and I have 100 health points

	while (player.healthpoints > 0) // as long as I'm alive
	{
		camera.x = player.x - 200 * cos(player.pan); // 200 = distance between the player and the camera
		camera.y = player.y - 200 * sin(player.pan); // same value here
		camera.z = player.z + 50; // above the player
		camera.pan = player.pan; // looks in the same direction with the player
		camera.tilt = -10; // look down at the player

		my.pan +=0 * (key_a - key_d) * time_step - 20 * mouse_force.x * time_step; // rotates with the keys A and D or with the mouse

		player_distance.x = 10 * (key_w - key_s) * time_step; // moves forward / backward with W / S
		player_distance.y = 5 * (key_a - key_d) * time_step; // move side to side
		player_distance.z = 0;

		ent_move(player_distance, nullvector);
		wait (1);
	}
}



action enemy_fight // attached to the enemy

{
	while(player == NULL){wait(1);}
	
	enemy = me; // I'm the enemy
	enemy.healthpoints = 20; // and I have 100 healthpoints

	while (my.healthpoints > 0) // as long as I'm alive
	{
		if (vec_dist (my.x, player.x) < 600 && player.healthpoints > 0) // the player approaches the enemy
		{
			vec_set(temp, player.x);
			vec_sub(temp, my.x);
			vec_to_angle(my.pan, temp);

			my.tilt = 0; // I'm a maniac

			enemy_distance.x = 5 * time_step;
			enemy_distance.y = 0;
			enemy_distance.z = 0;

			ent_move(enemy_distance, nullvector);
		}
		wait (1);
	}
}


Does it still do the same thing using this code?
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/03/08 03:28

no, it changed, i just had to adjust my enemy character a bit in med, but he comes directly for the player. now i just have to re-insert the movement lines to see if he will attack, and they will be able to actually fight. have you attempted this yet, Tiger?
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/03/08 04:11

oh my...i tried to insert the movement scripts, and royal screwed it up, errors galore!!!! let me know if you tried it, Tiger. thanks for your efforts.
Posted By: MDMDFSS

Re: sword/fist fighting a.i code.... - 08/04/08 18:34

what for movement scripts??? smile
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/04/08 19:50

@ MDMDFSS: Not sure what you are asking.
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/05/08 15:13

still having difficulty, anyone else have any ideas?
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/15/08 12:23

//
Code:
/*
blinkSword.wdl
2008.08.15
tD
pseudo-auto generated from src: Blink
incomplete: died of boredom
untested; probably errors
already have similar but more advanced (sword fighting)
	available upon request
	working on model parts right now
*/

var video_mode = 7; // 800x600
var video_depth = 32; // 16 bit mode

var pl_vDist[3]; // moves the player
var ai_nDist; // moves the enemy

define _wpBase, skill12; 			
define _wpTip, skill15; 				
define _hp, skill18;
define _st1, skill21;
define _st2, skill22;
define _anip, skill23;	

		
font standard_font = <ackfont.pcx>, 6, 9; // panel font
/*******************************
images
*******************************/
bmap spark_map = <spkpart.pcx>;			// for spark particles
bmap blood_map = <bloodspray.pcx>; /		/ for blood particles
/*******************************
sounds
*******************************/
sound beep_sound = <msg.wav>;
sound sword_snd = <knife.wav>;

/*******************************
animation strings
*******************************/

var _STAND = 0;
var _WALK = 1;
var _ATK1 = 2;
var _DEAD = 3;
TEXT ani_t1 {
	strings  = 4;
	string = "stand";
	string = "walk";
	string = "attack";
	string = "death";
}

/*******************************
function prototypes
*******************************/
function particle_sparks();
function particle_blood();
function fade_particle();

string lvl_s1 =  "testswordlvl.wmb";	// level string
string hp_s1 = "STRENGTH: ";		// hitpoints string

ENTITY* ai_e1; 	// useful only if you want to display its health

var k_st[512];
var k_ml = 280; 	// mouse left
var k_mr = 281; 
var k_mm = 282; 

panel hp_p1 {  // sux compared to alternatives
	pos_x = 0;
	pos_y = 0;
	digits =120, 575, 4, standard_font, 1, player._hp;
	flags = refresh, visible;
}

text hp_t1 { 	// sux compared to alternatives
	pos_x = 30;
	pos_y = 550;
	font = standard_font;
	string = hp_s1;
	flags = visible;
}


/*******************************
main
*******************************/
function main() {
	level_load (lvl_s1);
	wait (2); // wait for the level to be loaded
	clip_size = 0; // show all the triangles for all the models
	on_d = null; // disable the debug panel (key "D") because it is used for movement
	fps_max = 40; // lock the frame rate to 40 fps
}

action player_fight { // attached to the player
	player = ME; // I'm the player
	player._hp = 100; // and I have 100 health points

	while (player._hp > 0) { // as long as I'm alive
		// camera
		camera.x = player.x - 200 * cos(player.pan); // 200 = distance between the player and the camera
		camera.y = player.y - 200 * sin(player.pan); // same value here
		camera.z = player.z + 50; // above the player
		camera.pan = player.pan; // looks in the same direction with the player
		camera.tilt = -10; // look down at the player
		
		//move
		my.pan +=0 * (key_a - key_d) * time_step - 20 * mouse_force.x * time_step; // rotates with the keys A and D or with the mouse
		pl_vDist.x = 10 * ((key_w || cuu) - (key_s || cud)) * time_step; // moves forward / backward with W / S
		pl_vDist.y = 5 * ((key_a || cul) - (key_d || cur)) * time_step; // move side to side
		pl_vDist.z = 0;
		var pl_bWalk; pl_bWalk = (abs(pl_vDist.x) > 0) || (abs(pl_vDist.y) > 0); 
		if (pl_bWalk) { // the player is walking
			my._st1 = _WALK;	// reset ani
			if (my._st1 != my._st2) { my._anip = 0; }
			ent_animate(ani_t1.string[my._st1], my._anip, ANM_CYCLE); // play walk frames animation
			my._anip += 4 * time_step; // "walk" animation speed
			my._anip %= 100;
		} else {  // not walking
			my._st1 = _STAND
			if (my._st1 != my._st2) { my._anip = 0; }	// reset ani
			ent_animate(ani_t1.string[my._st1, my._anip, ANM_CYCLE); // play stand frames animation
			my._anip += 2 * time_step; // "stand" animation speed
			my._anip %= 100;
		}
		ent_move(pl_vDist, nullvector);

		if (mouse_left && k_st[k_mm]) {// if we press the left mouse button
			my._anip = 0; // reset "attack" frames
			while (my._anip < 100) {
				ent_vertex(my._wpTip, 601); // sword tip vertex coords - get the value in Med
				ent_vertex(my._wpBase, 5); // sword base vertex coords - get the value in Med
				trace_mode = ignore_me + ignore_passable; // ignore me (the player) and all the entities that are passable
				result = trace (my._wpBase, my._wpTip); // trace between these sword positions

				if (result != 0) { // the player has hit something
					effect (particle_sparks, 10, target, normal);
					if (you != null) {you._hp -= 6 * time_step;} // if it hasn't hit a wall, decrease health
					ent_playsound (my, sword_snd, 50); // sword sound
				}
				my._st1 = _ATK1;
				if (my._st1 != my._st2) { my._anip = 0; }	// reset ani
				ent_animate(ani_t1.string[my._st1], my._anip); // play attack frames animation
				my._anip += 17 * time_step; // "attack" animation speed
				my._st2 = my._st1;
				
				wait (1);
			}
			k_st[k_mm] = !mouse_left; // can't use autofire on a sword
		}
		my._st2 = my._st1;
		wait (1);
	}
	my._st1 = _DEAD;
	my._anip = 0;
	while (my._anip < 90) { // the player is dead
		ent_animate(ani_t1.string[my._anip], my._anip); // play death frames animation
		my._anip += 3 * time_step; // "death" animation speed
		wait (1);
	}
	my.passable = on; // the corpse can't be hit by the enemy sword from now on
}



action enemy_fight { // attached to the enemy
	ai_e1 = me; // I'm the enemy

	ai_e1._hp = 20; // and I have 100 _hp

	while (my._hp > 0)  { // as long as I'm alive
		if (vec_dist (my.x, player.x) < 600 && player._hp > 0)  {// the player approaches the enemy
			vec_diff(temp, player.x, my.x);
			vec_to_angle(my.pan, temp);
			my.tilt = 0; // I'm a maniac
			
			ai_nDist.x = 5 * time_step;
			ai_nDist.y = 0;
			ai_nDist.z = 0;
			my._st1 = _WALK;
			if (my._st1 != my._st2) { my._anip = 0; }
			ent_move(ai_nDist, nullvector);
			ent_animate(ani_t1.string[my._st1], my.skill19); // play walk frames animation
			my._anip += 10 * time_step; // "walk" animation speed
			my._anip %= 100;

			if (vec_dist (my.x, player.x) < (my.max_x * 1.10)) { // if the player comes closer than 50 quants attack him 
				my._st1 = _ATK1;
				my._anip = 0; // reset "attack" frames
				while (my._anip < 100){
					ent_vertex(my._wpTip, 2124); // sword tip vertex coords - get the value in Med
					ent_vertex(my._wpBase, 517); // sword base vertex coords - get the value in Med
					trace_mode = ignore_me + ignore_passable;
					result = trace (my._wpBase, my._wpTip); // trace between these sword positions
					if (result != 0) { // hit something, could be the player or another enemy
						effect (particle_blood, 2, target, normal);
						if (you != null) {you._hp -= 0.5 * time_step;}
						ent_playsound (my, sword_snd, 50); // sword sound
					}
					ent_animate(ani_t.string[my._st1], my._anip); // play attack frames animation
					my._anip += 5 * time_step; // "attack" animation speed
					wait (1);
				}
				wait (-6); // slows down the enemy and reduces the number of traces per second
			}

		} else { // the player is farther than 200 quants away
			my._st1 = _STAND;
			if (my._st1 != my._st2) { my._anip = 0; }
			ent_animate(ani_t1.string[my._anip], my._anip, ANM_CYCLE); // play stand frames animation
			my._anip += 2 * time_step; // "stand" animation speed
			my._anip %= 100;
		}
		my._st2 = my._st1;
		wait (1);
	}

	while (my._anip < 80) {// the enemy is dead
		my._st1 = _DEAD
		ent_animate(ani_t1.string[my._st1], my._anip); // play death frames animation
		my._anip += 1 * time_step; // "death" animation speed
		wait (1);
	}
	my.passable = on; // the corpse can't be hit by the sword from now on
}
function particle_sparks() {
	temp.x = random(2) - 1;
	temp.y = random(2) - 1;
	temp.z = random(1) - 1.5;
	vec_add (my.vel_x, temp);
	my.alpha = 30 + random(50);
	my.bmap = spark_map;
	my.size = 5;
	my.flare = on;
	my.bright = on;
	my.move = on;
	my.lifespan = 20;
	my.function = fade_particle;
	my.gravity =0;
	my.beam = on;
	my.streak = on;
}
function particle_blood() {
	temp.x = random(2) - 1;
	temp.y = random(2) - 1;
	temp.z = random(1) - 1.5;
	vec_add (my.vel_x, temp);
	my.alpha = 70 + random(30);
	my.bmap = blood_map;
	my.size = 6;
	my.flare = on;
	my.bright = on;
	my.move = on;
	my.lifespan = 20;
	my.function = fade_particle;
}

function fade_particle() {
	my.alpha -= 5 * time_step;
	if (my.alpha < 0) {my.lifespan = 0;}
}
// 


Quote:
still having difficulty, anyone else have any ideas?

Yes. Instead, just use 'my' scripts which do something similar but more.
(available upon request)
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/17/08 01:26


Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/17/08 04:11

i would love to have your script instead tD.
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/17/08 04:32

the code you posted gave errors, so please post the one you offered by request.
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/17/08 13:22

It's a series of scripts.
That being the case, it isn't easily pluggable into other 'architectures'.
The whole project should be accessible, bit-by-bit, under item _dd1, from this link:
3DGS_index.html

using slooooooooow dial-up connection right now
If 'you' see ads on the page, 'your' ad blocker is not configured properly. wink
Posted By: TigerTao

Re: sword/fist fighting a.i code.... - 08/17/08 15:17

I downloaded the .7z files but only getting corrupted files when extracting.

Any chance of putting these particular files up again?

Cheers.
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/17/08 17:51

The server might inject ads into non-zip files, or files with unknown extensions.
7z just compresses better sometimes.

.zip versions of the files should be up now.
core.zip was updated

1. extract core.zip (just overwrite, if appropriate)
2. extract all other files into folder _dd1 (dir from core.zip)
3. optionally modify ack.bat so the path points to the location of acknex.exe
4a. double click ack.bat if step 3 was used
4b. else: load main1.wdl into SED, make SED point to it, and execute

version A6.60 only
Should work on Win98 with modified .dll.
Don't expect a lot.

Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/18/08 04:40

thanks for the downloads, but the images, models, levels, and plugins links are dead.
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/18/08 14:05

The links have been updated.
models.zip has been updated.
The following files have been downloaded from page 3DGS_index.html , and verified:

images.zip
levels.zip
models.zip
plugins.zip
sounds.zip

Next, 'I' may try and emulate some or most A6 Template functionality in my own way.
The result should be freely available.
'I' intend to use the result 'myself', thus some support may be implied, for some length of time.
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/18/08 22:53

i guess its not meant to work for me. i am having issues with files in a dll not found. this is way too complicated for me, as usual,lol. thanks for the effort, tD.
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/19/08 02:23

ack.bat was missing, but the files were extracted per the instructions, 'I' believe, and it worked fine here.
(core.zip was updated to include ack.bat, an updated readme, and a settings.ini file with a greater resolution setting.)

If 'you' don't feel that it can work there, or don't want to bother with it, that is fine, but it did take a bit of effort to put it up.
If 'you' wouldn't mind offering a bit more about what error was produced, that would be grand.
It would be nice to know a bit about what issues are found on machines outside 'our' scope for this type of 'production'.
If that is just too much trouble to bother with, that is fine.

From the updated _dd1-readme.txt:

following .dlls were in 3DGS root install dir:
*d3dx9_30.dll (DirectX .dll shipped with acknex engine?)
---(move to _dd1 dir, if errors about missing)
*mfc71.dll (7.10.3077.0; M$ C++ Runtime)
*msvcp71.dll (7.10.3077.0; M$ C++ Runtime)
*msvcr71.dll (7.10.3052.4; M$ C++ Runtime)
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/19/08 08:33

thanks tD, i will give it a try and let you know what the results are.
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/19/08 13:38

Information about any errors produced on other systems would be greatly appreciated.

In the meantime, an attempt will be made to refactor 'your' original code (above or page n back) or a derivative thereof, so that no errors are produced.
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/19/08 15:34

'Your' original code has been successfully refactored!
It's working, slimmer, and more flexible.
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/19/08 18:00

This is 'your' thread.
If issue feedback is / was desired, a separate thread would have been more appropriate.

There are three parts (files):


rsr1.wdl
Code:
font standard_font = <ackfont.pcx>, 6, 9; // panel font
/*******************************
	images
*******************************/
bmap imSpark = <spkpart.pcx>;			// for spark particles
bmap imBlood = <bloodspray.pcx>;  // for blood particles
/*******************************
	sounds
*******************************/
sound sdBeep = <msg.wav>;
sound sdSword = <knife.wav>;

/*******************************
levels
*******************************/
STRING lvl_s1 =  <testswordlvl1.wmb>;	// level string


sword-fight.wdl
Code:
/*
sword-fight.wdl
2008.08.15
tD
pseudo-auto generated from src: Blink
---trimmed!?
---made more flexible!?
---but no additional functionality added;
*/

/*******************************
	skills
*******************************/
define _wpBase, skill12; 	// vertex sword base (override)
define _wpTip, skill15; 	// vertex sword tip (override)
define _hp, skill18;			// hit points
define _st1, skill21;
define _anip, skill23;		// ani percent
define _atkTm, skill24;		// not active, gave problems

/*******************************
	animation strings
*******************************/

var _STAND = 0;
var _WALK = 1;
var _ATK1 = 2;
var _DEAD = 3;

// for dummies (comment these out)
TEXT ani_t1 {
	strings = 4;
	string = "standB";
	string = "walkA";
	string = "slashR";
	string = "dieA";
}
// below are the original ani strings
// uncomment these
/*
TEXT ani_t1 {
	strings  = 4;
	string = "stand";
	string = "walk";
	string = "attack";
	string = "death";
}*/
var ani_nSpAtk = 5;		// ani sp attack
var ani_nSpDie = 3;		// ani sp die
var ani_nSpStand = 2;	// ani sp stand
var ani_nSpWalk = 4;	// ani sp walk




var pl_vDist[3]; // moves the player
var pl_nHpD = 100;

string hp_s1 = "STRENGTH: ";		// hitpoints string


/*******************************
	ai
*******************************/
ENTITY* ai_e1; 			//  
var ai_vDist[3]; // moves the enemy

var ai_nAlertDist = 5;	// alert distance; multiple of entity size x
var ai_nAtkDist = 1.5;	// attack distance; multiple of entity size x
var ai_nHpD = 20;				// default enemy hit points
/*******************************
	weapon
*******************************/
// originals; uncomment these
//var wp_nBaseD = 601;	// vertex sword base default
//var wp_nTipD = 5;			// vertex sword tip default

// for dummies; comment these out
var wp_nBaseD = 138;
var wp_nTipD = 145;


var wp_nResult = 0;				// a temp for wp trace result
var wp_nSwordDamage = 10;	// sword damage multiplier


panel hp_p1 {  // sux compared to alternatives
	pos_x = 0;
	pos_y = 0;
	digits =120, 575, 4, standard_font, 1, player._hp;
	flags = refresh, visible;
}

text hp_t1 { 	// sux compared to alternatives
	pos_x = 30;
	pos_y = 550;
	font = standard_font;
	string = hp_s1;
	flags = visible;
}


/*******************************
	temps
*******************************/
var v1[3];
var v2[3];



/*******************************
	function prototypes
*******************************/
function particle_sparks();
function particle_blood();
function fade_particle();


/*******************************
	cam3f_1
		camera 3rd person function 
		player only for now
*******************************/
function cam3f_1() {
	// camera
	camera.x = player.x - 200 * cos(player.pan); // 200 = distance between the player and the camera
	camera.y = player.y - 200 * sin(player.pan); // same value here
	camera.z = player.z + 50; // above the player
	camera.pan = player.pan; // looks in the same direction with the player
	camera.tilt = -10; // look down at the player
}
/*******************************
	plf_input
		player input function
*******************************/
function plf_input() {
	my.pan += -20 * mouse_force.x * time_step; // rotates with the mouse
	pl_vDist.x = 10 * ((key_w || key_cuu) - (key_s || key_cud)) * time_step; // moves forward / backward with W / S
	pl_vDist.y = 5 * ((key_a || key_cul) - (key_d || key_cur)) * time_step; // move side to side
	pl_vDist.z = 0;
}
/*******************************
zotf_ani
	zot ~= actor
*******************************/
function zotf_ani(_nAniSp, _bLoop) {
	ent_animate(me, ani_t1.string[my._st1], my._anip, ANM_CYCLE);
	my._anip += _nAniSp;
	if (_bLoop) {
		my._anip %= 100;
		return(0); 
	} else {
		my._anip = min(my._anip, 100);
	}
	if (my._anip >= 100) {
		return(100);
	}
}
/*******************************
	zotf_die
*******************************/
function zotf_die() {
	zotf_st(_DEAD);
	while (my._anip < 90) { // the player is dead
		zotf_ani(ani_nSpDie, 0);
		wait (1);
	}
	my.passable = ON; // the corpse can't be hit by the enemy sword from now on
}
/*******************************
	zotf_st
		reset ani on state change
*******************************/
function zotf_st(_st) {
	if (my._st1 == _st) {
		return(0);
	}
	my._st1 = _st;
	my._anip = 0;
	return(1);
}
/*******************************
	zotf_new
*******************************/
function zotf_new() {
	c_setminmax(me);
	wait(1);
	my._wpBase = wp_nBaseD;
	my._wpTip = wp_nTipD;
}
/*******************************
	wpf_trace
		weapon trace function
*******************************/
function wpf_trace() {
	vec_for_vertex(v1, ME, my._wpBase); // sword tip vertex coords - get the value in Med
	vec_for_vertex(v2, ME, my._wpTip); // sword base vertex coords - get the value in Med
	trace_mode = ignore_me + ignore_passable; // ignore me (the player) and all the entities that are passable
	wp_nResult = trace(v1, v2); // trace between these sword positions

	if (wp_nResult != 0) { // the player has hit something
		effect (particle_sparks, 10, target, normal);
		if (you != null) {you._hp -= wp_nSwordDamage * time_step;} // if it hasn't hit a wall, decrease health
			ent_playsound (me, sdSword, 500); // sword sound
		}
	}
}
/*******************************
	player_fight
		pl_a1
		player action
*******************************/
action player_fight { // attached to the player
	player = ME; // I'm the player
	player._hp = pl_nHpD; // and I have 100 health points
	zotf_new();
	while (ME != NULL) { // as long as I'm alive
		if (my._hp <= 0) { break; }
		plf_input();
		//move
		cam3f_1();

		
		var pl_bWalk; pl_bWalk = (abs(pl_vDist.x) > 0) || (abs(pl_vDist.y) > 0); 
		if (pl_bWalk) { // the player is walking
			zotf_st(_WALK);
			zotf_ani(ani_nSpWalk, 1);
		} else {  // not walking
			zotf_st(_STAND);
			zotf_ani(ani_nSpStand, 1);
		}
		ent_move(pl_vDist, nullvector);

		if (mouse_left) {// if we press the left mouse button
			zotf_st(_ATK1);
			while (my._anip < 100) {
			
				zotf_ani(ani_nSpAtk, 0);
				wpf_trace();
				wait (1);
			}
			//k_st[k_ml] = !mouse_left; // can't use autofire on a sword
		}
		str_for_num(hp_s1, my._hp);
		wait (1);
	}
	zotf_die();

}

/*******************************
	enemy_fight
		ai_a1
		enemy / ai action
*******************************/
action enemy_fight { // attached to the enemy
	ai_e1 = me; // I'm the enemy
	ai_e1._hp = ai_nHpD; // and I have 100 _hp
	zotf_new();
	while (ME != NULL)  { // as long as I'm alive
		if (my._hp <= 0) { break; }
		if ((vec_dist (my.x, player.x) < ((my.max_x - my.min_x) * ai_nAlertDist))
		 && player._hp > 0)  {// the player approaches the enemy
			vec_diff(temp, player.x, my.x);
			vec_to_angle(my.pan, temp);
			my.tilt = 0; // I'm a maniac
			
			ai_vDist.x = 5 * time_step;
			ai_vDist.y = 0;
			ai_vDist.z = 0;
			zotf_st(_WALK);
			ent_move(ai_vDist, nullvector);
			zotf_ani(ani_nSpWalk, 1);
			if (vec_dist (my.x, player.x) < (my.max_x * ai_nAtkDist)) { // if the player comes closer than 50 quants attack him 
				zotf_st(_ATK1);
				while (my._anip < 100){
					//if (my._atkTm <= 0) {
						wpf_trace();
						//my._atkTm = 16 * 6;
					//}
					zotf_ani(ani_nSpAtk, 0);
					wait (1);
				}
			}

		} else { // the player is farther than 200 quants away
			zotf_st(_STAND);
			zotf_ani(ani_nSpStand, 1);
		}
		wait (1);
	}
	zotf_die();
}
function particle_sparks() {
	temp.x = random(2) - 1;
	temp.y = random(2) - 1;
	temp.z = random(1) - 1.5;
	vec_add (my.vel_x, temp);
	my.alpha = 30 + random(50);
	my.bmap = imSpark;
	my.size = 5;
	my.flare = on;
	my.bright = on;
	my.move = on;
	my.lifespan = 20;
	my.function = fade_particle;
	my.gravity =0;
	my.beam = on;
	my.streak = on;
}
function particle_blood() {
	temp.x = random(2) - 1;
	temp.y = random(2) - 1;
	temp.z = random(1) - 1.5;
	vec_add (my.vel_x, temp);
	my.alpha = 70 + random(30);
	my.bmap = imBlood;
	my.size = 6;
	my.flare = on;
	my.bright = on;
	my.move = on;
	my.lifespan = 20;
	my.function = fade_particle;
}

function fade_particle() {
	my.alpha -= 5 * time_step;
	if (my.alpha < 0) {my.lifespan = 0;}
}


main1.wdl
Code:

path "images";
path "models";
path "sounds";


include <rsr1.wdl>;
include <sword-fight.wdl>;


var video_mode = 7; // 800x600
var video_depth = 32; // 16 bit mode


function main() {
	level_load(lvl_s1);
	wait(2); // wait for the level to be loaded
	clip_size = 0; // show all the triangles for all the models
	on_d = null; // disable the debug panel (key "D") because it is used for movement
	fps_max = 40; // lock the frame rate to 40 fps
	wait(1);
}


see comments.
Right about now would be a good time to complain about browser-specific code text box copy-n-paste functionality!?
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/19/08 21:19

i tested, but i got a bunch of errors. i had to add the font, so that error was fixed, and then when the level ran, i got this error in a dialog box:

Empty Pointer in enemy fight:

((vec_dist(my.x,player.x)<((my.max_x-my.min_x)*ai_nAlertDist))&&player._hp>0)
Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/19/08 21:20

i am still running A6.60, if that helps at all.
Posted By: testDummy

Re: sword/fist fighting a.i code.... - 08/19/08 21:44

Perhaps, if the player is added first in WED, before the enemies, the error is not produced.
Regardless, there should be a check against NULL for the player pointer.
trade out the entire function:
Code:
/*******************************
	enemy_fight
		ai_a1
		enemy / ai action
*******************************/
action enemy_fight { // attached to the enemy
	ai_e1 = me; // I'm the enemy
	my._hp = ai_nHpD; // and I have 100 _hp
	zotf_new();
	while (ME != NULL)  { // as long as I'm alive
		if (my._hp <= 0) { break; }
		if (player == NULL) { goto(fNullPlayer); }
		if ((vec_dist (my.x, player.x) < ((my.max_x - my.min_x) * ai_nAlertDist))
		 && player._hp > 0)  {// the player approaches the enemy
			vec_diff(temp, player.x, my.x);
			vec_to_angle(my.pan, temp);
			my.tilt = 0; // I'm a maniac
			
			ai_vDist.x = 5 * time_step;
			ai_vDist.y = 0;
			ai_vDist.z = 0;
			zotf_st(_WALK);
			ent_move(ai_vDist, nullvector);
			zotf_ani(ani_nSpWalk, 1);
			if (vec_dist (my.x, player.x) < (my.max_x * ai_nAtkDist)) { // if the player comes closer than 50 quants attack him 
				zotf_st(_ATK1);
				while (my._anip < 100){
					//if (my._atkTm <= 0) {
						wpf_trace();
						//my._atkTm = 16 * 6;
					//}
					zotf_ani(ani_nSpAtk, 0);
					wait (1);
				}
			}

		} else { // the player is farther than 200 quants away
			zotf_st(_STAND);
			zotf_ani(ani_nSpStand, 1);
		}
		fNullPlayer:
		wait (1);
	}
	zotf_die();
}

If the player pointer is NULL, or becomes NULL, execution should skip to label fNullPlayer, over the rest of the code, then to wait(1), and back to the while evaluation.
This should remove the error.
Perhaps, that 'solution' is superior to a one time check (while(player == NULL) { wait(1); }).
Quote:
i had to add the font, so that error was fixed

The font had to be added to the images folder here also.
The source of the original snippet in this thread, probably wasn't found here.
It was assumed, that resource file and others were already added there.
If error solved, well done, all the same.



Posted By: Blink

Re: sword/fist fighting a.i code.... - 08/19/08 23:33

works perfectly! wow, how many times have you saved me from myself, trying to write a script and failing,lol? i really appreciate your efforts as usual, and already have you down for credits in my other projects. you are amazing, tD.
© 2024 lite-C Forums