Solved Return projectile to launcher

Question that is answered or resolved.

kimono

Well-known member
Good evening, Openbor fans;
I'd like to return a projectile to its launcher.
The projectile will return in the opposite direction and may injure the enemy.

The change of direction and the candamage changes simply by attacking it.
This is how the knife entity is made up:
Code:
name        knife
health        1
type        none
shootnum     10
candamage     player obstacle
projectilehit 0
remove         0
shadow        0

alternatepal data/chars/soldier/alt1.gif
alternatepal data/chars/soldier/alt2.gif
alternatepal data/chars/soldier/alt3.gif

##ANIMATIONS#############################################################################

anim idle
    loop    1
    delay    5
    offset    4 4
    bbox 0 0 16 5
    attack1 0 0 16 5 2 1 0 0 0 0
    frame    data/chars/weapons/knife.png
Have you ever coded something like this?
I think @Kratus did something like this in SORX.
 

Attachments

  • My Mod - 0003.png
    My Mod - 0003.png
    25 KB · Views: 4
Solution
I made the mistake of setting the ondoattackscript to Kenshiro, which I've since corrected.
The two main issues now are the knife getting stuck in the attack box and the fact that its launcher is invincible against the projectile.
Thank you for your study, and I remain at your disposal if there's anything I can do for you ;)
I was able to make it work properly, but a few changes were required in some scripts. You can download the update here but I will post the scripts to explain how it works.

First, I added some custom types in the library to manage the projectiles better.

Code:
#define PLAYER_SHOT 16384
#define ENEMY_SHOT 32768
#define REFLECTED_SHOT 65536

Then, made some changes in the projectile_return.c (don't...
Nice, I will try this. But I think its missing a change on candamage, right?
Thanks :) And yes, the candamage should be added in case it's necessary.

Also, the knife is a type_obstacle? I never understood why type_pshot were removed, since its perfect for this kind of situation.
In the SORX I defined all shared weapons as "type none" and "subtype weapon" (for melee weapons) or "subtype project" (for single use projectiles, like grenade, pepper spray and bottle). But when necessary, I change the "type" property with scripts to a custom one, like this:

Code:
//CUSTOM TYPES USED BY SOME SCRIPTS
setglobalvar("PLAYER_SHOT", 16384);
setglobalvar("NPC_SHOT", 32768);
setglobalvar("ENEMY_SHOT", 65536);
setglobalvar("HIDE_ENTITY", 131072);
setglobalvar("LEVEL_ENTITY", 262144);

changeentityproperty(vShot, "type", getglobalvar("PLAYER_SHOT"));

I changed the script as follows:
Code:
void main()
{
    void self = getlocalvar("self");
    int currentDir = getentityproperty(self, "direction");
    int newDir;

    newDir = currentDir == 1 ? 0 : 1; //DEFINE DIRECTION

    if(getlocalvar("which")){ //WORKS FOR DEFENDER
        changeopenborvariant("lasthitc", 0); //DISABLE THE COLLISION
        changeentityproperty(self, "direction", newDir); //CHANGE DIRECTION
        changeentityproperty(self, "candamage", "type_enemy", "type_obstacle"); //CHANGE CANDAMAGE ENEMY OBSTACLE
    }
}
The projectile changes direction well and damages enemies.
There are two minor issues, however:
- If you attack from the end of the attack box, the projectile is deflected, but if you make contact right in the middle, the knife gets trapped inside, bouncing back and forth.
- The initial throw is immune to the projectile.
How do I resolve these two specific issues?
I'm downloading your game and will take a look to understand better.
 
I made the mistake of setting the ondoattackscript to Kenshiro, which I've since corrected.
The two main issues now are the knife getting stuck in the attack box and the fact that its launcher is invincible against the projectile.
Thank you for your study, and I remain at your disposal if there's anything I can do for you ;)
 
I made the mistake of setting the ondoattackscript to Kenshiro, which I've since corrected.
The two main issues now are the knife getting stuck in the attack box and the fact that its launcher is invincible against the projectile.
Thank you for your study, and I remain at your disposal if there's anything I can do for you ;)
I was able to make it work properly, but a few changes were required in some scripts. You can download the update here but I will post the scripts to explain how it works.

First, I added some custom types in the library to manage the projectiles better.

Code:
#define PLAYER_SHOT 16384
#define ENEMY_SHOT 32768
#define REFLECTED_SHOT 65536

Then, made some changes in the projectile_return.c (don't forget to call it through the "ondoattack" event in the knife header).
Code:
#include "data/scripts/library/types.h"

void main()
{
    //WORKS FOR DEFENDER ONLY
    if(getlocalvar("which"))
    {
        void self = getlocalvar("self");
        void other = getlocalvar("other");
        void sType = getentityproperty(self, "type");
        void oType = getentityproperty(other, "type");
        void candamage;
        int currentDir = getentityproperty(self, "direction");
        int rightDir = 1;
        int leftDir = 0;
        int newDir;
        
        if(sType != openborconstant("TYPE_PLAYER") && sType != openborconstant("TYPE_ENEMY"))
        {
            newDir = currentDir == rightDir ? leftDir : rightDir; //DEFINE DIRECTION
            changeopenborvariant("lasthitc", 0); //DISABLE COLLISIONS
            changeentityproperty(self, "type", REFLECTED_SHOT); //CHANGE TYPE TO PREVENT MULTIHITS
            changeentityproperty(self, "direction", newDir); //CHANGE DIRECTION
            changeentityproperty(self, "owner", other); //CHANGE OWNER

            if(oType == openborconstant("TYPE_PLAYER"))
            {
                candamage = openborconstant("type_enemy")+openborconstant("type_npc")+openborconstant("type_obstacle");
            }
            else
            if(oType == openborconstant("TYPE_ENEMY"))
            {
                candamage = openborconstant("type_player")+openborconstant("type_npc")+openborconstant("type_obstacle");
            }
            changeentityproperty(self, "candamage", candamage);
        }
    }
}

Since the character header does not accept custom types, I created a scripted version that works during player character spawn in the onspawn.c file.
Code:
#include "data/scripts/library/types.h"

void main()
{
    void self = getlocalvar("self");
    void type = getentityproperty(self, "type");
    void candamage;

    if(type == openborconstant("TYPE_PLAYER"))
    {
        candamage = openborconstant("type_enemy")+openborconstant("type_npc")+openborconstant("type_obstacle")+ENEMY_SHOT;
    }
    else
    if(type == openborconstant("TYPE_ENEMY"))
    {
        candamage = openborconstant("type_player")+openborconstant("type_npc")+openborconstant("type_obstacle")+PLAYER_SHOT;
    }
    changeentityproperty(self, "candamage", candamage);
}

Kenshiro header, added a onspawnscript to apply a scripted candamage.
Code:
onspawnscript         data/scripts/onspawn.c

In addition, I added a new "shooter3" in the script library.
Code:
void shooter3(void Shot, float dx, float dy, float dz, float Vx, float Vy, float Vz)
{ // Shooting projectile with speed control
   void self = getlocalvar("self");
   void type = getentityproperty(self, "type");
   void candamage = getentityproperty(self, "candamage");
   int Direction = getentityproperty(self, "direction");
   int x = getentityproperty(self, "x");
   int y = getentityproperty(self, "a");
   int z = getentityproperty(self, "z");
   void vShot;

   if (Direction == 0){ //Is entity facing left?                 
      dx = -dx; //Reverse X direction to match facing
   }

   vShot = projectile(Shot, x+dx, z+dz, y+dy, Direction, 1, 0, 0);
   changeentityproperty(vShot, "velocity", Vx, Vz, Vy);
   changeentityproperty(vShot, "speed", Vx);
   changeentityproperty(vShot, "candamage", candamage);

   if(type == openborconstant("TYPE_PLAYER"))
   {
      changeentityproperty(vShot, "type", PLAYER_SHOT);
   }
   else
   if(type == openborconstant("TYPE_ENEMY"))
   {
      changeentityproperty(vShot, "type", ENEMY_SHOT);
   }
}

 
Solution
Well done, really, excellent work!
Setting the knife to
Code:
remove 1
also allows it to disappear on the first target hit and not go through all the enemies on the map like a missile :)
I will watch with you so that you are also rewarded as you should ;)
Thank you again for looking into this.
 
Back
Top Bottom