PDA

View Full Version : Changing weapon models (Extended)


Cheap_Suit
07-04-2006, 08:05
Note if this example is in your plugin: If its 80%> of your whole script, I suggest you dont post it as a plugin.

This is a small example on changing weapon models for Counter-Strike. With this example you can change all 3 types of the model: view, player, and world. I have written one with Engine and Fakemeta for people who are not familiar with Fakemeta yet, and one only on fakemeta for L33T coders.

I hope this helps, so people dont need to keep posting about this ;).

Engine and Fakemeta

#include <amxmodx>
#include <engine>
#include <fakemeta>

new VIEW_MODEL[] = "models/v_<model name>.mdl"
new PLAYER_MODEL[] = "models/p_<model name>.mdl"
new WORLD_MODEL[] = "models/w_<model name>.mdl"

new OLDWORLD_MODEL[] = "models/w_<model name>.mdl" // the world model you want replaced

new PLUGIN_NAME[] = "Custom Weapon Model"
new PLUGIN_AUTHOR[] = "Cheap_Suit"
new PLUGIN_VERSION[] = "1.0"

public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
register_event("CurWeapon", "Event_CurWeapon", "be","1=1")
register_forward(FM_SetModel, "fw_SetModel")
}

public plugin_precache()
{
precache_model(VIEW_MODEL)
precache_model(PLAYER_MODEL)
precache_model(WORLD_MODEL)
}

public Event_CurWeapon(id)
{
// might not work for other mods
new weaponID = read_data(2)

// eg, if weapon is not ak then continue
if(weaponID != CSW_AK47)
return PLUGIN_CONTINUE

// this set's the view model (what you see when holding the gun)
entity_set_string(id, EV_SZ_viewmodel, VIEW_MODEL)

// this set's the player model (what you see when people holding the gun)
entity_set_string(id, EV_SZ_weaponmodel, PLAYER_MODEL)

return PLUGIN_CONTINUE
}

public fw_SetModel(entity, model[])
{
// check if its a valid entity or else we'll get errors
if(!is_valid_ent(entity))
return FMRES_IGNORED

// checks if it's the model we want to change
if(!equali(model, OLDWORLD_MODEL))
return FMRES_IGNORED

new className[33]
entity_get_string(entity, EV_SZ_classname, className, 32)

// dropped weapons map weapons c4 + grenades
if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{
// set's the world model (what you see on the ground)
entity_set_model(entity, WORLD_MODEL)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}
Only Fakemeta


public Event_CurWeapon(id)
{
// might not work for other mods
new weaponID = read_data(2)

// eg, if weapon is not ak then continue
if(weaponID != CSW_AK47)
return PLUGIN_CONTINUE

// this set's the view model (what you see when holding the gun)
set_pev(id, pev_viewmodel, engfunc(EngFunc_AllocString, VIEW_MODEL))

// this set's the player model (what you see when people holding the gun)
set_pev(id, pev_weaponmodel, engfunc(EngFunc_AllocString, PLAYER_MODEL))

return PLUGIN_CONTINUE
}

public fw_SetModel(entity, model[])
{
// check if its a valid entity or else we get errors
if(!pev_valid(entity))
return FMRES_IGNORED

// checks if its the model we want to change
if(!equali(model, OLDWORLD_MODEL))
return FMRES_IGNORED

new className[33]
pev(entity, pev_classname, className, 32)

// dropped weapons map weapons c4 + grenades
if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{
engfunc(EngFunc_SetModel, entity, WORLD_MODEL)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

Note: Using viewmodel2 and weaponmodel2.

(http://forums.alliedmods.net/showpost.php?p=352483&postcount=10)Weapon ID reference
#define CSW_P228 1
#define CSW_SCOUT 3
#define CSW_HEGRENADE 4
#define CSW_XM1014 5
#define CSW_C4 6
#define CSW_MAC10 7
#define CSW_AUG 8
#define CSW_SMOKEGRENADE 9
#define CSW_ELITE 10
#define CSW_FIVESEVEN 11
#define CSW_UMP45 12
#define CSW_SG550 13
#define CSW_GALI 14
#define CSW_GALIL 14
#define CSW_FAMAS 15
#define CSW_USP 16
#define CSW_GLOCK18 17
#define CSW_AWP 18
#define CSW_MP5NAVY 19
#define CSW_M249 20
#define CSW_M3 21
#define CSW_M4A1 22
#define CSW_TMP 23
#define CSW_G3SG1 24
#define CSW_FLASHBANG 25
#define CSW_DEAGLE 26
#define CSW_SG552 27
#define CSW_AK47 28
#define CSW_KNIFE 29
#define CSW_P90 30

Example of changing the skin of the knife


#include <amxmodx>
#include <engine>
#include <fakemeta>

new VIEW_MODEL[] = "models/v_newKnife.mdl"
new PLAYER_MODEL[] = "models/p_newKnife.mdl"
new WORLD_MODEL[] = "models/w_knife.mdl"

new OLDWORLD_MODEL[] = "models/w_knife.mdl"

new PLUGIN_NAME[] = "Custom Knife Model"
new PLUGIN_AUTHOR[] = "Cheap_Suit"
new PLUGIN_VERSION[] = "1.0"

public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
register_event("CurWeapon", "Event_CurWeapon", "be","1=1")
register_forward(FM_SetModel, "fw_SetModel")
}

public plugin_precache()
{
precache_model(VIEW_MODEL)
precache_model(PLAYER_MODEL)
precache_model(WORLD_MODEL)
}

public Event_CurWeapon(id)
{
new weaponID = read_data(2)

if(weaponID != CSW_KNIFE)
return PLUGIN_CONTINUE

set_pev(id, pev_viewmodel2, VIEW_MODEL)
set_pev(id, pev_weaponmodel2, PLAYER_MODEL)

return PLUGIN_CONTINUE
}

public fw_SetModel(entity, model[])
{
if(!is_valid_ent(entity))
return FMRES_IGNORED

if(!equali(model, OLDWORLD_MODEL))
return FMRES_IGNORED

new className[33]
entity_get_string(entity, EV_SZ_classname, className, 32)

if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{
engfunc(EngFunc_SetModel, entity, WORLD_MODEL)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

TheNewt
07-04-2006, 09:30
Cool. One question though, does this work for both player models and weapon models?

Hawk552
07-04-2006, 11:25
I'd like to know why you include both engine and fakemeta. You should probably use one or the other. If I were you, I'd use fakemeta though.

Cheap_Suit
07-05-2006, 06:37
Works with v_, p_, and w_ models.

@Hawk552
I used fakemeta for FM_SetModel, and engine to simplify the example.







I suck with fakemeta.

Hawk552
07-05-2006, 11:05
Works with v_, p_, and w_ models.

@Hawk552
I used fakemeta for FM_SetModel, and engine to simplify the example.







I suck with fakemeta.

I figured... use pev and pev_viewmodel / pev_weaponmodel

Brad
07-05-2006, 13:33
Show an example using only engine and then using only fakemeta. :up:

Cheap_Suit
07-05-2006, 20:46
@ Hawk552
Done.

@ Brad
Dont know if engine has a forward like FM_SetModel. You can probably try hooking it in server_frame or pfn_spawn.

@ RapHero2000
Model isn't found. Most probably you have v_<model name>.mdl
You must use the actual model name eg. "v_custom_m4a1.mdl".

TheNewt
07-10-2006, 12:24
I tried making this so that it only showed the model when a player has 'purchased' the model through a menu, but when I try to give the player a weapon at the begining of the round (resethud) it crashes the server... If I give the gun to the player as soon as he/she buys it, and doesn't die, then the next round the model loads, but not immediatly... It does show the model when I drop the weapon to the ground... I'm so confused...

-Edit: Fixed it.

vittu
07-14-2006, 15:37
For Fakemeta you don't need to allocate the string when using viewmodel2 or weaponmodel2.
Amended above example:
// this set's the view model (what you see when holding the gun)
set_pev(id, pev_viewmodel2, VIEW_MODEL)

// this set's the player model (what you see when people holding the gun)
set_pev(id, pev_weaponmodel2, PLAYER_MODEL)

Cheap_Suit
07-15-2006, 05:10
This will be noted. Thanks

[DoD]GoldenEagle
07-21-2006, 19:48
So what do i do with the
#include <amxmodx>
#include <engine>
#include <fakemeta>

new VIEW_MODEL[] = "models/v_<model name>.mdl"
new PLAYER_MODEL[] = "models/p_<model name>.mdl"
new WORLD_MODEL[] = "models/w_<model name>.mdl"

new OLDWORLD_MODEL[] = "models/w_<model name>.mdl" // the world model you want replaced

new PLUGIN_NAME[] = "Custom Weapon Model"
new PLUGIN_AUTHOR[] = "Cheap_Suit"
new PLUGIN_VERSION[] = "1.0"

public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
register_event("CurWeapon", "Event_CurWeapon", "be","1=1")
register_forward(FM_SetModel, "fw_SetModel")
}

public plugin_precache()
{
precache_model(VIEW_MODEL)
precache_model(PLAYER_MODEL)
precache_model(WORLD_MODEL)
}

public Event_CurWeapon(id)
{
// might not work for other mods
new weaponID = read_data(2)

// eg, if weapon is not ak then continue
if(weaponID != CSW_AK47)
return PLUGIN_CONTINUE

// this set's the view model (what you see when holding the gun)
entity_set_string(id, EV_SZ_viewmodel, VIEW_MODEL)

// this set's the player model (what you see when people holding the gun)
entity_set_string(id, EV_SZ_weaponmodel, PLAYER_MODEL)

return PLUGIN_CONTINUE
}

public fw_SetModel(entity, model[])
{
// check if its a valid entity or else we'll get errors
if(!is_valid_ent(entity))
return FMRES_IGNORED

// checks if it's the model we want to change
if(!equali(model, OLDWORLD_MODEL))
return FMRES_IGNORED

new className[33]
entity_get_string(entity, EV_SZ_classname, className, 32)

// dropped weapons map weapons c4 + grenades
if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{
// set's the world model (what you see on the ground)
entity_set_model(entity, WORLD_MODEL)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

[DoD]GoldenEagle
07-22-2006, 00:13
Ok i get it now but what do i do with this now?

[DoD]GoldenEagle
07-22-2006, 01:05
Ok see now that i copoed that code what do i do change something or put it on notepad.....what?

tupacaveli
07-23-2006, 09:54
i tried this one but the w_ mdls still dont work on my server after several days of trying and retrying.

Cheap_Suit
07-23-2006, 13:00
You didnt put that right mode in new OLDMODEL[]

[DoD]GoldenEagle
07-27-2006, 12:43
Can you jsut psot a whole script or not jsut those two pieces?

Cheap_Suit
07-28-2006, 00:12
The whole script is the first one. The second one is only if you want to use fakemeta.

Dave-orangekiller
07-28-2006, 00:24
hi,

im lost with code can a person explain to me how to put knife model with this code (i wan a v_knife.mdl and p_knife.mdl)

#include <amxmodx>
#include <engine>
#include <fakemeta>

new VIEW_MODEL[] = "models/v_<model name>.mdl"
new PLAYER_MODEL[] = "models/p_<model name>.mdl"
new WORLD_MODEL[] = "models/w_<model name>.mdl"

new OLDWORLD_MODEL[] = "models/w_<model name>.mdl" // the world model you want replaced

new PLUGIN_NAME[] = "Custom Weapon Model"
new PLUGIN_AUTHOR[] = "Cheap_Suit"
new PLUGIN_VERSION[] = "1.0"

public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
register_event("CurWeapon", "Event_CurWeapon", "be","1=1")
register_forward(FM_SetModel, "fw_SetModel")
}

public plugin_precache()
{
precache_model(VIEW_MODEL)
precache_model(PLAYER_MODEL)
precache_model(WORLD_MODEL)
}

public Event_CurWeapon(id)
{
// might not work for other mods
new weaponID = read_data(2)

// eg, if weapon is not ak then continue
if(weaponID != CSW_AK47)
return PLUGIN_CONTINUE

// this set's the view model (what you see when holding the gun)
entity_set_string(id, EV_SZ_viewmodel, VIEW_MODEL)

// this set's the player model (what you see when people holding the gun)
entity_set_string(id, EV_SZ_weaponmodel, PLAYER_MODEL)

return PLUGIN_CONTINUE
}

public fw_SetModel(entity, model[])
{
// check if its a valid entity or else we'll get errors
if(!is_valid_ent(entity))
return FMRES_IGNORED

// checks if it's the model we want to change
if(!equali(model, OLDWORLD_MODEL))
return FMRES_IGNORED

new className[33]
entity_get_string(entity, EV_SZ_classname, className, 32)

// dropped weapons map weapons c4 + grenades
if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{
// set's the world model (what you see on the ground)
entity_set_model(entity, WORLD_MODEL)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

Dave-orangekiller
07-29-2006, 13:05
oki but the problem it's in that code wat i change i want to know all i change sorry code and me are not friend lol all time i try they said not work i put wat i change


#include <amxmodx>
#include <engine>
#include <fakemeta>


new VIEW_MODEL[] = "models/v_knife.mdl"
new PLAYER_MODEL[] = "models/p_knife.mdl"
new WORLD_MODEL[] = "models/w_<model name>.mdl" (WAT I DO)


new OLDWORLD_MODEL[] = "models/w_<model name>.mdl" // the world model you want replaced (WAT I DO)


new PLUGIN_NAME[] = "knife"
new PLUGIN_AUTHOR[] = "Cheap_Suit"
new PLUGIN_VERSION[] = "1.0"


public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
register_event("CurWeapon", "Event_CurWeapon", "be","1=1")
register_forward(FM_SetModel, "fw_SetModel") (WAT I DO)
}


public plugin_precache()
{
precache_model(v_knife)
precache_model(p_knife)
precache_model(WORLD_MODEL) (WAT I DO)
}


public Event_CurWeapon(id)
{
// might not work for other mods
new weaponID = read_data(2)


// eg, if weapon is not ak then continue
if(weaponID != CSW_KNIFE)
return PLUGIN_CONTINUE


// this set's the view model (what you see when holding the gun)
entity_set_string(id, EV_SZ_viewmodel, v_knife.mdl)


// this set's the player model (what you see when people holding the gun)
entity_set_string(id, EV_SZ_weaponmodel, p_knife.mdl)


return PLUGIN_CONTINUE
}


public fw_SetModel(entity, model[])
{
// check if its a valid entity or else we'll get errors (WAT I DO)
if(!is_valid_ent(entity))
return FMRES_IGNORED


// checks if it's the model we want to change
if(!equali(model, OLDWORLD_MODEL)) (WAT I DO)
return FMRES_IGNORED


new className[33]
entity_get_string(entity, EV_SZ_classname, className, 32) (WAT I DO)


// dropped weapons map weapons c4 + grenades
if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade")) (WAT I DO)
{
// set's the world model (what you see on the ground)
entity_set_model(entity, WORLD_MODEL) (WAT I DO)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

Cheap_Suit
07-29-2006, 23:50
WORLD_MODEL - is the new w_ skin for the custom weapon.
OLDWORLD_MODEL - is the default skin for weapon. This is needed because a lot of entities goes through FM_SetModel. We just need to filter out which model we want to change.

As for your knife model, if you dont have an custom world knife model, then just use the default.

Example:

new WORLD_MODEL[] = "models/w_knife.mdl"
new OLDWORLD_MODEL[] = "models/w_knife.mdl"

Dave-orangekiller
07-30-2006, 13:03
oki but i have problem i dont undertand lollll

sorry i didnt do code before

Cheap_Suit
07-30-2006, 21:02
Just change what I wrote on the example I showed.

Dave-orangekiller
07-30-2006, 22:50
okay and i put this in amxmodx studio and i clic on the green arrow is that right???

and i have just v_knife and p_knife.mdl i dont have the w_knife.mdl

Cheap_Suit
07-31-2006, 01:10
Yea. If you dont, just use the default model. which is w_knife.mdl

Dave-orangekiller
07-31-2006, 02:18
he dont work man do you have msn or something for chat because now its difficult for me it say spawn compiler not found

Cheap_Suit
07-31-2006, 06:20
What dont work? Not compiling?

Dave-orangekiller
07-31-2006, 12:40
yeah its said pawn compiled not found check your setting and try again

can i add you in msn

tupacaveli
08-01-2006, 05:08
.








.

Cheap_Suit
08-01-2006, 06:20
Which world model are you trying to change?

tupacaveli
08-01-2006, 09:16
.








.

Cheap_Suit
08-01-2006, 13:50
There OLDWORLD_MODELS are

w_awp.mdl
w_m4a1.mdl
w_hegrenade.mdl
w_flashbang.mdl
w_smokegrenade.mdl

tupacaveli
08-01-2006, 14:56
can u post the code to compile plz ? i have no what to do .lol sry man (noob = me )
server is linux and amxx 1.75

szlend
08-18-2006, 17:33
God damnit why not learn the basics before you try coding someting...

Xanimos
08-20-2006, 18:09
calm down szlend. That kind of language wasn't needed for that.

k007
09-20-2006, 22:48
could someone how would i do the w_ models for 3 nades the same time in the FM_setmodel forward?

Cheap_Suit
09-21-2006, 03:30
if(equali(model, FLASH_MODEL))
// set flash model
else if(equali(model, HE_MODEL))
// set he model
else if(equali(model, SMOKE_MODEL))
// set smoke model
?

k007
09-21-2006, 10:06
so i have to do is check if the entity is valid and then the old model and just set it?
and how about this do i use this?

new className[33]
entity_get_string(entity, EV_SZ_classname, className, 32)

if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{

Zenith77
09-21-2006, 12:30
Just "filter" your entity, through a check of some kind (i.e. Check for classname), change the model, and return FMRES_SUPERCEDE.

k007
09-21-2006, 17:45
ok ill see

k007
09-21-2006, 22:59
could someone tell me if this is right or wrong i realy don't have time to test

public fw_SetModel(entity, model[])
{
if(!pev_valid(entity))
{
return FMRES_IGNORED
}

new className[33]
pev(entity, pev_classname, className, 32)

if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{
if(!equali(model, OLD_HE_MODEL) || !equal(model, OLD_SG_MODEL) || !equal(model, OLD_FB_MODEL))
return FMRES_IGNORED

else if(equali(model, OLD_FB_MODEL))
{
engfunc(EngFunc_SetModel, entity, W_FB_MODEL)
return FMRES_SUPERCEDE
}
else if(equali(model, OLD_HE_MODEL))
{
engfunc(EngFunc_SetModel, entity, W_HE_MODEL)
return FMRES_SUPERCEDE
}
else if(equali(model, OLD_SG_MODEL))
{
engfunc(EngFunc_SetModel, entity, W_SG_MODEL)
return FMRES_SUPERCEDE
}
}
return FMRES_IGNORED
}

Cheap_Suit
09-22-2006, 02:31
if(!equali(model, OLD_HE_MODEL) || !equal(model, OLD_SG_MODEL) || !equal(model, OLD_FB_MODEL))
return FMRES_IGNORED

else

remove

k007
09-22-2006, 10:04
ok thanks

Zenith77
09-22-2006, 12:28
When I'm get the time, I am going to research grenade entities, because they're has to be some way the game knows what type of grenade they are, and not by checking the model like plugins do.

Xanimos
09-23-2006, 00:12
1) If you don't have time to test, then why are you doing it?
2) It would work but it is very inefficient.
3) Hello [/CODE]PM
4) HAPPY APPLE DAY!

k007
09-23-2006, 00:29
could soneone show me how do i add an index to that forward do i just do.
new id = index
?
edit: it didn't work now i used owner as an id as zenith told me, but i don't know if it's right/save to use owner as an id

Zenith77
09-27-2006, 12:30
Yes, store the id of the owner of the grenade in pev_owner, the whole point is to keep track of data. There really is no other way, unless you want to be stupid, and create a 500 (default) cell array that keeps tracks of every entities owner (atleasy grenades). The latter is simply not an option.


set_pev(myEnt, pev_owner, idOrWhatever);

P34nut
09-27-2006, 12:57
There really is no other way

what about
set_pev(Ent, pev_euser1, ID)
or
set_pev(Ent, pev_euser2, ID)
or
set_pev(Ent, pev_euser3, ID)
or
set_pev(Ent, pev_euser4, ID)

Zenith77
09-27-2006, 17:30
I'm just not going to say anything, hopefully somebody else will see the stupidity in your post.

k007
09-27-2006, 20:35
zenith don't start freaking out pev_owner works amazing for me :0

Zenith77
09-27-2006, 21:49
No, he didn't even, I don't know what, and then went on to correct me. I don't take that very well.

k007
09-27-2006, 22:01
Chillaxe!

Whosat
11-11-2007, 07:13
Thanks for this tutorial! Really helped me alot!

Was just wondering though, when does the event CurWeapon occur? I'm trying to use it to set the clip of the pistol in Natural Selection.

purple_pixie
11-13-2007, 06:45
Change weapon, switch firemode?, reload, fire ... anything weapon related.

raidmax
03-28-2008, 17:03
A little problem, when using THIS MODEL (http://www.cs-lab.xz.lt/knife_22.rar)
ME:
http://www.cs-lab.xz.lt/knife.bmp

OTHER PLAYER:
http://img168.**************/img168/6176/erorus5.jpg (http://**************)



#include <amxmodx>
#include <engine>
#include <fakemeta>

new VIEW_MODEL[] = "models/bk/v_knife.mdl"
new PLAYER_MODEL[] = "models/bk/v_knife.mdl"
new WORLD_MODEL[] = "models/bk/v_knife.mdl"

new OLDWORLD_MODEL[] = "models/w_knife.mdl"

new PLUGIN_NAME[] = "Custom Knife Model"
new PLUGIN_AUTHOR[] = "Cheap_Suit"
new PLUGIN_VERSION[] = "1.0"

public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
register_event("CurWeapon", "Event_CurWeapon", "be","1=1")
register_forward(FM_SetModel, "fw_SetModel")
}

public plugin_precache()
{
precache_model(VIEW_MODEL)
precache_model(PLAYER_MODEL)
precache_model(WORLD_MODEL)
}

public Event_CurWeapon(id)
{
new weaponID = read_data(2)

if(weaponID != CSW_KNIFE)
return PLUGIN_CONTINUE

set_pev(id, pev_viewmodel2, VIEW_MODEL)
set_pev(id, pev_weaponmodel2, PLAYER_MODEL)

return PLUGIN_CONTINUE
}

public fw_SetModel(entity, model[])
{
if(!is_valid_ent(entity))
return FMRES_IGNORED

if(!equali(model, OLDWORLD_MODEL))
return FMRES_IGNORED

new className[33]
entity_get_string(entity, EV_SZ_classname, className, 32)

if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{
engfunc(EngFunc_SetModel, entity, WORLD_MODEL)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

micke1101
03-30-2008, 04:21
it makes you cant plant the c4 for me but its the right model

#include <amxmodx>
#include <engine>
#include <fakemeta>
new VIEW_MODEL[] = "models/shmod/psp.mdl"
new PLAYER_MODEL[] = "models/shmod/psp.mdl"
new WORLD_MODEL[] = "models/shmod/psp.mdl"
new OLDWORLD_MODEL[] = "models/shmod/psp.mdl" // the world model you want replaced
new PLUGIN_NAME[] = "Custom Weapon Model"
new PLUGIN_AUTHOR[] = "Cheap_Suit"
new PLUGIN_VERSION[] = "1.0"
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
register_event("CurWeapon", "Event_CurWeapon", "be","1=1")
register_forward(FM_SetModel, "fw_SetModel")
}
public plugin_precache()
{
precache_model(VIEW_MODEL)
precache_model(PLAYER_MODEL)
precache_model(WORLD_MODEL)
}
public Event_CurWeapon(id)
{
// might not work for other mods
new weaponID = read_data(2)

// eg, if weapon is not ak then continue
if(weaponID != CSW_C4)
return PLUGIN_CONTINUE

// this set's the view model (what you see when holding the gun)
entity_set_string(id, EV_SZ_viewmodel, VIEW_MODEL)

// this set's the player model (what you see when people holding the gun)
entity_set_string(id, EV_SZ_weaponmodel, PLAYER_MODEL)

return PLUGIN_CONTINUE
}
public fw_SetModel(entity, model[])
{
// check if its a valid entity or else we'll get errors
if(!is_valid_ent(entity))
return FMRES_IGNORED
// checks if it's the model we want to change
if(!equali(model, OLDWORLD_MODEL))
return FMRES_IGNORED
new className[33]
entity_get_string(entity, EV_SZ_classname, className, 32)
// dropped weapons map weapons c4 + grenades
if(equal(className, "weaponbox") || equal(className, "armoury_entity") || equal(className, "grenade"))
{
// set's the world model (what you see on the ground)
entity_set_model(entity, WORLD_MODEL)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

K.K.Lv
12-18-2008, 02:01
So Cool!!
I like this Plugin!
thanks Cheap Suit

Duracell_sk8
12-19-2008, 03:46
Work :D Thanks!:)

FutureGanja
04-28-2009, 14:28
CAn anyone help me with this?

Ciio
11-11-2009, 14:24
good

Jumper
12-17-2009, 07:41
it's work ty man . . . . . . . .

ConnorMcLeod
12-17-2009, 15:49
That way doesn't increase precached models number :
(Similar way than here (http://forums.alliedmods.net/showthread.php?p=514507))

configs/weapons_models.ini :
example :

// View Models
"models/v_ak47.mdl" "models/css/v_ak47.mdl"
"models/v_aug.mdl" "models/css/v_aug.mdl"
"models/v_awp.mdl" "models/css/v_awp.mdl"
"models/v_c4.mdl" "models/css/v_c4.mdl"
"models/v_deagle.mdl" "models/css/v_deagle.mdl"
"models/v_elite.mdl" "models/css/v_elite.mdl"
"models/v_famas.mdl" "models/css/v_famas.mdl"
"models/v_fiveseven.mdl" "models/css/v_fiveseven.mdl"
"models/v_flashbang.mdl" "models/css/v_flashbang.mdl"
"models/v_g3sg1.mdl" "models/css/v_g3sg1.mdl"
"models/v_galil.mdl" "models/css/v_galil.mdl"
"models/v_glock18.mdl" "models/css/v_glock18.mdl"
"models/v_hegrenade.mdl" "models/css/v_hegrenade.mdl"
"models/v_knife.mdl" "models/css/v_knife.mdl"
"models/v_m3.mdl" "models/css/v_m3.mdl"
"models/v_m4a1.mdl" "models/css/v_m4a1.mdl"
"models/v_m249.mdl" "models/css/v_m249.mdl"
"models/v_mac10.mdl" "models/css/v_mac10.mdl"
"models/v_mp5.mdl" "models/css/v_mp5.mdl"
"models/v_p90.mdl" "models/css/v_p90.mdl"
"models/v_p228.mdl" "models/css/v_p228.mdl"
"models/v_scout.mdl" "models/css/v_scout.mdl"
"models/v_sg550.mdl" "models/css/v_sg550.mdl"
"models/v_sg552.mdl" "models/css/v_sg552.mdl"
"models/v_smokegrenade.mdl" "models/css/v_smokegrenade.mdl"
"models/v_tmp.mdl" "models/css/v_tmp.mdl"
"models/v_ump45.mdl" "models/css/v_ump45.mdl"
"models/v_usp.mdl" "models/css/v_usp.mdl"
"models/v_xm1014.mdl" "models/css/v_xm1014.mdl"

// World Models
"models/w_ak47.mdl" "models/css/w_ak47.mdl"
"models/w_backpack.mdl" "models/css/w_backpack.mdl"

/* Formatright © 2012, ConnorMcLeod

Weapons Models is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Weapons Models; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

#define VERSION "0.2.3"

/* ChangeLog
*
* v 0.2.3
* - Added shield support
* - Fixed missing p_ models support that were causing crashes
* - Added pdata check on player on weapon deploy event
*
* v0.2.2
* - Fixed bad formating that was preventing default models from being detected
*
* v0.2.1
* - Fixed "Invalid trie handle provided" error
*
* v0.2.0
* - Check viewmodel and weaponmodel instead of weapon type, so it now supports shield models
*
* v0.1.7
* - Adjustments
*
* v0.1.6
* - Remove FreeEntPrivateData, was causing player from not being reconnected to server
*
* v0.1.5
* - Don't need to precache backpack model anymore
*
* v0.1.4
* - Store allocated view and weapon models in arrays instead of Trie
* - Fixed w_backpack.mdl was not precached (bad string name was passed...)
*
* v0.1.3
* - Store precache returns values into a trie instead of checking trie and resend precache
* - Only store world new models strings in a trie
*
* v0.1.2
* - Moved backpack model precache from precache hook to plugin_precache
*
* v0.1.1
* - Fixed first map where weapons were not registering
*
* v0.1.0
* - Hook Ham_Item_Deploy and set models there instead of hooking FM_ModelIndex that was intensive
*
* v0.0.1 First Shot
*/

// #define DONT_BLOCK_PRECACHE

#include <amxmodx>
#include <fakemeta_stocks>
#include <hamsandwich>

#pragma semicolon 1

const MAX_MODEL_LENGTH = 64;

new const m_rgpPlayerItems_CBasePlayer[6] = {367, 368, ...};
const m_pActiveItem = 373;

const XO_WEAPON = 4;
const m_pPlayer = 41;

new Trie:g_tszWorldModels; // handles new models strings (all excepted v_ and w_ models)
new Trie:g_tiPrecacheReturns; // handles all new models precache returns integers
new Trie:g_tiszViewModels;
new Trie:g_tiszWeaponModels;

public plugin_precache()
{
new szModelsFile[128];
get_localinfo("amxx_configsdir", szModelsFile, charsmax(szModelsFile));
add(szModelsFile, charsmax(szModelsFile), "/weapons_models.ini");

new iFile = fopen(szModelsFile, "rt");
if(!iFile)
{
return;
}

new szDatas[192], szOldModel[MAX_MODEL_LENGTH], szNewModel[MAX_MODEL_LENGTH];
new szWeaponClass[32], Trie:tRegisterWeaponDeploy = TrieCreate(), iId;

new Trie:tWeaponsIds = TrieCreate();
TrieSetCell(tWeaponsIds, "p228", CSW_P228);
TrieSetCell(tWeaponsIds, "scout", CSW_SCOUT);
TrieSetCell(tWeaponsIds, "hegrenade", CSW_HEGRENADE);
TrieSetCell(tWeaponsIds, "xm1014", CSW_XM1014);
TrieSetCell(tWeaponsIds, "c4", CSW_C4);
TrieSetCell(tWeaponsIds, "mac10", CSW_MAC10);
TrieSetCell(tWeaponsIds, "aug", CSW_AUG);
TrieSetCell(tWeaponsIds, "smokegrenade", CSW_SMOKEGRENADE);
TrieSetCell(tWeaponsIds, "elite", CSW_ELITE);
TrieSetCell(tWeaponsIds, "fiveseven", CSW_FIVESEVEN);
TrieSetCell(tWeaponsIds, "ump45", CSW_UMP45);
TrieSetCell(tWeaponsIds, "sg550", CSW_SG550);
TrieSetCell(tWeaponsIds, "galil", CSW_GALIL);
TrieSetCell(tWeaponsIds, "famas", CSW_FAMAS);
TrieSetCell(tWeaponsIds, "usp", CSW_USP);
TrieSetCell(tWeaponsIds, "glock18", CSW_GLOCK18);
TrieSetCell(tWeaponsIds, "awp", CSW_AWP);
TrieSetCell(tWeaponsIds, "mp5navy", CSW_MP5NAVY);
TrieSetCell(tWeaponsIds, "m249", CSW_M249);
TrieSetCell(tWeaponsIds, "m3", CSW_M3);
TrieSetCell(tWeaponsIds, "m4a1", CSW_M4A1);
TrieSetCell(tWeaponsIds, "tmp", CSW_TMP);
TrieSetCell(tWeaponsIds, "g3sg1", CSW_G3SG1);
TrieSetCell(tWeaponsIds, "flashbang", CSW_FLASHBANG);
TrieSetCell(tWeaponsIds, "deagle", CSW_DEAGLE);
TrieSetCell(tWeaponsIds, "sg552", CSW_SG552);
TrieSetCell(tWeaponsIds, "ak47", CSW_AK47);
TrieSetCell(tWeaponsIds, "knife", CSW_KNIFE);
TrieSetCell(tWeaponsIds, "p90", CSW_P90);

new c, bool:bServerDeactivateRegistered, iExtPos, bShieldModel;
while(!feof(iFile))
{
fgets(iFile, szDatas, charsmax(szDatas));
trim(szDatas);
if(!(c=szDatas[0]) || c == ';' || c == '#' || (c == '/' && szDatas[1] == '/'))
{
continue;
}

if( parse(szDatas, szOldModel, charsmax(szOldModel), szNewModel, charsmax(szNewModel)) == 2
&& file_exists(szNewModel) )
{
// models/[p/v]_
// models/shield/[p/v]_shield_
bShieldModel = equal(szOldModel, "models/shield/", 14);
if( ( (c=szOldModel[bShieldModel ? 14 : 7]) == 'p' || c == 'v' ) && szOldModel[bShieldModel ? 15 : 8] == '_' )
{
if( equal(szOldModel[9], "mp5", 3 ) )
{
copy(szWeaponClass, charsmax(szWeaponClass), "weapon_mp5navy");
}
else
{
iExtPos = strlen(szOldModel) - 4;
szOldModel[ iExtPos ] = EOS;
formatex(szWeaponClass, charsmax(szWeaponClass), "weapon_%s", szOldModel[bShieldModel ? 23 : 9]);
szOldModel[ iExtPos ] = '.';
}

if( !TrieGetCell(tWeaponsIds, szWeaponClass[7], iId) )
{
continue;
}

if( c == 'v' )
{
if( !g_tiszViewModels )
{
g_tiszViewModels = TrieCreate();
}

TrieSetCell(g_tiszViewModels, szOldModel, EF_AllocString( szNewModel ) );
}
else
{
if( !g_tiszWeaponModels )
{
g_tiszWeaponModels = TrieCreate();
}

TrieSetCell(g_tiszWeaponModels, szOldModel, EF_AllocString( szNewModel ) );
}

if( !TrieKeyExists(tRegisterWeaponDeploy, szWeaponClass) )
{
TrieSetCell
(
tRegisterWeaponDeploy,
szWeaponClass,
RegisterHam(Ham_Item_Deploy, szWeaponClass, "OnCBasePlayerWeapon_Deploy_P", true)
);
}
}
else
{
if( !bServerDeactivateRegistered && equal(szOldModel, "models/w_backpack.mdl") )
{
bServerDeactivateRegistered = true;
register_forward(FM_ServerDeactivate, "OnServerDeactivate");
}

if( !g_tszWorldModels )
{
g_tszWorldModels = TrieCreate();
}
else if( TrieKeyExists(g_tszWorldModels, szOldModel) )
{
new szModel[MAX_MODEL_LENGTH];
TrieGetString(g_tszWorldModels, szOldModel, szModel, charsmax(szModel));
log_amx("%s world model is already set to %s, can't set it to %s !!", szWeaponClass, szModel, szNewModel);
continue;
}
TrieSetString(g_tszWorldModels, szOldModel, szNewModel);
}

if( !g_tiPrecacheReturns )
{
g_tiPrecacheReturns = TrieCreate();
}
TrieSetCell(g_tiPrecacheReturns, szOldModel, EF_PrecacheModel(szNewModel));
#if defined DONT_BLOCK_PRECACHE
EF_PrecacheModel(szOldModel);
#endif
}
}
fclose(iFile);

TrieDestroy(tRegisterWeaponDeploy);
TrieDestroy(tWeaponsIds);

if( g_tiPrecacheReturns )
{
register_forward(FM_PrecacheModel, "OnPrecacheModel");

if( g_tszWorldModels )
{
register_forward(FM_SetModel, "OnSetModel");
}
}
}

public plugin_init()
{
register_plugin("Weapons Models", VERSION, "ConnorMcLeod");
}

public OnServerDeactivate()
{
static bool:bDontPassThisTwice = false;
if( bDontPassThisTwice ) // unregister this would be waste of time
{
return;
}
bDontPassThisTwice = true;

new id, c4 = FM_NULLENT;
while( (c4 = EF_FindEntityByString(c4, "classname", "weapon_c4")) > 0 )
{
id = get_pdata_cbase(c4, m_pPlayer);
if( id > 0 )
{
// can't use set_pdata_cbase on players at this point
set_pdata_int(id, m_rgpPlayerItems_CBasePlayer[5], 0);
set_pdata_int(id, m_pActiveItem, 0);
// tried to remove c4 entity but server just stucks
}
}
}

public OnPrecacheModel(const szModel[])
{
static iReturn;
if( TrieGetCell(g_tiPrecacheReturns, szModel, iReturn) )
{
forward_return(FMV_CELL, iReturn);
return FMRES_SUPERCEDE;
}
return FMRES_IGNORED;
}

public OnCBasePlayerWeapon_Deploy_P( iWeapon )
{
new id = get_pdata_cbase(iWeapon, m_pPlayer, XO_WEAPON);
if( pev_valid(id) == 2 && get_pdata_cbase(id, m_pActiveItem) == iWeapon )
{
new iszNewModel, szOldModel[MAX_MODEL_LENGTH];
if( g_tiszViewModels )
{
pev(id, pev_viewmodel2, szOldModel, charsmax(szOldModel));
if( TrieGetCell(g_tiszViewModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_viewmodel, iszNewModel);
}
}
if( g_tiszWeaponModels )
{
pev(id, pev_weaponmodel2, szOldModel, charsmax(szOldModel));
if( TrieGetCell(g_tiszWeaponModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_weaponmodel, iszNewModel);
}
}
}
}

public OnSetModel(const iEnt, const szModel[])
{
new szNewModel[MAX_MODEL_LENGTH];
if( TrieGetString(g_tszWorldModels, szModel, szNewModel, charsmax(szNewModel)) )
{
EF_SetModel(iEnt, szNewModel);
return FMRES_SUPERCEDE;
}
return FMRES_IGNORED;
}

FiFiX
10-02-2010, 07:52
How to make otpimal weapon model change for all players(each player has different model) at round start?

Ex1ne
12-08-2011, 16:43
How do you make it so if you have admin you can "give" the model to someone via a cvar?

lqlqlq
02-01-2012, 15:37
yep i got a precache error on first map.
Can you fixed ?

ConnorMcLeod
02-01-2012, 18:12
If you are talking about SV_ModelIndex error, it is fixed in version 0.1.1.

leonard19941
05-27-2012, 16:57
Connor you could add to the models of the weapons are for 'CT' or 'T', thanks.

Example:

"models/v_knife.mdl" "models/arms/v_knifet.mdl" "T"
"models/v_knife.mdl" "models/arms/v_knifect.mdl" "CT"


In this example the knife of the 'CT' would be different from the 'T'.

Lolz0r
05-27-2012, 17:06
LOL! Nice idea.

leonard19941
07-03-2012, 20:33
Bump 1.0.

leonard19941
07-20-2012, 17:27
Bump 1.1.

Doc-Holiday
07-22-2012, 12:49
Connor you could add to the models of the weapons are for 'CT' or 'T', thanks.

Example:



In this example the knife of the 'CT' would be different from the 'T'.

This will make a CARZY amount of downloads to the client to play on your server lol doubling the downloads

Fr33m@n
11-06-2012, 12:39
If he have a good fast download, whynot ?


I didn't noticied that one.
Thanks again Mr Connor.

Edit : dat bug :


http://img11.hostingpics.net/pics/1386253850572012110600008.jpg


happens to all v_models :
viewmodel is set correctly.
but the custom viewmodel used on viewmodel is also applied on weaponmodel right here :

if( (iszModel = g_iAllocatedViewModels[ iId ]) )
{
set_pev(id, pev_viewmodel, iszModel)
}
if( (iszModel = g_iAllocatedViewModels[ iId ]) )
{
set_pev(id, pev_weaponmodel, iszModel)
}

dFF
11-07-2012, 08:03
Edit : dat bug :


Its a weapon model bug.

ConnorMcLeod
11-07-2012, 11:33
Thanks.
Change storage system so the bug is fixed and shield is now supported.

Fr33m@n
11-11-2012, 10:16
Sorry ConnorMcLeod, but the plugin don't work for most of my view models :
here is my weapons_models.ini only mp5 model is working, (all of them worked when i fixed myself the error from previous version.)

// View Models
"models/v_ak47.mdl" "models/codmw2_vLs/v_ak47_v2.mdl"
"models/v_aug.mdl" "models/codmw2_vLs/v_aug.mdl"
"models/v_awp.mdl" "models/codmw2_vLs/v_awp_v2.mdl"
"models/v_c4.mdl" "models/codmw2_vLs/v_c4.mdl"
"models/v_famas.mdl" "models/codmw2_vLs/v_famas.mdl"
"models/v_glock18.mdl" "models/codmw2_vLs/v_glock18.mdl"
"models/v_knife.mdl" "models/codmw2_vLs/v_knife.mdl"
"models/v_m3.mdl" "models/codmw2_vLs/v_m3.mdl"
"models/v_m4a1.mdl" "models/codmw2_vLs/v_m4a1.mdl"
"models/v_mac10.mdl" "models/codmw2_vLs/v_mac10.mdl"
"models/v_mp5.mdl" "models/codmw2_vLs/v_mp5.mdl"
"models/v_p90.mdl" "models/codmw2_vLs/v_p90.mdl"
"models/v_tmp.mdl" "models/codmw2_vLs/v_tmp.mdl"
"models/v_xm1014.mdl" "models/codmw2_vLs/v_xm1014.mdl"

// World Models
"models/w_c4.mdl" "models/codmw2_vLs/w_c4.mdl"


and i get :


L 11/11/2012 - 16:11:33: [AMXX] Displaying debug trace (plugin "weapons_models.amxx")
L 11/11/2012 - 16:11:33: [AMXX] Run time error 10: native error (native "TrieGetCell")
L 11/11/2012 - 16:11:33: [AMXX] [0] weapons_models.sma::MiscWeapon_Deploy_Post (line 271)
L 11/11/2012 - 16:11:34: Invalid trie handle provided (0)bomb world model is also working.

ConnorMcLeod
11-11-2012, 13:00
Thanks, fixed.

dFF
11-11-2012, 14:23
Connor why you don't destroy main Tries (like: g_tszWorldModels; g_tiPrecacheReturns; g_tiszViewModels; g_tiszWeaponModels) in plugin_end to prevent memory leak ? (sorry if i say bullshits).

Fr33m@n
11-11-2012, 16:52
Error log fixed, but bug described before still here.

ConnorMcLeod
11-11-2012, 17:40
Connor why you don't destroy main Tries (like: g_tszWorldModels; g_tiPrecacheReturns; g_tiszViewModels; g_tiszWeaponModels) in plugin_end to prevent memory leak ? (sorry if i say bullshits).

Tries are destroyed at map end (by amxx core), so no memory leak.

Error log fixed, but bug described before still here.

Should be definitely fixed, thanks for feed back.


Still have to make plugin support shield, and prefixes and per map cfg file, then i may release it instead of let it in middle of that thread.

lqlqlq
04-17-2013, 10:36
i get error using the last version http://forums.alliedmods.net/showthread.php?p=1020078#post1020078

L 04/17/2013 - 17:30:32: [HAMSANDWICH] Invalid player 1 (not in-game)
L 04/17/2013 - 17:30:32: [AMXX] Run time error 10 (plugin "weapons_models.amxx") (native "get_pdata_cbase") - debug not enabled!
L 04/17/2013 - 17:30:32: [AMXX] To enable debug mode, add "debug" after the plugin name in plugins.ini (without quotes).

lqlqlq
05-04-2013, 12:01
New problem (cause server crash):

FATAL ERROR (shutting down): SV_ModelIndex: model models/v_glock18.mdl not precached

lqlqlq
06-13-2013, 03:37
Up again.

lqlqlq
06-13-2013, 07:11
Please, give me a fixed code.

XControlX
06-13-2013, 09:14
Hey, nice thread, but I think is better to change a model with Ham_Item_Deploy (if its just one/two weapons) because the CurWeapon event calling even when you shoot. Here is an example:

#include < amxmodx >
#include < fakemeta >
#include < hamsandwich >

new const g_szViewKnife[ ] = "models/Weapons/v_knife.mdl";

new const g_szWeaponKnife[ ] = "models/Weapons/p_knife.mdl";

public plugin_init( )
{
RegisterHam( Ham_Item_Deploy, "weapon_knife", "FwdKnifeDeploy", 1 );
}

public plugin_precache( )
{
precache_model( g_szWeaponKnife );

precache_model( g_szViewKnife );
}

public FwdKnifeDeploy( iEntity )
{
new client = get_pdata_cbase( iEntity, 41, 4 );

if ( ! is_user_connected( client ) || ! is_user_alive( client ) ) return 1;

if ( get_user_weapon( client ) == CSW_KNIFE )
{
set_pev( client, pev_viewmodel2, g_szViewKnife );

set_pev( client, pev_weaponmodel2, g_szWeaponKnife );
}

return 1;
}

Bos93
06-13-2013, 14:05
XControlX, Seriously? https://forums.alliedmods.net/showpost.php?p=1020078&postcount=61

And your code doesn't work. You need check cs_get_user_weapon.

+ remove is_user_connected,and you need check private data

lqlqlq
06-14-2013, 05:21
@Bos93, can you fixed ConnorMcleod's code ?

Bos93
06-14-2013, 05:35
show your latest version + debug and give me error

lqlqlq
06-14-2013, 06:57
version:

/* Formatright © 2009, ConnorMcLeod

Players Models is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Players Models; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

// #define SET_MODELINDEX

#include <amxmodx>
#include <fakemeta>

#define VERSION "1.3.1"

#define SetUserModeled(%1) g_bModeled |= 1<<(%1 & 31)
#define SetUserNotModeled(%1) g_bModeled &= ~( 1<<(%1 & 31) )
#define IsUserModeled(%1) ( g_bModeled & 1<<(%1 & 31) )

#define SetUserConnected(%1) g_bConnected |= 1<<(%1 & 31)
#define SetUserNotConnected(%1) g_bConnected &= ~( 1<<(%1 & 31) )
#define IsUserConnected(%1) ( g_bConnected & 1<<(%1 & 31) )

#define MAX_MODEL_LENGTH 16
#define MAX_AUTHID_LENGTH 25

#define MAX_PLAYERS 32

#define ClCorpse_ModelName 1
#define ClCorpse_PlayerID 12

#define m_iTeam 114
#define g_ulModelIndexPlayer 491
#define fm_cs_get_user_team_index(%1) get_pdata_int(%1, m_iTeam)

new const MODEL[] = "model";
new g_bModeled;
new g_szCurrentModel[MAX_PLAYERS+1][MAX_MODEL_LENGTH];

new Trie:g_tTeamModels[2];
new Trie:g_tModelIndexes;
new Trie:g_tDefaultModels;

new g_szAuthid[MAX_PLAYERS+1][MAX_AUTHID_LENGTH];
new g_bPersonalModel[MAX_PLAYERS+1];

new g_bConnected;

public plugin_init()
{
register_plugin("Players Models", VERSION, "ConnorMcLeod");

register_forward(FM_SetClientKeyValue, "SetClientKeyValue");
register_message(get_user_msgid("ClCorpse"), "Message_ClCorpse");
}

public plugin_precache()
{
new szConfigFile[128];
get_localinfo("amxx_configsdir", szConfigFile, charsmax(szConfigFile));
format(szConfigFile, charsmax(szConfigFile), "%s/players_models.ini", szConfigFile);

new iFile = fopen(szConfigFile, "rt");
if( iFile )
{
new const szDefaultModels[][] = {"", "urban", "terror", "leet", "arctic", "gsg9",
"gign", "sas", "guerilla", "vip", "militia", "spetsnaz" };
g_tDefaultModels = TrieCreate();
for(new i=1; i<sizeof(szDefaultModels); i++)
{
TrieSetCell(g_tDefaultModels, szDefaultModels[i], i);
}

g_tModelIndexes = TrieCreate();

g_tTeamModels[0] = TrieCreate();
g_tTeamModels[1] = TrieCreate();

new szDatas[70], szRest[40], szKey[MAX_AUTHID_LENGTH], szModel1[MAX_MODEL_LENGTH], szModel2[MAX_MODEL_LENGTH];
while( !feof(iFile) )
{
fgets(iFile, szDatas, charsmax(szDatas));
trim(szDatas);
if(!szDatas[0] || szDatas[0] == ';' || szDatas[0] == '#' || (szDatas[0] == '/' && szDatas[1] == '/'))
{
continue;
}

parse(szDatas, szKey, charsmax(szKey), szModel1, charsmax(szModel1), szModel2, charsmax(szModel2));

if( TrieKeyExists(g_tDefaultModels, szKey) )
{
if( szModel1[0] && !equal(szModel1, szKey) && PrecachePlayerModel(szModel1) )
{
TrieSetString(g_tDefaultModels, szKey, szModel1);
}
}
else if( equal(szKey, "STEAM_", 6) || equal(szKey, "BOT") )
{
parse(szRest, szModel1, charsmax(szModel1), szModel2, charsmax(szModel2));
if( szModel1[0] && PrecachePlayerModel(szModel1) )
{
TrieSetString(g_tTeamModels[1], szKey, szModel1);
}
if( szModel2[0] && PrecachePlayerModel(szModel2) )
{
TrieSetString(g_tTeamModels[0], szKey, szModel2);
}
}
}
fclose( iFile );
}
}

PrecachePlayerModel( const szModel[] )
{
if( TrieKeyExists(g_tModelIndexes, szModel) )
{
return 1;
}

new szFileToPrecache[64];
formatex(szFileToPrecache, charsmax(szFileToPrecache), "models/player/%s/%s.mdl", szModel, szModel);
if( !file_exists( szFileToPrecache ) && !TrieKeyExists(g_tDefaultModels, szModel) )
{
return 0;
}

TrieSetCell(g_tModelIndexes, szModel, precache_model(szFileToPrecache));

formatex(szFileToPrecache, charsmax(szFileToPrecache), "models/player/%s/%st.mdl", szModel, szModel);
if( file_exists( szFileToPrecache ) )
{
precache_model(szFileToPrecache);
return 1;
}
formatex(szFileToPrecache, charsmax(szFileToPrecache), "models/player/%s/%sT.mdl", szModel, szModel);
if( file_exists( szFileToPrecache ) )
{
precache_model(szFileToPrecache);
return 1;
}

return 1;
}

public plugin_end()
{
TrieDestroy(g_tTeamModels[0]);
TrieDestroy(g_tTeamModels[1]);
TrieDestroy(g_tModelIndexes);
TrieDestroy(g_tDefaultModels);
}

public client_authorized( id )
{
get_user_authid(id, g_szAuthid[id], MAX_AUTHID_LENGTH-1);

for(new i=1; i<=2; i++)
{
if( TrieKeyExists(g_tTeamModels[2-i], g_szAuthid[id]) )
{
g_bPersonalModel[id] |= i;
}
else
{
g_bPersonalModel[id] &= ~i;
}
}
}

public client_putinserver(id)
{
if( !is_user_hltv(id) )
{
SetUserConnected(id);
}
}

public client_disconnect(id)
{
g_bPersonalModel[id] = 0;
SetUserNotModeled(id);
SetUserNotConnected(id);
}

public SetClientKeyValue(id, const szInfoBuffer[], const szKey[], const szValue[])
{
if( equal(szKey, MODEL) && IsUserConnected(id) )
{
new iTeam = fm_cs_get_user_team_index(id);
if( 1 <= iTeam <= 2 )
{
new szSupposedModel[MAX_MODEL_LENGTH];

if( g_bPersonalModel[id] & iTeam )
{
TrieGetString(g_tTeamModels[2-iTeam], g_szAuthid[id], szSupposedModel, charsmax(szSupposedModel));
}
else
{
TrieGetString(g_tDefaultModels, szValue, szSupposedModel, charsmax(szSupposedModel));
}

if( szSupposedModel[0] )
{
if( !IsUserModeled(id)
|| !equal(g_szCurrentModel[id], szSupposedModel)
|| !equal(szValue, szSupposedModel) )
{
copy(g_szCurrentModel[id], MAX_MODEL_LENGTH-1, szSupposedModel);
SetUserModeled(id);
set_user_info(id, MODEL, szSupposedModel);
#if defined SET_MODELINDEX
new iModelIndex;
TrieGetCell(g_tModelIndexes, szSupposedModel, iModelIndex);
// set_pev(id, pev_modelindex, iModelIndex); // is this needed ?
set_pdata_int(id, g_ulModelIndexPlayer, iModelIndex);
#endif
return FMRES_SUPERCEDE;
}
}

if( IsUserModeled(id) )
{
SetUserNotModeled(id);
g_szCurrentModel[id][0] = 0;
}
}
}
return FMRES_IGNORED;
}

public Message_ClCorpse()
{
new id = get_msg_arg_int(ClCorpse_PlayerID);
if( IsUserModeled(id) )
{
set_msg_arg_string(ClCorpse_ModelName, g_szCurrentModel[id]);
}
}

Errors (2):

L 04/17/2013 - 17:30:32: [HAMSANDWICH] Invalid player 1 (not in-game)
L 04/17/2013 - 17:30:32: [AMXX] Run time error 10 (plugin "weapons_models.amxx") (native "get_pdata_cbase") - debug not enabled!
L 04/17/2013 - 17:30:32: [AMXX] To enable debug mode, add "debug" after the plugin name in plugins.ini (without quotes).

I not have debuged version of the error. Maybe it will means to have a check for the player live/not live.

second:
FATAL ERROR (shutting down): SV_ModelIndex: model models/v_glock18.mdl not precached

lqlqlq
06-14-2013, 07:27
here:

"models/v_ak47.mdl" "models/css/v_ak47.mdl"
"models/v_aug.mdl" "models/css/v_aug.mdl"
"models/v_awp.mdl" "models/css/v_awp.mdl"
"models/v_c4.mdl" "models/css/v_c4.mdl"
"models/v_deagle.mdl" "models/css/v_deagle.mdl"
"models/v_elite.mdl" "models/css/v_elite.mdl"
"models/v_famas.mdl" "models/css/v_famas.mdl"
"models/v_fiveseven.mdl" "models/css/v_fiveseven.mdl"
"models/v_flashbang.mdl" "models/css/v_flashbang.mdl"
"models/v_g3sg1.mdl" "models/css/v_g3sg1.mdl"
"models/v_galil.mdl" "models/css/v_galil.mdl"
"models/v_glock18.mdl" "models/css/v_glock18.mdl"
"models/v_hegrenade.mdl" "models/css/v_hegrenade.mdl"
"models/v_knife.mdl" "models/css/v_knife.mdl"
"models/v_m3.mdl" "models/css/v_m3.mdl"
"models/v_m4a1.mdl" "models/css/v_m4a1.mdl"
"models/v_m249.mdl" "models/css/v_m249.mdl"
"models/v_mac10.mdl" "models/css/v_mac10.mdl"
"models/v_mp5.mdl" "models/css/v_mp5.mdl"
"models/v_p90.mdl" "models/css/v_p90.mdl"
"models/v_p228.mdl" "models/css/v_p228.mdl"
"models/v_scout.mdl" "models/css/v_scout.mdl"
"models/v_sg550.mdl" "models/css/v_sg550.mdl"
"models/v_sg552.mdl" "models/css/v_sg552.mdl"
"models/v_smokegrenade.mdl" "models/css/v_smokegrenade.mdl"
"models/v_tmp.mdl" "models/css/v_tmp.mdl"
"models/v_ump45.mdl" "models/css/v_ump45.mdl"
"models/v_usp.mdl" "models/css/v_usp.mdl"
"models/v_xm1014.mdl" "models/css/v_xm1014.mdl"

The errors happens on every 4-5 retry's from players.

Bos93
06-14-2013, 07:39
this plugin http://forums.alliedmods.net/showpost.php?p=1970148&postcount=87 for change player models,but your config for weapon models.

?

lqlqlq
06-14-2013, 07:49
awww...Sorry.
here is:

/* Formatright © 2012, ConnorMcLeod

Weapons Models is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Weapons Models; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

#define VERSION "0.2.2"

/* ChangeLog
*
* v0.2.2
* - Fixed bad formating that was preventing default models from being detected
*
* v0.2.1
* - Fixed "Invalid trie handle provided" error
*
* v0.2.0
* - Check viewmodel and weaponmodel instead of weapon type, so it now supports shield models
*
* v0.1.7
* - Adjustments
*
* v0.1.6
* - Remove FreeEntPrivateData, was causing player from not being reconnected to server
*
* v0.1.5
* - Don't need to precache backpack model anymore
*
* v0.1.4
* - Store allocated view and weapon models in arrays instead of Trie
* - Fixed w_backpack.mdl was not precached (bad string name was passed...)
*
* v0.1.3
* - Store precache returns values into a trie instead of checking trie and resend precache
* - Only store world new models strings in a trie
*
* v0.1.2
* - Moved backpack model precache from precache hook to plugin_precache
*
* v0.1.1
* - Fixed first map where weapons were not registering
*
* v0.1.0
* - Hook Ham_Item_Deploy and set models there instead of hooking FM_ModelIndex that was intensive
*
* v0.0.1 First Shot
*/

#include <amxmodx>
#include <fakemeta_stocks>
#include <hamsandwich>

const MAX_MODEL_LENGTH = 64

new const m_rgpPlayerItems_CBasePlayer[6] = {367,368,...}
const m_pActiveItem = 373

const XO_WEAPON = 4
const m_pPlayer = 41

new Trie:g_tszWorldModels // handles new models strings (all excepted v_ and w_ models)
new Trie:g_tiPrecacheReturns // handles all new models precache returns integers
new Trie:g_tiszViewModels
new Trie:g_tiszWeaponModels

public plugin_precache()
{
new szModelsFile[128]
get_localinfo("amxx_configsdir", szModelsFile, charsmax(szModelsFile))
add(szModelsFile, charsmax(szModelsFile), "/weapons_models.ini")

new iFile = fopen(szModelsFile, "rt")
if(!iFile)
{
return
}

new szDatas[192], szOldModel[MAX_MODEL_LENGTH], szNewModel[MAX_MODEL_LENGTH]
new szWeaponClass[32], Trie:tRegisterWeaponDeploy = TrieCreate(), iId

new Trie:tWeaponsIds = TrieCreate()
TrieSetCell(tWeaponsIds, "p228", CSW_P228)
TrieSetCell(tWeaponsIds, "scout", CSW_SCOUT)
TrieSetCell(tWeaponsIds, "hegrenade", CSW_HEGRENADE)
TrieSetCell(tWeaponsIds, "xm1014", CSW_XM1014)
TrieSetCell(tWeaponsIds, "c4", CSW_C4)
TrieSetCell(tWeaponsIds, "mac10", CSW_MAC10)
TrieSetCell(tWeaponsIds, "aug", CSW_AUG)
TrieSetCell(tWeaponsIds, "smokegrenade", CSW_SMOKEGRENADE)
TrieSetCell(tWeaponsIds, "elite", CSW_ELITE)
TrieSetCell(tWeaponsIds, "fiveseven", CSW_FIVESEVEN)
TrieSetCell(tWeaponsIds, "ump45", CSW_UMP45)
TrieSetCell(tWeaponsIds, "sg550", CSW_SG550)
TrieSetCell(tWeaponsIds, "galil", CSW_GALIL)
TrieSetCell(tWeaponsIds, "famas", CSW_FAMAS)
TrieSetCell(tWeaponsIds, "usp", CSW_USP)
TrieSetCell(tWeaponsIds, "glock18", CSW_GLOCK18)
TrieSetCell(tWeaponsIds, "awp", CSW_AWP)
TrieSetCell(tWeaponsIds, "mp5navy", CSW_MP5NAVY)
TrieSetCell(tWeaponsIds, "m249", CSW_M249)
TrieSetCell(tWeaponsIds, "m3", CSW_M3)
TrieSetCell(tWeaponsIds, "m4a1", CSW_M4A1)
TrieSetCell(tWeaponsIds, "tmp", CSW_TMP)
TrieSetCell(tWeaponsIds, "g3sg1", CSW_G3SG1)
TrieSetCell(tWeaponsIds, "flashbang", CSW_FLASHBANG)
TrieSetCell(tWeaponsIds, "deagle", CSW_DEAGLE)
TrieSetCell(tWeaponsIds, "sg552", CSW_SG552)
TrieSetCell(tWeaponsIds, "ak47", CSW_AK47)
TrieSetCell(tWeaponsIds, "knife", CSW_KNIFE)
TrieSetCell(tWeaponsIds, "p90", CSW_P90)

new c, bool:bServerDeactivateRegistered, iExtPos
while(!feof(iFile))
{
fgets(iFile, szDatas, charsmax(szDatas))
trim(szDatas)
if(!(c=szDatas[0]) || c == ';' || c == '#' || (c == '/' && szDatas[1] == '/'))
{
continue
}

if( parse(szDatas, szOldModel, charsmax(szOldModel), szNewModel, charsmax(szNewModel)) == 2
&& file_exists(szNewModel) )
{
if( ( (c=szOldModel[7]) == 'p' || c == 'v' ) && szOldModel[8] == '_' )
{
if( equal(szOldModel[9], "mp5", 3 ) )
{
copy(szWeaponClass, charsmax(szWeaponClass), "weapon_mp5navy")
}
else
{
iExtPos = strlen(szOldModel) - 4
szOldModel[ iExtPos ] = EOS
formatex(szWeaponClass, charsmax(szWeaponClass), "weapon_%s", szOldModel[9])
szOldModel[ iExtPos ] = '.'
}

if( !TrieGetCell(tWeaponsIds, szWeaponClass[7], iId) )
{
continue
}

if( c == 'v' )
{
if( !g_tiszViewModels )
{
g_tiszViewModels = TrieCreate()
}

TrieSetCell(g_tiszViewModels, szOldModel, EF_AllocString( szNewModel ) )
}
else
{
if( !g_tiszWeaponModels )
{
g_tiszWeaponModels = TrieCreate()
}

TrieSetCell(g_tiszWeaponModels, szOldModel, EF_AllocString( szNewModel ) )
}

if( !TrieKeyExists(tRegisterWeaponDeploy, szWeaponClass) )
{
TrieSetCell
(
tRegisterWeaponDeploy,
szWeaponClass,
RegisterHam(Ham_Item_Deploy, szWeaponClass, "MiscWeapon_Deploy_Post", true)
)
}
}
else
{
if( !bServerDeactivateRegistered && equal(szOldModel, "models/w_backpack.mdl") )
{
bServerDeactivateRegistered = true
register_forward(FM_ServerDeactivate, "ServerDeactivate")
}

if( !g_tszWorldModels )
{
g_tszWorldModels = TrieCreate()
}
else if( TrieKeyExists(g_tszWorldModels, szOldModel) )
{
new szModel[MAX_MODEL_LENGTH]
TrieGetString(g_tszWorldModels, szOldModel, szModel, charsmax(szModel))
log_amx("%s world model is already set to %s, can't set it to %s !!", szWeaponClass, szModel, szNewModel)
continue
}
TrieSetString(g_tszWorldModels, szOldModel, szNewModel)
}

if( !g_tiPrecacheReturns )
{
g_tiPrecacheReturns = TrieCreate()
}
TrieSetCell(g_tiPrecacheReturns, szOldModel, EF_PrecacheModel(szNewModel))
}
}
fclose(iFile)

TrieDestroy(tRegisterWeaponDeploy)
TrieDestroy(tWeaponsIds)

if( g_tiPrecacheReturns )
{
register_forward(FM_PrecacheModel, "PrecacheModel")

if( g_tszWorldModels )
{
register_forward(FM_SetModel, "SetModel")
}
}
}

public plugin_init()
{
register_plugin("Weapons Models", VERSION, "ConnorMcLeod")
}

public ServerDeactivate()
{
static bool:bDontPassThisTwice = false
if( bDontPassThisTwice ) // unregister this would be waste of time
{
return
}
bDontPassThisTwice = true

new id, c4 = FM_NULLENT
while( (c4 = EF_FindEntityByString(c4, "classname", "weapon_c4")) > 0 )
{
id = get_pdata_cbase(c4, m_pPlayer)
if( id > 0 )
{
// can't use set_pdata_cbase on players at this point
set_pdata_int(id, m_rgpPlayerItems_CBasePlayer[5], 0)
set_pdata_int(id, m_pActiveItem, 0)
// tried to remove c4 entity but server just stucks
}
}
}

public PrecacheModel(const szModel[])
{
static iReturn
if( TrieGetCell(g_tiPrecacheReturns, szModel, iReturn) )
{
forward_return(FMV_CELL, iReturn)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

public MiscWeapon_Deploy_Post( iWeapon )
{
new id = get_pdata_cbase(iWeapon, m_pPlayer, XO_WEAPON)
if( !is_user_alive(id) )
{
return
}
if( get_pdata_cbase(id, m_pActiveItem) == iWeapon )
{
new iszNewModel, szOldModel[MAX_MODEL_LENGTH]
pev(id, pev_viewmodel2, szOldModel, charsmax(szOldModel))
if( g_tiszViewModels && TrieGetCell(g_tiszViewModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_viewmodel, iszNewModel)
}
if( g_tiszWeaponModels && TrieGetCell(g_tiszWeaponModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_weaponmodel, iszNewModel)
}
}
}

public SetModel(const iEnt, const szModel[])
{
new szNewModel[MAX_MODEL_LENGTH]
if( TrieGetString(g_tszWorldModels, szModel, szNewModel, charsmax(szNewModel)) )
{
EF_SetModel(iEnt, szNewModel)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}
Im confuse in speed ...

Bos93
06-14-2013, 08:00
enable debug, to know exactly where the error.

but why default model doesn't precache, i do not know.

ConnorMcLeod
06-14-2013, 11:44
enable debug, to know exactly where the error.

but why default model doesn't precache, i do not know.

I can't see his messages since he is on my ignorelist, but if it can help, i think the error may comes from another plugin trying to set the default model.
Or latest cs version having something changed and not hooked by plugin.

Bos93
06-14-2013, 11:52
Connor,

here is:

/* Formatright © 2012, ConnorMcLeod

Weapons Models is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Weapons Models; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

#define VERSION "0.2.2"

/* ChangeLog
*
* v0.2.2
* - Fixed bad formating that was preventing default models from being detected
*
* v0.2.1
* - Fixed "Invalid trie handle provided" error
*
* v0.2.0
* - Check viewmodel and weaponmodel instead of weapon type, so it now supports shield models
*
* v0.1.7
* - Adjustments
*
* v0.1.6
* - Remove FreeEntPrivateData, was causing player from not being reconnected to server
*
* v0.1.5
* - Don't need to precache backpack model anymore
*
* v0.1.4
* - Store allocated view and weapon models in arrays instead of Trie
* - Fixed w_backpack.mdl was not precached (bad string name was passed...)
*
* v0.1.3
* - Store precache returns values into a trie instead of checking trie and resend precache
* - Only store world new models strings in a trie
*
* v0.1.2
* - Moved backpack model precache from precache hook to plugin_precache
*
* v0.1.1
* - Fixed first map where weapons were not registering
*
* v0.1.0
* - Hook Ham_Item_Deploy and set models there instead of hooking FM_ModelIndex that was intensive
*
* v0.0.1 First Shot
*/

#include <amxmodx>
#include <fakemeta_stocks>
#include <hamsandwich>

const MAX_MODEL_LENGTH = 64

new const m_rgpPlayerItems_CBasePlayer[6] = {367,368,...}
const m_pActiveItem = 373

const XO_WEAPON = 4
const m_pPlayer = 41

new Trie:g_tszWorldModels // handles new models strings (all excepted v_ and w_ models)
new Trie:g_tiPrecacheReturns // handles all new models precache returns integers
new Trie:g_tiszViewModels
new Trie:g_tiszWeaponModels

public plugin_precache()
{
new szModelsFile[128]
get_localinfo("amxx_configsdir", szModelsFile, charsmax(szModelsFile))
add(szModelsFile, charsmax(szModelsFile), "/weapons_models.ini")

new iFile = fopen(szModelsFile, "rt")
if(!iFile)
{
return
}

new szDatas[192], szOldModel[MAX_MODEL_LENGTH], szNewModel[MAX_MODEL_LENGTH]
new szWeaponClass[32], Trie:tRegisterWeaponDeploy = TrieCreate(), iId

new Trie:tWeaponsIds = TrieCreate()
TrieSetCell(tWeaponsIds, "p228", CSW_P228)
TrieSetCell(tWeaponsIds, "scout", CSW_SCOUT)
TrieSetCell(tWeaponsIds, "hegrenade", CSW_HEGRENADE)
TrieSetCell(tWeaponsIds, "xm1014", CSW_XM1014)
TrieSetCell(tWeaponsIds, "c4", CSW_C4)
TrieSetCell(tWeaponsIds, "mac10", CSW_MAC10)
TrieSetCell(tWeaponsIds, "aug", CSW_AUG)
TrieSetCell(tWeaponsIds, "smokegrenade", CSW_SMOKEGRENADE)
TrieSetCell(tWeaponsIds, "elite", CSW_ELITE)
TrieSetCell(tWeaponsIds, "fiveseven", CSW_FIVESEVEN)
TrieSetCell(tWeaponsIds, "ump45", CSW_UMP45)
TrieSetCell(tWeaponsIds, "sg550", CSW_SG550)
TrieSetCell(tWeaponsIds, "galil", CSW_GALIL)
TrieSetCell(tWeaponsIds, "famas", CSW_FAMAS)
TrieSetCell(tWeaponsIds, "usp", CSW_USP)
TrieSetCell(tWeaponsIds, "glock18", CSW_GLOCK18)
TrieSetCell(tWeaponsIds, "awp", CSW_AWP)
TrieSetCell(tWeaponsIds, "mp5navy", CSW_MP5NAVY)
TrieSetCell(tWeaponsIds, "m249", CSW_M249)
TrieSetCell(tWeaponsIds, "m3", CSW_M3)
TrieSetCell(tWeaponsIds, "m4a1", CSW_M4A1)
TrieSetCell(tWeaponsIds, "tmp", CSW_TMP)
TrieSetCell(tWeaponsIds, "g3sg1", CSW_G3SG1)
TrieSetCell(tWeaponsIds, "flashbang", CSW_FLASHBANG)
TrieSetCell(tWeaponsIds, "deagle", CSW_DEAGLE)
TrieSetCell(tWeaponsIds, "sg552", CSW_SG552)
TrieSetCell(tWeaponsIds, "ak47", CSW_AK47)
TrieSetCell(tWeaponsIds, "knife", CSW_KNIFE)
TrieSetCell(tWeaponsIds, "p90", CSW_P90)

new c, bool:bServerDeactivateRegistered, iExtPos
while(!feof(iFile))
{
fgets(iFile, szDatas, charsmax(szDatas))
trim(szDatas)
if(!(c=szDatas[0]) || c == ';' || c == '#' || (c == '/' && szDatas[1] == '/'))
{
continue
}

if( parse(szDatas, szOldModel, charsmax(szOldModel), szNewModel, charsmax(szNewModel)) == 2
&& file_exists(szNewModel) )
{
if( ( (c=szOldModel[7]) == 'p' || c == 'v' ) && szOldModel[8] == '_' )
{
if( equal(szOldModel[9], "mp5", 3 ) )
{
copy(szWeaponClass, charsmax(szWeaponClass), "weapon_mp5navy")
}
else
{
iExtPos = strlen(szOldModel) - 4
szOldModel[ iExtPos ] = EOS
formatex(szWeaponClass, charsmax(szWeaponClass), "weapon_%s", szOldModel[9])
szOldModel[ iExtPos ] = '.'
}

if( !TrieGetCell(tWeaponsIds, szWeaponClass[7], iId) )
{
continue
}

if( c == 'v' )
{
if( !g_tiszViewModels )
{
g_tiszViewModels = TrieCreate()
}

TrieSetCell(g_tiszViewModels, szOldModel, EF_AllocString( szNewModel ) )
}
else
{
if( !g_tiszWeaponModels )
{
g_tiszWeaponModels = TrieCreate()
}

TrieSetCell(g_tiszWeaponModels, szOldModel, EF_AllocString( szNewModel ) )
}

if( !TrieKeyExists(tRegisterWeaponDeploy, szWeaponClass) )
{
TrieSetCell
(
tRegisterWeaponDeploy,
szWeaponClass,
RegisterHam(Ham_Item_Deploy, szWeaponClass, "MiscWeapon_Deploy_Post", true)
)
}
}
else
{
if( !bServerDeactivateRegistered && equal(szOldModel, "models/w_backpack.mdl") )
{
bServerDeactivateRegistered = true
register_forward(FM_ServerDeactivate, "ServerDeactivate")
}

if( !g_tszWorldModels )
{
g_tszWorldModels = TrieCreate()
}
else if( TrieKeyExists(g_tszWorldModels, szOldModel) )
{
new szModel[MAX_MODEL_LENGTH]
TrieGetString(g_tszWorldModels, szOldModel, szModel, charsmax(szModel))
log_amx("%s world model is already set to %s, can't set it to %s !!", szWeaponClass, szModel, szNewModel)
continue
}
TrieSetString(g_tszWorldModels, szOldModel, szNewModel)
}

if( !g_tiPrecacheReturns )
{
g_tiPrecacheReturns = TrieCreate()
}
TrieSetCell(g_tiPrecacheReturns, szOldModel, EF_PrecacheModel(szNewModel))
}
}
fclose(iFile)

TrieDestroy(tRegisterWeaponDeploy)
TrieDestroy(tWeaponsIds)

if( g_tiPrecacheReturns )
{
register_forward(FM_PrecacheModel, "PrecacheModel")

if( g_tszWorldModels )
{
register_forward(FM_SetModel, "SetModel")
}
}
}

public plugin_init()
{
register_plugin("Weapons Models", VERSION, "ConnorMcLeod")
}

public ServerDeactivate()
{
static bool:bDontPassThisTwice = false
if( bDontPassThisTwice ) // unregister this would be waste of time
{
return
}
bDontPassThisTwice = true

new id, c4 = FM_NULLENT
while( (c4 = EF_FindEntityByString(c4, "classname", "weapon_c4")) > 0 )
{
id = get_pdata_cbase(c4, m_pPlayer)
if( id > 0 )
{
// can't use set_pdata_cbase on players at this point
set_pdata_int(id, m_rgpPlayerItems_CBasePlayer[5], 0)
set_pdata_int(id, m_pActiveItem, 0)
// tried to remove c4 entity but server just stucks
}
}
}

public PrecacheModel(const szModel[])
{
static iReturn
if( TrieGetCell(g_tiPrecacheReturns, szModel, iReturn) )
{
forward_return(FMV_CELL, iReturn)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

public MiscWeapon_Deploy_Post( iWeapon )
{
new id = get_pdata_cbase(iWeapon, m_pPlayer, XO_WEAPON)
if( !is_user_alive(id) )
{
return
}
if( get_pdata_cbase(id, m_pActiveItem) == iWeapon )
{
new iszNewModel, szOldModel[MAX_MODEL_LENGTH]
pev(id, pev_viewmodel2, szOldModel, charsmax(szOldModel))
if( g_tiszViewModels && TrieGetCell(g_tiszViewModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_viewmodel, iszNewModel)
}
if( g_tiszWeaponModels && TrieGetCell(g_tiszWeaponModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_weaponmodel, iszNewModel)
}
}
}

public SetModel(const iEnt, const szModel[])
{
new szNewModel[MAX_MODEL_LENGTH]
if( TrieGetString(g_tszWorldModels, szModel, szNewModel, charsmax(szNewModel)) )
{
EF_SetModel(iEnt, szNewModel)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED
}

Errors (2):

L 04/17/2013 - 17:30:32: [HAMSANDWICH] Invalid player 1 (not in-game)
L 04/17/2013 - 17:30:32: [AMXX] Run time error 10 (plugin "weapons_models.amxx") (native "get_pdata_cbase") - debug not enabled!
L 04/17/2013 - 17:30:32: [AMXX] To enable debug mode, add "debug" after the plugin name in plugins.ini (without quotes).[/code]

I not have debuged version of the error. Maybe it will means to have a check for the player live/not live.

second:
FATAL ERROR (shutting down): SV_ModelIndex: model models/v_glock18.mdl not precached

The errors happens on every 4-5 retry's from players.

ConnorMcLeod
06-14-2013, 13:21
You have to search for a plugin sending Ham_Item_Deploy and using ExecuteHam, then replace with ExecuteHamB

Bos93
06-14-2013, 13:46
why do not you check private data?

ConnorMcLeod
06-14-2013, 18:52
When Ham functions are sent, entity should have valid pdata, to remove entity it is better to delay it setting FL_KILLME + nextthink. Could add it anyway for sure.

lqlqlq
06-15-2013, 03:24
Yep, i use a plugin with Ham_Item_Deploy:
backweapons from cheap_suit.
Can you fixed?


#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>

#define PLUGIN "Back Weapons"
#define AUTHOR "hoboman313/cheap_suit"
#define VERSION "1.87"

#define MAX_PLAYERS 32
#define OFFSET_PRIMARYWEAPON 116
#define OFFSET_WEAPONTYPE 43
#define EXTRAOFFSET_WEAPONS 4
#define OFFSET_AUTOSWITCH 509
#define OFFSET_SHIELD 510
#define HAS_SHIELD (1<<24)

#define PRIMARY_WEAPONS (1<<CSW_SCOUT | 1<<CSW_XM1014 | 1<<CSW_MAC10 | 1<<CSW_AUG | 1<<CSW_UMP45 | 1<<CSW_SG550 | 1<<CSW_GALIL | 1<<CSW_FAMAS | 1<<CSW_AWP | 1<<CSW_MP5NAVY | 1<<CSW_M249 | 1<<CSW_M3 | 1<<CSW_M4A1 | 1<<CSW_TMP | 1<<CSW_G3SG1 | 1<<CSW_SG552 | 1<<CSW_AK47 | 1<<CSW_P90)

#define is_weapon_primary(%1) (PRIMARY_WEAPONS & (1<<%1))
#define cs_get_weapon_type(%1) get_pdata_int(%1, OFFSET_WEAPONTYPE, EXTRAOFFSET_WEAPONS)
#define cs_get_user_hasprim(%1) get_pdata_int(%1, OFFSET_PRIMARYWEAPON)
#define cs_get_user_autoswitch(%1) get_pdata_int(%1, OFFSET_AUTOSWITCH)
#define cs_get_user_shield(%1) (get_pdata_int(%1, OFFSET_SHIELD) & HAS_SHIELD) ? 1 : 0

enum
{
MODEL_NULL = 0,
MODEL_AUG = 1,
MODEL_AK47 = 2,
MODEL_AWP = 3,
MODEL_MP5NAVY = 4,
MODEL_P90 = 5,
MODEL_GALIL = 6,
MODEL_M4A1 = 7,
MODEL_SG550 = 8,
MODEL_SG552 = 9,
MODEL_SCOUT = 10,
MODEL_XM1014 = 11,
MODEL_M3 = 12,
MODEL_G3SG1 = 13,
MODEL_M249 = 14,
MODEL_FAMAS = 15,
MODEL_UMP45 = 16
}

new g_weapons[][] =
{
"weapon_p228",
"weapon_scout",
"weapon_hegrenade",
"weapon_xm1014",
"weapon_c4",
"weapon_mac10",
"weapon_aug",
"weapon_smokegrenade",
"weapon_elite",
"weapon_fiveseven",
"weapon_ump45",
"weapon_sg550",
"weapon_galil",
"weapon_famas",
"weapon_usp",
"weapon_glock18",
"weapon_awp",
"weapon_mp5navy",
"weapon_m249",
"weapon_m3",
"weapon_m4a1",
"weapon_tmp",
"weapon_g3sg1",
"weapon_flashbang",
"weapon_deagle",
"weapon_sg552",
"weapon_ak47",
"weapon_knife",
"weapon_p90"
}

new g_weaponclass[] = "backweapon"
new g_weaponmodel[] = "models/backweapons34.mdl"

new g_weaponent[MAX_PLAYERS+1]

public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_cvar(PLUGIN, VERSION, FCVAR_SPONLY|FCVAR_SERVER)

RegisterHam(Ham_Killed, "player", "bacon_killed")
RegisterHam(Ham_Spawn, "player", "bacon_spawn_post", 1)
RegisterHam(Ham_AddPlayerItem, "player", "bacon_addplayeritem")
RegisterHam(Ham_RemovePlayerItem, "player", "bacon_removeplayeritem")

for(new i = 0; i < sizeof g_weapons; i++)
{
RegisterHam(Ham_Item_AttachToPlayer, g_weapons[i], "bacon_item_attachtoplayer_post", 1)
RegisterHam(Ham_Item_Deploy, g_weapons[i], "bacon_item_deploy_post", 1)
}
}

public plugin_precache()
precache_model(g_weaponmodel)

public client_putinserver(id)
{
static infotarget
if(!infotarget) infotarget = engfunc(EngFunc_AllocString, "info_target")

g_weaponent[id] = engfunc(EngFunc_CreateNamedEntity, infotarget)
if(pev_valid(g_weaponent[id]))
{
engfunc(EngFunc_SetModel, g_weaponent[id], g_weaponmodel)
set_pev(g_weaponent[id], pev_classname, g_weaponclass)
set_pev(g_weaponent[id], pev_movetype, MOVETYPE_FOLLOW)
set_pev(g_weaponent[id], pev_effects, EF_NODRAW)
set_pev(g_weaponent[id], pev_aiment, id)
}
}

public client_disconnect(id)
{
if(g_weaponent[id] > 0 && pev_valid(g_weaponent[id]))
engfunc(EngFunc_RemoveEntity, g_weaponent[id])

g_weaponent[id] = 0
}

public bacon_killed(id, idattacker, shouldgib)
fm_set_entity_visibility(g_weaponent[id], 0)

public bacon_addplayeritem(id, ent)
{
static weaponid; weaponid = cs_get_weapon_type(ent)
if(is_weapon_primary(weaponid) && pev_valid(g_weaponent[id]))
{
fm_set_entity_visibility(g_weaponent[id], 0)
set_pev(g_weaponent[id], pev_body, get_weapon_model(weaponid))
}
}

public bacon_removeplayeritem(id, ent)
{
if(is_weapon_primary(cs_get_weapon_type(ent)) && pev_valid(g_weaponent[id]))
fm_set_entity_visibility(g_weaponent[id], 0)
}

public bacon_spawn_post(id) if(is_user_alive(id))
{
if(!cs_get_user_hasprim(id))
fm_set_entity_visibility(g_weaponent[id], 0)
}

public bacon_item_attachtoplayer_post(ent, id) if(is_user_alive(id) && !cs_get_user_autoswitch(id))
{
if(is_weapon_primary(cs_get_weapon_type(ent)) && pev_valid(g_weaponent[id]))
fm_set_entity_visibility(g_weaponent[id], 1)
}

public bacon_item_deploy_post(ent)
{
static id; id = pev(ent, pev_owner)
if(is_user_alive(id))
{
static weapon; weapon = cs_get_weapon_type(ent)
if(is_weapon_primary(weapon) || cs_get_user_shield(id))
fm_set_entity_visibility(g_weaponent[id], 0)

else if(cs_get_user_hasprim(id))
fm_set_entity_visibility(g_weaponent[id], 1)
}
}

stock get_weapon_model(weapon)
{
switch(weapon)
{
case CSW_SCOUT: return MODEL_SCOUT
case CSW_XM1014: return MODEL_XM1014
case CSW_AUG: return MODEL_AUG
case CSW_UMP45: return MODEL_UMP45
case CSW_SG550: return MODEL_SG550
case CSW_GALIL: return MODEL_GALIL
case CSW_FAMAS: return MODEL_FAMAS
case CSW_AWP: return MODEL_AWP
case CSW_MP5NAVY: return MODEL_MP5NAVY
case CSW_M249: return MODEL_M249
case CSW_M3: return MODEL_M3
case CSW_M4A1: return MODEL_M4A1
case CSW_G3SG1: return MODEL_G3SG1
case CSW_SG552: return MODEL_SG552
case CSW_AK47: return MODEL_AK47
case CSW_P90: return MODEL_P90
}
return 0
}

stock fm_set_entity_visibility(index, visible = 1)
set_pev(index, pev_effects, visible == 1 ? pev(index, pev_effects) & ~EF_NODRAW : pev(index, pev_effects) | EF_NODRAW)

Fr33m@n
07-12-2013, 10:03
Hi,

So first, ConnorMcLeod, there is many bugs with your plugin :

- With this plugin i experienced more crash server than usual (2 per day)
I can confirm this, because now, since one month, i'm using a modified GHW_model_replacement (ham for deploy instead of CurWeapon) instead and i experience a lot less crash.

- I also experienced this issue :
L 06/26/2013 - 01:47:19: [HAMSANDWICH] Invalid player 1 (not in-game)
L 06/26/2013 - 01:47:19: [AMXX] Displaying debug trace (plugin "weapons_models.amxx")
L 06/26/2013 - 01:47:19: [AMXX] Run time error 10: native error (native "get_pdata_cbase")
L 06/26/2013 - 01:47:19: [AMXX] [0] weapons_models.sma::MiscWeapon_Deploy_Post (line 273)
When i "fixed" with is_user_alive, it jumped to 1 crash per map.

So i bet deploy is sometimes called when is_user_alive is false for this player but this call is necessary for server stability.

When i used pev_valid or is_user_connected, it fixed the log issue but not the crashing issue (2 per day crash is still alive)

- So i bet that somewhere in hl/cs source there is weapons models set, outside of deploy.

- Setting a p_ + a v_ model in the same time

Bos93
07-12-2013, 14:58
try check private data:

public MiscWeapon_Deploy_Post( iWeapon )
{
if( pev_valid(iWeapon) != 2 )
return;

new id = get_pdata_cbase(iWeapon, m_pPlayer, XO_WEAPON)

if( get_pdata_cbase(id, m_pActiveItem) == iWeapon )
{
new iszNewModel, szOldModel[MAX_MODEL_LENGTH]
pev(id, pev_viewmodel2, szOldModel, charsmax(szOldModel))
if( g_tiszViewModels && TrieGetCell(g_tiszViewModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_viewmodel, iszNewModel)
}
if( g_tiszWeaponModels && TrieGetCell(g_tiszWeaponModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_weaponmodel, iszNewModel)
}
}
}

ConnorMcLeod
07-13-2013, 08:14
This won't help, in his case, the error line is the next line, weapon has correct pdata since m_pPlayer has already returned 1.

ConnorMcLeod
07-13-2013, 09:42
https://forums.alliedmods.net/showthread.php?p=1020078#post1020078

* v 0.2.3
* - Added shield support
* - Fixed missing p_ models support that were causing crashes
* - Added pdata check on player on weapon deploy event

Also, Freeman, if you experience crashes, you can enable line 65 and recompile :
// #define DONT_BLOCK_PRECACHE
And see if crashes still happen or not.

If line 286 ( get_pdata_cbase(id, m_pActiveItem) ) still throw errors after pev_valid(id) == 2 check is passed, fault is on hamsandwich module, can't do anything on it (bug reported https://bugs.alliedmods.net/show_bug.cgi?id=5787 ).

baneado
11-06-2013, 15:46
That way doesn't increase precached models number :
(Similar way than here (http://forums.alliedmods.net/showthread.php?p=514507))

configs/weapons_models.ini :
example :

// View Models
"models/v_ak47.mdl" "models/css/v_ak47.mdl"
"models/v_aug.mdl" "models/css/v_aug.mdl"
"models/v_awp.mdl" "models/css/v_awp.mdl"
"models/v_c4.mdl" "models/css/v_c4.mdl"
"models/v_deagle.mdl" "models/css/v_deagle.mdl"
"models/v_elite.mdl" "models/css/v_elite.mdl"
"models/v_famas.mdl" "models/css/v_famas.mdl"
"models/v_fiveseven.mdl" "models/css/v_fiveseven.mdl"
"models/v_flashbang.mdl" "models/css/v_flashbang.mdl"
"models/v_g3sg1.mdl" "models/css/v_g3sg1.mdl"
"models/v_galil.mdl" "models/css/v_galil.mdl"
"models/v_glock18.mdl" "models/css/v_glock18.mdl"
"models/v_hegrenade.mdl" "models/css/v_hegrenade.mdl"
"models/v_knife.mdl" "models/css/v_knife.mdl"
"models/v_m3.mdl" "models/css/v_m3.mdl"
"models/v_m4a1.mdl" "models/css/v_m4a1.mdl"
"models/v_m249.mdl" "models/css/v_m249.mdl"
"models/v_mac10.mdl" "models/css/v_mac10.mdl"
"models/v_mp5.mdl" "models/css/v_mp5.mdl"
"models/v_p90.mdl" "models/css/v_p90.mdl"
"models/v_p228.mdl" "models/css/v_p228.mdl"
"models/v_scout.mdl" "models/css/v_scout.mdl"
"models/v_sg550.mdl" "models/css/v_sg550.mdl"
"models/v_sg552.mdl" "models/css/v_sg552.mdl"
"models/v_smokegrenade.mdl" "models/css/v_smokegrenade.mdl"
"models/v_tmp.mdl" "models/css/v_tmp.mdl"
"models/v_ump45.mdl" "models/css/v_ump45.mdl"
"models/v_usp.mdl" "models/css/v_usp.mdl"
"models/v_xm1014.mdl" "models/css/v_xm1014.mdl"

// World Models
"models/w_ak47.mdl" "models/css/w_ak47.mdl"
"models/w_backpack.mdl" "models/css/w_backpack.mdl"

/* Formatright © 2012, ConnorMcLeod

Weapons Models is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Weapons Models; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

#define VERSION "0.2.3"

/* ChangeLog
*
* v 0.2.3
* - Added shield support
* - Fixed missing p_ models support that were causing crashes
* - Added pdata check on player on weapon deploy event
*
* v0.2.2
* - Fixed bad formating that was preventing default models from being detected
*
* v0.2.1
* - Fixed "Invalid trie handle provided" error
*
* v0.2.0
* - Check viewmodel and weaponmodel instead of weapon type, so it now supports shield models
*
* v0.1.7
* - Adjustments
*
* v0.1.6
* - Remove FreeEntPrivateData, was causing player from not being reconnected to server
*
* v0.1.5
* - Don't need to precache backpack model anymore
*
* v0.1.4
* - Store allocated view and weapon models in arrays instead of Trie
* - Fixed w_backpack.mdl was not precached (bad string name was passed...)
*
* v0.1.3
* - Store precache returns values into a trie instead of checking trie and resend precache
* - Only store world new models strings in a trie
*
* v0.1.2
* - Moved backpack model precache from precache hook to plugin_precache
*
* v0.1.1
* - Fixed first map where weapons were not registering
*
* v0.1.0
* - Hook Ham_Item_Deploy and set models there instead of hooking FM_ModelIndex that was intensive
*
* v0.0.1 First Shot
*/

// #define DONT_BLOCK_PRECACHE

#include <amxmodx>
#include <fakemeta_stocks>
#include <hamsandwich>

#pragma semicolon 1

const MAX_MODEL_LENGTH = 64;

new const m_rgpPlayerItems_CBasePlayer[6] = {367, 368, ...};
const m_pActiveItem = 373;

const XO_WEAPON = 4;
const m_pPlayer = 41;

new Trie:g_tszWorldModels; // handles new models strings (all excepted v_ and w_ models)
new Trie:g_tiPrecacheReturns; // handles all new models precache returns integers
new Trie:g_tiszViewModels;
new Trie:g_tiszWeaponModels;

public plugin_precache()
{
new szModelsFile[128];
get_localinfo("amxx_configsdir", szModelsFile, charsmax(szModelsFile));
add(szModelsFile, charsmax(szModelsFile), "/weapons_models.ini");

new iFile = fopen(szModelsFile, "rt");
if(!iFile)
{
return;
}

new szDatas[192], szOldModel[MAX_MODEL_LENGTH], szNewModel[MAX_MODEL_LENGTH];
new szWeaponClass[32], Trie:tRegisterWeaponDeploy = TrieCreate(), iId;

new Trie:tWeaponsIds = TrieCreate();
TrieSetCell(tWeaponsIds, "p228", CSW_P228);
TrieSetCell(tWeaponsIds, "scout", CSW_SCOUT);
TrieSetCell(tWeaponsIds, "hegrenade", CSW_HEGRENADE);
TrieSetCell(tWeaponsIds, "xm1014", CSW_XM1014);
TrieSetCell(tWeaponsIds, "c4", CSW_C4);
TrieSetCell(tWeaponsIds, "mac10", CSW_MAC10);
TrieSetCell(tWeaponsIds, "aug", CSW_AUG);
TrieSetCell(tWeaponsIds, "smokegrenade", CSW_SMOKEGRENADE);
TrieSetCell(tWeaponsIds, "elite", CSW_ELITE);
TrieSetCell(tWeaponsIds, "fiveseven", CSW_FIVESEVEN);
TrieSetCell(tWeaponsIds, "ump45", CSW_UMP45);
TrieSetCell(tWeaponsIds, "sg550", CSW_SG550);
TrieSetCell(tWeaponsIds, "galil", CSW_GALIL);
TrieSetCell(tWeaponsIds, "famas", CSW_FAMAS);
TrieSetCell(tWeaponsIds, "usp", CSW_USP);
TrieSetCell(tWeaponsIds, "glock18", CSW_GLOCK18);
TrieSetCell(tWeaponsIds, "awp", CSW_AWP);
TrieSetCell(tWeaponsIds, "mp5navy", CSW_MP5NAVY);
TrieSetCell(tWeaponsIds, "m249", CSW_M249);
TrieSetCell(tWeaponsIds, "m3", CSW_M3);
TrieSetCell(tWeaponsIds, "m4a1", CSW_M4A1);
TrieSetCell(tWeaponsIds, "tmp", CSW_TMP);
TrieSetCell(tWeaponsIds, "g3sg1", CSW_G3SG1);
TrieSetCell(tWeaponsIds, "flashbang", CSW_FLASHBANG);
TrieSetCell(tWeaponsIds, "deagle", CSW_DEAGLE);
TrieSetCell(tWeaponsIds, "sg552", CSW_SG552);
TrieSetCell(tWeaponsIds, "ak47", CSW_AK47);
TrieSetCell(tWeaponsIds, "knife", CSW_KNIFE);
TrieSetCell(tWeaponsIds, "p90", CSW_P90);

new c, bool:bServerDeactivateRegistered, iExtPos, bShieldModel;
while(!feof(iFile))
{
fgets(iFile, szDatas, charsmax(szDatas));
trim(szDatas);
if(!(c=szDatas[0]) || c == ';' || c == '#' || (c == '/' && szDatas[1] == '/'))
{
continue;
}

if( parse(szDatas, szOldModel, charsmax(szOldModel), szNewModel, charsmax(szNewModel)) == 2
&& file_exists(szNewModel) )
{
// models/[p/v]_
// models/shield/[p/v]_shield_
bShieldModel = equal(szOldModel, "models/shield/", 14);
if( ( (c=szOldModel[bShieldModel ? 14 : 7]) == 'p' || c == 'v' ) && szOldModel[bShieldModel ? 15 : 8] == '_' )
{
if( equal(szOldModel[9], "mp5", 3 ) )
{
copy(szWeaponClass, charsmax(szWeaponClass), "weapon_mp5navy");
}
else
{
iExtPos = strlen(szOldModel) - 4;
szOldModel[ iExtPos ] = EOS;
formatex(szWeaponClass, charsmax(szWeaponClass), "weapon_%s", szOldModel[bShieldModel ? 23 : 9]);
szOldModel[ iExtPos ] = '.';
}

if( !TrieGetCell(tWeaponsIds, szWeaponClass[7], iId) )
{
continue;
}

if( c == 'v' )
{
if( !g_tiszViewModels )
{
g_tiszViewModels = TrieCreate();
}

TrieSetCell(g_tiszViewModels, szOldModel, EF_AllocString( szNewModel ) );
}
else
{
if( !g_tiszWeaponModels )
{
g_tiszWeaponModels = TrieCreate();
}

TrieSetCell(g_tiszWeaponModels, szOldModel, EF_AllocString( szNewModel ) );
}

if( !TrieKeyExists(tRegisterWeaponDeploy, szWeaponClass) )
{
TrieSetCell
(
tRegisterWeaponDeploy,
szWeaponClass,
RegisterHam(Ham_Item_Deploy, szWeaponClass, "OnCBasePlayerWeapon_Deploy_P", true)
);
}
}
else
{
if( !bServerDeactivateRegistered && equal(szOldModel, "models/w_backpack.mdl") )
{
bServerDeactivateRegistered = true;
register_forward(FM_ServerDeactivate, "OnServerDeactivate");
}

if( !g_tszWorldModels )
{
g_tszWorldModels = TrieCreate();
}
else if( TrieKeyExists(g_tszWorldModels, szOldModel) )
{
new szModel[MAX_MODEL_LENGTH];
TrieGetString(g_tszWorldModels, szOldModel, szModel, charsmax(szModel));
log_amx("%s world model is already set to %s, can't set it to %s !!", szWeaponClass, szModel, szNewModel);
continue;
}
TrieSetString(g_tszWorldModels, szOldModel, szNewModel);
}

if( !g_tiPrecacheReturns )
{
g_tiPrecacheReturns = TrieCreate();
}
TrieSetCell(g_tiPrecacheReturns, szOldModel, EF_PrecacheModel(szNewModel));
#if defined DONT_BLOCK_PRECACHE
EF_PrecacheModel(szOldModel);
#endif
}
}
fclose(iFile);

TrieDestroy(tRegisterWeaponDeploy);
TrieDestroy(tWeaponsIds);

if( g_tiPrecacheReturns )
{
register_forward(FM_PrecacheModel, "OnPrecacheModel");

if( g_tszWorldModels )
{
register_forward(FM_SetModel, "OnSetModel");
}
}
}

public plugin_init()
{
register_plugin("Weapons Models", VERSION, "ConnorMcLeod");
}

public OnServerDeactivate()
{
static bool:bDontPassThisTwice = false;
if( bDontPassThisTwice ) // unregister this would be waste of time
{
return;
}
bDontPassThisTwice = true;

new id, c4 = FM_NULLENT;
while( (c4 = EF_FindEntityByString(c4, "classname", "weapon_c4")) > 0 )
{
id = get_pdata_cbase(c4, m_pPlayer);
if( id > 0 )
{
// can't use set_pdata_cbase on players at this point
set_pdata_int(id, m_rgpPlayerItems_CBasePlayer[5], 0);
set_pdata_int(id, m_pActiveItem, 0);
// tried to remove c4 entity but server just stucks
}
}
}

public OnPrecacheModel(const szModel[])
{
static iReturn;
if( TrieGetCell(g_tiPrecacheReturns, szModel, iReturn) )
{
forward_return(FMV_CELL, iReturn);
return FMRES_SUPERCEDE;
}
return FMRES_IGNORED;
}

public OnCBasePlayerWeapon_Deploy_P( iWeapon )
{
new id = get_pdata_cbase(iWeapon, m_pPlayer, XO_WEAPON);
if( pev_valid(id) == 2 && get_pdata_cbase(id, m_pActiveItem) == iWeapon )
{
new iszNewModel, szOldModel[MAX_MODEL_LENGTH];
if( g_tiszViewModels )
{
pev(id, pev_viewmodel2, szOldModel, charsmax(szOldModel));
if( TrieGetCell(g_tiszViewModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_viewmodel, iszNewModel);
}
}
if( g_tiszWeaponModels )
{
pev(id, pev_weaponmodel2, szOldModel, charsmax(szOldModel));
if( TrieGetCell(g_tiszWeaponModels, szOldModel, iszNewModel) )
{
set_pev(id, pev_weaponmodel, iszNewModel);
}
}
}
}

public OnSetModel(const iEnt, const szModel[])
{
new szNewModel[MAX_MODEL_LENGTH];
if( TrieGetString(g_tszWorldModels, szModel, szNewModel, charsmax(szNewModel)) )
{
EF_SetModel(iEnt, szNewModel);
return FMRES_SUPERCEDE;
}
return FMRES_IGNORED;
}
I didn't know those models can be blocked from precache

This should be posted in the plugins section.

bibu
12-08-2013, 09:11
Add w_thighpack support and post this as seperate plugin maybe? :D

tequila
02-06-2014, 14:24
In my opinion the best method to change weapon models is to use Orpheu

bLacK-bLooD
11-06-2014, 13:05
https://forums.alliedmods.net/showthread.php?p=1020078#post1020078

* v 0.2.3
* - Added shield support
* - Fixed missing p_ models support that were causing crashes
* - Added pdata check on player on weapon deploy event

Also, Freeman, if you experience crashes, you can enable line 65 and recompile :
// #define DONT_BLOCK_PRECACHE
And see if crashes still happen or not.

If line 286 ( get_pdata_cbase(id, m_pActiveItem) ) still throw errors after pev_valid(id) == 2 check is passed, fault is on hamsandwich module, can't do anything on it (bug reported https://bugs.alliedmods.net/show_bug.cgi?id=5787 ).

I am now using this because i have reached my precache limit and your version does not precache the changed weapons. The only problem is that i still get the error you were talking about earlier on:

L 11/06/2014 - 19:45:37: [HAMSANDWICH] Invalid player 3 (not in-game)
L 11/06/2014 - 19:45:37: [AMXX] Run time error 10 (plugin "weapons_models.amxx") (native "get_pdata_cbase") - debug not enabled!
L 11/06/2014 - 19:45:37: [AMXX] To enable debug mode, add "debug" after the plugin name in plugins.ini (without quotes).

This hasn't been solved by the amxx guys yet?

Is there any other weapon model changer plugin that unprecaches the old weapons as this does?

Arkshine
11-06-2014, 13:33
This hasn't been solved by the amxx guys yet?
This has been fixed in the dev build.

yokomo
11-06-2014, 23:13
This has been fixed in the dev build.
Wait.. wait.. so we don't need to check this "pev_valid(id) == 2" anymore on get|set_pdata_* natives?

Arkshine
11-07-2014, 05:25
what.

bLacK-bLooD
11-10-2014, 08:51
This has been fixed in the dev build.

Is there a chance my server would crash if I run 1.8.2 having that error?

Banana.
10-11-2015, 12:33
Great , thanks for this tutorial. :D

ish12321
08-11-2016, 14:29
On drop can we check which weapon(name) is dropped ?
For ex - weapon_ak47 or something ...

HamletEagle
08-11-2016, 15:37
get_user_weapon(id) + get_weaponname.

ish12321
08-12-2016, 04:19
get_user_weapon(id) + get_weaponname.
Will it work with entity as the weapon's dropped I think the user will have another weapon in hand and it may give result of that instead of what we dropped ?

HamletEagle
08-12-2016, 06:25
Hooking drop command + what I told you would work just fine. At this point, the next weapon is not yet deployed and m_pActiveItem still points to the item that you are going to drop.

klippy
08-12-2016, 08:20
Hooking drop command + what I told you would work just fine. At this point, the next weapon is not yet deployed and m_pActiveItem still points to the item that you are going to drop.

Remember that a weapon can also be dropped by its name. For example, if you held a knife and executed "drop weapon_ak47", get_user_weapon() will return knife's, not AK47's ID.

HamletEagle
08-12-2016, 10:30
The thing is, I've realised that after posting and wanted to edit, but I forgot. Thanks for reminding.

ish12321, you can use such code:

public ClientCommand_Drop(id)
{
new WeaponName[32]
read_argv(1, WeaponName, charsmax(WeaponName))
if(WeaponName[0] == EOS)
{
new WeaponIndex = get_user_weapon(id)
get_weaponname(WeaponIndex, WeaponName, charsmax(WeaponName))
}
}


WeaponName holds what you are asking for.

ish12321
08-12-2016, 13:26
The thing is, I've realised that after posting and wanted to edit, but I forgot. Thanks for reminding.

ish12321, you can you such code:

public ClientCommand_Drop(id)
{
new WeaponName[32]
read_argv(1, WeaponName, charsmax(WeaponName))
if(WeaponName[0] == EOS)
{
new WeaponIndex = get_user_weapon(id)
get_weaponname(WeaponIndex, WeaponName, charsmax(WeaponName))
}
}


WeaponName holds what you are asking for.
When will your function be called that call command also can you supply please :cry:

HamletEagle
08-12-2016, 13:31
register_clcmd("drop", "ClientCommand_Drop") :facepalm:

ish12321
08-12-2016, 13:56
In my plugin at fw_SetModel() I want get the name of weapon dropped and as per that I want replace the model (name is required in correct location of the weapon model)

Please can you tell me what edits should be done so as to do that as in my code I'm getting a error
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <engine>
#include <fakemeta>

#define PLUGIN "VIP Models"
#define VERSION "1.0"
#define AUTHOR "Ish Chhabra"

const ADMIN_VIP = (1<<26); //Declaring new admin flag 'w'
new AllWeapons[] = {
"weapon_p228",
"weapon_shield",
"weapon_scout",
"weapon_hegrenade",
"weapon_xm1014",
"weapon_c4",
"weapon_mac10",
"weapon_aug",
"weapon_smokegrenade",
"weapon_elite",
"weapon_fiveseven",
"weapon_ump45",
"weapon_sg550",
"weapon_galil",
"weapon_famas",
"weapon_usp",
"weapon_glock18",
"weapon_awp",
"weapon_mp5navy",
"weapon_m249",
"weapon_m3",
"weapon_m4a1",
"weapon_tmp",
"weapon_g3sg1",
"weapon_flashbang",
"weapon_deagle",
"weapon_sg552",
"weapon_ak47",
"weapon_knife",
"weapon_p90"
}

new v_ModelLocation[200]
new w_ModelLocation[200]
new p_ModelLocation[200]

new iWeapon
new WeaponName[31]

public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("CurWeapon", "vipChangeModel", "be","1=1")
// Add your code here...
register_forward(FM_SetModel, "fw_SetModel")
}

public plugin_precache() {
for(new i=0;i<sizeof(AllWeapons);i++) {

format(v_ModelLocation,199,"models/vip/v_%s.mdl",AllWeapons[i])
format(w_ModelLocation,199,"models/vip/w_%s.mdl",AllWeapons[i])
format(p_ModelLocation,199,"models/vip/p_%s.mdl",AllWeapons[i])

if(file_exists(v_ModelLocation))
precache_model(v_ModelLocation)

if(file_exists(w_ModelLocation))
precache_model(w_ModelLocation)

if(file_exists(p_ModelLocation))
precache_model(p_ModelLocation)
}
}

public vipChangeModel(id) {
//if(get_user_flags(id) & ADMIN_VIP) { // 'w' flag is for VIP's

iWeapon = get_user_weapon(id)
get_weaponname(iWeapon, WeaponName, 30)

format(v_ModelLocation,199,"models/vip/v_%s.mdl",WeaponName)
format(w_ModelLocation,199,"models/vip/w_%s.mdl",WeaponName)

if (file_exists(v_ModelLocation))
entity_set_string(id, EV_SZ_viewmodel, v_ModelLocation)

if (file_exists(p_ModelLocation))
entity_set_string(id, EV_SZ_weaponmodel, p_ModelLocation)

//}
}

public fw_SetModel (id, entity, model[]) {
// check if its a valid entity or else we'll get errors
//if(!is_valid_ent(entity))
// return FMRES_IGNORED

iWeapon = get_user_weapon(id)
get_weaponname(iWeapon, WeaponName, 30)

format(w_ModelLocation,199,"models/vip/w_%s.mdl",WeaponName)

if (file_exists(w_ModelLocation)) {
entity_set_model(entity, w_ModelLocation)
return FMRES_SUPERCEDE
}
return FMRES_IGNORED

}

HamletEagle
08-12-2016, 14:58
You should have asked this directly.....
Use pev_classname to get the entity classname.

ish12321
08-12-2016, 15:10
That way I get weaponbox but I want the name of weapon ,i.e., something link weapon_ak47 or. ...

So is that possible ?

ish12321
08-12-2016, 15:56
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <engine>
#include <fakemeta>

#define PLUGIN "VIP Models"
#define VERSION "1.0"
#define AUTHOR "Ish Chhabra"

const ADMIN_VIP = (1<<26); //Declaring new admin flag 'w'
new AllWeapons[] = {
"weapon_p228",
"weapon_shield",
"weapon_scout",
"weapon_hegrenade",
"weapon_xm1014",
"weapon_c4",
"weapon_mac10",
"weapon_aug",
"weapon_smokegrenade",
"weapon_elite",
"weapon_fiveseven",
"weapon_ump45",
"weapon_sg550",
"weapon_galil",
"weapon_famas",
"weapon_usp",
"weapon_glock18",
"weapon_awp",
"weapon_mp5navy",
"weapon_m249",
"weapon_m3",
"weapon_m4a1",
"weapon_tmp",
"weapon_g3sg1",
"weapon_flashbang",
"weapon_deagle",
"weapon_sg552",
"weapon_ak47",
"weapon_knife",
"weapon_p90"
}

new v_ModelLocation[200]
new w_ModelLocation[200]
new p_ModelLocation[200]

new iWeapon
new WeaponName[31]

public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("CurWeapon", "vipChangeModel", "be","1=1")
// Add your code here...
register_forward(FM_SetModel, "fw_SetModel")
}

public plugin_precache() {
for(new i=0;i<sizeof(AllWeapons);i++) {

format(v_ModelLocation,199,"models/vip/v_%s.mdl",AllWeapons[i])
format(w_ModelLocation,199,"models/vip/w_%s.mdl",AllWeapons[i])
format(p_ModelLocation,199,"models/vip/p_%s.mdl",AllWeapons[i])

if(file_exists(v_ModelLocation))
precache_model(v_ModelLocation)

if(file_exists(w_ModelLocation))
precache_model(w_ModelLocation)

if(file_exists(p_ModelLocation))
precache_model(p_ModelLocation)
}
}

public vipChangeModel(id) {
//if(get_user_flags(id) & ADMIN_VIP) { // 'w' flag is for VIP's
iWeapon = get_user_weapon(id)
get_weaponname(iWeapon, WeaponName, 30)

format(v_ModelLocation,199,"models/vip/v_%s.mdl",WeaponName)
format(w_ModelLocation,199,"models/vip/w_%s.mdl",WeaponName)

if (file_exists(v_ModelLocation))
set_pev(id,pev_viewmodel2,v_ModelLocation)

if (file_exists(p_ModelLocation))
set_pev(id,pev_weaponmodel2,p_ModelLocation)

//}
}

public fw_SetModel (entity, model[]) {
// check if its a valid entity or else we'll get errors
if(!is_valid_ent(entity))
return FMRES_IGNORED
pev(entity, pev_classname, WeaponName,31)

for(new i=0;i<sizeof(AllWeapons);i++) {
if (equal(WeaponName, AllWeapons[i])) {
format(w_ModelLocation,199,"models/vip/w_%s.mdl",WeaponName)
if (file_exists(w_ModelLocation)) {
entity_set_model(entity, w_ModelLocation)
return FMRES_SUPERCEDE
}
}
}

return FMRES_IGNORED

}



Did this much still not working can you tell me what's wrong

HamletEagle
08-13-2016, 11:11
That way I get weaponbox but I want the name of weapon ,i.e., something link weapon_ak47 or. ...

So is that possible ?

Sorry. You need to loop all slots from the box to get the weapon entity, after that you can use pev_classname on it and get what you need.

RetrieveWeapon(WeaponBoxEntity)
{
const XoWeapon = 4
new const m_rgpPlayerItems_CWeaponBox[6] = {34, 35, ...}

new WeaponEntity
for(new i = 1; i< sizeof m_rgpPlayerItems_CWeaponBox; i++)
{
WeaponEntity = get_pdata_cbase(WeaponBoxEntity, m_rgpPlayerItems_CWeaponBox[i], XoWeapon)
if(pev_valid(WeaponEntity))
{
break
}
}

return WeaponEntity
}

Use this stock, it returns the weapon entity index that's inside the weaponbox.

lolerto
02-01-2021, 02:19
https://forums.alliedmods.net/showthread.php?p=1020078#post1020078

* v 0.2.3
* - Added shield support
* - Fixed missing p_ models support that were causing crashes
* - Added pdata check on player on weapon deploy event

Also, Freeman, if you experience crashes, you can enable line 65 and recompile :
// #define DONT_BLOCK_PRECACHE
And see if crashes still happen or not.

If line 286 ( get_pdata_cbase(id, m_pActiveItem) ) still throw errors after pev_valid(id) == 2 check is passed, fault is on hamsandwich module, can't do anything on it (bug reported https://bugs.alliedmods.net/show_bug.cgi?id=5787 ).

How can I restrict only for administrators?