Originally posted by Dark Echo
Mining System Ok, what this is.. Is well basically what it saids it is.. It is a basic mining system.. You will be able to place them down on the map and players walk up to them and press attack.. Right now it uses the Player Level to see if you are good enough to mine something, and it will give you an item. You can make more mines for people to mine, etc. This is a very simple system, which can be used to make a fishing system, or woodcutting system. Enjoy!!
Difficulty: 2/5
Part 1 - Server Side Okay, first things first, go into modConstants and find:
Public Const MAX_TRADES = 10
Now, add this:
Public Const MAX_MINES = 255
What this is, is just a public constant which tells us how many mines we can have. Not how many we can place, but how many different types of mines we can store..
Right, now find:
Public Const TILE_TYPE_KEYOPEN = 6
And add right below it:
Public Const TILE_TYPE_MINE = 8
Ok, with modConstants done, head over to modDatabase.. Time for saving the mines we make.. Now scroll all the way to the bottom and add this:
Sub SaveMine(ByVal MineNum As Long) Dim FileName As String Dim i As Long
FileName = App.Path & "\data\mines.ini" Call PutVar(FileName, "MINE" & MineNum, "Name", Trim(Mine(MineNum).Name)) Call PutVar(FileName, "MINE" & MineNum, "ClassReq", Trim(Mine(MineNum).ClassReq)) Call PutVar(FileName, "MINE" & MineNum, "LevelReq", Trim(Mine(MineNum).LevelReq)) Call PutVar(FileName, "MINE" & MineNum, "MaxLevel", Trim(Mine(MineNum).MaxLevel)) Call PutVar(FileName, "MINE" & MineNum, "Item", Trim(Mine(MineNum).Item)) End Sub
Sub SaveMines() Dim i As Long
For i = 1 To MAX_MINES Call SaveMine(i) Next i End Sub
Sub LoadMines() Dim FileName As String Dim i As Long
Call CheckMines FileName = App.Path & "\data\mines.ini" For i = 1 To MAX_MINES Mine(i).Name = GetVar(FileName, "MINE" & i, "Name") Mine(i).ClassReq = Val(GetVar(FileName, "MINE" & i, "ClassReq")) Mine(i).LevelReq = Val(GetVar(FileName, "MINE" & i, "LevelReq")) Mine(i).MaxLevel = Val(GetVar(FileName, "MINE" & i, "MaxLevel")) Mine(i).Item = Val(GetVar(FileName, "MINE" & i, "Item")) DoEvents Next i End Sub
Sub CheckMines() If Not FileExist("data\mines.ini") Then Call SaveMines End If End Sub
Now, with saving and loading done, head over to modGameLogic and add at the bottom, but before the player functions:
Sub ClearMine(ByVal Index As Long) Mine(Index).Name = "" Mine(Index).ClassReq = 0 Mine(Index).LevelReq = 0 Mine(Index).MaxLevel = 0 Mine(Index).Item = 0 End Sub
Sub ClearMines() Dim i As Long
For i = 1 To MAX_MINES Call ClearMine(i) Next i End Sub
Now this is the main mining section.. Ad the very bottom of the module add:
Sub CanPlayerMine(ByVal Index As Long, ByVal MineNum As Long) Dim c As Integer If Mine(MineNum).ClassReq = 0 Then If GetPlayerLevel(Index) <= Mine(MineNum).MaxLevel Then c = Int((Rnd * Mine(MineNum).MaxLevel) + 1) - GetPlayerLevel(Index) If c <= Mine(MineNum).LevelReq Then Call GiveItem(Index, Val(Mine(MineNum).Item), 1) Call PlayerMsg(Index, "You found some " & Trim(Mine(MineNum).Name) & "!", BrightBlue) Else Call PlayerMsg(Index, "You found nothing!", BrightRed) End If Else Call PlayerMsg(Index, "You are not skilled enough to mine " & Trim(Mine(MineNum).Name) & "!", Red) End If Else If GetPlayerClass(Index) <> Mine(MineNum).ClassReq Then If GetPlayerLevel(Index) <= Mine(MineNum).MaxLevel Then c = Int((Rnd * Mine(MineNum).MaxLevel) + 1) - GetPlayerLevel(Index) If c <= Mine(MineNum).LevelReq Then Call GiveItem(Index, Val(Mine(MineNum).Item), 1) Call PlayerMsg(Index, "You found some " & Trim(Mine(MineNum).Name) & "!", BrightBlue) Else Call PlayerMsg(Index, "You found nothing!", BrightRed) End If Else Call PlayerMsg(Index, "You are not skilled enough to mine " & Trim(Mine(MineNum).Name) & "!", Red) End If Else Call PlayerMsg(Index, "You cannot mine this because you are not the required class!", Red) End If End If End Sub
Alrighty, now to load the mines when the server starts up.. Alrighty, so go into modGeneral and find:
Sub InitServer()
Now, find:
Call SetStatus("Loading spells...") Call LoadSpells
And add after it:
Call SetStatus("Loading mines...") Call LoadMines
Ok, thats all for that module.. Now go into the next module down the list, which is modGloabals and find:
Public Spell(1 To MAX_SPELLS) As SpellRec
Now add this under it:
Public Mine(1 To MAX_MINES) As MineRec
Ok, now go into modHandleData and at the bottom of the mod, but before the end sub add this:
' :::::::::::::::::::::::::::::: ' :: Request edit mine packet :: ' :::::::::::::::::::::::::::::: If LCase(Parse(0)) = "requesteditmine" Then ' Prevent hacking If GetPlayerAccess(Index) < ADMIN_DEVELOPER Then Call HackingAttempt(Index, "Admin Cloning") Exit Sub End If Call SendDataTo(Index, "MINEEDITOR" & SEP_CHAR & END_CHAR) Exit Sub End If ' :::::::::::::::::::::: ' :: Edit mine packet :: ' :::::::::::::::::::::: If LCase(Parse(0)) = "editmine" Then ' Prevent hacking If GetPlayerAccess(Index) < ADMIN_DEVELOPER Then Call HackingAttempt(Index, "Admin Cloning") Exit Sub End If ' The mine # n = Val(Parse(1)) ' Prevent hacking If n < 0 Or n > MAX_MINES Then Call HackingAttempt(Index, "Invalid Mine Index") Exit Sub End If Call AddLog(GetPlayerName(Index) & " editing mine #" & n & ".", ADMIN_LOG) Call SendEditMineTo(Index, n) End If ' :::::::::::::::::::::: ' :: Save mine packet :: ' :::::::::::::::::::::: If (LCase(Parse(0)) = "savemine") Then ' Prevent hacking If GetPlayerAccess(Index) < ADMIN_DEVELOPER Then Call HackingAttempt(Index, "Admin Cloning") Exit Sub End If ' Mine # n = Val(Parse(1)) ' Prevent hacking If n < 0 Or n > MAX_MINES Then Call HackingAttempt(Index, "Invalid Mine Index") Exit Sub End If ' Update the mine Mine(n).Name = Trim(Parse(2)) Mine(n).ClassReq = Trim(Parse(3)) Mine(n).LevelReq = Trim(Parse(4)) Mine(n).MaxLevel = Trim(Parse(5)) Mine(n).Item = Trim(Parse(6)) ' Save it Call SendUpdateMineToAll(n) Call SaveMine(n) Call AddLog(GetPlayerName(Index) & " saving mine #" & n & ".", ADMIN_LOG) Exit Sub End If
' :::::::::::::::::: ' :: Request Mine :: ' :::::::::::::::::: If (LCase(Parse(0)) = "requestmine") Then ' Mine # n = Val(Parse(1)) ' Prevent hacking If n < 0 Or n > MAX_MINES Then Call HackingAttempt(Index, "Invalid Mine Index") Exit Sub End If ' Send mine info Call CanPlayerMine(Index, n) End If
Now, with that all done.. Go into modServerTCP and all the way at the bottom of that module add this:
Sub SendMine(ByVal Index As Long) Dim i As Long
For i = 1 To MAX_MINES If Trim(Mine(i).Name) <> "" Then Call SendUpdateMineTo(Index, i) End If Next i End Sub
Sub SendUpdateMineToAll(ByVal MineNum As Long) Dim Packet As String
Packet = "UPDATEMINE" & SEP_CHAR & MineNum & SEP_CHAR & Trim(Mine(MineNum).Name) & SEP_CHAR & END_CHAR Call SendDataToAll(Packet) End Sub
Sub SendUpdateMineTo(ByVal Index As Long, ByVal MineNum As Long) Dim Packet As String
Packet = "UPDATEMINE" & SEP_CHAR & MineNum & SEP_CHAR & Trim(Mine(MineNum).Name) & SEP_CHAR & END_CHAR Call SendDataTo(Index, Packet) End Sub
Sub SendEditMineTo(ByVal Index As Long, ByVal MineNum As Long) Dim Packet As String
Packet = "EDITMINE" & SEP_CHAR & MineNum & SEP_CHAR & Trim(Mine(MineNum).Name) & SEP_CHAR & Mine(MineNum).ClassReq & SEP_CHAR & Mine(MineNum).LevelReq & SEP_CHAR & Mine(MineNum).MaxLevel & SEP_CHAR & Mine(MineNum).Item & SEP_CHAR & END_CHAR Call SendDataTo(Index, Packet) End Sub
Sub SendmineTo(ByVal Index As Long, ByVal mineNum As Long) Dim Packet As String
Packet = "MINE" & SEP_CHAR & MineNum & SEP_CHAR & Trim(Mine(MineNum).Name) & SEP_CHAR & Mine(MineNum).ClassReq & SEP_CHAR & Mine(MineNum).LevelReq & SEP_CHAR & Mine(MineNum).MaxLevel & SEP_CHAR & Mine(MineNum).Item & SEP_CHAR & END_CHAR Call SendDataTo(Index, Packet) End Sub
Alrighty, server side basically done, just one more important thing.. Go into your modTypes and at the very bottom, add this:
Type MineRec Name As String * NAME_LENGTH ClassReq As Byte LevelReq As Byte MaxLevel As Byte Item As Byte End Type
Woohoo, thats server side done.. Phew.. Alrighty, now for the happy client side of things.. Hehe..
Part 2 - Client Side Alrighty, for client side, like the sign tutorial i made.. You will have to make a mine editor, and mine chooser.. Let's go through that now, so we can get it out of the way.. First things first, open up your frmIndex and go into the coding section and add this in the Private Sub cmdOk_Click():
If InMineEditor = True Then Call SendData("EDITMINE" & SEP_CHAR & EditorIndex & SEP_CHAR & END_CHAR) End If
Make sure you add it straight after the:
If InSpellEditor = True Then Call SendData("EDITSPELL" & SEP_CHAR & EditorIndex & SEP_CHAR & END_CHAR) End If
Now go into Private Sub cmdCanel_Click() and add:
InMineEditor = False
Make sure its after the End Sub. Ok, now go into frmMapEditor and add a new option button in the attributes section and call it optMine. Double click optMine and enter in this code:
frmMineChooser.Show vbModal
Thats all for the mapeditor.. Lets move on.. Now to make the basic Mine Chooser.. Ok, this is basically exactly the same as the Sign Choose from my previous tutorial.. Ok, make a new form called frmMineChooser and add a horizontal scrollbar called scrlMineNum, a label called lblMineNum, and two command buttoms called cmdOk and cmdCancel.. Now get into the forms coding and add this:
Private Sub cmdCancel_Click() Unload Me End Sub
Private Sub cmdOk_Click() MineNum = scrlMineNum.Value Unload Me End Sub
Private Sub scrlMineNum_Change() lblMineNum.Caption = STR(scrlMineNum.Value) End Sub
Ok, for the Mine Editor, we need.. one textbox called txtMineName, three i repeat three, combo boxes called: cmbClassReq, and cmbItem.. Now we also need two horizontal scroll bars, called: scrlLevelReq, and scrlMaxLevel. Check the spellings to make sure they match up with code later on..
Now, go into the coding and add this: Private Sub cmdCancel_Click() Call MineEditorCancel End Sub
Private Sub cmdOk_Click() Call MineEditorOk End Sub
Ok go into modClientTCP and at the bottom of that module, add these subs:
Sub SendRequestEditMine() Dim Packet As String
Packet = "REQUESTEDITMINE" & SEP_CHAR & END_CHAR Call SendData(Packet) End Sub
Public Sub SendSaveMine(ByVal MineNum As Long) Dim Packet As String
With Mine(MineNum) Packet = "SAVEMINE" & SEP_CHAR & MineNum & SEP_CHAR & Trim(.Name) & SEP_CHAR & .ClassReq & SEP_CHAR & .LevelReq & SEP_CHAR & .MaxLevel & SEP_CHAR & .Item & SEP_CHAR & END_CHAR End With Call SendData(Packet) End Sub
Now, go into the modConstants and find:
Public Const MAX_TRADES = 10
And add below that:
Public Const MAX_MINES = 255
Alrighty, now go into modGameLogic and find:
Public Sub GameLoop()
Now, scroll down and find:
If .Type = TILE_TYPE_KEYOPEN Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "O", QBColor(White))
Right under that add:
If .Type = TILE_TYPE_MINE Then Call DrawText(TexthDC, x * PIC_X + 8, y * PIC_Y + 8, "M", QBColor(White))
Okay, now find:
Sub HandleKeypresses(ByVal KeyAscii As Integer)
Now, scroll down and find:
' // Mapper Admin Commands //
Okay, you gotta scroll down a bit more till you find:
' Banning a player If LCase(Mid(MyText, 1, 4)) = "/ban" Then If Len(MyText) > 5 Then MyText = Mid(MyText, 6, Len(MyText) - 5) Call SendBan(MyText) MyText = "" End If Exit Sub End If
And add right below that (Oh, please be sure to nest it properly so it is inside the beginning if statement):
' Editing mine request If Mid(MyText, 1, 10) = "/editmine" Then Call SendRequestEditMine MyText = "" Exit Sub End If
Ok, now find:
Public Sub CheckAttack()
And add at the end of the sub:
'Mining Dim MinePacket As String If ControlDown = True And Map.Tile(GetPlayerX(MyIndex), GetPlayerY(MyIndex)).Type = TILE_TYPE_MINE Then MineNum = Map.Tile(GetPlayerX(MyIndex), GetPlayerY(MyIndex)).Data1 MinePacket = "requestmine" & SEP_CHAR & MineNum & SEP_CHAR & END_CHAR Call SendData(MinePacket) End If
God, im getting tired.. Arghh, i cant believe im even making this tutorial.. Anyway, i must finish.. I Planned to do this, so im going to do it.. Alrighty, find:
Public Sub EditorMouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Scroll down and find:
If frmMapEditor.optKeyOpen.Value = True Then .Type = TILE_TYPE_KEYOPEN .Data1 = KeyOpenEditorX .Data2 = KeyOpenEditorY .Data3 = 0 End If
And add right below it, making sure its in the End With, and nested nicely this:
If frmMapEditor.optMine.Value = True Then .Type = TILE_TYPE_MINE .Data1 = MineNum .Data2 = 0 .Data3 = 0 End If
Arghh.. Fingers hurting, but i must peddle on.. Okie, dokie.. at the bottom of this module, which should be modGameLogic add:
Public Sub MineEditorInit() Dim a As Long Dim b As Long
frmMineEditor.cmbClassReq.AddItem "All Classes" For a = 0 To Max_Classes frmMineEditor.cmbClassReq.AddItem Trim(Class(a).Name) Next a frmMineEditor.txtMineName.Text = Trim(Mine(EditorIndex).Name) frmMineEditor.cmbClassReq.ListIndex = Mine(EditorIndex).ClassReq
If Mine(EditorIndex).LevelReq = 0 Then frmMineEditor.scrlLevelReq.Value = 1 Else frmMineEditor.scrlLevelReq.Value = Mine(EditorIndex).LevelReq End If
If Mine(EditorIndex).MaxLevel = 0 Then frmMineEditor.scrlMaxLevel.Value = 1 Else frmMineEditor.scrlMaxLevel.Value = Mine(EditorIndex).MaxLevel End If For b = 1 To MAX_ITEMS frmMineEditor.cmbItem.AddItem b & ": " & Trim(Item(b).Name) Next b
frmMineEditor.Show vbModal End Sub
Public Sub MineEditorOk() Mine(EditorIndex).Name = frmMineEditor.txtMineName.Text Mine(EditorIndex).ClassReq = frmMineEditor.cmbClassReq.ListIndex Mine(EditorIndex).LevelReq = frmMineEditor.scrlLevelReq.Value Mine(EditorIndex).MaxLevel = frmMineEditor.scrlMaxLevel.Value Mine(EditorIndex).Item = frmMineEditor.cmbItem.ListIndex + 1 Call SendSaveMine(EditorIndex) InMineEditor = False Unload frmMineEditor End Sub
Public Sub MineEditorCancel() InMineEditor = False Unload frmMineEditor End Sub
Alrighty, now go into modGlobals and find:
' Used for map key opene ditor Public KeyOpenEditorX As Long Public KeyOpenEditorY As Long
Now add right below it:
' Used for map mine editor Public MineNum As Integer
Right, now find:
Public EditorIndex As Long
And add straight below that:
Public InMineEditor As Boolean
Now find:
Public Spell(1 To MAX_SPELLS) As SpellRec
And add below that:
Public Mine(1 To MAX_MINES) As MineRec
Now, go into modHandleData, yes we're almost done.. Arghh this is my last tutorial, i swear.. Arghh.. Ok, go into modHandleData and at the bottom of that module, but before the end sub add:
' :::::::::::::::::::::::: ' :: Mine editor packet :: ' :::::::::::::::::::::::: If (LCase(Parse(0)) = "mineeditor") Then InMineEditor = True frmIndex.Show frmIndex.lstIndex.Clear ' Add the names For i = 1 To MAX_MINES frmIndex.lstIndex.AddItem i & ": " & Trim(Mine(i).Name) Next i frmIndex.lstIndex.ListIndex = 0 Exit Sub End If ' :::::::::::::::::::::::: ' :: Update mine packet :: ' :::::::::::::::::::::::: If (LCase(Parse(0)) = "updatemine") Then n = Val(Parse(1)) ' Update the mine name Mine(n).Name = Trim(Parse(2)) Exit Sub End If ' :::::::::::::::::::::: ' :: Edit mine packet :: <- Used for mine editor admins only ' :::::::::::::::::::::: If (LCase(Parse(0)) = "editmine") Then n = Val(Parse(1)) ' Update the mine Mine(n).Name = Trim(Parse(2)) Mine(n).ClassReq = Val(Parse(3)) Mine(n).LevelReq = Val(Parse(4)) Mine(n).Maxlevel = Val(Parse(5)) Mine(n).Item = Val(Parse(6)) ' Initialize the mine editor Call MineEditorInit Exit Sub End If
Woohoo, nearly there.. Go into modTypes, and add this:
Type MineRec Name As String * NAME_LENGTH ClassReq As Byte LevelReq As Byte MaxLevel As Byte Item As Byte End Type
Lets just hope you know where.. Im too tired to type anymore.. Anyway, if you went through this tutorial properly, you should now have a nice and simple mining system.. I'm planning on making another tutorial, a cooking tutorial.. Where you combine two items to become one.. It will be very similar to this, with a few adjustments.. Im too tired right now, but i'll work on it later.. Enjoy guys!!
Oh yeah, i have yet to add in the class requirement, although that is not too hard to accomplish.. Go into the server side, and look for the CanPlayerMine sub, and just make a basic if statement before the level req check.. ;)
|