home *** CD-ROM | disk | FTP | other *** search
/ Micromanía 92 / CDMM92_1.ISO / SOF 2 SDK / sof2sdk-101.msi / _92D6AC311BB48EBA344BBABC89DA6AB0 / _A2BA00A7DDC6401AA955D428C4D4D5CD < prev    next >
Encoding:
Text File  |  2002-06-05  |  13.7 KB  |  475 lines

  1. // Copyright (C) 2001-2002 Raven Software, Inc.
  2. //
  3. // cg_weaponinit.c -- weapon initialization
  4.  
  5. #include "cg_local.h"
  6. #include "../game/bg_weapons.h"
  7.  
  8. // FIMXE: This is defined in a C++ header file. 
  9. // We need to break it up or somethin so we don't have mutliple defs.
  10. #define GHOUL2_NORENDER 2
  11.  
  12. /*
  13. =================
  14. CG_RegisterItemVisuals
  15.  
  16. The server says this item is used on this level
  17. =================
  18. */
  19. void CG_RegisterItemVisuals( int itemNum ) 
  20. {
  21.     itemInfo_t        *itemInfo;
  22.     gitem_t            *item;
  23.     char            simpleName[MAX_QPATH];
  24.  
  25.     if ( itemNum < 0 || itemNum >= bg_numItems ) 
  26.     {
  27.         Com_Error( ERR_FATAL, "CG_RegisterItemVisuals: itemNum %d out of range [0-%d]", itemNum, bg_numItems-1 );
  28.     }
  29.  
  30.     itemInfo = &cg_items[ itemNum ];
  31.     if ( itemInfo->registered ) 
  32.     {
  33.         return;
  34.     }
  35.  
  36.     item = &bg_itemlist[ itemNum ];
  37.  
  38.     memset( itemInfo, 0, sizeof( &itemInfo ) );
  39.     itemInfo->registered = qtrue;
  40.  
  41.     itemInfo->models[0] = trap_R_RegisterModel( item->world_model[0] );
  42.  
  43.     // Check to see if its a ghoul model we loaded
  44.     if (!Q_stricmp(&item->world_model[0][strlen(item->world_model[0]) - 4], ".glm"))
  45.     {
  46.         trap_G2API_InitGhoul2Model(&itemInfo->g2Models[0], item->world_model[0], 0 , 0, 0, 0, 0);
  47.         itemInfo->radius[0] = 60;
  48.  
  49.         // Special case to make sure some sufaces are show that should be
  50.         switch ( item->giTag )
  51.         {
  52.             case WP_AK74_ASSAULT_RIFLE:
  53.                 trap_G2API_SetSurfaceOnOff( itemInfo->g2Models[0], 0, "bayonet_off", 0 );
  54.                 break;
  55.  
  56.             case WP_M4_ASSAULT_RIFLE:
  57.                 trap_G2API_SetSurfaceOnOff( itemInfo->g2Models[0], 0, "m203_off", 0 );
  58.                 break;
  59.         }
  60.     }
  61.  
  62.     itemInfo->icon = trap_R_RegisterShaderNoMip( item->icon );
  63.  
  64.     if (item->icon[0])
  65.     {
  66.         Com_sprintf(simpleName, sizeof(simpleName), "%s_simple", item->icon);
  67.         itemInfo->mSimpleIcon = trap_R_RegisterShader(simpleName);
  68.     }
  69.     else
  70.     {
  71.         itemInfo->mSimpleIcon = 0;
  72.     }
  73.  
  74.  
  75.     if ( item->giType == IT_WEAPON ) 
  76.     {
  77.         CG_RegisterWeapon( item->giTag );
  78.     }
  79. }
  80.  
  81. /*
  82. =================
  83. CG_RegisterWeapon
  84.  
  85. The server says this item is used on this level
  86. =================
  87. */
  88. void CG_RegisterWeapon(int weaponNum)
  89. {
  90.     weaponInfo_t        *weaponInfo;
  91.     weaponData_t        *weaponDat;
  92.     TWeaponModel        *weaponModel;
  93.     TOptionalWeapon        *optionalPart;
  94.     gitem_t                *item,*ammo;
  95.     TWeaponParseInfo    *wPI;
  96.     int                    weaponIndex;    // model index for view weapon models
  97.     int                    bufferIndex;    // weapon + buffer + rhand [+ lhand ]
  98.     int                    rHandIndex;
  99.     int                    lHandIndex;
  100.     int                    boltonIndex;
  101.     int                    boltIndex;
  102.     int                    soundSet, i;
  103.     int                    *indexes;
  104.  
  105.     if(weaponNum==0)
  106.     {
  107.         return;
  108.     }
  109.     
  110.     boltonIndex = -1;
  111.  
  112.     assert(weaponNum < WP_NUM_WEAPONS);
  113.  
  114.     weaponInfo=&cg_weapons[weaponNum];
  115.     if(weaponInfo->registered)
  116.     {
  117.         return;
  118.     }
  119.  
  120.     memset(weaponInfo,0,sizeof(*weaponInfo));
  121.  
  122.     // Register the item visuals (icons etc).
  123.     weaponInfo->item = item = BG_FindWeaponItem ( weaponNum );
  124.  
  125.     // Must be initial loading
  126.     if ( !weaponInfo->registered )
  127.     {
  128.         CG_LoadingItem( item - bg_itemlist );
  129.     }
  130.  
  131.     // Ok, successfully registered the entire weapon.
  132.     weaponInfo->registered=qtrue;
  133.  
  134.     weaponDat = &weaponData[weaponNum];
  135.  
  136.     if(item->classname)
  137.     {
  138.         // Hmmm... this needs some work... some of it looks if now.
  139.         CG_RegisterItemVisuals( item - bg_itemlist );    
  140.     }
  141.     else
  142.     {    
  143.         Com_Error( ERR_FATAL, "CG_RegisterWeapon: Couldn't find weapon item %i", weaponNum);
  144.     }
  145.  
  146.     // Register the weapon's world ammo model.
  147.     for ( ammo = bg_itemlist + 1 ; ammo->classname ; ammo++ )
  148.     {
  149.         if ( ammo->giType == IT_AMMO && ammo->giTag == weaponNum )
  150.         {
  151.             break;
  152.         }
  153.     }
  154.  
  155.     if( ammo->classname && ammo->world_model[0] )
  156.     {
  157.         trap_G2API_InitGhoul2Model(&weaponInfo->ammoG2Model,ammo->world_model[0],0,0,0,0,0);
  158.     }
  159.  
  160.     // Create the world model.
  161.     weaponIndex = trap_G2API_InitGhoul2Model(&weaponInfo->weaponG2Model,weaponDat->worldModel,0,0,0,0,0);
  162.     if(!trap_G2_HaveWeGhoul2Models(weaponInfo->weaponG2Model))
  163.     {
  164.         Com_Printf("CG_RegisterWeapon: Unable to load weapon world model: %s\n", weaponDat->worldModel);
  165.     }
  166.  
  167.     // Register the weapon model...
  168.     indexes=weaponInfo->viewG2Indexes;
  169.     for(i=0;i<8;i++)
  170.     {
  171.         indexes[i]=-1;
  172.     }
  173.  
  174.     wPI=&weaponParseInfo[weaponNum];
  175.     
  176.     // Create the view weapon model in slot 0.
  177.     indexes[0] = weaponIndex = trap_G2API_InitGhoul2Model(&weaponInfo->viewG2Model,
  178.                                                           wPI->mWeaponModel.mModel,0,0,0,0,0);
  179.  
  180.     // Now build and assemble all the other bits.
  181.     if(trap_G2_HaveWeGhoul2Models(weaponInfo->viewG2Model))
  182.     {        
  183.         // Add a bolt to the weapon so buffer can be bolted on.
  184.         boltIndex=trap_G2API_AddBolt(weaponInfo->viewG2Model,weaponIndex,wPI->mWeaponModel.mBufferBoltToBone);
  185.  
  186.         // Every view weapon has a buffer, create this in slot 1.
  187.         indexes[1]=bufferIndex=trap_G2API_InitGhoul2Model(&weaponInfo->viewG2Model,
  188.                                                           wPI->mWeaponModel.mBufferModel,
  189.                                                           weaponIndex+1,0,0,0,0);
  190.         // Bolt the buffer to the weapon.
  191.         trap_G2API_AttachG2Model(weaponInfo->viewG2Model,bufferIndex, 
  192.                                  weaponInfo->viewG2Model,boltIndex,weaponIndex);
  193.  
  194.         // Right hand??
  195.         if(wPI->mWeaponModel.mRightHandsBoltToBone[0])
  196.         {
  197.             // Add a right hand bolt to the buffer.
  198.             boltIndex=trap_G2API_AddBolt(weaponInfo->viewG2Model,bufferIndex,
  199.                                          wPI->mWeaponModel.mRightHandsBoltToBone);
  200.  
  201.             // Right hand.. create this now in slot 2.
  202.             indexes[2]=rHandIndex=trap_G2API_InitGhoul2Model(&weaponInfo->viewG2Model,
  203.                                                               "models/weapons/rhand/rhand.glm",
  204.                                                              weaponIndex+2,0,0,0,0);
  205.             
  206.             // Bolt the right hand to the buffer.
  207.             trap_G2API_AttachG2Model(weaponInfo->viewG2Model,rHandIndex,weaponInfo->viewG2Model,
  208.                                      boltIndex,bufferIndex);
  209.         }
  210.  
  211.         // Left hand??
  212.         if(wPI->mWeaponModel.mLeftHandsBoltToBone[0])
  213.         {
  214.             // Add a left hand bolt to the buffer.
  215.             boltIndex=trap_G2API_AddBolt(weaponInfo->viewG2Model,bufferIndex,
  216.                                          wPI->mWeaponModel.mLeftHandsBoltToBone);
  217.  
  218.             // Left hand.. create this now in slot 3.
  219.             indexes[3]=lHandIndex=trap_G2API_InitGhoul2Model(&weaponInfo->viewG2Model,
  220.                                                             "models/weapons/lhand/lhand.glm",
  221.                                                             weaponIndex+3,0,0,0,0);
  222.  
  223.             // Bolt the left hand to the buffer.
  224.             trap_G2API_AttachG2Model(weaponInfo->viewG2Model,lHandIndex,weaponInfo->viewG2Model,
  225.                                      boltIndex,bufferIndex);
  226.         }
  227.  
  228.         // Boltons like the M4's grenade launcher?
  229.         if(wPI->mWeaponModel.mBolton)
  230.         {
  231.             // Add a bolton bolt to the buffer.
  232.             boltIndex=trap_G2API_AddBolt(weaponInfo->viewG2Model,bufferIndex,
  233.                                          wPI->mWeaponModel.mBolton->mBoltToBone);
  234.  
  235.             // Bolton.. create this now in slot 4.
  236.             indexes[4]=boltonIndex=trap_G2API_InitGhoul2Model(&weaponInfo->viewG2Model,
  237.                                                               wPI->mWeaponModel.mBolton->mModel,
  238.                                                               weaponIndex+4,0,0,0,0);
  239.             // Bolt the bolton to the buffer.
  240.             if ( boltonIndex != -1 )
  241.             {
  242.                 trap_G2API_AttachG2Model(weaponInfo->viewG2Model,boltonIndex,weaponInfo->viewG2Model,
  243.                                          boltIndex,bufferIndex);
  244.             }
  245.         }
  246.  
  247.         // Turn of any weapon surfaces that are never seen.
  248.         weaponModel=&wPI->mWeaponModel;
  249.         i=0;
  250.         while(weaponModel->mRightSideSurfaces[i])
  251.         {
  252.             trap_G2API_SetSurfaceOnOff(weaponInfo->viewG2Model,weaponInfo->viewG2Indexes[0],
  253.                                        weaponModel->mRightSideSurfaces[i],GHOUL2_NORENDER);
  254.             i++;
  255.         }
  256.         i=0;
  257.         while(weaponModel->mFrontSurfaces[i])
  258.         {
  259.             trap_G2API_SetSurfaceOnOff(weaponInfo->viewG2Model,weaponInfo->viewG2Indexes[0],
  260.                                        weaponModel->mFrontSurfaces[i],GHOUL2_NORENDER);
  261.             i++;
  262.         }
  263.         if(weaponModel->mBolton && boltonIndex != -1 )
  264.         {
  265.             i=0;
  266.             while(weaponModel->mBolton->mRightSide[i])
  267.             {
  268.                 trap_G2API_SetSurfaceOnOff(weaponInfo->viewG2Model,boltonIndex,
  269.                                            weaponModel->mBolton->mRightSide[i],GHOUL2_NORENDER);
  270.                 i++;
  271.             }
  272.         }
  273.         
  274.         // Turn off optional parts as we don't want em.
  275.         optionalPart=weaponModel->mOptionalList;
  276.         while(optionalPart)
  277.         {
  278.             // No real way of knowing what single player intended so we have to
  279.             // hard-code 'em.
  280.             if((!strcmp(optionalPart->mName,"lasersight"))||
  281.                (!strcmp(optionalPart->mName,"silencer"))||
  282.                (!strcmp(optionalPart->mName,"utl")))
  283.             {
  284.                 i=0;
  285.                 while(optionalPart->mSurfaces[i])
  286.                 {
  287.                     trap_G2API_SetSurfaceOnOff(weaponInfo->viewG2Model,weaponInfo->viewG2Indexes[0],
  288.                                                optionalPart->mSurfaces[i],GHOUL2_NORENDER);
  289.                     i++;
  290.                 }
  291.             }
  292.             optionalPart=optionalPart->mNext;
  293.         }
  294.     }
  295.     else
  296.     {
  297.         Com_Printf("CG_RegisterWeapon: Unable to load weapon view model: %s\n", wPI->mWeaponModel.mModel);
  298.     }
  299.  
  300.     weaponInfo->attack[ATTACK_NORMAL].missileTrailFunc = CG_ProjectileThink;
  301.     weaponInfo->attack[ATTACK_ALTERNATE].missileTrailFunc = CG_ProjectileThink;
  302.  
  303.     // Register weapon list menu icons.
  304.     if (weaponDat->menuImage[0])
  305.     {
  306.         weaponInfo->weaponIcon = trap_R_RegisterShader( va( "gfx/menus/%s", weaponDat->menuImage) );
  307.     }
  308.  
  309.     // Register the alternate weapon ammo icon for the hud
  310.     if (weaponDat->attack[ATTACK_ALTERNATE].icon[0] )
  311.     {
  312.         weaponInfo->attack[ATTACK_ALTERNATE].ammoIcon = trap_R_RegisterShader( va( "gfx/menus/%s", weaponDat->attack[ATTACK_ALTERNATE].icon ) );
  313.     }
  314.  
  315.     // Register attack stuff
  316.     for ( i = ATTACK_NORMAL; i < ATTACK_MAX; i ++ )
  317.     {
  318.         attackData_t*    attackData = &weaponDat->attack[i];
  319.         attackInfo_t*    attackInfo = &weaponInfo->attack[i];
  320.  
  321.         // Primary muzzle-flash.
  322.         if (attackData->muzzleEffect[0])
  323.             attackInfo->muzzleEffect = trap_FX_RegisterEffect(attackData->muzzleEffect);
  324.         if (attackData->muzzleEffectInWorld[0])
  325.             attackInfo->muzzleEffectInWorld = trap_FX_RegisterEffect(attackData->muzzleEffectInWorld);
  326.         if (!attackInfo->muzzleEffectInWorld)
  327.             attackInfo->muzzleEffectInWorld = attackInfo->muzzleEffect;
  328.  
  329.         // Primary shell eject.
  330.         if (attackData->shellEject[0])
  331.             attackInfo->shellEject = trap_FX_RegisterEffect(attackData->shellEject);
  332.         if (attackData->tracerEffect[0])
  333.             attackInfo->tracerEffect = trap_FX_RegisterEffect(attackData->tracerEffect);
  334.  
  335.         // Primary explosion.
  336.         if (attackData->explosionEffect[0])
  337.             attackInfo->explosionEffect = trap_FX_RegisterEffect(attackData->explosionEffect);
  338.         if (attackData->explosionSound[0])
  339.             attackInfo->explosionSound = trap_S_RegisterSound(attackData->explosionSound);
  340.         if (attackData->missileG2Model[0])
  341.             trap_G2API_InitGhoul2Model(&attackInfo->missileG2Model,attackData->missileG2Model,0,0,0,0,0);
  342.  
  343.         // Add the bolts for muzzle flash and shell eject onto the view model.
  344.         if (trap_G2_HaveWeGhoul2Models(weaponInfo->viewG2Model))
  345.         {
  346.             // shell eject bone.
  347.             if ( attackData->ejectBone[0] )
  348.             {
  349.                 attackInfo->shellEjectBoltView = 
  350.                     trap_G2API_AddBolt(weaponInfo->viewG2Model, 1, attackData->ejectBone);    
  351.             }
  352.  
  353.             // Muzzle flash bone.
  354.             attackInfo->muzzleFlashBoltView = -1; 
  355.             if(attackData->muzzleEffect[0])
  356.             {
  357.                 if ( attackData->muzzleEffectBone[0] )
  358.                 {
  359.                     attackInfo->muzzleFlashBoltView = 
  360.                         trap_G2API_AddBolt(weaponInfo->viewG2Model, 1, attackData->muzzleEffectBone );
  361.                 }
  362.                 else if (wPI->mWeaponModel.mBufferMuzzle[0])
  363.                 {
  364.                     attackInfo->muzzleFlashBoltView = 
  365.                         trap_G2API_AddBolt(weaponInfo->viewG2Model, 1, wPI->mWeaponModel.mBufferMuzzle);
  366.                 }
  367.             }
  368.         }
  369.  
  370.         // Add the bolts for muzzle flash and shell eject onto the world model.
  371.         if (trap_G2_HaveWeGhoul2Models(weaponInfo->weaponG2Model))
  372.         {
  373.             // shell eject bone.
  374.             if (attackData->ejectBone[0])
  375.             {    
  376.                 attackInfo->shellEjectBoltWorld = 
  377.                     trap_G2API_AddBolt(weaponInfo->weaponG2Model, 0, attackData->ejectBone);    
  378.             }
  379.             else
  380.             {
  381.                 attackInfo->shellEjectBoltWorld = -1;
  382.             }
  383.  
  384.             // Muzzle flash bone.
  385.             attackInfo->muzzleFlashBoltWorld = -1;
  386.             if ( attackData->muzzleEffectBone[0] )
  387.             {
  388.                 attackInfo->muzzleFlashBoltWorld = 
  389.                     trap_G2API_AddBolt(weaponInfo->weaponG2Model, 0, attackData->muzzleEffectBone );
  390.             }
  391.             else if (wPI->mWeaponModel.mBufferMuzzle[0])
  392.             {
  393.                 attackInfo->muzzleFlashBoltWorld = 
  394.                     trap_G2API_AddBolt(weaponInfo->weaponG2Model, 0, wPI->mWeaponModel.mBufferMuzzle);
  395.             }
  396.         }
  397.     }
  398.  
  399.     // Register sounds for weapon.
  400.     for (soundSet = 0; soundSet < MAX_WEAPON_SOUNDS; soundSet++)
  401.     {
  402.         if (Q_stricmp(wPI->mSoundNames[soundSet], "fire")==0)
  403.         {
  404.             for (i=0; i<MAX_WEAPON_SOUND_SLOTS; i++)
  405.             {
  406.                 if (wPI->mSounds[soundSet][i][0])
  407.                 {
  408.                     weaponInfo->attack[ATTACK_NORMAL].flashSound[i] = trap_S_RegisterSound(wPI->mSounds[soundSet][i]);
  409.                 }
  410.             }
  411.         }
  412.         else if (Q_stricmp(wPI->mSoundNames[soundSet], "altfire")==0)
  413.         {
  414.             for (i=0; i<MAX_WEAPON_SOUND_SLOTS; i++)
  415.             {
  416.                 if (wPI->mSounds[soundSet][i][0])
  417.                     weaponInfo->attack[ATTACK_ALTERNATE].flashSound[i] = trap_S_RegisterSound(wPI->mSounds[soundSet][i]);
  418.             }
  419.         }
  420.         else
  421.         {
  422.             // All other sounds.
  423.             for (i=0; i<MAX_WEAPON_SOUND_SLOTS; i++)
  424.             {
  425.                 if (wPI->mSounds[soundSet][i][0])
  426.                     weaponInfo->otherWeaponSounds[soundSet][i]=trap_S_RegisterSound(wPI->mSounds[soundSet][i]);
  427.             }
  428.         }
  429.     }
  430.  
  431.     // Special hard coded stuff
  432.     switch ( weaponNum )
  433.     {
  434.         case WP_KNIFE:
  435.         {
  436.             attackInfo_t* attackInfo = &weaponInfo->attack[ATTACK_ALTERNATE];
  437.             attackInfo->missileSound = trap_S_RegisterSound ( "sound/weapons/knife/throw_loop" );
  438.             break;
  439.         }
  440.  
  441.         case WP_RPG7_LAUNCHER:
  442.         {
  443.             attackInfo_t* attackInfo = &weaponInfo->attack[ATTACK_NORMAL];
  444.             attackInfo->missileSound = trap_S_RegisterSound ( "sound/weapons/rpg7/flyby" );
  445.             break;
  446.         }            
  447.     }    
  448. }
  449.  
  450. /*
  451. =================
  452. CG_ShutDownWeapons
  453.  
  454. Clean out any g2 models
  455. =================
  456. */
  457. void CG_ShutDownWeapons ( void )
  458. {
  459.     int                i;
  460.     attackType_t    a;
  461.  
  462.     for (i=0; i < WP_NUM_WEAPONS; i++)
  463.     {    
  464.         // free all ghoul2 models
  465.         trap_G2API_CleanGhoul2Models(&cg_weapons[i].weaponG2Model);
  466.         trap_G2API_CleanGhoul2Models(&cg_weapons[i].viewG2Model);
  467.         trap_G2API_CleanGhoul2Models(&cg_weapons[i].ammoG2Model);
  468.  
  469.         for ( a = ATTACK_NORMAL; a < ATTACK_MAX; a ++ )
  470.         {
  471.             trap_G2API_CleanGhoul2Models(&cg_weapons[i].attack[a].missileG2Model);
  472.         }
  473.     }
  474. }
  475.