Client.gd 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. extends NetInterface
  2. class_name NetClient
  3. #
  4. func WarpPlayer(mapID : int, playerPos : Vector2, _rpcID : int = NetworkCommons.RidSingleMode):
  5. if Launcher.Map:
  6. var mapData : FileData = DB.MapsDB.get(mapID, null)
  7. if mapData:
  8. Launcher.Map.EmplaceMapNode(mapID)
  9. Launcher.Camera.FocusPosition(playerPos)
  10. PushNotification(mapData._name, _rpcID)
  11. func EmotePlayer(playerID : int, emoteID : int, _rpcID : int = NetworkCommons.RidSingleMode):
  12. var entity : Entity = Entities.Get(playerID)
  13. if entity && entity.get_parent() && entity.interactive:
  14. entity.interactive.DisplayEmote.call_deferred(emoteID)
  15. func AddPlayer(agentID : int, entityType : ActorCommons.Type, shape : int, spirit : int, currentShape : int, nick : String, velocity : Vector2, position : Vector2i, orientation : Vector2, state : ActorCommons.State, skillCastID : int, level : int, health : int, hairstyle : int, haircolor : int, gender : ActorCommons.Gender, race : int, skintone : int, equipments : Dictionary, _rpcID : int = NetworkCommons.RidSingleMode):
  16. if Launcher.Map:
  17. var entity : Entity = Launcher.Map.AddPlayer(agentID, entityType, shape, spirit, currentShape, nick, velocity, position, orientation, state, skillCastID)
  18. if entity:
  19. UpdatePublicStats(agentID, level, health, hairstyle, haircolor, gender, race, skintone, currentShape, _rpcID)
  20. RefreshEquipments(agentID, equipments, _rpcID)
  21. func AddEntity(agentID : int, entityType : ActorCommons.Type, currentShape : int, velocity : Vector2, position : Vector2i, orientation : Vector2, state : ActorCommons.State, skillCastID : int, _rpcID : int = NetworkCommons.RidSingleMode):
  22. if Launcher.Map:
  23. Launcher.Map.AddEntity(agentID, entityType, currentShape, DB.UnknownHash, currentShape, "", velocity, position, orientation, state, skillCastID)
  24. func RemoveEntity(agentID : int, _rpcID : int = NetworkCommons.RidSingleMode):
  25. if Launcher.Map:
  26. Launcher.Map.RemoveEntity(agentID)
  27. func FullUpdateEntity(ridAgent : int, velocity : Vector2, position : Vector2, orientation : Vector2, state : ActorCommons.State, skillCastID : int):
  28. if Launcher.Map:
  29. Launcher.Map.FullUpdateEntity(ridAgent, velocity, position, orientation, state, skillCastID)
  30. func UpdateEntity(ridAgent : int, velocity : Vector2, position : Vector2):
  31. if Launcher.Map:
  32. Launcher.Map.UpdateEntity(ridAgent, velocity, position)
  33. func ChatAgent(ridAgent : int, text : String, _rpcID : int = NetworkCommons.RidSingleMode):
  34. if Launcher.Map:
  35. var entity : Entity = Entities.Get(ridAgent)
  36. if entity && entity.get_parent():
  37. if entity.type == ActorCommons.Type.PLAYER && Launcher.GUI:
  38. Launcher.GUI.chatContainer.AddPlayerText(entity.nick, text)
  39. if entity.interactive:
  40. entity.interactive.DisplaySpeech.call_deferred(text)
  41. func ToggleContext(enable : bool, _rpcID : int = NetworkCommons.RidSingleMode):
  42. Launcher.GUI.dialogueWindow.Toggle(enable)
  43. func ContextText(author : String, text : String, _rpcID : int = NetworkCommons.RidSingleMode):
  44. if not author.is_empty():
  45. Launcher.GUI.dialogueWindow.AddName(author)
  46. Launcher.GUI.dialogueWindow.AddDialogue(text)
  47. Launcher.GUI.dialogueWindow.ToggleButton(false, "")
  48. func ContextContinue(_rpcID : int = NetworkCommons.RidSingleMode):
  49. Launcher.GUI.dialogueWindow.ToggleButton(true, "Next")
  50. func ContextClose(_rpcID : int = NetworkCommons.RidSingleMode):
  51. Launcher.GUI.dialogueWindow.ToggleButton(true, "Close")
  52. func ContextChoice(texts : PackedStringArray, _rpcID : int = NetworkCommons.RidSingleMode):
  53. Launcher.GUI.dialogueWindow.AddChoices(texts)
  54. func TargetAlteration(ridAgent : int, targetID : int, value : int, alteration : ActorCommons.Alteration, skillID : int, _rpcID : int = NetworkCommons.RidSingleMode):
  55. if Launcher.Map:
  56. var entity : Entity = Entities.Get(targetID)
  57. var caller : Entity = Entities.Get(ridAgent)
  58. if caller && entity && entity.get_parent() and entity.interactive:
  59. entity.interactive.DisplayAlteration.call_deferred(entity, caller, value, alteration, skillID)
  60. func Casted(agentID : int, skillID : int, cooldown : float, _rpcID : int = NetworkCommons.RidSingleMode):
  61. var entity : Entity = Entities.Get(agentID)
  62. if entity and entity.get_parent() and entity.interactive:
  63. entity.interactive.DisplaySkill.call_deferred(entity, skillID, cooldown)
  64. func ThrowProjectile(agentID : int, targetPos : Vector2, skillID: int, _rpcID : int = NetworkCommons.RidSingleMode):
  65. if not Launcher.Map or not Launcher.Map.currentFringe:
  66. return
  67. var skill : SkillCell = DB.SkillsDB.get(skillID, null)
  68. if not skill:
  69. return
  70. var entity : Entity = Entities.Get(agentID)
  71. if not entity or not entity.get_parent() or not entity.interactive:
  72. return
  73. entity.interactive.DisplayProjectile.call_deferred(targetPos, skill)
  74. func Morphed(ridAgent : int, morphID : int, morphed : bool, _rpcID : int = NetworkCommons.RidSingleMode):
  75. var entity : Entity = Entities.Get(ridAgent)
  76. if entity:
  77. var morphData : EntityData = DB.EntitiesDB.get(morphID, null)
  78. if morphData:
  79. entity.stat.Morph(morphData)
  80. entity.SetVisual(morphData, morphed)
  81. func UpdatePublicStats(ridAgent : int, level : int, health : int, hairstyle : int, haircolor : int, gender : ActorCommons.Gender, race : int, skintone : int, currentShape : int, _rpcID : int = NetworkCommons.RidSingleMode):
  82. if Launcher.Map:
  83. var entity : Entity = Entities.Get(ridAgent)
  84. if entity and entity.stat:
  85. entity.stat.level = level
  86. entity.stat.health = health
  87. entity.stat.currentShape = currentShape
  88. var newHair : bool = entity.stat.hairstyle != hairstyle or entity.stat.haircolor != haircolor
  89. entity.stat.hairstyle = hairstyle
  90. entity.stat.haircolor = haircolor
  91. if newHair and entity.visual:
  92. entity.visual.SetHair()
  93. var newBody : bool = entity.stat.gender != gender or entity.stat.race != race or entity.stat.skintone != skintone
  94. entity.stat.gender = gender
  95. entity.stat.race = race
  96. entity.stat.skintone = skintone
  97. if newBody and entity.visual:
  98. entity.visual.SetBody()
  99. entity.visual.SetFace()
  100. entity.stat.RefreshVitalStats()
  101. func UpdatePrivateStats(experience : int, gp : int, mana : int, stamina : int, karma : int, weight : float, shape : int, spirit : int, _rpcID : int = NetworkCommons.RidSingleMode):
  102. if Launcher.Player and Launcher.Player.stat:
  103. Launcher.Player.stat.experience = experience
  104. Launcher.Player.stat.gp = gp
  105. Launcher.Player.stat.mana = mana
  106. Launcher.Player.stat.stamina = stamina
  107. Launcher.Player.stat.karma = karma
  108. Launcher.Player.stat.weight = weight
  109. Launcher.Player.stat.shape = shape
  110. Launcher.Player.stat.spirit = spirit
  111. Launcher.Player.stat.RefreshVitalStats()
  112. func UpdateAttributes(strength : int, vitality : int, agility : int, endurance : int, concentration : int, _rpcID : int = NetworkCommons.RidSingleMode):
  113. if Launcher.Player and Launcher.Player.stat:
  114. Launcher.Player.stat.strength = strength
  115. Launcher.Player.stat.vitality = vitality
  116. Launcher.Player.stat.agility = agility
  117. Launcher.Player.stat.endurance = endurance
  118. Launcher.Player.stat.concentration = concentration
  119. Launcher.Player.stat.RefreshAttributes()
  120. func LevelUp(ridAgent : int, _rpcID : int = NetworkCommons.RidSingleMode):
  121. if Launcher.Map:
  122. var entity : Entity = Entities.Get(ridAgent)
  123. if entity and entity.get_parent() and entity.stat:
  124. entity.LevelUp()
  125. func ItemAdded(itemID : int, customfield : StringName, count : int, _rpcID : int = NetworkCommons.RidSingleMode):
  126. if Launcher.Player:
  127. var cell : BaseCell = DB.GetItem(itemID, customfield)
  128. if cell and Launcher.Player.inventory.PushItem(cell, count):
  129. if Launcher.GUI:
  130. Launcher.GUI.pickupPanel.AddLast(cell, count)
  131. Launcher.GUI.inventoryWindow.RefreshInventory()
  132. cell.used.emit()
  133. func ItemRemoved(itemID : int, customfield : StringName, count : int, _rpcID : int = NetworkCommons.RidSingleMode):
  134. if Launcher.Player:
  135. var cell : BaseCell = DB.GetItem(itemID, customfield)
  136. if cell:
  137. Launcher.Player.inventory.PopItem(cell, count)
  138. CellTile.RefreshShortcuts(cell)
  139. if Launcher.GUI and Launcher.GUI.inventoryWindow:
  140. Launcher.GUI.inventoryWindow.RefreshInventory()
  141. cell.used.emit()
  142. func ItemEquiped(ridAgent : int, itemID : int, customfield : StringName, state : bool, _rpcID : int = NetworkCommons.RidSingleMode):
  143. var entity : Entity = Entities.Get(ridAgent)
  144. if entity:
  145. var cell : ItemCell = DB.GetItem(itemID, customfield)
  146. if cell:
  147. if state:
  148. entity.inventory.EquipItem(cell)
  149. else:
  150. entity.inventory.UnequipItem(cell)
  151. entity.visual.SetEquipment(cell.slot)
  152. if entity == Launcher.Player:
  153. Launcher.GUI.inventoryWindow.RefreshInventory()
  154. cell.used.emit()
  155. func RefreshInventory(cells : Array[Dictionary], _rpcID : int = NetworkCommons.RidSingleMode):
  156. if Launcher.Player and Launcher.Player.inventory:
  157. Launcher.Player.inventory.ImportInventory(cells)
  158. if Launcher.GUI and Launcher.GUI.inventoryWindow:
  159. Launcher.GUI.inventoryWindow.RefreshInventory()
  160. func RefreshEquipments(ridAgent : int, equipments : Dictionary, _rpcID : int = NetworkCommons.RidSingleMode):
  161. var entity : Entity = Entities.Get(ridAgent)
  162. if entity:
  163. if entity.inventory:
  164. entity.inventory.ImportEquipment(equipments)
  165. if entity == Launcher.Player and Launcher.GUI and Launcher.GUI.inventoryWindow:
  166. Launcher.GUI.inventoryWindow.RefreshInventory()
  167. func DropAdded(dropID : int, itemID : int, customfield : StringName, pos : Vector2, _rpcID : int = NetworkCommons.RidSingleMode):
  168. if Launcher.Map:
  169. Launcher.Map.AddDrop(dropID, DB.GetItem(itemID, customfield), pos)
  170. func DropRemoved(dropID : int, _rpcID : int = NetworkCommons.RidSingleMode):
  171. if Launcher.Map:
  172. Launcher.Map.RemoveDrop(dropID)
  173. #
  174. func PushNotification(notif : String, _rpcID : int = NetworkCommons.RidSingleMode):
  175. if Launcher.GUI:
  176. Launcher.GUI.notificationLabel.AddNotification(notif)
  177. #
  178. func AuthError(err : NetworkCommons.AuthError, _rpcID : int = NetworkCommons.RidSingleMode):
  179. if Launcher.GUI:
  180. Launcher.GUI.loginPanel.FillWarningLabel(err)
  181. if err == NetworkCommons.AuthError.ERR_OK:
  182. FSM.EnterState(FSM.States.CHAR_SCREEN)
  183. func CharacterError(err : NetworkCommons.AuthError, _rpcID : int = NetworkCommons.RidSingleMode):
  184. if Launcher.GUI:
  185. Launcher.GUI.characterPanel.FillWarningLabel(err)
  186. func CharacterInfo(info : Dictionary, equipment : Dictionary, _rpcID : int = NetworkCommons.RidSingleMode):
  187. Launcher.GUI.characterPanel.AddCharacter(info, equipment)
  188. # Progress
  189. func UpdateSkill(skillID : int, level : int, _rpcID : int = NetworkCommons.RidSingleMode):
  190. if Launcher.Player:
  191. var skill : SkillCell = DB.GetSkill(skillID)
  192. if skill:
  193. Launcher.Player.progress.AddSkill(skill, 1.0, level)
  194. if Launcher.GUI and Launcher.GUI.skillWindow:
  195. Launcher.GUI.skillWindow.RefreshSkills()
  196. func UpdateBestiary(mobID : int, count : int, _rpcID : int = NetworkCommons.RidSingleMode):
  197. if Launcher.Player:
  198. Launcher.Player.progress.AddBestiary(mobID, count)
  199. if Launcher.GUI and Launcher.GUI.progressWindow:
  200. Launcher.GUI.progressWindow.RefreshBestiary(mobID, count)
  201. func UpdateQuest(questID : int, state : int, _rpcID : int = NetworkCommons.RidSingleMode):
  202. if Launcher.Player:
  203. Launcher.Player.progress.SetQuest(questID, state)
  204. if Launcher.GUI and Launcher.GUI.progressWindow:
  205. Launcher.GUI.progressWindow.RefreshQuest(questID, state)
  206. func RefreshProgress(skills : Dictionary, quests : Dictionary, bestiary : Dictionary, _rpcID : int = NetworkCommons.RidSingleMode):
  207. if Launcher.GUI and Launcher.GUI.progressWindow:
  208. Launcher.GUI.progressWindow.Clear()
  209. if Launcher.Player:
  210. for skill in skills:
  211. UpdateSkill(skill, skills[skill])
  212. for quest in quests:
  213. UpdateQuest(quest, quests[quest])
  214. for mob in bestiary:
  215. UpdateBestiary(mob, bestiary[mob])
  216. #
  217. func ConnectServer():
  218. if not isOffline:
  219. uniqueID = multiplayerAPI.get_unique_id()
  220. if Launcher.GUI and Launcher.GUI.loginPanel:
  221. Launcher.GUI.loginPanel.EnableButtons.call_deferred(true)
  222. Peers.AddPeer(NetworkCommons.RidSingleMode, NetworkCommons.UseWebSocket and not NetworkCommons.UseENet)
  223. func DisconnectServer():
  224. uniqueID = NetworkCommons.RidDefault
  225. Launcher.Mode(true, true)
  226. FSM.EnterState(FSM.States.LOGIN_SCREEN)
  227. Peers.RemovePeer(NetworkCommons.RidSingleMode)
  228. func ConnectionFailed():
  229. DisconnectServer()
  230. AuthError(NetworkCommons.AuthError.ERR_SERVER_UNREACHABLE)
  231. #
  232. func _enter_tree():
  233. if isOffline:
  234. uniqueID = NetworkCommons.RidSingleMode
  235. ConnectServer.call_deferred()
  236. return
  237. if not multiplayerAPI.connected_to_server.is_connected(ConnectServer):
  238. multiplayerAPI.connected_to_server.connect(ConnectServer)
  239. if not multiplayerAPI.connection_failed.is_connected(ConnectionFailed):
  240. multiplayerAPI.connection_failed.connect(ConnectionFailed)
  241. if not multiplayerAPI.server_disconnected.is_connected(DisconnectServer):
  242. multiplayerAPI.server_disconnected.connect(DisconnectServer)
  243. var serverAddress : String = NetworkCommons.LocalServerAddress if isLocal else NetworkCommons.ServerAddress
  244. var serverPort : int = NetworkCommons.WebSocketPort if useWebSocket else NetworkCommons.ENetPort
  245. if LauncherCommons.IsTesting:
  246. serverAddress = NetworkCommons.LocalServerAddress if isLocal else NetworkCommons.ServerAddressTesting
  247. serverPort = NetworkCommons.WebSocketPortTesting if useWebSocket else NetworkCommons.ENetPortTesting
  248. var ret : Error = FAILED
  249. var tlsOptions : TLSOptions = TLSOptions.client_unsafe()
  250. if useWebSocket:
  251. var prefix : String = "ws://" if isLocal else "wss://"
  252. ret = currentPeer.create_client(prefix + serverAddress + ":" + str(serverPort), tlsOptions)
  253. else:
  254. ret = currentPeer.create_client(serverAddress, serverPort)
  255. if ret == OK and not isLocal:
  256. ret = currentPeer.host.dtls_client_setup(serverAddress, tlsOptions)
  257. assert(ret == OK, "Client could not connect, please check the server adress %s and port number %d" % [serverAddress, serverPort])
  258. if ret == OK:
  259. currentPeer.set_target_peer(MultiplayerPeer.TARGET_PEER_SERVER)
  260. multiplayerAPI.multiplayer_peer = currentPeer
  261. Util.PrintLog("Client", "Initialized with: %s, %s, %s, %s" % [
  262. "WebSocket" if useWebSocket else "ENet",
  263. "Offline" if isOffline else "Online",
  264. "Local" if isLocal else "Public",
  265. "Testing" if LauncherCommons.IsTesting else "Release"
  266. ])
  267. func Destroy():
  268. if multiplayerAPI.connected_to_server.is_connected(ConnectServer):
  269. multiplayerAPI.connected_to_server.disconnect(ConnectServer)
  270. if multiplayerAPI.connection_failed.is_connected(ConnectionFailed):
  271. multiplayerAPI.connection_failed.disconnect(ConnectionFailed)
  272. if multiplayerAPI.server_disconnected.is_connected(DisconnectServer):
  273. multiplayerAPI.server_disconnected.disconnect(DisconnectServer)
  274. super.Destroy()