cssdm_weapons.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jun 25 23:30:11 2007 +0000 (2007-06-25)
changeset 8 fdcccf33a7cf
parent 7 1107793f4b1c
child 32 9a3116df1513
permissions -rw-r--r--
fixed translation file not being loaded
     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 <string.h>
    28 #include "cssdm_weapons.h"
    29 #include "cssdm_headers.h"
    30 #include "sm_trie.h"
    31 #include <sh_vector.h>
    32 
    33 using namespace SourceHook;
    34 
    35 Trie *g_pWeaponLookup = NULL;
    36 CVector<dm_weapon_t *> g_Weapons;
    37 
    38 dm_weapon_t *DM_FindWeapon(const char *name)
    39 {
    40 	dm_weapon_t *wp = NULL;
    41 	if (!sm_trie_retrieve(g_pWeaponLookup, name,  (void **)&wp))
    42 	{
    43 		return NULL;
    44 	}
    45 	return wp;
    46 }
    47 
    48 dm_weapon_t *DM_GetWeapon(unsigned int index)
    49 {
    50 	if (index >= g_Weapons.size())
    51 	{
    52 		return NULL;
    53 	}
    54 
    55 	return g_Weapons[index];
    56 }
    57 
    58 char *DM_CopyString(const char *str)
    59 {
    60 	size_t length = strlen(str);
    61 	char *ptr = new char[length+1];
    62 	strcpy(ptr, str);
    63 	return ptr;
    64 }
    65 
    66 bool DM_ParseWeapons(char *error, size_t maxlength)
    67 {
    68 	char path[PLATFORM_MAX_PATH];
    69 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "cfg/cssdm/cssdm.weapons.txt");
    70 
    71 	KeyValues *kv = new KeyValues("Weapons");
    72 	if (!kv->LoadFromFile(basefilesystem, path))
    73 	{
    74 		kv->deleteThis();
    75 		snprintf(error, maxlength, "Could not load weapons file \"%s\"", error);
    76 		return false;
    77 	}
    78 
    79 	KeyValues *weapons = kv->FindKey("cstrike", false);
    80 	if (!weapons)
    81 	{
    82 		kv->deleteThis();
    83 		snprintf(error, maxlength, "Could not find \"cstrike\" section in weapons file");
    84 		return false;
    85 	}
    86 
    87 	g_pWeaponLookup = sm_trie_create();
    88 
    89 	for (weapons = weapons->GetFirstTrueSubKey(); weapons != NULL; weapons = weapons->GetNextTrueSubKey())
    90 	{
    91 		dm_weapon_t *wp = new dm_weapon_t;
    92 
    93 		/* Deal with section name */
    94 		char name[64];
    95 		snprintf(name, sizeof(name), "weapon_%s", weapons->GetName());
    96 		wp->classname = DM_CopyString(name);
    97 
    98 		/* Deal with other strings */
    99 		wp->display = DM_CopyString(weapons->GetString("name", weapons->GetName()));
   100 
   101 		const char *type = weapons->GetString("type", "");
   102 		wp->type = WeaponType_Invalid;
   103 		if (strcmp(type, "primary") == 0)
   104 		{
   105 			wp->type = WeaponType_Primary;
   106 		} else if (strcmp(type, "secondary") == 0) {
   107 			wp->type = WeaponType_Secondary;
   108 		} else if (strcmp(type, "grenade") == 0) {
   109 			wp->type = WeaponType_Grenade;
   110 		} else if (strcmp(type, "c4") == 0) {
   111 			wp->type = WeaponType_C4;
   112 		}
   113 
   114 		if (wp->type != WeaponType_Invalid)
   115 		{
   116 			wp->id = (int)g_Weapons.size();
   117 			sm_trie_insert(g_pWeaponLookup, weapons->GetName(), wp);
   118 			g_Weapons.push_back(wp);
   119 		}
   120 	}
   121 
   122 	kv->deleteThis();
   123 
   124 	return true;
   125 }
   126 
   127 void DM_FreeWeapons()
   128 {
   129 	for (size_t i=0; i<g_Weapons.size(); i++)
   130 	{
   131 		dm_weapon_t *wp = g_Weapons[i];
   132 		delete [] wp->classname;
   133 		delete [] wp->display;
   134 		delete wp;
   135 	}
   136 	g_Weapons.clear();
   137 
   138 	if (g_pWeaponLookup)
   139 	{
   140 		sm_trie_destroy(g_pWeaponLookup);
   141 		g_pWeaponLookup = NULL;
   142 	}
   143 }