Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GLOBAL VARIABLE DEFINITIONS
- global gCharacterManager
- global gMapManager
- global gItemManager
- -- PROPERTY DEFINITIONS (PARENT)
- property pCharacters
- -- PROPERTY DEFINITIONS (CHILD)
- property pUsername
- property pData
- property pTempItemDatabase
- -- PARENT FUNCTION
- -- Returns the newly created script object.
- on new(me)
- return(me)
- end new
- -- PARENT FUNCTION
- -- Initializes the parent script object.
- on startParent(me)
- me.pCharacters = [:]
- end startParent
- -- CHILD FUNCTION
- -- Initializes the child script object.
- on startChild(me, username)
- me.pUsername = username
- me.pData = fread("Database\Characters\" & username & ".dat", #value)
- me.pTempItemDatabase = [:]
- end startChild
- -- PARENT FUNCTION
- -- Adds a character object based on username given.
- -- Will not add if object already exists.
- on addCharacterObject(me, username)
- if (me.pCharacters.getaProp(username) <> void) then exit
- characterObject = script("CharacterManager").new()
- characterObject.startChild(username)
- me.pCharacters.setaProp(username, characterObject)
- characterObject.loadCharacter()
- end addCharacterObject
- -- PARENT FUNCTION
- -- Removes a character object based on username given.
- -- Will not remove if object does not exist.
- on removeCharacterObject(me, username)
- if (me.pCharacters.getaProp(username) = void) then exit
- characterObject = me.getCharacterObject(username)
- characterObject.saveCharacter()
- position = listPos(me.pCharacters, username)
- me.pCharacters.deleteAt(position)
- end removeCharacterObject
- -- PARENT FUNCTION
- -- Returns a character object based on username given.
- on getCharacterObject(me, username)
- characterObject = me.pCharacters.getaProp(username)
- return(characterObject)
- end getCharacterObject
- -- CHILD FUNCTION
- -- Synchronizes all character data to the client.
- -- Then joins the character object to the map.
- on loadCharacter(me)
- me.syncBackpackData()
- me.syncEquipmentData()
- data = me.getData()
- sendPacket(me.pUsername, "CharacterLoaded", data)
- gMapManager.joinMap(#character, data.map.coords, me.pUsername)
- end loadCharacter
- -- CHILD FUNCTION
- -- Saves character object data to the database.
- on saveCharacter(me)
- data = me.getData()
- fwrite("Database/Characters/" & me.pUsername & ".dat", data, #value)
- end saveCharacter
- -- PARENT FUNCTION
- -- Removes the character object when they have disconnected.
- on logout(me, username)
- characterObject = me.getCharacterObject(username)
- if (characterObject = void) then exit
- data = characterObject.getData()
- mapObject = gMapManager.getMapObject(data.map.coords)
- if (mapObject = void) then exit
- mapObject.removeCharacter(username)
- me.removeCharacterObject(username)
- end logout
- -- CHILD FUNCTION
- -- Returns the data for the character object.
- on getData(me)
- return(me.pData)
- end getData
- -- CHILD FUNCTION
- -- Sets the data for the character object.
- on setData(me, data)
- me.pData = data
- end setData
- -- CHILD FUNCTION
- -- Returns the list required to draw a character sprite on the client.
- on getDrawList(me)
- data = me.getData()
- drawList = [:]
- drawList.addProp(#location, data.map.location)
- drawList.addProp(#facing, data.map.facing)
- drawList.addProp(#appearance, data.appearance)
- drawList.addProp(#equipment, data.equipment)
- return(drawList)
- end getDrawList
- -- CHILD FUNCTION
- -- Walks the character object depending on the direction given.
- -- Synchronizes movement to all characters on the map.
- on walkCharacter(me, walkData)
- direction = walkData[1]
- location = walkData[2]
- data = me.getData()
- if (data.map.location <> location) then sendPacket(me.pUsername, "InterpolateCharacter", data.map.location)
- data.map.facing = direction
- location = data.map.location
- mapObject = gMapManager.getMapObject(data.map.coords)
- nextLocation = [:]
- nextLocation.setaProp(#x, location.x)
- nextLocation.setaProp(#y, location.y)
- case (direction) of
- #north: nextLocation.y = nextLocation.y - 1
- #south: nextLocation.y = nextLocation.y + 1
- #east: nextLocation.x = nextLocation.x + 1
- #west: nextLocation.x = nextLocation.x - 1
- end case
- if (mapObject.checkForBlockedTile(nextLocation)) then exit
- characterList = mapObject.getCharacters()
- sendPacket(characterList, "WalkCharacter", [me.pUsername, direction])
- case (direction) of
- #north: location.y = location.y - 1
- #south: location.y = location.y + 1
- #east: location.x = location.x + 1
- #west: location.x = location.x - 1
- end case
- data.map.location = location
- if (location.y = 0) then
- coords = data.map.coords
- data.map.coords = [#x: coords.x, #y: coords.y - 1]
- data.map.location = [#x: location.x, #y: 13]
- gMapManager.leaveMap(#character, coords, me.pUsername)
- gMapManager.joinMap(#character, data.map.coords, me.pUsername)
- end if
- if (location.y = 14) then
- coords = data.map.coords
- data.map.coords = [#x: coords.x, #y: coords.y + 1]
- data.map.location = [#x: location.x, #y: 1]
- gMapManager.leaveMap(#character, coords, me.pUsername)
- gMapManager.joinMap(#character, data.map.coords, me.pUsername)
- end if
- if (location.x = 23) then
- coords = data.map.coords
- data.map.coords = [#x: coords.x + 1, #y: coords.y]
- data.map.location = [#x: 1, #y: location.y]
- gMapManager.leaveMap(#character, coords, me.pUsername)
- gMapManager.joinMap(#character, data.map.coords, me.pUsername)
- end if
- if (location.x = 0) then
- coords = data.map.coords
- data.map.coords = [#x: coords.x - 1, #y: coords.y]
- data.map.location = [#x: 22, #y: location.y]
- gMapManager.leaveMap(#character, coords, me.pUsername)
- gMapManager.joinMap(#character, data.map.coords, me.pUsername)
- end if
- mapObject.setObjectLoc(#character, me.pUsername, data.map.location)
- end walkCharacter
- -- CHILD FUNCTION
- -- Makes the character object attack based on direction given.
- -- Synchronizes that character object is attacking to all characters on map.
- on attackCharacter(me, direction)
- data = me.getData()
- data.map.facing = direction
- mapObject = gMapManager.getMapObject(data.map.coords)
- characterList = mapObject.getCharacters()
- sendPacket("@allusers", "AttackCharacter", [me.pUsername, direction])
- attackLocation = [:]
- case (direction) of
- #north:
- attackLocation.setaProp(#x, data.map.location.x)
- attackLocation.setaProp(#y, data.map.location.y - 1)
- #south:
- attackLocation.setaProp(#x, data.map.location.x)
- attackLocation.setaProp(#y, data.map.location.y + 1)
- #east:
- attackLocation.setaProp(#x, data.map.location.x + 1)
- attackLocation.setaProp(#y, data.map.location.y)
- #west:
- attackLocation.setaProp(#x, data.map.location.x - 1)
- attackLocation.setaProp(#y, data.map.location.y)
- end case
- objectList = mapObject.searchLoc(attackLocation)
- repeat with i = 1 to objectList.count
- attackPower = me.getAttackPower()
- type = objectList[i].type
- target = objectList.getPropAt(i)
- me.dealDamage(type, target, attackPower)
- end repeat
- end attackCharacter
- -- CHILD FUNCTION
- -- Determines how much power a character object's attack has.
- -- Returns the determination as an integer.
- on getAttackPower(me)
- return(1)
- end getAttackPower
- -- CHILD FUNCTION
- -- Determines how much damage has been dealt.
- -- Informs the client that damage was dealt.
- on dealDamage(me, type, target, amount)
- case (type) of
- #character:
- characterObject = gCharacterManager.getCharacterObject(target)
- characterObject.takeDamage(me.pUsername, amount)
- message = "[Combat] You have attacked" && target && "for 1 damage."
- sendPacket(me.pUsername, "ChatMessage", [message, 30])
- end case
- end dealDamage
- -- CHILD FUNCTION
- -- Determines how much damage has been taken and modifies stats.
- -- Informs the client that damage was taken.
- on takeDamage(me, attacker, amount)
- data = me.getData()
- data.stats.health = data.stats.health - amount
- me.setData(data)
- sendPacket(me.pUsername, "SyncStats", data.stats)
- if (data.stats.health = 0) then
- data.stats.health = data.stats.maxhealth
- me.setData(data)
- sendPacket(me.pUsername, "SyncStats", data.stats)
- message = "[Combat] You have been killed by" && attacker & "."
- sendPacket(me.pUsername, "ChatMessage", [message, 30])
- characterObject = gCharacterManager.getCharacterObject(attacker)
- characterObject.killedCharacter(me.pUsername)
- respawnCoords = genPropList(data.respawn.coords)
- respawnLocation = genPropList(data.respawn.location)
- me.warp(respawnCoords, respawnLocation)
- end if
- end takeDamage
- -- CHILD FUNCTION
- -- Informs that the character object has killed another character.
- -- Informs the client that the character was killed.
- -- Gives appropriate awards for the kill.
- on killedCharacter(me, target)
- data = me.getData()
- data.stats.experience = data.stats.experience + 1
- if (data.stats.experience = 10) then
- data.stats.experience = 0
- data.stats.level = data.stats.level + 1
- data.stats.maxhealth = data.stats.maxhealth + 5
- data.stats.health = data.stats.maxhealth
- end if
- me.setData(data)
- sendPacket(me.pUsername, "SyncStats", data.stats)
- message = "[Combat] You have killed" && target && "and gained 1 experience point."
- sendPacket(me.pUsername, "ChatMessage", [message, 30])
- end killedCharacter
- -- CHILD FUNCTION
- --- Synchronizes all map data to the client.
- on syncMapData(me)
- data = me.getData()
- sendPacket(me.pUsername, "SyncMap", data.map)
- end syncMapData
- -- CHILD FUNCTION
- -- Synchronizes all backpack data to the client.
- on syncBackpackData(me)
- data = me.getData()
- repeat with i = 1 to data.backpack.slots.count
- if (data.backpack.slots.getaProp(i) = void) then next repeat
- me.sendItemData(data.backpack.slots[i].itemID)
- end repeat
- sendPacket(me.pUsername, "SyncBackpack", data.backpack)
- end syncBackpackData
- -- CHILD FUNCTION
- -- Synchronizes all equipment data to the client.
- on syncEquipmentData(me)
- data = me.getData()
- repeat with i = 1 to data.equipment.count
- if (data.equipment[i] = void) then next repeat
- me.sendItemData(data.equipment[i].itemID)
- end repeat
- sendPacket(me.pUsername, "SyncEquipment", data.equipment)
- end syncEquipmentData
- -- CHILD FUNCTION
- -- Synchronizes the character object's state to all characters on the map.
- -- Each character's client then updates the sprite accordingly.
- on updateSpriteToMap(me)
- data = me.getData()
- mapObject = gMapManager.getMapObject(data.map.coords)
- mapObject.syncItemData()
- characterList = mapObject.getCharacters()
- drawList = me.getDrawList()
- drawData = [:]
- drawData.addProp(me.pUsername, drawList)
- sendPacket(characterList, "UpdateCharacterSprite", drawData)
- end updateSpriteToMap
- -- CHILD FUNCTION
- -- Adds an item object to the character object's backpack.
- -- The script properly determines all possible outcomes of adding the item object.
- -- Alerts the character if there was an error adding the item object to the backpack.
- on addToBackpack(me, itemObject)
- data = me.getData()
- itemData = gItemManager.getItemData(itemObject.itemID)
- amount = itemObject.amount
- repeat with i = 1 to itemObject.amount
- itemPlaced = false
- repeat with j = 1 to data.backpack.size
- if (data.backpack.slots[j] = void) then next repeat
- if (itemPlaced) then exit repeat
- comparison = gItemManager.compareItemObjects(data.backpack.slots[j], itemObject)
- if (comparison) then
- if (data.backpack.slots[j].amount >= itemData.stackAmount) then next repeat
- data.backpack.slots[j].amount = data.backpack.slots[j].amount + 1
- me.setData(data)
- amount = amount - 1
- itemPlaced = true
- end if
- end repeat
- repeat with j = 1 to data.backpack.size
- if (itemPlaced) then exit repeat
- if (data.backpack.slots[j] = void) then
- newItemObject = gItemManager.createItemObject(itemObject.itemID, 1)
- data.backpack.slots[j] = newItemObject
- me.setData(data)
- amount = amount - 1
- itemPlaced = true
- end if
- end repeat
- end repeat
- if (amount > 0) then
- sendPacket(me.pUsername, "Alert", "ERROR: Only" && (itemObject.amount - amount) && "of the" && itemObject.amount && "item(s) were added to your backpack.")
- return(#error)
- end if
- end addToBackpack
- -- CHILD FUNCTION
- -- Removes an item object from the backpack.
- -- If a slot is given it will remove from the specified backpack slot.
- on removeFromBackpack(me, itemObject, slot)
- data = me.getData()
- repeat with i = 1 to data.backpack.size
- if (slot <> void) then i = slot
- if (data.backpack.slots[i].uid = itemObject.uid) then
- if (data.backpack.slots[i].amount = 1) then
- data.backpack.slots[i] = void
- else
- data.backpack.slots[i].amount = data.backpack.slots[i].amount - 1
- end if
- me.setData(data)
- me.syncBackpackData()
- exit repeat
- end if
- end repeat
- end removeFromBackpack
- -- CHILD FUNCTION
- -- Equips an item object based on the backpack slot given.
- on equipItem(me, slot)
- data = me.getData()
- itemObject = data.backpack.slots[slot]
- if (itemObject = void) then exit
- itemData = gItemManager.getItemData(itemObject.itemID)
- equipSlot = itemData.parameters.slot
- if (data.equipment.getProp(equipSlot) <> void) then
- sendPacket(me.pUsername, "Alert", "ERROR: You must first unequip your current equipment.")
- exit
- end if
- me.removeFromBackpack(itemObject, slot)
- data.equipment.setaProp(equipSlot, genPropList(itemObject))
- data.equipment[equipSlot].amount = 1
- me.setData(data)
- me.syncBackpackData()
- me.syncEquipmentData()
- me.updateSpriteToMap()
- end equipItem
- -- CHILD FUNCTION
- -- Unequips an item object based on the equipment slot given.
- on unequipItem(me, equipSlot)
- data = me.getData()
- itemObject = data.equipment[equipSlot]
- if (itemObject = void) then exit
- if (me.addToBackpack(itemObject) = #error) then
- sendPacket(me.pUsername, "Alert", "ERROR: There is no room in your backpack.")
- exit
- end if
- data.equipment.setaProp(equipSlot, void)
- me.setData(data)
- me.syncBackpackData()
- me.syncEquipmentData()
- me.updateSpriteToMap()
- end unequipItem
- -- CHILD FUNCTION
- -- Adds a specified amount to the character object's current gold amount.
- on addGold(me, amount)
- data = me.getData()
- data.backpack.gold = data.backpack.gold + amount
- me.setData(data)
- end addGold
- -- CHILD FUNCTION
- -- Removes a specified amount from the character object's current gold amount.
- on removeGold(me, amount)
- data = me.getData()
- data.backpack.gold = data.backpack.gold - amount
- if (data.backpack.gold < 0) then
- return(#error)
- else
- me.setData(data)
- return(#done)
- end if
- end removeGold
- -- CHILD FUNCTION
- -- Adds a specified amount to the character object's current platinum amount.
- on addPlatinum(me, amount)
- data = me.getData()
- data.backpack.platinum = data.backpack.platinum + amount
- me.setData(data)
- end addPlatinum
- -- CHILD FUNCTION
- -- Removes a specified amount from the character object's current platinum amount.
- on removePlatinum(me, amount)
- data = me.getData()
- data.backpack.platinum = data.backpack.platinum - amount
- if (data.backpack.platinum < 0) then
- return(#error)
- else
- me.setData(data)
- return(#done)
- end if
- end removePlatinum
- -- CHILD FUNCTION
- -- Warps a character object based on the coords and location given.
- on warp(me, coords, location)
- sendPacket(me.pUsername, "MapLoadingScreen", void)
- sendPacket(me.pUsername, "RemoveAllButMe", void)
- data = me.getData()
- gMapManager.leaveMap(#character, data.map.coords, me.pUsername)
- data.map.coords = coords
- data.map.location = location
- me.setData(data)
- me.syncMapData()
- gMapManager.joinMap(#character, coords, me.pUsername)
- end warp
- -- CHILD FUNCTION
- -- Synchronizes item related data with the client's temporary item database.
- on sendItemData(me, itemID)
- if (itemID = void) then exit
- sendToClient = true
- itemData = gItemManager.getItemData(itemID)
- if (me.pTempItemDatabase.getaProp(itemID) = void) then
- me.pTempItemDatabase.addProp(itemID, itemData)
- else
- currentItemData = me.pTempItemDatabase.getProp(itemID)
- if (itemData = currentItemData) then
- sendToClient = false
- else
- me.pTempItemDatabase.setProp(itemID, itemData)
- end if
- end if
- if (sendToClient) then sendPacket(me.pUsername, "ItemDatabaseEntry", [itemID, itemData])
- end sendItemData
- -- PARENT FUNCTION
- -- Creates a new character and stores the data in the database.
- on createCharacter(me, content, senderID)
- username = content[1]
- password = content[2]
- skinColour = content[3]
- hairStyle = content[4]
- hairColour = content[5]
- count = username.char.count
- repeat with i = 1 to count
- ascii = charToNum(username.char[i])
- if not((ascii >= 48 and ascii <= 57) or (ascii >= 65 and ascii <= 90) or (ascii >= 97 and ascii <= 122)) then
- sendPacket(senderID, "Alert", "Your username may only contain letters or numbers.")
- exit
- end if
- end repeat
- minLength = 3
- maxLength = 13
- if (count < minLength or count > maxLength) then
- sendPacket(senderID, "Alert", "Your username must be between" && minLength && "and" && maxLength && "characters.")
- exit
- end if
- filePath = "Database\Characters\" & username & ".dat"
- if (file(filePath).exists) then
- sendPacket(senderID, "Alert", "A character with this username already exists.")
- exit
- end if
- character = [:]
- character.setaProp(#username, username)
- character.setaProp(#password, password)
- character.setaProp(#appearance, [:])
- character.appearance.setaProp(#gender, "Male")
- character.appearance.setaProp(#skincolour, skinColour)
- character.appearance.setaProp(#hairstyle, hairStyle)
- character.appearance.setaProp(#haircolour, hairColour)
- character.setaProp(#map, [:])
- character.map.setaProp(#coords, [:])
- character.map.coords.setaProp(#x, 1)
- character.map.coords.setaProp(#y, 1)
- character.map.setaProp(#location, [:])
- character.map.location.setaProp(#x, 5)
- character.map.location.setaProp(#y, 5)
- character.map.setaProp(#facing, #south)
- character.setaProp(#respawn, [:])
- character.respawn.setaProp(#coords, [:])
- character.respawn.coords.setaProp(#x, 1)
- character.respawn.coords.setaProp(#y, 1)
- character.respawn.setaProp(#location, [:])
- character.respawn.location.setaProp(#x, 5)
- character.respawn.location.setaProp(#y, 5)
- character.setaProp(#stats, [:])
- character.stats.setaProp(#level, 1)
- character.stats.setaProp(#experience, 0)
- character.stats.setaProp(#requiredexperience, 10)
- character.stats.setaProp(#health, 10)
- character.stats.setaProp(#maxhealth, 10)
- character.stats.setaProp(#energy, 10)
- character.stats.setaProp(#maxenergy, 10)
- character.stats.setaProp(#strength, 1)
- character.stats.setaProp(#dexterity, 1)
- character.stats.setaProp(#endurance, 1)
- character.setaProp(#backpack, [:])
- character.backpack.setaProp(#gold, 0)
- character.backpack.setaProp(#platinum, 0)
- character.backpack.setaProp(#size, 15)
- character.backpack.setaProp(#slots, [:])
- repeat with i = 1 to 30
- character.backpack.slots.setaProp(i, void)
- end repeat
- character.setaProp(#equipment, [:])
- character.equipment.setaProp("Main Hand", void)
- character.equipment.setaProp("Off Hand", void)
- character.equipment.setaProp("Head", void)
- character.equipment.setaProp("Body", void)
- character.equipment.setaProp("Legs", void)
- character.equipment.setaProp("Hands", void)
- character.equipment.setaProp("Feet", void)
- fwrite(filePath, character, #value)
- sendPacket(senderID, "Alert", "Your character has been created successfully.")
- sendPacket("system.user.delete", empty, senderID)
- end createCharacter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement