Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Inventory System by ThePhenix aka ThreeKingz
- */
- /*==============================================================================
- Includes
- ===============================================================================*/
- #include <a_samp>
- #include <zcmd>
- #include <sscanf2>
- #include <YSI\y_ini>
- #include <mSelection>
- #include <MapAndreas>
- #define SendFormattedMessage(%0,%1,%2,%3) \
- new text[128]; \
- format(text,sizeof(text),%2,%3); \
- SendClientMessage(%0,%1,text)
- /*==============================================================================
- Defines
- ===============================================================================*/
- #define MAX_INVENTORY_SLOTS 50 //Max slots to save items per player
- #define INV_SAVING_PATH "INV/Inventory/%s.ini" //The folder where the inventory data of each player will be stored.
- #define OBJ_SAVING_PATH "INV/Objects/Object%d.ini" //The folder where the objects created by the RCON will be stored.
- #define EMPTY_SLOT_OBJ 1239 //((Empty slot)) The object shown in mSelection for an empty slot.
- #define MAX_ITEMS 200 //The maximun amount of items the RCON Administrator can create.
- #define OBJECTS_MENU 1
- #define I_MENU 2
- #define PRESSED(%0) \
- (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
- new ObjectID[MAX_ITEMS];
- new Text3D:LabelID[MAX_ITEMS];
- new ObjTimer[MAX_ITEMS];
- //You can add as much models as you wish
- new objects[]=
- {
- 1484, 1210, 1342, 2804, 1341, 3028, 1512, 2992, 3086, 3030, 2966, 3027, 3082, 1600
- };
- /*==============================================================================
- Enums
- ===============================================================================*/
- enum oInfo
- {
- Model,
- Float: pX,
- Float: pY,
- Float: pZ
- }
- new ObjInfo[MAX_ITEMS][oInfo];
- enum iInfo
- {
- Slot[MAX_INVENTORY_SLOTS]
- }
- new InvInfo[MAX_PLAYERS][iInfo];
- /*==============================================================================
- Commands
- ===============================================================================*/
- CMD:citem(playerid, params[])
- {
- if(IsPlayerAdmin(playerid))
- {
- ShowModelSelectionMenuEx(playerid, objects, 12, "Select object", OBJECTS_MENU, 16.0, 0.0, -55.0);
- }
- else
- {
- SendClientMessage(playerid, 0xFF00FFFF, "You need to log in as RCON Administrator");
- }
- return 1;
- }
- CMD:ci(playerid, params[])
- {
- return cmd_citem(playerid, params);
- }
- CMD:myitems(playerid, params[])
- {
- ShowModelSelectionMenuEx(playerid, InvInfo[playerid][Slot], MAX_INVENTORY_SLOTS, "Your items", I_MENU, 16.0, 0.0, -55.0);
- return 1;
- }
- COMMAND:car(playerid,params[])
- {
- new car;
- if(IsPlayerInAnyVehicle(playerid)) return 1;
- if(sscanf(params, "i", car)) return SendClientMessage(playerid, -1, "USE: /car [car id]");
- if(strval(params) >611 || strval(params) <400) return SendClientMessage(playerid, -1, "That id doesn't exist[400-611]");
- new Float:x, Float:y, Float:z, Float:a;
- GetPlayerPos(playerid, x,y,z);
- GetPlayerFacingAngle(playerid, a);
- car = CreateVehicle(strval(params), x, y, z,a, -1, -1, 60);
- PutPlayerInVehicle(playerid, car, 0);
- return 1;
- }
- /*==============================================================================
- Public functions
- ===============================================================================*/
- public OnPlayerModelSelectionEx(playerid, response, extraid, modelid)
- {
- if(extraid == OBJECTS_MENU)
- {
- if(response)
- {
- new objectid = EmptySlot();
- new Float: X, Float: Y, Float: Z;
- GetPlayerPos(playerid, X, Y, Z);
- MapAndreas_FindZ_For2DCoord(X, Y, Z);
- ObjectID[objectid] = CreateObject(modelid, X, Y, Z, 0.0, 0.0, 96.0);
- LabelID[ObjectID[objectid]] = Create3DTextLabel("Press Y to take this item", 0x20B2AAAA, X, Y, Z, 15.0, 0);
- SendFormattedMessage(playerid, 0xADFF2FAA, "You have created an object successfully. [Object ID: %d | Model %d]", ObjectID[objectid], modelid);
- new obj = ObjectID[objectid];
- ObjInfo[obj][Model] = modelid;
- ObjInfo[obj][pX] = X;
- ObjInfo[obj][pY] = Y;
- ObjInfo[obj][pZ] = Z;
- SaveObject(obj);
- }
- }
- return 1;
- }
- stock EmptySlot()
- {
- new
- i = 0;
- while (i < sizeof (ObjectID) && ObjectID[i])
- {
- i++;
- }
- if (i == sizeof (ObjectID)) return -1;
- return i;
- }
- public OnFilterScriptInit()
- {
- MapAndreas_Init(MAP_ANDREAS_MODE_FULL);
- new count;
- for(new i; i < MAX_ITEMS; i++)
- {
- if(fexist(ObjPath(i)))
- {
- count++;
- INI_ParseFile(ObjPath(i),"loadobj_ob", .bExtra = true, .extra = i);
- ObjectID[i] = CreateObject(ObjInfo[i][Model], ObjInfo[i][pX], ObjInfo[i][pY], ObjInfo[i][pZ], 0.0, 0.0, 96.0);
- LabelID[ObjectID[i]] = Create3DTextLabel("Press Y to take this item", 0x20B2AAAA, ObjInfo[i][pX], ObjInfo[i][pY], ObjInfo[i][pZ], 15.0, 0);
- }
- }
- printf("Objects loaded: %d", count);
- return 1;
- }
- public OnFilterScriptExit()
- {
- return 1;
- }
- public OnPlayerRequestClass(playerid, classid)
- {
- SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
- SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
- SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
- return 1;
- }
- public OnPlayerConnect(playerid)
- {
- if(fexist(InvPath(playerid)))
- {
- INI_ParseFile(InvPath(playerid),"loadinv_user", .bExtra = true, .extra = playerid);
- }
- else
- {
- for(new i; i < MAX_INVENTORY_SLOTS; i++)
- {
- new string[14];
- format(string, sizeof(string), "Slot%d", i);
- InvInfo[playerid][Slot][i] = EMPTY_SLOT_OBJ;
- new INI:file = INI_Open(InvPath(playerid));
- INI_SetTag(file,"Inventory Data");
- INI_WriteInt(file, string, EMPTY_SLOT_OBJ);
- INI_Close(file);
- }
- }
- return 1;
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- SaveInventory(playerid);
- return 1;
- }
- public OnPlayerSpawn(playerid)
- {
- ApplyAnimation(playerid,"SWEET","null",0.0,0,0,0,0,0);
- return 1;
- }
- public OnPlayerDeath(playerid, killerid, reason)
- {
- return 1;
- }
- public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
- {
- if (PRESSED(KEY_YES))
- {
- if(IsPlayerNearAnyObject(playerid))
- {
- new objectid = GetNearestObjectForPlayer(playerid);
- new model = ObjInfo[objectid][Model];
- new slot = FindEmptySlot(playerid);
- ApplyAnimation(playerid, "BOMBER", "BOM_Plant", 4.1, 0, 1, 1, 0, 3000, 1);
- SetTimerEx("TakingItem", 3000, false, "i", playerid);
- SendClientMessage(playerid, 0xB4B5B7AA, "Taking item...");
- InvInfo[playerid][Slot][slot] = model;
- SetPVarInt(playerid, "object", objectid);
- }
- }
- return 1;
- }
- /*==============================================================================
- Functions
- ===============================================================================*/
- forward TakingItem(playerid);
- public TakingItem(playerid)
- {
- new objectid = GetPVarInt(playerid, "object");
- SendClientMessage(playerid, 0xB0C4DEFF, "You have successfully put that item into your inventory.");
- MoveObject(objectid, ObjInfo[objectid][pX], ObjInfo[objectid][pY], ObjInfo[objectid][pZ]-100, 5.0);
- Delete3DTextLabel(LabelID[objectid]);
- ObjTimer[objectid] = SetTimerEx("RespawnObject", 120000, false, "i", objectid);
- DeletePVar(playerid, "object");
- return 1;
- }
- forward RespawnObject(objectid);
- public RespawnObject(objectid)
- {
- ObjectID[objectid] = MoveObject(objectid, ObjInfo[objectid][pX], ObjInfo[objectid][pY], ObjInfo[objectid][pZ], 10.0);
- LabelID[ObjectID[objectid]] = Create3DTextLabel("Press Y to take this item", 0x20B2AAAA, ObjInfo[objectid][pX], ObjInfo[objectid][pY], ObjInfo[objectid][pZ], 15.0, 0);
- KillTimer(ObjTimer[objectid]);
- return 1;
- }
- forward loadinv_user(playerid, name[], value[]);
- public loadinv_user(playerid, name[], value[])
- {
- for(new i; i < MAX_INVENTORY_SLOTS; i++)
- {
- new string[7];
- format(string, sizeof(string), "Slot%d", i);
- INI_Int(string, InvInfo[playerid][Slot][i]);
- }
- return 1;
- }
- forward loadobj_obj(objectid, name[], value[]);
- public loadobj_obj(objectid, name[], value[])
- {
- INI_Int("Model", ObjInfo[objectid][Model]);
- INI_Float("pX", ObjInfo[objectid][pX]);
- INI_Float("pY", ObjInfo[objectid][pY]);
- INI_Float("pZ", ObjInfo[objectid][pZ]);
- return 1;
- }
- stock SaveInventory(playerid)
- {
- for(new i; i < MAX_INVENTORY_SLOTS; i++)
- {
- new string[7];
- format(string, sizeof(string), "Slot%d", i);
- new INI:file = INI_Open(InvPath(playerid));
- INI_SetTag(file,"Inventory Data");
- INI_WriteInt(file, string, InvInfo[playerid][Slot][i]);
- INI_Close(file);
- }
- return 1;
- }
- stock pName(playerid)
- {
- new name[MAX_PLAYER_NAME];
- GetPlayerName(playerid, name, sizeof(name));
- return name;
- }
- stock InvPath(playerid)
- {
- new str[40];
- format(str,sizeof(str), INV_SAVING_PATH , pName(playerid));
- return str;
- }
- stock ObjPath(object)
- {
- new str[40];
- format(str,sizeof(str), OBJ_SAVING_PATH , object);
- return str;
- }
- stock SaveObject(object)
- {
- new INI:file = INI_Open(ObjPath(object));
- INI_SetTag(file,"ObjectID Data");
- INI_WriteInt(file, "Model", ObjInfo[object][Model]);
- INI_WriteFloat(file, "pX", ObjInfo[object][pX]);
- INI_WriteFloat(file, "pY", ObjInfo[object][pY]);
- INI_WriteFloat(file, "pZ", ObjInfo[object][pZ]);
- INI_Close(file);
- return 1;
- }
- stock FindEmptySlot(playerid)
- {
- new slot;
- for(new i; i<MAX_INVENTORY_SLOTS; i++)
- {
- if(InvInfo[playerid][Slot][i] == EMPTY_SLOT_OBJ)
- {
- slot = i;
- break;
- }
- }
- return slot;
- }
- stock IsPlayerNearObject(playerid, objectid, Float:range)
- {
- new Float:X, Float:Y, Float:Z;
- GetObjectPos(objectid, X, Y, Z);
- if(IsPlayerInRangeOfPoint(playerid, range, X, Y, Z))return true;
- return false;
- }
- stock IsPlayerNearAnyObject(playerid)
- {
- for(new i = 0; i < MAX_ITEMS; i++)
- {
- if(ObjectID[i] = i)
- {
- if(IsPlayerNearObject(playerid, ObjectID[i], 2.4)) return true;
- }
- }
- return false;
- }
- stock GetNearestObjectForPlayer(playerid, exception = INVALID_OBJECT_ID)
- {
- new Float:Distance, target = -1;
- for(new v; v < MAX_ITEMS; v++) if(IsValidObject(ObjectID[v]))
- {
- if(ObjectID[v] != exception && (target < 0 || Distance > GetDistancePlayerObject(playerid, ObjectID[v])))
- {
- target = ObjectID[v];
- Distance = GetDistancePlayerObject(playerid, ObjectID[v]);
- }
- }
- return target;
- }
- stock CheckItem(playerid, modelid)
- {
- for(new i; i<MAX_INVENTORY_SLOTS; i++)
- {
- if(InvInfo[playerid][Slot][i] != modelid) return true;
- }
- return false;
- }
- stock GetDistancePlayerObject(playerid, objectid)
- {
- new Float:Floats[7];
- GetPlayerPos(playerid, Floats[0], Floats[1], Floats[2]);
- GetObjectPos(objectid, Floats[3], Floats[4], Floats[5]);
- Floats[6] = floatsqroot((Floats[3]-Floats[0])*(Floats[3]-Floats[0])+(Floats[4]-Floats[1])*(Floats[4]-Floats[1])+(Floats[5]-Floats[2])*(Floats[5]-Floats[2]));
- return floatround(Floats[6]);
- }
Advertisement
Add Comment
Please, Sign In to add comment