mostly sensible init for real players and bots

This commit is contained in:
Chris Forbes
2010-08-25 19:23:25 +12:00
parent cfc937e8eb
commit 0f9221dc5a
6 changed files with 52 additions and 38 deletions

View File

@@ -37,6 +37,7 @@ namespace OpenRA
public readonly int Index; public readonly int Index;
public readonly bool NonCombatant = false; public readonly bool NonCombatant = false;
public readonly int ClientIndex; public readonly int ClientIndex;
public readonly PlayerReference PlayerRef;
public ShroudRenderer Shroud; public ShroudRenderer Shroud;
public World World { get; private set; } public World World { get; private set; }
@@ -59,27 +60,30 @@ namespace OpenRA
Country = world.GetCountries() Country = world.GetCountries()
.FirstOrDefault(c => pr.Race == c.Race); .FirstOrDefault(c => pr.Race == c.Race);
PlayerRef = pr;
RegisterPlayerColor(world, Palette); RegisterPlayerColor(world, Palette);
} }
public Player( World world, Session.Client client ) public Player( World world, Session.Client client, PlayerReference pr, int index )
{ {
World = world; World = world;
Shroud = new ShroudRenderer(this, world.Map); Shroud = new ShroudRenderer(this, world.Map);
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) }); PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) });
Index = client.Index; Index = index;
Palette = "player"+client.Index; Palette = "player"+index;
Color = client.Color1; Color = client.Color1;
Color2 = client.Color2; Color2 = client.Color2;
PlayerName = client.Name; PlayerName = client.Name;
InternalName = "Multi{0}".F(client.Index); InternalName = pr.Name;
Country = world.GetCountries() Country = world.GetCountries()
.FirstOrDefault(c => client != null && client.Country == c.Race) .FirstOrDefault(c => client != null && client.Country == c.Race)
?? world.GetCountries().Random(world.SharedRandom); ?? world.GetCountries().Random(world.SharedRandom);
ClientIndex = client.Index; ClientIndex = client.Index;
PlayerRef = pr;
RegisterPlayerColor(world, Palette); RegisterPlayerColor(world, Palette);
} }

View File

@@ -163,6 +163,8 @@ namespace OpenRA.Traits
public interface IGameStarted { void GameStarted(World w); } public interface IGameStarted { void GameStarted(World w); }
public interface ICreatePlayers { void CreatePlayers(World w); } public interface ICreatePlayers { void CreatePlayers(World w); }
public interface IBot { void Activate(Player p); }
public interface IActivity public interface IActivity
{ {
IActivity NextActivity { get; set; } IActivity NextActivity { get; set; }

View File

@@ -8,7 +8,6 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Network; using OpenRA.Network;
using OpenRA.Traits; using OpenRA.Traits;
@@ -21,11 +20,30 @@ namespace OpenRA.Mods.RA
{ {
public void CreatePlayers(World w) public void CreatePlayers(World w)
{ {
// Add real players var playerIndex = 0;
w.SetLocalPlayer(Game.LocalClientId); foreach (var slot in Game.LobbyInfo.Slots)
{
var client = Game.LobbyInfo.Clients.FirstOrDefault(c => c.Slot == slot.Index);
if (client != null)
{
/* spawn a real player in this slot. */
var player = new Player(w, client, w.Map.Players[slot.MapPlayer], playerIndex++);
w.AddPlayer(player);
if (client.Index == Game.LocalClientId)
w.SetLocalPlayer(player.Index); // bind this one to the local player.
}
else if (slot.Bot != null)
{
/* spawn a bot in this slot, "owned" by the host */
var player = new Player(w, w.Map.Players[slot.MapPlayer], playerIndex++);
w.AddPlayer(player);
foreach (var c in Game.LobbyInfo.Clients) /* todo: init its bot -- but only on the host! */
w.AddPlayer(new Player(w, c)); if (Game.IsHost)
foreach (var bot in player.PlayerActor.TraitsImplementing<IBot>())
bot.Activate(player);
}
}
foreach (var p in w.players.Values) foreach (var p in w.players.Values)
foreach (var q in w.players.Values) foreach (var q in w.players.Values)

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Mods.RA
/* a pile of hacks, which control a local player on the host. */ /* a pile of hacks, which control a local player on the host. */
class HackyAI : IGameStarted, ITick class HackyAI : ITick, IBot
{ {
bool enabled; bool enabled;
int ticks; int ticks;
@@ -62,24 +62,15 @@ namespace OpenRA.Mods.RA
BuildState state = BuildState.WaitForFeedback; BuildState state = BuildState.WaitForFeedback;
public void GameStarted(World w) /* called by the host's player creation code */
{ public void Activate(Player p)
try
{
p = Game.world.players.First(c => c.Value.PlayerName.Equals("bot")).Value;
}
catch (Exception)
{
//Could not find a bot.
}
//p = Game.world.LocalPlayer;
enabled = Game.IsHost && p != null;
if (enabled)
{ {
this.p = p;
enabled = true;
productionQueue = p.PlayerActor.Trait<ProductionQueue>(); productionQueue = p.PlayerActor.Trait<ProductionQueue>();
playerResources = p.PlayerActor.Trait<PlayerResources>(); playerResources = p.PlayerActor.Trait<PlayerResources>();
} }
}
int GetPowerProvidedBy(string building) int GetPowerProvidedBy(string building)
{ {
@@ -341,6 +332,5 @@ namespace OpenRA.Mods.RA
break; break;
} }
} }
} }
} }

View File

@@ -238,7 +238,7 @@
<Compile Include="CreateMapPlayers.cs" /> <Compile Include="CreateMapPlayers.cs" />
<Compile Include="CreateMPPlayers.cs" /> <Compile Include="CreateMPPlayers.cs" />
<Compile Include="LocalPlayerFromMap.cs" /> <Compile Include="LocalPlayerFromMap.cs" />
<Compile Include="World\HackyAI.cs" /> <Compile Include="HackyAI.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -8,9 +8,6 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Traits; using OpenRA.Traits;
@@ -28,6 +25,9 @@ namespace OpenRA.Mods.RA
void SpawnUnitsForPlayer(Player p, int2 sp) void SpawnUnitsForPlayer(Player p, int2 sp)
{ {
if (!p.PlayerRef.DefaultStartingUnits)
return; /* they don't want an mcv, the map provides something else for them. */
p.World.CreateActor("mcv", new TypeDictionary p.World.CreateActor("mcv", new TypeDictionary
{ {
new LocationInit( sp ), new LocationInit( sp ),