CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

ABSTRACT.CPP (7750B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 
     16 /* $Header:   F:\projects\c&c\vcs\code\abstract.cpv   2.20   16 Oct 1995 16:49:04   JOE_BOSTIC  $ */
     17 /***********************************************************************************************
     18  ***              C O N F I D E N T I A L  ---  W E S T W O O D  S T U D I O S               ***
     19  ***********************************************************************************************
     20  *                                                                                             *
     21  *                 Project Name : Command & Conquer                                            *
     22  *                                                                                             *
     23  *                    File Name : ABSTRACT.CPP                                                 *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 01/26/95                                                     *
     28  *                                                                                             *
     29  *                  Last Update : May 22, 1995 [JLB]                                           *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   AbstractClass::Distance -- Determines distance to target.                                 *
     34  *   AbstractTypeClass::AbstractTypeClass -- Constructor for abstract type objects.            *
     35  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     36 
     37 #include	"function.h"
     38 
     39 
     40 /***********************************************************************************************
     41  * AbstractClass::Distance -- Determines distance to target.                                   *
     42  *                                                                                             *
     43  *    This will determine the distance (direct line) to the target. The distance is in         *
     44  *    'leptons'. This routine is typically used for weapon range checks.                       *
     45  *                                                                                             *
     46  * INPUT:   target   -- The target to determine range to.                                      *
     47  *                                                                                             *
     48  * OUTPUT:  Returns with the range to the specified target (in leptons).                       *
     49  *                                                                                             *
     50  * WARNINGS:   none                                                                            *
     51  *                                                                                             *
     52  * HISTORY:                                                                                    *
     53  *   08/17/1994 JLB : Created.                                                                 *
     54  *=============================================================================================*/
     55 int AbstractClass::Distance(TARGET target) const
     56 {
     57 	/*
     58 	**	Should subtract a fudge-factor distance for building targets.
     59 	*/
     60 	BuildingClass	*obj = As_Building(target);
     61 	int dist = Distance(As_Coord(target));
     62 
     63 	/*
     64 	** If the object is a building the adjust it by the average radius
     65 	** of the object.
     66 	*/
     67 	if (obj && obj->IsActive) {
     68 		dist -= ((obj->Class->Width() + obj->Class->Height()) * (0x100 / 4));
     69 		if (dist < 0) dist = 0;
     70 	}
     71 
     72 	/*
     73 	** Return the distance to the target
     74 	*/
     75 	return(dist);
     76 }
     77 
     78 
     79 /***********************************************************************************************
     80  * AbstractTypeClass::AbstractTypeClass -- Constructor for abstract type objects.              *
     81  *                                                                                             *
     82  *    This is the constructor for AbstractTypeClass objects. It initializes the INI name and   *
     83  *    the text name for this object type.                                                      *
     84  *                                                                                             *
     85  * INPUT:   name  -- Text number for the full name of the object.                              *
     86  *                                                                                             *
     87  *          ini   -- The ini name for this object type.                                        *
     88  *                                                                                             *
     89  * OUTPUT:  none                                                                               *
     90  *                                                                                             *
     91  * WARNINGS:   none                                                                            *
     92  *                                                                                             *
     93  * HISTORY:                                                                                    *
     94  *   05/22/1995 JLB : Created.                                                                 *
     95  *=============================================================================================*/
     96 AbstractTypeClass::AbstractTypeClass(int name, char const * ini)
     97 {
     98 	Name = name;
     99 	strncpy((char *)IniName, ini, sizeof(IniName));
    100 	((char &)IniName[sizeof(IniName)-1]) = '\0';
    101 }
    102 
    103 RTTIType AbstractTypeClass::What_Am_I(void) const {return RTTI_ABSTRACTTYPE;};
    104 COORDINATE AbstractTypeClass::Coord_Fixup(COORDINATE coord) const {return coord;}
    105 int AbstractTypeClass::Full_Name(void) const {return Name;};
    106 unsigned short AbstractTypeClass::Get_Ownable(void) const {return 0xffff;};
    107 
    108 
    109 
    110 
    111 void AbstractClass::Delete_This(void)
    112 {
    113 	/*
    114 	** Apparently Watcom preserved the vtable through the virtual destructor chain, otherwise C&C would crash all the time.
    115 	**
    116 	** MSVC doesn't. It overwrites the vtable pointer with the vtable of the class currently being destructed - presumably to 
    117 	** prevent virtual functions of the classes already destructed from being called.
    118 	**
    119 	** So this is a hacky workaround to restore the original vtable pointer after the destructor chain has finished, for when the
    120 	** C&C code wants to call a virtual function on an object it just 'deleted'.
    121 	**
    122 	** ST - 1/9/2019 6:02PM
    123 	*/
    124 	unsigned long *this_ptr = (unsigned long*) this;
    125 	unsigned long vtable_ptr = *this_ptr;
    126 
    127 	/*
    128 	** delete this calls the operator delete, it doesn't actually deallocate the memory.
    129 	*/
    130 	delete this;
    131 
    132 	*this_ptr = vtable_ptr;
    133 }