cssdm_weapons.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jun 25 19:26:00 2007 +0000 (2007-06-25)
changeset 4 6db3288d2e63
parent 0 67dcd379a2d5
child 7 1107793f4b1c
permissions -rw-r--r--
- cleaned up some confusing translations phrases
- removed unused struct members from dm_weapon_t
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * By David "BAILOPAN" Anderson
     6  * All rights reserved.
     7  * ===============================================================
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or (at
    12  * your option) any later version.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but
    15  * WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17  * General Public License for more details.
    18  * 
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; see the file COPYING; if not, write to the
    21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    22  * MA 02110-1301 USA
    23  * 
    24  * Version: $Id$
    25  */
    26 
    27 #include "cssdm_weapons.h"
    28 #include "cssdm_headers.h"
    29 #include "sm_trie.h"
    30 #include <sh_vector.h>
    31 
    32 using namespace SourceHook;
    33 
    34 Trie *g_pWeaponLookup = NULL;
    35 CVector<dm_weapon_t *> g_Weapons;
    36 
    37 dm_weapon_t *DM_FindWeapon(const char *name)
    38 {
    39 	dm_weapon_t *wp = NULL;
    40 	if (!sm_trie_retrieve(g_pWeaponLookup, name,  (void **)&wp))
    41 	{
    42 		return NULL;
    43 	}
    44 	return wp;
    45 }
    46 
    47 dm_weapon_t *DM_GetWeapon(unsigned int index)
    48 {
    49 	if (index >= g_Weapons.size())
    50 	{
    51 		return NULL;
    52 	}
    53 
    54 	return g_Weapons[index];
    55 }
    56 
    57 char *DM_CopyString(const char *str)
    58 {
    59 	size_t length = strlen(str);
    60 	char *ptr = new char[length+1];
    61 	strcpy(ptr, str);
    62 	return ptr;
    63 }
    64 
    65 bool DM_ParseWeapons(char *error, size_t maxlength)
    66 {
    67 	char path[PLATFORM_MAX_PATH];
    68 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "cfg/cssdm/cssdm.weapons.txt");
    69 
    70 	KeyValues *kv = new KeyValues("Weapons");
    71 	if (!kv->LoadFromFile(basefilesystem, path))
    72 	{
    73 		kv->deleteThis();
    74 		snprintf(error, maxlength, "Could not load weapons file \"%s\"", error);
    75 		return false;
    76 	}
    77 
    78 	KeyValues *weapons = kv->FindKey("cstrike", false);
    79 	if (!weapons)
    80 	{
    81 		kv->deleteThis();
    82 		snprintf(error, maxlength, "Could not find \"cstrike\" section in weapons file");
    83 		return false;
    84 	}
    85 
    86 	g_pWeaponLookup = sm_trie_create();
    87 
    88 	for (weapons = weapons->GetFirstTrueSubKey(); weapons != NULL; weapons = weapons->GetNextTrueSubKey())
    89 	{
    90 		dm_weapon_t *wp = new dm_weapon_t;
    91 
    92 		/* Deal with section name */
    93 		char name[64];
    94 		snprintf(name, sizeof(name), "weapon_%s", weapons->GetName());
    95 		wp->classname = DM_CopyString(name);
    96 
    97 		/* Deal with other strings */
    98 		wp->display = DM_CopyString(weapons->GetString("name", weapons->GetName()));
    99 
   100 		const char *type = weapons->GetString("type", "");
   101 		wp->type = WeaponType_Invalid;
   102 		if (strcmp(type, "primary") == 0)
   103 		{
   104 			wp->type = WeaponType_Primary;
   105 		} else if (strcmp(type, "secondary") == 0) {
   106 			wp->type = WeaponType_Secondary;
   107 		} else if (strcmp(type, "grenade") == 0) {
   108 			wp->type = WeaponType_Grenade;
   109 		} else if (strcmp(type, "c4") == 0) {
   110 			wp->type = WeaponType_C4;
   111 		}
   112 
   113 		if (wp->type != WeaponType_Invalid)
   114 		{
   115 			wp->id = (int)g_Weapons.size();
   116 			sm_trie_insert(g_pWeaponLookup, weapons->GetName(), wp);
   117 			g_Weapons.push_back(wp);
   118 		}
   119 	}
   120 
   121 	kv->deleteThis();
   122 
   123 	return true;
   124 }
   125 
   126 void DM_FreeWeapons()
   127 {
   128 	for (size_t i=0; i<g_Weapons.size(); i++)
   129 	{
   130 		dm_weapon_t *wp = g_Weapons[i];
   131 		delete [] wp->classname;
   132 		delete [] wp->display;
   133 		delete wp;
   134 	}
   135 	g_Weapons.clear();
   136 
   137 	if (g_pWeaponLookup)
   138 	{
   139 		sm_trie_destroy(g_pWeaponLookup);
   140 		g_pWeaponLookup = NULL;
   141 	}
   142 }