Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AbrahamR, AndrewAMD, ozgur), 763 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
How to make a turret scan and shoot the first entity on sight? #314544
03/09/10 09:23
03/09/10 09:23
Joined: Mar 2010
Posts: 3
D
duracellrabbid Offline OP
Guest
duracellrabbid  Offline OP
Guest
D

Joined: Mar 2010
Posts: 3
Hi, I am new to Lite-C and 3DGS. How do I code a turret to scan and shoot the first entity it detected on sight?

Any advice would be appreciated.

Re: How to make a turret scan and shoot the first entity on sight? [Re: duracellrabbid] #314568
03/09/10 12:00
03/09/10 12:00
Joined: Apr 2009
Posts: 69
Karlsruhe, Baden Württemberg, ...
luckyxxl Offline
Junior Member
luckyxxl  Offline
Junior Member

Joined: Apr 2009
Posts: 69
Karlsruhe, Baden Württemberg, ...
c_scan: look the manual


Sry for my bad English!! I'm German!!
A7 Extra Edition
Intel Core i7-960 (4x3.2GHz), 8GB DDR3, 9800GT 1GB
My homepage: www.ultitech.de
Re: How to make a turret scan and shoot the first entity on sight? [Re: luckyxxl] #314595
03/09/10 15:36
03/09/10 15:36
Joined: Aug 2007
Posts: 1,922
Schweiz
Widi Offline
Serious User
Widi  Offline
Serious User

Joined: Aug 2007
Posts: 1,922
Schweiz
In the AUM they have some example of that

Re: How to make a turret scan and shoot the first entity on sight? [Re: luckyxxl] #314600
03/09/10 16:16
03/09/10 16:16
Joined: Dec 2008
Posts: 1,660
North America
Redeemer Offline
Serious User
Redeemer  Offline
Serious User

Joined: Dec 2008
Posts: 1,660
North America
You'll need to look into state machines for this. Those are the most basic AI structures in game programming, yet they are the most useful and are definitely applicable in your situation.

Here, I'll walk you through what needs to be done. laugh

Let's start at the beginning. We want the turret to sit in the same place, turning back and forth looking for entities. When it finds one, we want the turret to switch to "attack mode" and fire at the entity until it disappears again, at which point the turret switches back to "search mode". How can we turn this idea into code?

Well, there are some very important functions (computer instructions) we need to know about that comes with Gamestudio: c_scan() and c_trace(). The first one lets you cast a "vision cone" from an entity to look for other entities. The second one lets you trace an invisible line from one location to another, detecting if there are any obstacles in the way. Why do we need the second function, you might ask? Well, c_scan doesn't take collision into account when it casts a "vision cone" from an entity. So if we don't use c_trace, we can see through walls!

So what do we need to do? First, you need to have the turret c_scan() its area for any entities. If it finds one, execute a c_trace() instruction from the turret to the entity to be sure that the entity is visible from the turret's point of view. (so the turret doesn't see through walls!) If the c_trace instruction returns a success, then activate the turret's "attack mode". How do we do this? Well, like I said, we need a state machine. wink

In case you haven't read up on state machines yet (you really should!) state machines are basically where you write code that has different "modes" an entity can be in: for example, "wait" mode, "attack" mode, "hunt" mode, and even "die" mode. How do we know which mode to be in at one time? Well, you can store which mode an entity is in by using skills, like this:

Code:
// these are "function prototypes"
// they are fleshed out (initialized) below
function wait_state();
function attack_state();
function die_state();

action state_machine()
{
  my.SKILL1 = 1; // "wait" mode

  while( my ) // while I exist!
  {
    switch( my.SKILL1 )
    {
      case 1: wait_state(); break;
      case 2: attack_state(); break;
      case 3: die_state(); break;
    }

    wait(1);
  }
}

function wait_state()
{
  // do some waiting stuff!
}
function attack_state()
{
  // do some attacking stuff!
}
function die_state()
{
  // do some dying stuff!
}



So, by changing the value of "my.SKILL1" above, you can change what the entity with "action state_machine()" does!

Great! But, how exactly do we know when to change states? Remember, we must use c_scan() and c_trace() for that. In wait_state(), just have the turret do some c_scan() instructions. But how do we know when c_scan() found anything? We can use another new thing: events!

Events are another very useful thing that comes with Lite-C. I suggest you read up on that (you'll find them in the manual) since they are best covered there laugh

I'll show you a practical example of how to use events:
Code:
function event_see()
{
  if( event_type == EVENT_DETECT )
  {
    if( your != NULL ) // Just as a precaution, so the next instruction doesn't crash
      c_trace( my.x, your.x, IGNORE_PASSABLE|IGNORE_ME ); // trace a line from me to the entity I saw with "c_scan()"
    if( your != NULL ) // I have a direct line of sight to the entity I saw with c_scan()!
      my.SKILL1 = 2; // go to "attack mode!"
  }
}

action spot_you()
{
  my.emask |= EVENT_DETECT; // if this isn't here, EVENT_DETECT won't be recognized!
  my.event = event_see;
  my.SKILL1 = 1; // put me in "search mode"
  while( my ) // only while I exist!
  {
    if( my.SKILL1 == 1 ) // search mode
    {
      c_scan( my.x, my.pan, scan[0], IGNORE_ME|SCAN_ENTS );
    }
    if( my.SKILL1 == 2 ) // attack mode
    {
      // do some shooting!
    }

    wait(1);
  }
}



In this code, an entity with action "spot_you()" scans for entities using c_scan(). We can figure out if it found anything with events. If it found anything, use a c_trace instruction to make sure we have a clear line of sight between our spot_you() entity and the entity we found. If we have a clear line of sight, go to attack mode!

So, we now know how to make the turret sit still and look for entities, and if it finds one, we can do something to it (perhaps shoot it? You can use "bullet" entities or c_trace for that). With this knowledge we could probably even make the turret blow up or something when it runs out of health by going to a "die" mode! But, we are forgetting something: how do we make the turret turn back and forth? Well, this is a very simple thing to do. Just change it's pan value (or use c_rotate, but changing the pan value is easier and we don't need to use c_rotate since the turret isn't moving anyway):

Code:
action turning_entity()
{
  while( my )
  {
    my.pan += time_step; // Turn the entity around

    wait(1);
  }
}



Ok, so now we should know everything we need to make a turret that turns and looks for enemies. I'll let you come up with the final code that puts all these ideas together, it shouldn't be too hard. wink

However, there are a few things you will need to add to the code, like:
-control how far the turret turns (hint: use skills!)
-make it shoot entities when it sees them (the easiest thing would be to use ent_create() to make some bullet entities, which fly through the air until they hit something)
-make it blow up when it goes to "die" mode!

Remember: the 3DGS manual is your greatest resource. Get used to using the "search" function in your help program to look for articles on "c_scan", "c_trace", events, skills, and others. If you're having trouble tackling the basics of Lite-C, check out the AUMs, workshops, and online tutorials (they'll help you a lot!) And remember: if you have any other questions that can't be answered anywhere else, don't hesitate to put them down here. wink


Eats commas for breakfast.

Play Barony: Cursed Edition!
Re: How to make a turret scan and shoot the first entity on sight? [Re: duracellrabbid] #315802
03/19/10 06:02
03/19/10 06:02
Joined: Mar 2010
Posts: 3
D
duracellrabbid Offline OP
Guest
duracellrabbid  Offline OP
Guest
D

Joined: Mar 2010
Posts: 3
hi,
thanks for the reply. but i managed to make it work with c_scan and c_trace and my turrets can track my player. However, I have a few queries regarding that:
1) how do I check for the distance between the turret and the player? am I supposed to use vec_dist?
2) how do I make the turret know which angle to rotate? i tried to use this:
Code:
angle = acos(vec_dot(v1,v2)/(vec_length(v1)*vec_length(v2)));


but it doesn't work.

Any advice would be appreciated

Re: How to make a turret scan and shoot the first entity on sight? [Re: duracellrabbid] #315807
03/19/10 08:37
03/19/10 08:37
Joined: Feb 2009
Posts: 2,154
Damocles_ Offline
Expert
Damocles_  Offline
Expert

Joined: Feb 2009
Posts: 2,154
Here, from the manual:

Code:
function turn_towards_target()
{
  // get the direction from the entity MY to the entity YOU
  vec_set(temp,your.x); 
  vec_sub(temp,my.x);
  vec_to_angle(my.pan,temp); // now MY looks at YOU
}



Re: How to make a turret scan and shoot the first entity on sight? [Re: Damocles_] #316200
03/22/10 10:41
03/22/10 10:41
Joined: Mar 2010
Posts: 3
D
duracellrabbid Offline OP
Guest
duracellrabbid  Offline OP
Guest
D

Joined: Mar 2010
Posts: 3
Thanks,
but what is temp? is temp a zero vector? In order to check the distance between my turret and the entity, should I be using vec_dist? I used vec_dist, but it does not seems to work.

Re: How to make a turret scan and shoot the first entity on sight? [Re: duracellrabbid] #316203
03/22/10 11:59
03/22/10 11:59
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
'temp' is any vector. All it does it temporarly hold information, to be used in another calculation. 'vec_dist' will give you the distance between 2 entities, yes.

Btw, 'c_trace' returns the distance to a hit entity.

Last edited by DJBMASTER; 03/22/10 12:03.

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1