Level Editor/Scripting/Examples/Service

From TeamSpec Wiki
Jump to navigation Jump to search

Each team guards a boss in its own base, and both teams attack and defend at once. There is no clock: the round ends the moment either boss dies, and the other team wins. A team is warned when its boss comes under attack.

The map

  • A Boss Spawn (Functional > Boss Spawn) in each base, one with Team 1 and one with Team 2. How tough each boss is, and how fast it heals, is set on the Boss Spawn (Max Health, Regen Per Second, Regen Delay), not in the script.
  • Spawn points for team 1 and team 2.
  • A World Boundary below the map.

The script declares no teams, so the map plays with the two default teams, "Team 1" and "Team 2".

server.gd

extends GamemodeServerScript

## A team hears its boss is under attack when it is hit after this many
## seconds without a hit.
const ALERT_QUIET := 15.0

## team -> its boss's entity id. bot.gd reads it too.
var boss_of: Dictionary = {}
## team -> seconds since its boss was last hit.
var _quiet: Dictionary = {}


func _declare() -> void:
	api.declare_team_counter(&"boss_health")
	api.declare_team_counter(&"boss_max_health")


func _start() -> void:
	for boss in api.bosses():
		var team: int = api.entity_team(boss)
		if team == 0 or boss_of.has(team):
			continue
		boss_of[team] = boss
		_quiet[team] = ALERT_QUIET
		api.set_team(team, &"boss_max_health", ceili(api.entity_max_health(boss)))
	for team in api.teams():
		if not boss_of.has(team):
			api.log("team %d has no boss" % team)


func _player_died(entity_id: int, _killer_id: int, default_seconds: float) -> void:
	api.respawn_in(entity_id, default_seconds)


func _entity_damaged(entity: int, _amount: float, _attacker: int) -> void:
	var team: int = api.entity_team(entity)
	if boss_of.get(team, 0) != entity:
		return
	if _quiet[team] >= ALERT_QUIET:
		api.notify_team(team, "Your boss is under attack", "Get back to your base.", true)
	_quiet[team] = 0.0


func _entity_died(entity: int, _killer: int) -> void:
	var team: int = api.entity_team(entity)
	if boss_of.get(team, 0) != entity:
		return
	api.set_team(team, &"boss_health", 0)
	for winner in api.teams():
		if winner != team:
			api.declare_winner(winner, "killed %s's boss" % api.team_name(team))
			return


func _tick(delta: float) -> void:
	for team in boss_of:
		_quiet[team] += delta
		# Rounded up, so a boss on its last sliver still shows 1.
		api.set_team(team, &"boss_health", ceili(api.entity_health(boss_of[team])))

What it uses:

  • api.bosses lists every boss and api.entity_team says whose it is. _start keeps one boss per team; a boss on no team is scenery.
  • The two team counters are declared with only a name, so they start at 0 and have no upper limit. boss_max_health is set once from the boss itself with api.entity_max_health, so the Boss Spawn is the only place its health is written down.
  • The health is published every tick, rounded up, so a boss on its last sliver still shows 1 and 0 always means dead. A counter is only sent to players when its value changes, so a boss nobody is hurting costs nothing.
  • _entity_damaged warns the team with api.notify_team, but only when the hits start again after 15 quiet seconds, so a long siege is one warning, not one per hit.
  • _entity_died ends the round. Only the enemy team's weapons hurt a boss, which the game sees to, so the winner is simply the other team. See Bosses.
  • There is no _tick win check and no clock: _tick only keeps the health bars current.

client.gd

extends GamemodeClientScript


func _start() -> void:
	var names: Dictionary = {}
	for team in api.teams():
		names[team] = api.team_name(team)
	api.show_widget(&"boss_health", {
		"counter": "boss_health",
		"max_counter": "boss_max_health",
		"warn_share": 0.4,
		"team_names": names,
	})

The boss_health widget draws both bosses, the player's own on the left, each with the health it has left. A bar takes its warning look below 40 percent of its boss's health. A spectator, who has no team, sees the teams' names on the bars instead of "Your boss" and "Enemy boss". See boss_health.

bot.gd

extends GamemodeBotScript

const REVIEW_SECONDS := 1.0
## Posts stand this many meters in front of a boss, toward the other base.
const STANDOFF := 12.0

var _review: float = 0.0
## team -> where its bots hold their own base, and where they attack the enemy's.
var _home: Dictionary = {}
var _attack: Dictionary = {}


func _start() -> void:
	for team in server.boss_of:
		for enemy in server.boss_of:
			if enemy != team:
				var own: Vector3 = api.entity_position(server.boss_of[team])
				var theirs: Vector3 = api.entity_position(server.boss_of[enemy])
				_home[team] = own.move_toward(theirs, STANDOFF)
				_attack[team] = theirs.move_toward(own, STANDOFF)


func _tick(delta: float) -> void:
	_review -= delta
	if _review > 0.0:
		return
	_review = REVIEW_SECONDS
	for bot in api.bots():
		var team: int = api.team_of(bot)
		if not api.is_alive(bot) or not _home.has(team):
			continue
		if api.bot_role(bot) == &"defense":
			api.bot_defend(bot, _home[team], 12.0)
		else:
			api.bot_defend(bot, _attack[team], 8.0)

Each team gets two posts, worked out once from where the bosses stand: one 12 meters in front of its own boss and one 12 meters in front of the enemy's, each on the side facing the other base. Bots with a defense kit hold the home post; everybody else holds the post at the enemy boss, where they fight whoever defends it and shoot the boss whenever they see it. The posts come from the boss_of Dictionary in server.gd, read through server.

Going further

The base game's Service on Darkcove also has:

  • Spawn rooms with immunity, class changes and a refill; see Spawn rooms.
  • A split that follows the match. About a third of each team's bots defend. A boss low on health pulls more of its team home, the team whose enemy boss is the more damaged sends more to finish it, and enemies inside a base pull at least as many defenders home. A boss's base is its Arena Zone, read with api.entity_zone and counted with api.zone_occupants.
TeamSpec Level Editor
Basics Getting started · Interface · Viewport and camera · Selecting and hiding · Keyboard shortcuts · Settings · Map Config · Troubleshooting
Menus File · Edit · View · Tools · Functional
Panels Scene tree · Inspector · Materials · Props · Snapping · UV editor · Displacement · Terrain
Tools Move, Rotate and Scale · Box · Sphere · Cylinder · Prism · Stairs · Spiral Stairs · Spiral Tower · Arch · Path Brush · Plane · Terrain · Clip · Duplicate · Ruler and Cordon · Edit Vertex and Edge · Edit Face · Loop Cut · Material Brush · Vertex Color Brush · Displacement · Prop Path
Entities Brush · Zone · Group · Mirror Node · Prop · Spawn Point · Supply Pickup · World Boundary · Lights · Fog Volume · Water · Sounds · Anchor Platform · Payload Track · Boss Spawn · Map Marker · World Text · Decal · Door · Button · Logic entities · 3D Skybox · Nav Region
Workflows Your first map · Brushes and CSG · Materials and UVs · Lighting and lightmaps · Bots and navigation · Mirrored maps · Outputs and logic · Custom assets · Playtesting · Exporting and publishing · Importing Source maps
Scripting Overview · Server rules · Client HUD · Bots · Teams and spawning · Zones · Pickups · Payload tracks · Map entities · Language · Sandbox limits · Testing · King of the Hill · Team Deathmatch · Payload · Conquer · Soul Banking · Service · Training Range · API reference · What a script may use