Thread: Generalising methods to handle multiple weapon types

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Generalising methods to handle multiple weapon types

    When my game is done, I'm going to have 6/7 different weapon types (although I may want to add new weapons in later, so I want to adopt a general technique for handling weapons in the game). At the moment, I have only lasers, and already have 8 methods in a class for that, although 6 of those 8 could be used for other weapon types aswell. So rather than having 6/7 versions of same 6 methods, to handle all the weapons, the plan was to write the code so that 6-10 methods could handle all different weapon types.

    Anyway, at present, I use linked lists for the weapons, with different linked lists for the weapon type. That way, every time a laser etc goes off the screen, its just a case of getting rid of that link in the list. But I'm having some problems generalising the code.

    These are some of the weapon types:
    Code:
    /* Laser Structure */
    struct LaserStruct
    {
           unsigned int weapon_position[2];
           unsigned int weapon_speed;
           LaserStruct *next_weapon;
    };
    
    /* Missile Structure */
    struct MissileStruct
    {
           unsigned int weapon_position[2];
           MissileStruct *next_weapon;
    };
    And this is some of the code:
    Code:
    unsigned int CGameWeapons::GetWeaponPosition(unsigned int array_idx1, void *current_node)
    {
             if( (array_idx1 < 2)&&(current_node != NULL) )
             {
                 return (current_node->weapon_position[array_idx1]);
             }
             else
             {
                 return (RETURN_ERROR);
             }
    }
    
    void CGameWeapons::SetWeaponPosition(unsigned int array_idx1, unsigned int position, void struct *current_node)
    {
         if( (current_node != NULL)&&(array_idx1 < 2) )
         {
                         current_node->weapon_position[array_idx1] = position;
         }
         
         return;
    }
    Although in order to generalise it, I had to declare the argument to the methods as void (rather than type LaserStruct or MissileStruct - which depends on the type of weapon), and because of that, when I try and compile, it says " `void*' is not a pointer-to-object type ". I understand the problem, but I'm not sure how to solve it.

    In the method declaration etc, I did try changing the pointer from type void to type void struct, although that didn't work, lol. Any ideas?
    Last edited by Swarvy; 05-21-2009 at 01:36 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why aren't those objected inherited from a base class? Then you could just pass the base weapon class around. Since you are allowed to pass a derived class to a pointer to its base, there shouldn't be a problem. Fit everything as generic as you can in a base class called 'Weapon'. 'LaseWeapon' or 'MissileWeapon' would be derived from that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by quzah View Post
    Why aren't those objected inherited from a base class? Then you could just pass the base weapon class around. Since you are allowed to pass a derived class to a pointer to its base, there shouldn't be a problem. Fit everything as generic as you can in a base class called 'Weapon'. 'LaseWeapon' or 'MissileWeapon' would be derived from that.


    Quzah.
    I agree with that.

    One slight problem with ANY weapons definition is that different types of weapons have different capabilities. If we take regular guns as an example: A shot-gun is deadly to humans at a few feet, but at 150 feet, you need a lot of luck to kill a human. On the other hand, a sniper-rifle will probably be lethal at 1500 feet. However, they also require different levels of skill. To hit a person at 20 feet with a shot-gun requires that you aim it roughly in the right direction. A sniper-rifle will require a bit more precision aiming, and to hit a person at 1500 feet is marksman-ship beyond most people.

    Other weapons have other behaviour. A heavy club may break bones and shields, but unless you hit the head of a person, it's not so likely to be fatal. A dagger is unlikely to reach through a shield, but it's almost definitely lethal if a stab on the body is achieved.

    So, the calculation of the damage done by a particular weapon will depend on the defense, skill of the user of the weapon, and skill of the counterpart (in defense), and the type of defense used (and perhaps distance).

    If we add magic, that may be used as a defense against some things, but probably not against some other things.

    And of course, in a sword fight, the sword would be both defense and weapon - so perhaps you actually need the weapon to have defensive values as well.

    How complicated and how many dimensions you give this depends on several things, but it's pretty much all about representing the capabilities, strengths and weaknesses of the weapon and defense mechanisms in a "realistic" way. And you probably need to add the players skill as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accepting Multiple Types of Input
    By Junior89 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2006, 11:25 PM
  2. multiple types in one decleration
    By hebele in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2004, 07:23 AM
  3. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM
  4. Returning multiple types w/o a struct
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2003, 11:04 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM