Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.49 KB | None | 0 0
  1. -- GLOBAL VARIABLE DEFINITIONS
  2. global gCharacterManager
  3. global gMapManager
  4. global gItemManager
  5.  
  6.  
  7. -- PROPERTY DEFINITIONS (PARENT)
  8. property pCharacters
  9.  
  10.  
  11. -- PROPERTY DEFINITIONS (CHILD)
  12. property pUsername
  13. property pData
  14. property pTempItemDatabase
  15.  
  16.  
  17. -- PARENT FUNCTION
  18. -- Returns the newly created script object.
  19. on new(me)
  20. return(me)
  21. end new
  22.  
  23.  
  24. -- PARENT FUNCTION
  25. -- Initializes the parent script object.
  26. on startParent(me)
  27. me.pCharacters = [:]
  28. end startParent
  29.  
  30.  
  31. -- CHILD FUNCTION
  32. -- Initializes the child script object.
  33. on startChild(me, username)
  34. me.pUsername = username
  35. me.pData = fread("Database\Characters\" & username & ".dat", #value)
  36. me.pTempItemDatabase = [:]
  37. end startChild
  38.  
  39.  
  40. -- PARENT FUNCTION
  41. -- Adds a character object based on username given.
  42. -- Will not add if object already exists.
  43. on addCharacterObject(me, username)
  44. if (me.pCharacters.getaProp(username) <> void) then exit
  45. characterObject = script("CharacterManager").new()
  46. characterObject.startChild(username)
  47. me.pCharacters.setaProp(username, characterObject)
  48. characterObject.loadCharacter()
  49. end addCharacterObject
  50.  
  51.  
  52. -- PARENT FUNCTION
  53. -- Removes a character object based on username given.
  54. -- Will not remove if object does not exist.
  55. on removeCharacterObject(me, username)
  56. if (me.pCharacters.getaProp(username) = void) then exit
  57. characterObject = me.getCharacterObject(username)
  58. characterObject.saveCharacter()
  59. position = listPos(me.pCharacters, username)
  60. me.pCharacters.deleteAt(position)
  61. end removeCharacterObject
  62.  
  63.  
  64. -- PARENT FUNCTION
  65. -- Returns a character object based on username given.
  66. on getCharacterObject(me, username)
  67. characterObject = me.pCharacters.getaProp(username)
  68. return(characterObject)
  69. end getCharacterObject
  70.  
  71.  
  72. -- CHILD FUNCTION
  73. -- Synchronizes all character data to the client.
  74. -- Then joins the character object to the map.
  75. on loadCharacter(me)
  76. me.syncBackpackData()
  77. me.syncEquipmentData()
  78. data = me.getData()
  79. sendPacket(me.pUsername, "CharacterLoaded", data)
  80. gMapManager.joinMap(#character, data.map.coords, me.pUsername)
  81. end loadCharacter
  82.  
  83.  
  84. -- CHILD FUNCTION
  85. -- Saves character object data to the database.
  86. on saveCharacter(me)
  87. data = me.getData()
  88. fwrite("Database/Characters/" & me.pUsername & ".dat", data, #value)
  89. end saveCharacter
  90.  
  91.  
  92. -- PARENT FUNCTION
  93. -- Removes the character object when they have disconnected.
  94. on logout(me, username)
  95. characterObject = me.getCharacterObject(username)
  96. if (characterObject = void) then exit
  97. data = characterObject.getData()
  98. mapObject = gMapManager.getMapObject(data.map.coords)
  99. if (mapObject = void) then exit
  100. mapObject.removeCharacter(username)
  101. me.removeCharacterObject(username)
  102. end logout
  103.  
  104.  
  105. -- CHILD FUNCTION
  106. -- Returns the data for the character object.
  107. on getData(me)
  108. return(me.pData)
  109. end getData
  110.  
  111.  
  112. -- CHILD FUNCTION
  113. -- Sets the data for the character object.
  114. on setData(me, data)
  115. me.pData = data
  116. end setData
  117.  
  118.  
  119. -- CHILD FUNCTION
  120. -- Returns the list required to draw a character sprite on the client.
  121. on getDrawList(me)
  122. data = me.getData()
  123. drawList = [:]
  124. drawList.addProp(#location, data.map.location)
  125. drawList.addProp(#facing, data.map.facing)
  126. drawList.addProp(#appearance, data.appearance)
  127. drawList.addProp(#equipment, data.equipment)
  128. return(drawList)
  129. end getDrawList
  130.  
  131.  
  132. -- CHILD FUNCTION
  133. -- Walks the character object depending on the direction given.
  134. -- Synchronizes movement to all characters on the map.
  135. on walkCharacter(me, walkData)
  136. direction = walkData[1]
  137. location = walkData[2]
  138. data = me.getData()
  139. if (data.map.location <> location) then sendPacket(me.pUsername, "InterpolateCharacter", data.map.location)
  140. data.map.facing = direction
  141. location = data.map.location
  142. mapObject = gMapManager.getMapObject(data.map.coords)
  143. nextLocation = [:]
  144. nextLocation.setaProp(#x, location.x)
  145. nextLocation.setaProp(#y, location.y)
  146. case (direction) of
  147. #north: nextLocation.y = nextLocation.y - 1
  148. #south: nextLocation.y = nextLocation.y + 1
  149. #east: nextLocation.x = nextLocation.x + 1
  150. #west: nextLocation.x = nextLocation.x - 1
  151. end case
  152. if (mapObject.checkForBlockedTile(nextLocation)) then exit
  153. characterList = mapObject.getCharacters()
  154. sendPacket(characterList, "WalkCharacter", [me.pUsername, direction])
  155. case (direction) of
  156. #north: location.y = location.y - 1
  157. #south: location.y = location.y + 1
  158. #east: location.x = location.x + 1
  159. #west: location.x = location.x - 1
  160. end case
  161. data.map.location = location
  162. if (location.y = 0) then
  163. coords = data.map.coords
  164. data.map.coords = [#x: coords.x, #y: coords.y - 1]
  165. data.map.location = [#x: location.x, #y: 13]
  166. gMapManager.leaveMap(#character, coords, me.pUsername)
  167. gMapManager.joinMap(#character, data.map.coords, me.pUsername)
  168. end if
  169. if (location.y = 14) then
  170. coords = data.map.coords
  171. data.map.coords = [#x: coords.x, #y: coords.y + 1]
  172. data.map.location = [#x: location.x, #y: 1]
  173. gMapManager.leaveMap(#character, coords, me.pUsername)
  174. gMapManager.joinMap(#character, data.map.coords, me.pUsername)
  175. end if
  176. if (location.x = 23) then
  177. coords = data.map.coords
  178. data.map.coords = [#x: coords.x + 1, #y: coords.y]
  179. data.map.location = [#x: 1, #y: location.y]
  180. gMapManager.leaveMap(#character, coords, me.pUsername)
  181. gMapManager.joinMap(#character, data.map.coords, me.pUsername)
  182. end if
  183. if (location.x = 0) then
  184. coords = data.map.coords
  185. data.map.coords = [#x: coords.x - 1, #y: coords.y]
  186. data.map.location = [#x: 22, #y: location.y]
  187. gMapManager.leaveMap(#character, coords, me.pUsername)
  188. gMapManager.joinMap(#character, data.map.coords, me.pUsername)
  189. end if
  190. mapObject.setObjectLoc(#character, me.pUsername, data.map.location)
  191. end walkCharacter
  192.  
  193.  
  194. -- CHILD FUNCTION
  195. -- Makes the character object attack based on direction given.
  196. -- Synchronizes that character object is attacking to all characters on map.
  197. on attackCharacter(me, direction)
  198. data = me.getData()
  199. data.map.facing = direction
  200. mapObject = gMapManager.getMapObject(data.map.coords)
  201. characterList = mapObject.getCharacters()
  202. sendPacket("@allusers", "AttackCharacter", [me.pUsername, direction])
  203. attackLocation = [:]
  204. case (direction) of
  205. #north:
  206. attackLocation.setaProp(#x, data.map.location.x)
  207. attackLocation.setaProp(#y, data.map.location.y - 1)
  208. #south:
  209. attackLocation.setaProp(#x, data.map.location.x)
  210. attackLocation.setaProp(#y, data.map.location.y + 1)
  211. #east:
  212. attackLocation.setaProp(#x, data.map.location.x + 1)
  213. attackLocation.setaProp(#y, data.map.location.y)
  214. #west:
  215. attackLocation.setaProp(#x, data.map.location.x - 1)
  216. attackLocation.setaProp(#y, data.map.location.y)
  217. end case
  218. objectList = mapObject.searchLoc(attackLocation)
  219. repeat with i = 1 to objectList.count
  220. attackPower = me.getAttackPower()
  221. type = objectList[i].type
  222. target = objectList.getPropAt(i)
  223. me.dealDamage(type, target, attackPower)
  224. end repeat
  225. end attackCharacter
  226.  
  227.  
  228. -- CHILD FUNCTION
  229. -- Determines how much power a character object's attack has.
  230. -- Returns the determination as an integer.
  231. on getAttackPower(me)
  232. return(1)
  233. end getAttackPower
  234.  
  235.  
  236. -- CHILD FUNCTION
  237. -- Determines how much damage has been dealt.
  238. -- Informs the client that damage was dealt.
  239. on dealDamage(me, type, target, amount)
  240. case (type) of
  241. #character:
  242. characterObject = gCharacterManager.getCharacterObject(target)
  243. characterObject.takeDamage(me.pUsername, amount)
  244. message = "[Combat] You have attacked" && target && "for 1 damage."
  245. sendPacket(me.pUsername, "ChatMessage", [message, 30])
  246. end case
  247. end dealDamage
  248.  
  249.  
  250. -- CHILD FUNCTION
  251. -- Determines how much damage has been taken and modifies stats.
  252. -- Informs the client that damage was taken.
  253. on takeDamage(me, attacker, amount)
  254. data = me.getData()
  255. data.stats.health = data.stats.health - amount
  256. me.setData(data)
  257. sendPacket(me.pUsername, "SyncStats", data.stats)
  258. if (data.stats.health = 0) then
  259. data.stats.health = data.stats.maxhealth
  260. me.setData(data)
  261. sendPacket(me.pUsername, "SyncStats", data.stats)
  262. message = "[Combat] You have been killed by" && attacker & "."
  263. sendPacket(me.pUsername, "ChatMessage", [message, 30])
  264. characterObject = gCharacterManager.getCharacterObject(attacker)
  265. characterObject.killedCharacter(me.pUsername)
  266. respawnCoords = genPropList(data.respawn.coords)
  267. respawnLocation = genPropList(data.respawn.location)
  268. me.warp(respawnCoords, respawnLocation)
  269. end if
  270. end takeDamage
  271.  
  272.  
  273. -- CHILD FUNCTION
  274. -- Informs that the character object has killed another character.
  275. -- Informs the client that the character was killed.
  276. -- Gives appropriate awards for the kill.
  277. on killedCharacter(me, target)
  278. data = me.getData()
  279. data.stats.experience = data.stats.experience + 1
  280. if (data.stats.experience = 10) then
  281. data.stats.experience = 0
  282. data.stats.level = data.stats.level + 1
  283. data.stats.maxhealth = data.stats.maxhealth + 5
  284. data.stats.health = data.stats.maxhealth
  285. end if
  286. me.setData(data)
  287. sendPacket(me.pUsername, "SyncStats", data.stats)
  288. message = "[Combat] You have killed" && target && "and gained 1 experience point."
  289. sendPacket(me.pUsername, "ChatMessage", [message, 30])
  290. end killedCharacter
  291.  
  292.  
  293. -- CHILD FUNCTION
  294. --- Synchronizes all map data to the client.
  295. on syncMapData(me)
  296. data = me.getData()
  297. sendPacket(me.pUsername, "SyncMap", data.map)
  298. end syncMapData
  299.  
  300.  
  301. -- CHILD FUNCTION
  302. -- Synchronizes all backpack data to the client.
  303. on syncBackpackData(me)
  304. data = me.getData()
  305. repeat with i = 1 to data.backpack.slots.count
  306. if (data.backpack.slots.getaProp(i) = void) then next repeat
  307. me.sendItemData(data.backpack.slots[i].itemID)
  308. end repeat
  309. sendPacket(me.pUsername, "SyncBackpack", data.backpack)
  310. end syncBackpackData
  311.  
  312.  
  313. -- CHILD FUNCTION
  314. -- Synchronizes all equipment data to the client.
  315. on syncEquipmentData(me)
  316. data = me.getData()
  317. repeat with i = 1 to data.equipment.count
  318. if (data.equipment[i] = void) then next repeat
  319. me.sendItemData(data.equipment[i].itemID)
  320. end repeat
  321. sendPacket(me.pUsername, "SyncEquipment", data.equipment)
  322. end syncEquipmentData
  323.  
  324.  
  325. -- CHILD FUNCTION
  326. -- Synchronizes the character object's state to all characters on the map.
  327. -- Each character's client then updates the sprite accordingly.
  328. on updateSpriteToMap(me)
  329. data = me.getData()
  330. mapObject = gMapManager.getMapObject(data.map.coords)
  331. mapObject.syncItemData()
  332. characterList = mapObject.getCharacters()
  333. drawList = me.getDrawList()
  334. drawData = [:]
  335. drawData.addProp(me.pUsername, drawList)
  336. sendPacket(characterList, "UpdateCharacterSprite", drawData)
  337. end updateSpriteToMap
  338.  
  339.  
  340. -- CHILD FUNCTION
  341. -- Adds an item object to the character object's backpack.
  342. -- The script properly determines all possible outcomes of adding the item object.
  343. -- Alerts the character if there was an error adding the item object to the backpack.
  344. on addToBackpack(me, itemObject)
  345. data = me.getData()
  346. itemData = gItemManager.getItemData(itemObject.itemID)
  347. amount = itemObject.amount
  348. repeat with i = 1 to itemObject.amount
  349. itemPlaced = false
  350. repeat with j = 1 to data.backpack.size
  351. if (data.backpack.slots[j] = void) then next repeat
  352. if (itemPlaced) then exit repeat
  353. comparison = gItemManager.compareItemObjects(data.backpack.slots[j], itemObject)
  354. if (comparison) then
  355. if (data.backpack.slots[j].amount >= itemData.stackAmount) then next repeat
  356. data.backpack.slots[j].amount = data.backpack.slots[j].amount + 1
  357. me.setData(data)
  358. amount = amount - 1
  359. itemPlaced = true
  360. end if
  361. end repeat
  362. repeat with j = 1 to data.backpack.size
  363. if (itemPlaced) then exit repeat
  364. if (data.backpack.slots[j] = void) then
  365. newItemObject = gItemManager.createItemObject(itemObject.itemID, 1)
  366. data.backpack.slots[j] = newItemObject
  367. me.setData(data)
  368. amount = amount - 1
  369. itemPlaced = true
  370. end if
  371. end repeat
  372. end repeat
  373. if (amount > 0) then
  374. sendPacket(me.pUsername, "Alert", "ERROR: Only" && (itemObject.amount - amount) && "of the" && itemObject.amount && "item(s) were added to your backpack.")
  375. return(#error)
  376. end if
  377. end addToBackpack
  378.  
  379.  
  380. -- CHILD FUNCTION
  381. -- Removes an item object from the backpack.
  382. -- If a slot is given it will remove from the specified backpack slot.
  383. on removeFromBackpack(me, itemObject, slot)
  384. data = me.getData()
  385. repeat with i = 1 to data.backpack.size
  386. if (slot <> void) then i = slot
  387. if (data.backpack.slots[i].uid = itemObject.uid) then
  388. if (data.backpack.slots[i].amount = 1) then
  389. data.backpack.slots[i] = void
  390. else
  391. data.backpack.slots[i].amount = data.backpack.slots[i].amount - 1
  392. end if
  393. me.setData(data)
  394. me.syncBackpackData()
  395. exit repeat
  396. end if
  397. end repeat
  398. end removeFromBackpack
  399.  
  400.  
  401. -- CHILD FUNCTION
  402. -- Equips an item object based on the backpack slot given.
  403. on equipItem(me, slot)
  404. data = me.getData()
  405. itemObject = data.backpack.slots[slot]
  406. if (itemObject = void) then exit
  407. itemData = gItemManager.getItemData(itemObject.itemID)
  408. equipSlot = itemData.parameters.slot
  409. if (data.equipment.getProp(equipSlot) <> void) then
  410. sendPacket(me.pUsername, "Alert", "ERROR: You must first unequip your current equipment.")
  411. exit
  412. end if
  413. me.removeFromBackpack(itemObject, slot)
  414. data.equipment.setaProp(equipSlot, genPropList(itemObject))
  415. data.equipment[equipSlot].amount = 1
  416. me.setData(data)
  417. me.syncBackpackData()
  418. me.syncEquipmentData()
  419. me.updateSpriteToMap()
  420. end equipItem
  421.  
  422.  
  423. -- CHILD FUNCTION
  424. -- Unequips an item object based on the equipment slot given.
  425. on unequipItem(me, equipSlot)
  426. data = me.getData()
  427. itemObject = data.equipment[equipSlot]
  428. if (itemObject = void) then exit
  429. if (me.addToBackpack(itemObject) = #error) then
  430. sendPacket(me.pUsername, "Alert", "ERROR: There is no room in your backpack.")
  431. exit
  432. end if
  433. data.equipment.setaProp(equipSlot, void)
  434. me.setData(data)
  435. me.syncBackpackData()
  436. me.syncEquipmentData()
  437. me.updateSpriteToMap()
  438. end unequipItem
  439.  
  440.  
  441. -- CHILD FUNCTION
  442. -- Adds a specified amount to the character object's current gold amount.
  443. on addGold(me, amount)
  444. data = me.getData()
  445. data.backpack.gold = data.backpack.gold + amount
  446. me.setData(data)
  447. end addGold
  448.  
  449.  
  450. -- CHILD FUNCTION
  451. -- Removes a specified amount from the character object's current gold amount.
  452. on removeGold(me, amount)
  453. data = me.getData()
  454. data.backpack.gold = data.backpack.gold - amount
  455. if (data.backpack.gold < 0) then
  456. return(#error)
  457. else
  458. me.setData(data)
  459. return(#done)
  460. end if
  461. end removeGold
  462.  
  463.  
  464. -- CHILD FUNCTION
  465. -- Adds a specified amount to the character object's current platinum amount.
  466. on addPlatinum(me, amount)
  467. data = me.getData()
  468. data.backpack.platinum = data.backpack.platinum + amount
  469. me.setData(data)
  470. end addPlatinum
  471.  
  472.  
  473. -- CHILD FUNCTION
  474. -- Removes a specified amount from the character object's current platinum amount.
  475. on removePlatinum(me, amount)
  476. data = me.getData()
  477. data.backpack.platinum = data.backpack.platinum - amount
  478. if (data.backpack.platinum < 0) then
  479. return(#error)
  480. else
  481. me.setData(data)
  482. return(#done)
  483. end if
  484. end removePlatinum
  485.  
  486.  
  487. -- CHILD FUNCTION
  488. -- Warps a character object based on the coords and location given.
  489. on warp(me, coords, location)
  490. sendPacket(me.pUsername, "MapLoadingScreen", void)
  491. sendPacket(me.pUsername, "RemoveAllButMe", void)
  492. data = me.getData()
  493. gMapManager.leaveMap(#character, data.map.coords, me.pUsername)
  494. data.map.coords = coords
  495. data.map.location = location
  496. me.setData(data)
  497. me.syncMapData()
  498. gMapManager.joinMap(#character, coords, me.pUsername)
  499. end warp
  500.  
  501.  
  502. -- CHILD FUNCTION
  503. -- Synchronizes item related data with the client's temporary item database.
  504. on sendItemData(me, itemID)
  505. if (itemID = void) then exit
  506. sendToClient = true
  507. itemData = gItemManager.getItemData(itemID)
  508. if (me.pTempItemDatabase.getaProp(itemID) = void) then
  509. me.pTempItemDatabase.addProp(itemID, itemData)
  510. else
  511. currentItemData = me.pTempItemDatabase.getProp(itemID)
  512. if (itemData = currentItemData) then
  513. sendToClient = false
  514. else
  515. me.pTempItemDatabase.setProp(itemID, itemData)
  516. end if
  517. end if
  518. if (sendToClient) then sendPacket(me.pUsername, "ItemDatabaseEntry", [itemID, itemData])
  519. end sendItemData
  520.  
  521.  
  522. -- PARENT FUNCTION
  523. -- Creates a new character and stores the data in the database.
  524. on createCharacter(me, content, senderID)
  525. username = content[1]
  526. password = content[2]
  527. skinColour = content[3]
  528. hairStyle = content[4]
  529. hairColour = content[5]
  530. count = username.char.count
  531. repeat with i = 1 to count
  532. ascii = charToNum(username.char[i])
  533. if not((ascii >= 48 and ascii <= 57) or (ascii >= 65 and ascii <= 90) or (ascii >= 97 and ascii <= 122)) then
  534. sendPacket(senderID, "Alert", "Your username may only contain letters or numbers.")
  535. exit
  536. end if
  537. end repeat
  538. minLength = 3
  539. maxLength = 13
  540. if (count < minLength or count > maxLength) then
  541. sendPacket(senderID, "Alert", "Your username must be between" && minLength && "and" && maxLength && "characters.")
  542. exit
  543. end if
  544. filePath = "Database\Characters\" & username & ".dat"
  545. if (file(filePath).exists) then
  546. sendPacket(senderID, "Alert", "A character with this username already exists.")
  547. exit
  548. end if
  549. character = [:]
  550. character.setaProp(#username, username)
  551. character.setaProp(#password, password)
  552. character.setaProp(#appearance, [:])
  553. character.appearance.setaProp(#gender, "Male")
  554. character.appearance.setaProp(#skincolour, skinColour)
  555. character.appearance.setaProp(#hairstyle, hairStyle)
  556. character.appearance.setaProp(#haircolour, hairColour)
  557. character.setaProp(#map, [:])
  558. character.map.setaProp(#coords, [:])
  559. character.map.coords.setaProp(#x, 1)
  560. character.map.coords.setaProp(#y, 1)
  561. character.map.setaProp(#location, [:])
  562. character.map.location.setaProp(#x, 5)
  563. character.map.location.setaProp(#y, 5)
  564. character.map.setaProp(#facing, #south)
  565. character.setaProp(#respawn, [:])
  566. character.respawn.setaProp(#coords, [:])
  567. character.respawn.coords.setaProp(#x, 1)
  568. character.respawn.coords.setaProp(#y, 1)
  569. character.respawn.setaProp(#location, [:])
  570. character.respawn.location.setaProp(#x, 5)
  571. character.respawn.location.setaProp(#y, 5)
  572. character.setaProp(#stats, [:])
  573. character.stats.setaProp(#level, 1)
  574. character.stats.setaProp(#experience, 0)
  575. character.stats.setaProp(#requiredexperience, 10)
  576. character.stats.setaProp(#health, 10)
  577. character.stats.setaProp(#maxhealth, 10)
  578. character.stats.setaProp(#energy, 10)
  579. character.stats.setaProp(#maxenergy, 10)
  580. character.stats.setaProp(#strength, 1)
  581. character.stats.setaProp(#dexterity, 1)
  582. character.stats.setaProp(#endurance, 1)
  583. character.setaProp(#backpack, [:])
  584. character.backpack.setaProp(#gold, 0)
  585. character.backpack.setaProp(#platinum, 0)
  586. character.backpack.setaProp(#size, 15)
  587. character.backpack.setaProp(#slots, [:])
  588. repeat with i = 1 to 30
  589. character.backpack.slots.setaProp(i, void)
  590. end repeat
  591. character.setaProp(#equipment, [:])
  592. character.equipment.setaProp("Main Hand", void)
  593. character.equipment.setaProp("Off Hand", void)
  594. character.equipment.setaProp("Head", void)
  595. character.equipment.setaProp("Body", void)
  596. character.equipment.setaProp("Legs", void)
  597. character.equipment.setaProp("Hands", void)
  598. character.equipment.setaProp("Feet", void)
  599. fwrite(filePath, character, #value)
  600. sendPacket(senderID, "Alert", "Your character has been created successfully.")
  601. sendPacket("system.user.delete", empty, senderID)
  602. end createCharacter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement