[Tutorial] Dropping & Picking Up Weapons
#1

Sup, not going to get into too much detail with this. I made this in about 45 minutes, you can edit it to your liking.
If my scripting is bad, sorry, I don't even code in pawno, I just did this for fun.

Starting with the enum
Code:
enum eWeaponItemsDropped
{
	Float:flX, // The position of the dropped weapon
	Float:flY,
	Float:flZ,
	WeaponID, // The weapon ID
	iAmmo, // The ammo in the weapon when it was dropped
	Text3D:Label, 
	Object, // The object
	bUsed // We use this to check if the slot in the array is used

} new g_WeaponItems[MAX_DROPPED_WEAPONS][eWeaponItemsDropped];
MAX_DROPPED_WEAPONS can be your choice I guess. I have mine set to 500.

Next we need the function that handles the weapon drop. The function will be commented
Code:
stock CreateDroppedWeapon(playerid, wID /* weapon ID */, Float:flPos[], modelid)
{
	new i, strWeapon[32];
	for ( i = 0; i < MAX_DROPPED_WEAPONS; ++i )
	{
		// find a free spot in the array
		if ( g_WeaponItems[i][bUsed] )
			continue;
		else
			break; // break out the loop, saving the index
	}

	g_WeaponItems[i][iAmmo] = GetPlayerAmmo(playerid); // Get the ammo the weapon currently has
	g_WeaponItems[i][flX] = flPos[0]; // Position
        g_WeaponItems[i][flY] = flPos[1];
	g_WeaponItems[i][flZ] = flPos[2] - 0.9;
        g_WeaponItems[i][WeaponID] = wID; // The wep. id
	GetWeaponName(wID, strWeapon, sizeof(strWeapon)); 
 	
        // Create the object
        g_WeaponItems[i][Object] = CreateDynamicObject(modelid, flPos[0], flPos[1], flPos[2] - 0.9, 2, 4, 6 );
	
        // Create a label above the weapon on the ground
        g_WeaponItems[i][Label] = CreateDynamic3DTextLabel(strWeapon, RGBA(100, 200, 0, 255), flPos[0], flPos[1], flPos[2] - 0.6, 2.5); 
	new Float:rot[3]; // Rotate it so it looks like its on the ground
	GetDynamicObjectRot( g_WeaponItems[i][Object], rot[0], rot[1], rot[2]);
	SetDynamicObjectRot( g_WeaponItems[i][Object], -90, rot[1], rot[2]);
	
	g_WeaponItems[i][bUsed] = true; // Set the slot used in the array, so next time we can skip over this slot

	return 1;
}
We need two functions to find the weapon ID from a model, and the other way around.
Code:
GetWeaponModel(weaponid) 
{
	new model;
	switch(weaponid)
	{
	case 1: model=331; case 2: model=333; case 3: model=334;
	case 4: model=335; case 5: model=336; case 6: model=337;
	case 7: model=338; case 8: model=339; case 9: model=341;
	case 10: model=321; case 11: model=322; case 12: model=323;
	case 13: model=324; case 14: model=325; case 15: model=326;
	case 16: model=342; case 17: model=343; case 18: model=344;
	case 22: model=346; case 23: model=347; case 24: model=348;
	case 25: model=349; case 26: model=350; case 27: model=351;
	case 28: model=352; case 29: model=353; case 30: model=355;
	case 31: model=356; case 32: model=372; case 33: model=357;
	case 34: model=358; case 35: model=359; case 36: model=360;
	case 37: model=361; case 38: model=362; case 39: model=363;
	case 41: model=365; case 42: model=366; case 46: model=371;
	}
	if(model<300) return -1;
	return model;
}
GetWeaponIDFromModel(modelid)
{
	switch(modelid)
	{
		case 331: return 1; case 333: return 2; case 334: return 3;
		case 335: return 4; case 336: return 5; case 337: return 6;
		case 338: return 7; case 339: return 8; case 341: return 9;
		case 321: return 11; case 322: return 11; case 323: return 12;
		case 324: return 13; case 325: return 14; case 326: return 15;
		case 342: return 16; case 343: return 17; case 344: return 18;
		case 346: return 22; case 347: return 23; case 348: return 24;
		case 349: return 25; case 350: return 26; case 351: return 27;
		case 352: return 28; case 353: return 29; case 355: return 30;
		case 356: return 31; case 372: return 32; case 357: return 33; 
		case 358: return 34; case 359: return 35; case 360: return 36;
		case 361: return 37; case 362: return 38; case 363: return 39;
		case 365: return 41; case 366: return 42; case 371: return 46;
	}
	return -1;
}
Now we need the commands that handle the dropping and the picking up of a weapon.
Code:
COMMAND:dropweapon(playerid, params[])
{
	if ( g_PlayerInfo[playerid][iWeapon] > 0 )
	{
	    new action[128], szWepName[32];
	    GetWeaponName(g_PlayerInfo[playerid][iWeapon], szWepName, 32);
	    format(action, 128, "drops their %s on the ground.", szWepName);
	    // Remove the weapon and create the item on the ground.
            new Float:pos[3];
	    GetPlayerOrigin(playerid, pos);
	    new iModel = GetWeaponModel(g_PlayerInfo[playerid][iWeapon]);
	    CreateDroppedWeapon(playerid, g_PlayerInfo[playerid][iWeapon], pos, iModel);
	    RemoveWeapon(playerid, g_PlayerInfo[playerid][iWeapon]);		
	    CreateAction(playerid, action ); // optional
	}
	return 1;
}

COMMAND:pickupweapon(playerid, params[])
{ 
        // loop through all weapon items
	for ( new i = 0; i < MAX_DROPPED_WEAPONS; ++i)
	{
            // only slots that are being used
	    if ( g_WeaponItems[i][bUsed] ) {
	        if ( IsPlayerInRangeOfPoint(playerid, 1.0, g_WeaponItems[i][flX], g_WeaponItems[i][flY], g_WeaponItems[i][flZ] ) )
	        {
	        	new action[128], szWepName[32];
	        	GetWeaponName(g_WeaponItems[i][WeaponID], szWepName, 32);
	        	format(action, 128, "picks up a %s from the ground.", szWepName);
                        // Remove the object/label and give the player the weapon
	        	DestroyDynamic3DTextLabel(g_WeaponItems[i][Label]);
	        	DestroyDynamicObject(g_WeaponItems[i][Object]);
		        GivePlayerWeapon(playerid,g_WeaponItems[i][WeaponID], g_WeaponItems[i][iAmmo]);
			CreateAction(playerid, action);
			g_WeaponItems[i][bUsed] = false; // free the slot
	        }
	    }
	}
    return 1;
}


// This is optional
stock CreateAction( playerid, action[] )
{
	new msg[128], name[ 64 ];
	if ( g_PlayerInfo[playerid][bMasked] )
		format( name, 64, "Mask_%i", g_PlayerInfo[playerid][hName]);
	else
		format( name, 64, "%s", g_PlayerInfo[playerid][pName]);

	format(msg, sizeof(msg), "* %s %s", name, action );

	ProxDetector(8.0, playerid, msg, RGBA(170, 0, 140, 255) );
}
The result:


Reply


Messages In This Thread
Dropping & Picking Up Weapons - by Trucido - 24.11.2016, 19:12
Re: Dropping & Picking Up Weapons - by Micko123 - 24.11.2016, 19:15
Re: Dropping & Picking Up Weapons - by Trucido - 24.11.2016, 19:19
Re: Dropping & Picking Up Weapons - by Micko123 - 24.11.2016, 19:22
Re: Dropping & Picking Up Weapons - by Vince - 24.11.2016, 19:35
Re: Dropping & Picking Up Weapons - by Trucido - 24.11.2016, 20:02

Forum Jump:


Users browsing this thread: 1 Guest(s)