Print Page | Close Window

Player Housing

Printed From: Mirage Source
Category: Tutorials
Forum Name: Submitted Tutorials
Forum Discription: Tutorial submissions for MSE are posted here, waiting for approval
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=387
Printed Date: 20 December 2006 at 6:05pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: Player Housing
Posted By: tosuxo
Subject: Player Housing
Date Posted: 05 March 2006 at 5:00am
Difficulty - 3/5 (for beginners) and 1/5 (for semi-skilled programmers)

Notes:
This code is quite messy but it SHOULD work, if it doesn't then please tell me and I'll work it all out.

SERVER SIDE
In modTypes (please give me the new name so i can edit)

in the Declarations, add:
Public Const MAX_HOUSES = 25

below
Public Const MAX_ITEMS As Integer = 500

(anywhere in the "General Constants" list)

This lets the server know how many houses are allowed, for editing in the index. You can change this to how many you want to be used in your game, but you can always make the number bigger in the future (but if you make it more than 255, add "as long" after).

Beneath the "Type Playerrec" stuff (after the "End Type") add
Type HouseAdminRec
    Owner As String * NAME_LENGTH
    Guest1 As String * NAME_LENGTH
    Guest2 As String * NAME_LENGTH
    MapNum As String * NAME_LENGTH
    MapX As String * NAME_LENGTH
    MapY As String * NAME_LENGTH
End Type

Type HouseRec
    Owner As String * NAME_LENGTH
    Guest1 As String * NAME_LENGTH
    Guest2 As String * NAME_LENGTH
    MapNum As String * NAME_LENGTH
    MapX As String * NAME_LENGTH
    MapY As String * NAME_LENGTH
End Type


In modDatabase (if name has changed please tell me)

add anywhere:

Sub SaveHouse(ByVal HouseNum As Long)
Dim FileName As String
Dim i As Long

    FileName = App.Path & "\data\houses.ini"
  
    Call PutVar(FileName, "HOUSE" & HouseNum, "Owner", Trim(House(HouseNum).Owner))
    Call PutVar(FileName, "HOUSE" & HouseNum, "Guest1", Trim(House(HouseNum).Guest1))
    Call PutVar(FileName, "HOUSE" & HouseNum, "Guest2", Trim(House(HouseNum).Guest2))
    Call PutVar(FileName, "HOUSE" & HouseNum, "MapNum", Trim(House(HouseNum).MapNum))
    Call PutVar(FileName, "HOUSE" & HouseNum, "MapX", Trim(House(HouseNum).MapX))
    Call PutVar(FileName, "HOUSE" & HouseNum, "MapY", Trim(House(HouseNum).MapY))
End Sub
Sub SaveMyHouse(ByVal HouseNum2 As Long)
Dim FileName As String

    FileName = App.Path & "\data\houses.ini"
  
    Call PutVar(FileName, "HOUSE" & HouseNum2, "Owner", Trim(House(HouseNum2).Owner))
    Call PutVar(FileName, "HOUSE" & HouseNum2, "Guest1", Trim(House(HouseNum2).Guest1))
    Call PutVar(FileName, "HOUSE" & HouseNum2, "Guest2", Trim(House(HouseNum2).Guest2))
    Call PutVar(FileName, "HOUSE" & HouseNum2, "MapNum", Trim(House(HouseNum2).MapNum))
    Call PutVar(FileName, "HOUSE" & HouseNum2, "MapX", Trim(House(HouseNum2).MapX))
    Call PutVar(FileName, "HOUSE" & HouseNum2, "MapY", Trim(House(HouseNum2).MapY))

End Sub
Sub SaveMyHouses()
Dim i As Long

    For i = 1 To MAX_HOUSES
        Call SaveMyHouse(i)
    Next i
End Sub

Sub SaveHouses()
Dim i As Long

    For i = 1 To MAX_HOUSES
        Call SaveHouse(i)
    Next i
End Sub

Sub LoadHouses()
Dim FileName As String
Dim i As Long

    Call CheckHouses
  
    FileName = App.Path & "\data\houses.ini"
  
    For i = 1 To MAX_HOUSES
        House(i).Owner = Trim(GetVar(FileName, "HOUSE" & i, "Owner"))
        House(i).Guest1 = Trim(GetVar(FileName, "HOUSE" & i, "Guest1"))
        House(i).Guest2 = Trim(GetVar(FileName, "HOUSE" & i, "Guest2"))
        House(i).MapNum = Trim(GetVar(FileName, "HOUSE" & i, "MapNum"))
        House(i).MapX = Trim(GetVar(FileName, "HOUSE" & i, "MapX"))
        House(i).MapY = Trim(GetVar(FileName, "HOUSE" & i, "MapY"))
      
        DoEvents
    Next i
End Sub

Sub CheckHouses()
    If Not FileExist("data\houses.ini") Then
        Call SaveHouses
    End If
End Sub


These codes will write the information and read the information from the house list. This will, basically, tell the player where he gets warped to, and who owns it, and so on.

In modGameLogic (and again, please say if name changed)
add:

Sub ClearHouses(ByVal Index As Long)
    House(Index).Owner = ""
    House(Index).Guest1 = ""
    House(Index).Guest2 = ""
    House(Index).MapNum = ""
    House(Index).MapX = ""
    House(Index).MapY = ""
End Sub

Sub ClearHouse()
Dim i As Long

    For i = 1 To MAX_HOUSES
        Call ClearHouses(i)
    Next i
End Sub


This will clear the data for a certain house, or for them all if you get annoyed with people ;).

In modGeneral (and again lol)
In sub InitServer
Add:
    Call SetStatus("Loading houses...")
    Call LoadHouses

below:
    Call SetStatus("Loading spells...")
    Call LoadSpells

This just loads the houses when the server starts.

In modHandleData (is that the right name?)
add:
    ' :::::::::::::::::::::::::::::::
    ' :: Request edit house packet ::
    ' :::::::::::::::::::::::::::::::
    If LCase(Parse(0)) = "requestedithouse" Then
        ' Prevent hacking
        If GetPlayerAccess(Index) < ADMIN_MAPPER Then
         & ; ;nbs p;  Call HackingAttempt(Index, "Admin Cloning")
         & ; ;nbs p;  Exit Sub
        End If      
        Call SendDataTo(Index, "houseeditor" & SEP_CHAR & END_CHAR)
        Exit Sub
    End If
  
    ' :::::::::::::::::::::::
    ' :: Edit House packet ::
    ' :::::::::::::::::::::::
    If LCase(Parse(0)) = "edithouse" Then
        ' Prevent hacking
        If GetPlayerAccess(Index) < ADMIN_DEVELOPER Then
         & ; ;nbs p;  Call HackingAttempt(Index, "Admin Cloning")
         & ; ;nbs p;  Exit Sub
        End If
      
        ' The house #
        n = Val(Parse(1))
      
        ' Prevent hacking
        If n < 0 Or n > MAX_HOUSES Then
         & ; ;nbs p;  Call HackingAttempt(Index, "Invalid House Index")
         & ; ;nbs p;  Exit Sub
        End If
      
        Call AddLog(GetPlayerName(Index) & " editing house #" & n & ".", ADMIN_LOG)
        Call SendEditHouseTo(Index, n)
    End If
  
    ' :::::::::::::::::::::::
    ' :: Save house packet ::
    ' :::::::::::::::::::::::
    If (LCase(Parse(0)) = "savehouse") Then
        ' Prevent hacking
        If GetPlayerAccess(Index) < ADMIN_DEVELOPER Then
         & ; ;nbs p;  Call HackingAttempt(Index, "Admin Cloning")
         & ; ;nbs p;  Exit Sub
        End If
      
        ' house #
        n = Val(Parse(1))
      
        ' Prevent hacking
        If n < 0 Or n > MAX_HOUSES Then
         & ; ;nbs p;  Call HackingAttempt(Index, "Invalid House Index")
         & ; ;nbs p;  Exit Sub
        End If
      
        ' Update the house
        House(n).Owner = Trim(Parse(2))
        House(n).Guest1 = Trim(Parse(3))
        House(n).Guest2 = Trim(Parse(4))
        House(n).MapNum = Trim(Parse(5))
        House(n).MapX = Trim(Parse(6))
        House(n).MapY = Trim(Parse(7))
       
        ' Save it
        Call SaveHouse(n)
        Call SendUpdateHouseToAll(n)
        Call SaveHouses
        Call AddLog(GetPlayerName(Index) & " saving house #" & n & ".", ADMIN_LOG)
        Exit Sub
    End If

    ' :::::::::::::::::::::
    ' :: Request MYHOUSE ::
    ' :::::::::::::::::::::
    If (LCase(Parse(0)) = "houseadmincheck") Then
        n = Val(Parse(1))
       Dim filename2 As String
       Dim Ownername2
       Dim Guest12
       Dim Guest22
       Dim MapXmine
       Dim MapYmine
       Dim MapNumMine
    filename2 = App.Path & "\data\houses.ini"
    Ownername2 = ReadINI("HOUSE" & n, "Owner", filename2)
    Guest12 = ReadINI("HOUSE" & n, "Guest1", filename2)
    Guest22 = ReadINI("HOUSE" & n, "Guest2", filename2)
    MapNumMine = ReadINI("HOUSE" & n, "MapNum", filename2)
    MapXmine = ReadINI("HOUSE" & n, "MapX", filename2)
    MapYmine = ReadINI("HOUSE" & n, "MapY", filename2)
   
   
    If GetPlayerName(Index) = Ownername2 Then
     Call SendDataTo(Index, "canhouseadmin" & SEP_CHAR & Ownername2 & SEP_CHAR & Guest12 & SEP_CHAR & Guest22 & SEP_CHAR & MapNumMine & SEP_CHAR & MapXmine & SEP_CHAR & MapYmine & SEP_CHAR & END_CHAR)
    Else
     Call PlayerMsg(Index, "You are not the owner. The owner is " & Ownername2 & ".", TellColor)
    End If
    Exit Sub
End If

    ' :::::::::::::::::::::::::
    ' :: Save myhouse packet ::
    ' :::::::::::::::::::::::::
    If (LCase(Parse(0)) = "myhousesave") Then

Dim Filename3
        ' house #
        n = Val(Parse(1))
      
        ' Update the House
        House(n).Owner = Trim(Parse(4))
        House(n).Guest1 = Trim(Parse(3))
        House(n).Guest2 = Trim(Parse(2))
        'House(n).MapNum = Trim(Parse(5))
        'House(n).MapX = Trim(Parse(6))
        'House(n).MapY = Trim(Parse(7))
       
        ' Save it
        Call SaveMyHouse(n)
        Call SendUpdateHouseToAll(n)
        Call SaveMyHouses
        Call AddLog(GetPlayerName(Index) & " saving (his) house #" & n & ".", ADMIN_LOG)
        Exit Sub
    End If
   
   
    ' :::::::::::::::::::
    ' :: Request HOUSE ::
    ' :::::::::::::::::::
    If (LCase(Parse(0)) = "requesthouse") Then
         & ; ;nbs p;
        ' House # and stuff
        n = Val(Parse(1))
       Dim FileName As String
       Dim Ownername
       Dim guest1name
       Dim guest2name
       Dim targetmap
       Dim targetx
       Dim targety
       Dim doorknockplayer
      
    doorknockplayer = Val(Parse(2))
    FileName = App.Path & "\data\houses.ini"
    Ownername = ReadINI("HOUSE" & n, "Owner", FileName)
    guest1name = ReadINI("HOUSE" & n, "Guest1", FileName)
    guest2name = ReadINI("HOUSE" & n, "Guest2", FileName)
    targetmap = ReadINI("HOUSE" & n, "MapNum", FileName)
    targetx = ReadINI("HOUSE" & n, "MapX", FileName)
    targety = ReadINI("HOUSE" & n, "MapY", FileName)
   
        If GetPlayerName(Index) = Ownername Or GetPlayerName(Index) = guest1name Or GetPlayerName(Index) = guest2name Then
         & ; ;nbs p;  Call PlayerWarp(Index, targetmap, targetx, targety)
         & ; ;nbs p;  Call PlayerMsg(Index, "Welcome to the house " & GetPlayerName(Index), TellColor)
        Else
         & ; ;nbs p;  Call PlayerMsg(Index, "You are not invited. This house is owned by " & Ownername & ".", TellColor)
        End If
       
        ' Prevent hacking
        If n < 0 Or n > MAX_HOUSES Then
         & ; ;nbs p;  Call HackingAttempt(Index, "Invalid House Index")
         & ; ;nbs p;  Exit Sub
        End If
      
        ' Send house info
    '    Call SendHouseTo(Index, n)
    Exit Sub
    End If


This just handles all of the "can the player enter" and "can the player edit the house data" and so on.

In modServerTCP (I think it's still named this)

Add this anywhere:
Sub SendUpdateHouseToAll(ByVal HouseNum As Long)
Dim Packet As String

    Packet = "UPDATEHOUSE" & SEP_CHAR & HouseNum & SEP_CHAR & Trim(House(HouseNum).Owner) & SEP_CHAR & END_CHAR
    Call SendDataToAll(Packet)
End Sub

Sub SendUpdateHouseTo(ByVal Index As Long, ByVal HouseNum As Long)
Dim Packet As String

    Packet = "UPDATEHOUSE" & SEP_CHAR & HouseNum & SEP_CHAR & Trim(House(HouseNum).Owner) & SEP_CHAR & END_CHAR
    Call SendDataTo(Index, Packet)
End Sub

Sub SendEditHouseTo(ByVal Index As Long, ByVal HouseNum As Long)
Dim Packet As String

    Packet = "EDITHOUSE" & SEP_CHAR & HouseNum & SEP_CHAR & Trim(House(HouseNum).Owner) & SEP_CHAR & House(HouseNum).Guest1 & SEP_CHAR & House(HouseNum).Guest2 & SEP_CHAR & House(HouseNum).MapNum & SEP_CHAR & House(HouseNum).MapX & SEP_CHAR & House(HouseNum).MapY & SEP_CHAR & END_CHAR
    Call SendDataTo(Index, Packet)
End Sub

Sub SendHouseTo(ByVal Index As Long, ByVal HouseNum As Long)
Dim Packet As String

    Packet = "HOUSE" & SEP_CHAR & HouseNum & SEP_CHAR & Trim(House(HouseNum).Owner) & SEP_CHAR & House(HouseNum).Guest1 & SEP_CHAR & House(HouseNum).Guest2 & SEP_CHAR & House(HouseNum).MapNum & SEP_CHAR & House(HouseNum).MapX & SEP_CHAR & House(HouseNum).MapY & SEP_CHAR & END_CHAR
    Call SendDataTo(Index, Packet)
End Sub


That should be the lot server side. I will get round to adding the Client side soon.

If anyone can spot anything is missing, or anything like that, then please tell me. Thanks.

I can't reply so i'll say here:

I'm not sure what the difference is between savehouse and savemyhouse... like I said, it's totally jumbled up but it works for me... I'll edit stuff eventually so that it's optimised and improved.



Replies:
Posted By: Misunderstood
Date Posted: 05 March 2006 at 9:37am
whats the differences between savehouse and savemyhouse?



Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info