Mirage Source
http://miragesource.net/forums/

Adding Weight To An Item
http://miragesource.net/forums/viewtopic.php?f=210&t=3224
Page 1 of 2

Author:  Stomach Pulser [ Wed Jan 02, 2008 9:27 pm ]
Post subject:  Adding Weight To An Item

The feature of adding weight to your items was brought on by James and John's Fabulatra. I figured why not add it. But, as it turns out, since I'm still new, it took me awhile to get it done! Currently all it does is check to see when to add or subtract from the player's weight. Whenever you get/give an item from the ground/shop, it should update your current weight and tell you when you are carrying too much. I leave it up to anyone who wants to use to decide what happens when you are carrying too much weight. Without further ado, let's begin.

Difficulty 3/5 – Comprehension (I tried to not make it a C + P, but I fail at making things hard ^_^)

Client Side

Start off in ModTypes.

Add this into ItemRec, at the bottom
Code:
Weight As Double


Inside PlayerRec, just under the inventory category, add this:
Code:
CurWeight As Long
MaxWeight As Long


Now go into ModGameLogic.
Add the following functions/subs somewhere in there (preferably at the bottom):
Code:
Function GetPlayerCurWeight(ByVal Index As Long) As Double
    GetPlayerCurWeight = Player(Index).CurWeight
End Function

Sub SetPlayerCurWeight(ByVal Index As Long, ByVal Modifier As Double)
    Player(Index).CurWeight = Modifier
End Sub

Function GetPlayerMaxWeight(ByVal Index As Long) As Long
    GetPlayerMaxWeight = Player(Index).MaxWeight
End Function


Sub SetPlayerMaxWeight(ByVal Index As Long, ByVal Modifier As Long)
    Player(Index).MaxWeight = Modifier
End Sub


Now, find the sub “ItemEditorInit” without the quotes of course.
Add this in before the first if statement:
Code:
frmItemEditor.txtWeight.Text = Item(EditorIndex).Weight


Now find the sub “ItemEditorOK”.
Add this before the first if statement:
Code:
    Item(EditorIndex).Weight = frmItemEditor.txtWeight.Text


Now find the sub “ClearPlayer”
Add this line right before the armor statements (after the inventory clear):
Code:
    Player(Index).CurWeight = 0
    Player(Index).MaxWeight = 0


Now find the sub “ClearItem”.
Add this line at the end:
Code:
Item(Index).Weight = 0


Moving on to ModClientTCP
Find the sub “SendSaveItem”.
Inside the packet part, remove the END_CHAR and add this:
Code:
& .Weight & END_CHAR


Go to ModHandleData
Find the “Edit item packet”
Add this at the end of the “ 'Update the Item” block of code
Code:
Item(n).Weight = Val(Parse(8))


Find the “Update item packet”
Add this right before it says exit sub
Code:
Item(n).Weight = Val(Parse(5))


Now, go into frmItemEditor
Add a text box and a label.
Text Box.Name = txtWeight
Label.Name = lblWeight
Lablel.Caption = Weight

That is all you have to do client side, now onto...

Server Side

Start off in ModTypes again
Add the same variables as you did in the Client

Go to ModGameLogic and add these in there
Code:
Function GetPlayerCurWeight(ByVal Index As Long) As Double
    GetPlayerCurWeight = Player(Index).Char(Player(Index).CharNum).CurWeight
End Function

Sub SetPlayerCurWeight(ByVal Index As Long, ByVal Modifier As Double)
    Player(Index).Char(Player(Index).CharNum).CurWeight = Modifier
End Sub

Function GetPlayerMaxWeight(ByVal Index As Long) As Long
    GetPlayerMaxWeight = Player(Index).Char(Player(Index).CharNum).MaxWeight
End Function

Sub SetPlayerMaxWeight(ByVal Index As Long, ByVal Modifier As Long)
    Player(Index).Char(Player(Index).CharNum).MaxWeight = Modifier
End Sub


Now find the sub “TakeItem”.
Add the top of the sub add a boolean called curtake.
Find “ ' Is what we are trying to take away more then what they have? If so just set it to zero” and replace that and the lines below it (until “ ' Check to see if its any sort of ArmorSlot/WeaponSlot”) with this:
Code:
' Is what we are trying to take away more then what they have?  If so just set it to zero
                If ItemVal >= GetPlayerInvItemValue(Index, i) Then
                    TakeItem = True
                    takecur = True
                Else
                    Call SetPlayerInvItemValue(Index, i, GetPlayerInvItemValue(Index, i) - ItemVal)
                    Call SendInventoryUpdate(Index, i)
                End If
            Else
                takecur = False


Find the sub “GiveItem”.
Add this before the SendInventoryUpdate
Code:
Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) + (Item(GetPlayerInvItemNum(Index, i)).Weight * GetPlayerInvItemValue(Index, i))))
        If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then Call PlayerMsg(Index, "You are overencumbered!", White)


Find the Sub “PlayerMapGetItem”.
Replace the if statement
Code:
If n <> 0
with:
Code:
If n <> 0 Then
                    ' Set item in players inventor
                    Call SetPlayerInvItemNum(Index, n, MapItem(MapNum, i).Num)
                    If Item(GetPlayerInvItemNum(Index, n)).Type = ITEM_TYPE_CURRENCY Then
                        Call SetPlayerInvItemValue(Index, n, GetPlayerInvItemValue(Index, n) + MapItem(MapNum, i).Value)
                        Msg = "You picked up " & MapItem(MapNum, i).Value & " " & Trim(Item(GetPlayerInvItemNum(Index, n)).Name) & "."
                        Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) + (Item(GetPlayerInvItemNum(Index, n)).Weight * GetPlayerInvItemValue(Index, n))))
                    Else
                        Call SetPlayerInvItemValue(Index, n, 0)
                        Msg = "You picked up a " & Trim(Item(GetPlayerInvItemNum(Index, n)).Name) & "."
                        Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) + (Item(GetPlayerInvItemNum(Index, n)).Weight)))
                    End If
                    Call SetPlayerInvItemDur(Index, n, MapItem(MapNum, i).Dur)
                       
                    ' Erase item from the map
                    MapItem(MapNum, i).Num = 0
                    MapItem(MapNum, i).Value = 0
                    MapItem(MapNum, i).Dur = 0
                    MapItem(MapNum, i).x = 0
                    MapItem(MapNum, i).y = 0
                                           
                    Call SendInventoryUpdate(Index, n)
                    Call SpawnItemSlot(i, 0, 0, 0, GetPlayerMap(Index), GetPlayerX(Index), GetPlayerY(Index))
                    Call PlayerMsg(Index, Msg, Yellow)
                    If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then Call PlayerMsg(Index, "You are overencumbered!", White)
                    Exit Sub
                Else
                    Call PlayerMsg(Index, "Your inventory is full.", BrightRed)
                    Exit Sub
                End If


Find the Sub “PlayerDropMapItem”.
Replace the if statement
Code:
If Item(GetPlayerInvItemNum(Index, InvNum)).Type = ITEM_TYPE_CURRENCY Then
with:
Code:
If Item(GetPlayerInvItemNum(Index, InvNum)).Type = ITEM_TYPE_CURRENCY Then
                ' Check if its more then they have and if so drop it all
                If Ammount >= GetPlayerInvItemValue(Index, InvNum) Then
                    MapItem(GetPlayerMap(Index), i).Value = GetPlayerInvItemValue(Index, InvNum)
                    Call MapMsg(GetPlayerMap(Index), GetPlayerName(Index) & " drops " & GetPlayerInvItemValue(Index, InvNum) & " " & Trim(Item(GetPlayerInvItemNum(Index, InvNum)).Name) & ".", Yellow)
                    Call SetPlayerInvItemNum(Index, InvNum, 0)
                    Call SetPlayerInvItemValue(Index, InvNum, 0)
                    Call SetPlayerInvItemDur(Index, InvNum, 0)
                Else
                    MapItem(GetPlayerMap(Index), i).Value = Ammount
                    Call MapMsg(GetPlayerMap(Index), GetPlayerName(Index) & " drops " & Ammount & " " & Trim(Item(GetPlayerInvItemNum(Index, InvNum)).Name) & ".", Yellow)
                    Call SetPlayerInvItemValue(Index, InvNum, GetPlayerInvItemValue(Index, InvNum) - Ammount)
                End If
                Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) - (Item(GetPlayerInvItemNum(Index, InvNum)).Weight * GetPlayerInvItemValue(Index, InvNum))))
            Else
                ' Its not a currency object so this is easy
                MapItem(GetPlayerMap(Index), i).Value = 0
                If Item(GetPlayerInvItemNum(Index, InvNum)).Type >= ITEM_TYPE_WEAPON And Item(GetPlayerInvItemNum(Index, InvNum)).Type <= ITEM_TYPE_SHIELD Then
                    Call MapMsg(GetPlayerMap(Index), GetPlayerName(Index) & " drops a " & Trim(Item(GetPlayerInvItemNum(Index, InvNum)).Name) & " " & GetPlayerInvItemDur(Index, InvNum) & "/" & Item(GetPlayerInvItemNum(Index, InvNum)).Data1 & ".", Yellow)
                Else
                    Call MapMsg(GetPlayerMap(Index), GetPlayerName(Index) & " drops a " & Trim(Item(GetPlayerInvItemNum(Index, InvNum)).Name) & ".", Yellow)
                End If
                Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) - (Item(GetPlayerInvItemNum(Index, InvNum)).Weight)))
               
                Call SetPlayerInvItemNum(Index, InvNum, 0)
                Call SetPlayerInvItemValue(Index, InvNum, 0)
                Call SetPlayerInvItemDur(Index, InvNum, 0)
            End If


Now, make your way over to modHandleData
Find the “Save item packet”
At the end of the “ 'Update the item” block, add this:
Code:
Item(n).Weight = Val(Parse(8))


Next up is modDatabase
Find the Sub “Saveitem”.
Add this at the end of sub
Code:
Call PutVar(FileName, "ITEM" & ItemNum, "Weight", Trim(Item(ItemNum).Weight))


Find the Sub “Loaditems”.
Add this before the Do Events code:
Code:
Item(i).Weight = Val(GetVar(FileName, "ITEM" & i, "Weight"))


Find the Sub “Addchar”.
After
Code:
        Player(Index).Char(CharNum).Map = START_MAP
        Player(Index).Char(CharNum).x = START_X
        Player(Index).Char(CharNum).y = START_Y

Add this:
Code:
Player(Index).Char(CharNum).CurWeight = 0
        Player(Index).Char(CharNum).MaxWeight = 4 * (Class(ClassNum).STR + Class(ClassNum).DEF)


If you have your people start off with items, then edit the first line. Change the second line to the formula for the max weight that the character can carry.

Find the Sub “SavePlayer.
Add this is before the save spells code:
Code:
Call PutVar(FileName, "CHAR" & i, "CurWeight", STR(Player(Index).Char(i).CurWeight))
        Call PutVar(FileName, "CHAR" & i, "MaxWeight", STR(Player(Index).Char(i).MaxWeight)


Find the Sub “LoadPlayer”
Add this in before the load spells code:
Code:
Player(Index).Char(i).CurWeight = Val(GetVar(FileName, "CHAR" & i, "CurWeight"))
        Player(Index).Char(i).MaxWeight = Val(GetVar(FileName, "CHAR" & i, "MaxWeight"))


Now for modServerTCP
replace the three subs “SendUpdateItemToAll”, “SendUpdateItemTo”, and “SendEditItemTo” with the following code:
Code:
Sub SendUpdateItemToAll(ByVal ItemNum As Long)
Dim Packet As String

    Packet = "UPDATEITEM" & SEP_CHAR & ItemNum & SEP_CHAR & Trim(Item(ItemNum).Name) & SEP_CHAR & Item(ItemNum).Pic & SEP_CHAR & Item(ItemNum).Type & SEP_CHAR & Item(ItemNum).Data1 & SEP_CHAR & Item(ItemNum).Data2 & SEP_CHAR & Item(ItemNum).Data3 & SEP_CHAR & Item(ItemNum).Weight & END_CHAR
    Call SendDataToAll(Packet)
End Sub

Sub SendUpdateItemTo(ByVal Index As Long, ByVal ItemNum As Long)
Dim Packet As String

    Packet = "UPDATEITEM" & SEP_CHAR & ItemNum & SEP_CHAR & Trim(Item(ItemNum).Name) & SEP_CHAR & Item(ItemNum).Pic & SEP_CHAR & Item(ItemNum).Type & SEP_CHAR & Item(ItemNum).Weight & END_CHAR
    Call SendDataTo(Index, Packet)
End Sub

Sub SendEditItemTo(ByVal Index As Long, ByVal ItemNum As Long)
Dim Packet As String

    Packet = "EDITITEM" & SEP_CHAR & ItemNum & SEP_CHAR & Trim(Item(ItemNum).Name) & SEP_CHAR & Item(ItemNum).Pic & SEP_CHAR & Item(ItemNum).Type & SEP_CHAR & Item(ItemNum).Data1 & SEP_CHAR & Item(ItemNum).Data2 & SEP_CHAR & Item(ItemNum).Data3 & SEP_CHAR & Item(ItemNum).Weight & END_CHAR
    Call SendDataTo(Index, Packet)
End Sub


Lastly, if you want your players to be able to carry more weight when they use their stat points...

Go under [b]modHandleData/b] and in the “Use stats packet” add this code after the End Select:
Code:
Call SetPlayerMaxWeight(Index, 4 * (GetPlayerSTR(Index) + GetPlayerDEF(Index)))


Of course, change the formula to fit your needs.

I hope that you learned something from this because it took me awhile to put together all the code. And I ruined two source codes before getting it right ^_^ I will probably add in some comments later to explain what is happening. But, until then, figure it out. Any question just leave a message here. I will also attach the source code for a blank MSE1 build with this feature.

©2008 ~ This guide is my property.

Attachments:
File comment: This is a vanilla MSE Build 1 with the weight feature added. And yes, weight is spelled wrong in the item editor. Too lazy to fix it.
Item Weight.zip [481.54 KiB]
Downloaded 344 times

Author:  Rezeyu [ Wed Jan 02, 2008 9:28 pm ]
Post subject:  Re: Adding Weight To An Item

I prefer slots as opposed to weight.

That's what I used in GS, and when you equipped a backpack, it added more slots.

Author:  Stomach Pulser [ Wed Jan 02, 2008 9:30 pm ]
Post subject:  Re: Adding Weight To An Item

In my game you have 15 inventory slots and you can get up to 10 more through packs/pouches. I'm not sure if I'm having this weight system. Just made it for the hell of it.

Author:  William [ Sun Jan 06, 2008 4:47 pm ]
Post subject:  Re: Adding Weight To An Item

I prefer movable slots like most good games have. This is a pretty cool idea though.

Author:  Matt2 [ Thu Jan 10, 2008 6:49 pm ]
Post subject:  Re: Adding Weight To An Item

I want to point something out.

You defined Weight as a Double. Too big. Single should be fine. Unless you're planning to have each player be a super character. I'm pretty sure the average human can't lift more than 120 pounds... Single can cover that. :P

I'm from the US, I regret not knowing the metric system as well as the rest of the world. >.<

Also, CurWeight and MaxWeight, why are they Longs? Longs can't handle floating point numbers. So, if you have a limit of 3kg, and your item is 2.6 kg, if you use Fix(), your cur becomes 2[I'm assuming that's how you did it] instead of .4.

I'm editing this as i read the tut, so, I'll post when I've read this all the way through.

Other than what I mentioned, it's fine. I love weight, as opposed to slots. Makes games more realistic. Though, Pokemon doesn't call for it. Everything is a capsule, lol. So, yeah, good tut.

And I'm done. xD

Author:  Stomach Pulser [ Thu Jan 10, 2008 9:31 pm ]
Post subject:  Re: Adding Weight To An Item

CurWeight As Long was a mistake...I changed that to double later, after I finished the first part of the tutorial.
MaxWeight As Long is not a mistake though. Since I like your max weight to be a solid number, I made it a long.

And yes, it is somewhat of a waste to use a double, but I figured if someone wanted to have uber1337 extreme values, they could...

Author:  Matt2 [ Fri Jan 11, 2008 2:08 pm ]
Post subject:  Re: Adding Weight To An Item

Just giving you my input.

Btw, I dunno when you've come but I've been seeing you alot.

Welcome. And nice tut. And yeah, super uber weapons. Didn't consider that. Thinking as a player, not a developer. :P

Author:  Stomach Pulser [ Fri Jan 11, 2008 9:55 pm ]
Post subject:  Re: Adding Weight To An Item

Matt wrote:
Btw, I dunno when you've come but I've been seeing you alot.


If you're referring to me, I have been around for a little over a year and got more active when I decided to start up a project, Portal To Bakaria. I came from playerworlds...

Author:  Matt2 [ Sat Jan 12, 2008 5:02 pm ]
Post subject:  Re: Adding Weight To An Item

Stomach Pulser wrote:
Matt wrote:
Btw, I dunno when you've come but I've been seeing you alot.


If you're referring to me, I have been around for a little over a year and got more active when I decided to start up a project, Portal To Bakaria. I came from playerworlds...


Sweet. Welcome. Have fun! :P

Author:  Stomach Pulser [ Sun Jan 13, 2008 2:44 am ]
Post subject:  Re: Adding Weight To An Item

Welcoming me when I joined over a year ago. Me no understand explode logic brain gah!

Author:  Robin [ Sun Jan 13, 2008 2:20 pm ]
Post subject:  Re: Adding Weight To An Item

Matt.. wtf?

Author:  Matt2 [ Mon Jan 14, 2008 1:37 pm ]
Post subject:  Re: Adding Weight To An Item

Robin wrote:
Matt.. wtf?


I was indeed thinking the same thing...

I hadn't realized what I was doing. xD

It's this stupid programming class. Lights are dimmed, and I go to sleep alot. xD

Author:  laxika [ Wed Feb 06, 2008 4:30 pm ]
Post subject:  Re: Adding Weight To An Item

I found a bug.

If my max Weight 80, and my cur weight 79, but i want to pick an item when have 10 weight, i can... It says curr weight 89/ max weight 80... This is only in my source or the tutorial buggy? :roll:

Author:  Stomach Pulser [ Wed Feb 06, 2008 9:26 pm ]
Post subject:  Re: Adding Weight To An Item

Does it output that you have to much weight? So far, the code let's you pick up as much weight as you want without penalties, but it should say that you are overencumbered.

Author:  Rian [ Thu Feb 07, 2008 1:18 am ]
Post subject:  Re: Adding Weight To An Item

Code:
If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then Call PlayerMsg(Index, "You are over encumbered!", White)


Thats a key line of code for this system. If you want being over encumbered to carry penalties, that's pretty much how you would do it. I would probably take the above line of code and use it to determine whether a player can walk or not. Putting this in the appropriate sub would work:

Code:
If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then CanMove = False

Author:  laxika [ Thu Feb 07, 2008 5:42 pm ]
Post subject:  Re: Adding Weight To An Item

Sonire wrote:
Code:
If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then Call PlayerMsg(Index, "You are over encumbered!", White)


Thats a key line of code for this system. If you want being over encumbered to carry penalties, that's pretty much how you would do it. I would probably take the above line of code and use it to determine whether a player can walk or not. Putting this in the appropriate sub would work:

Code:
If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then CanMove = False


Ty this is nice, works good...:) my players can't walk if over encumbered... funny :twisted:

I now need another litle help. :roll:
I make a label(lblWeight) and a shape(shpWeight) to the inventory. I use this code:

Private Sub Label24_Click()
'Weight
lblWeight.Caption = GetPlayerMaxWeight & " / " & GetPlayerCurWeight
shpWeight.Width = (((GetPlayerMaxWeight / lblWeight.Width) / GetPlayerCurWeight / lblWeight.Width) * lblWeight.Width)
End Sub

But if i want to make my project, is says:

Argument not optional!

lblWeight.Caption = GetPlayerMaxWeight & " / " & GetPlayerCurWeight

Any tip :?: :roll:

Author:  Lea [ Thu Feb 07, 2008 6:02 pm ]
Post subject:  Re: Adding Weight To An Item

GetPlayerMaxWeight(MyIndex) & " / " & GetPlayerCurWeight(MyIndex)

the index arguement is, like it says, not optional.

Author:  laxika [ Thu Feb 07, 2008 6:28 pm ]
Post subject:  Re: Adding Weight To An Item

Dave wrote:
GetPlayerMaxWeight(MyIndex) & " / " & GetPlayerCurWeight(MyIndex)

the index arguement is, like it says, not optional.


lblWeight.Caption = GetPlayerMaxWeight(MyIndex) & " / " &

Variable not defined. :roll:

Author:  Matt2 [ Thu Feb 07, 2008 6:39 pm ]
Post subject:  Re: Adding Weight To An Item

laxika wrote:
Dave wrote:
GetPlayerMaxWeight(MyIndex) & " / " & GetPlayerCurWeight(MyIndex)

the index arguement is, like it says, not optional.


lblWeight.Caption = GetPlayerMaxWeight(MyIndex) & " / " &

Variable not defined. :roll:


Define it, or use one that's defined. Noob. It's common sense... -.-

Author:  Anthony [ Thu Feb 07, 2008 7:28 pm ]
Post subject:  Re: Adding Weight To An Item

Use Index instead of MyIndex.

Author:  laxika [ Thu Feb 07, 2008 8:31 pm ]
Post subject:  Re: Adding Weight To An Item

Vegeta wrote:
Use Index instead of MyIndex.


K this works thanks to you and Matt! :D

Author:  Reece [ Fri Feb 08, 2008 12:08 pm ]
Post subject:  Re: Adding Weight To An Item

laxika wrote:
Vegeta wrote:
Use Index instead of MyIndex.


K this works thanks to you and Matt! :D


All he done was call you a noob..

Author:  Lea [ Fri Feb 08, 2008 12:49 pm ]
Post subject:  Re: Adding Weight To An Item

MyIndex isn't defined client side? HMMmm..... ?

Author:  Robin [ Fri Feb 08, 2008 12:50 pm ]
Post subject:  Re: Adding Weight To An Item

Someone has a fucked up source.

Author:  Matt2 [ Fri Feb 08, 2008 2:34 pm ]
Post subject:  Re: Adding Weight To An Item

Lol.

Page 1 of 2 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/