225 lines
5.7 KiB
GDScript
225 lines
5.7 KiB
GDScript
extends Node
|
|
|
|
signal updateMap
|
|
signal updateUI
|
|
|
|
var Server = true #true if this is the server isntances - can be removed now
|
|
const UIBorder = 1310
|
|
const travelTime = 10
|
|
|
|
|
|
var worldmap
|
|
var worldmapLoaded = false
|
|
|
|
|
|
var worldTiles = {} #currently managed with strings, can be indexed by ints in Godotscript
|
|
var selectedRegion = null
|
|
var armiesOnMap = {}
|
|
var armies = {}
|
|
var selectedArmy = null
|
|
var selecterArmydiplo = ''
|
|
|
|
|
|
func addArmy(from, to, army, armydata, player=true): #army is the armyname, armydata is the object.
|
|
var arrive = from['turn'] + travelTime
|
|
var regionturn = to['turn']
|
|
if arrive <= regionturn:
|
|
var armies = to['armies']
|
|
armies[army] = armydata
|
|
setPresent(to)
|
|
if player:
|
|
if (to['playerTurnOn'] == -1) or (to['playerTurnOn'] > regionturn):
|
|
to['playerTurnOn'] = regionturn
|
|
else:
|
|
if (to['playerTurnOn'] == -1):# -1 Zeit ist für Felder die noch nie betreten wurden.
|
|
to['turn'] = arrive
|
|
var armies = to['armies']
|
|
print(armies)
|
|
# if not typeof(armies) == TYPE_DICTIONARY:
|
|
# to['armies']={}
|
|
# armies = to['armies']
|
|
armies[army] = armydata
|
|
setPresent(to)
|
|
if player:
|
|
to['playerTurnOn'] = regionturn
|
|
else:
|
|
if not to.has('incomming'):
|
|
to['incomming'] = {}
|
|
var incomming = to['incomming']
|
|
armydata['arrive'] = arrive
|
|
incomming[army] = armydata
|
|
if player:
|
|
addFaction(to, armydata['faction'])
|
|
|
|
|
|
|
|
|
|
|
|
func addTile(response):
|
|
var x = ''
|
|
if not worldTiles.has(String(response['x'])):
|
|
worldTiles[String(response['x'])] = {}
|
|
x = worldTiles[String(response['x'])]
|
|
x[String(response['y'])] = response
|
|
|
|
|
|
func addFaction(tile, factionName):
|
|
if not tile.factions.has(factionName):
|
|
tile.factions.append(factionName)
|
|
|
|
func loadAsServer():
|
|
var path = 'user://worldmap.json'
|
|
var directory = Directory.new()
|
|
var hasFile = directory.file_exists(path)
|
|
var file = File.new()
|
|
if !hasFile:
|
|
path = directory.get_current_dir()+'preset//worldmap.json'
|
|
file.open(path, file.READ)
|
|
var json = file.get_as_text()
|
|
# print('json:'+json)
|
|
var json_result = JSON.parse(json).result
|
|
#print(json_result)
|
|
for tile in json_result:
|
|
#ensure everyone who has an army is counted as present
|
|
var present = []
|
|
if tile.has('armies'):
|
|
for army in tile['armies']:
|
|
if not present.has(army['faction']):
|
|
present.append(army['faction'])
|
|
tile['present'] = present
|
|
WorldManager.addTile(tile)
|
|
#to do: function to load regions
|
|
|
|
|
|
|
|
func getTileOrNull(x,y):
|
|
var tile = null
|
|
x = String(x)
|
|
y = String(y)
|
|
print("find ",x," ", y)
|
|
if worldTiles.has(x):
|
|
var row = worldTiles[x]
|
|
print(row)
|
|
if row.has(y):
|
|
tile = row[y]
|
|
print("tileOrNull not null")
|
|
return tile
|
|
|
|
func getTileGuaranteed(x, y):
|
|
var tile = null
|
|
tile = getTileOrNull(x,y)
|
|
if tile == null:
|
|
tile = {
|
|
"x":x,
|
|
"y":y,
|
|
"terrain":"FOREST",
|
|
"factions":[],
|
|
"turn":1,
|
|
"armies":{},
|
|
"present":{},
|
|
"playerTurnOn":-1
|
|
}
|
|
addTile(tile)
|
|
return tile
|
|
|
|
remote func moveArmy(fromX, fromY, toX, toY, army):
|
|
var from = getTileOrNull(fromX, fromY)
|
|
print("request army move")
|
|
print("tile: ", from)
|
|
if not from == null:
|
|
if from.has('armies'):
|
|
var armies = from['armies']
|
|
if armies.has(army):
|
|
var armydata = armies[army]
|
|
if PlayerManager.confirmFaction(get_tree().get_rpc_sender_id(), armydata['faction']):
|
|
print("confirm move")
|
|
var targetTile = getTileGuaranteed(toX, toY)
|
|
print("target is: ",targetTile)
|
|
from['armies'].erase(army)
|
|
removeArmy(from, army)
|
|
addArmy(from, targetTile, army, armydata)
|
|
updateLocalPlayers(from)
|
|
updateLocalPlayers(targetTile)
|
|
pass
|
|
|
|
remote func receiveMap(tiles):
|
|
for tile in tiles:
|
|
addTile(tile)
|
|
emit_signal("updateMap")
|
|
emit_signal("updateUI")
|
|
|
|
|
|
func updateLocalPlayers(tile):
|
|
var factions = tile.present
|
|
var connections = PlayerManager.clients_factions
|
|
for faction in factions:
|
|
for connection in connections:
|
|
if connections[connection]==faction:
|
|
if not ((connection == 1) or (connection==0)):
|
|
rpc_id(connection, 'receiveMap', tile)
|
|
pass
|
|
|
|
func removeArmy(tile, army):
|
|
var armies = tile.armies
|
|
armies.erase(army)
|
|
setPresent(tile)
|
|
# addTile(tile)
|
|
# print(getTileGuaranteed(0,0))
|
|
|
|
|
|
func requestServer():
|
|
rpc_id(1, 'requestMap', PlayerManager.authcache['response']['faction'])
|
|
|
|
|
|
|
|
func setPresent(tile):
|
|
var armies = tile.armies
|
|
var present = {}
|
|
for army in armies:
|
|
var armyValue = 1#TO DO: have method to calculate army strength
|
|
var armydata = armies[army]
|
|
var faction = armydata.faction
|
|
if present.has(faction):
|
|
present[faction] += armyValue
|
|
else:
|
|
present[faction] = armyValue
|
|
tile['present']=present
|
|
return
|
|
|
|
|
|
|
|
remote func settle(tile, army, ascending): #if ascending, the cheapest ressources are used first. Otherwise the most expensive is used first.
|
|
var buildingtime = 10
|
|
if tile.owner == '':
|
|
tile.owner = army.faction
|
|
if army.inventory.getWood() >= 50 or army.inventory.getStone() >= 30: #inventory not properly defined yet, might need change
|
|
if army.leader.race == ['orcs']:
|
|
var finish = buildingtime * 1.5 + ['turn']
|
|
if army.leader.race == ['human']:
|
|
var finish = buildingtime / 1.5 + ['turn']
|
|
else:
|
|
var finish = buildingtime + ['turn']
|
|
|
|
var regions = {}
|
|
|
|
func _ready():
|
|
var newRegion = load("res://region/Region.gd").new()
|
|
newRegion.buildWorld()
|
|
regions[123] = newRegion
|
|
|
|
remote func requestTiles(unit,regionId = 123):
|
|
var clientId = get_tree().get_rpc_sender_id()
|
|
var tileStuff = regions[regionId].requestTiles(unit)
|
|
rpc_id(clientId, 'receiveTiles', tileStuff)
|
|
|
|
remote func requestUnits(regionId = 123):
|
|
var clientId = get_tree().get_rpc_sender_id()
|
|
var boardUnits = regions[regionId].requestUnits()
|
|
rpc_id(clientId, 'receiveUnits', boardUnits)
|
|
|
|
remote func requestMove(route, unitID, regionId = 123):
|
|
regions[regionId].moveUnit(route, unitID)
|
|
|
|
remote func endTurn(regionId = 123):
|
|
regions[regionId].nextRound()
|