-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerShipManager.cs
110 lines (88 loc) · 3.98 KB
/
ServerShipManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using MogreNewt;
using Ymfas;
namespace Ymfas
{
public class ServerShipManager
{
Dictionary<int, Ship> shipTable = new Dictionary<int, Ship>();
private World world;
private EventManager eventMgr;
private YmfasServer server;
private Mogre.Log serverShipLog;
private const float AUTOCORRECT_CUTOFF = 0.001f;
public ServerShipManager(World serverWorld, EventManager eventManager, YmfasServer _server)
{
serverShipLog = Mogre.LogManager.Singleton.CreateLog("server-ship.log");
serverShipLog.LogMessage("creating ssm");
world = serverWorld;
eventMgr = eventManager;
server = _server;
//init ships
ShipTypeData curShipType = new ShipTypeData();
curShipType.Class = ShipClass.Interceptor;
curShipType.Model = ShipModel.MogreFighter;
//player ships
foreach (int id in server.PlayerIds )
{
Vector3 curPosition = Vector3.ZERO;//new Vector3(Mogre.Math.RangeRandom(-TestEngine.WorldSizeParam / 1.5f, TestEngine.WorldSizeParam / 1.5f), Mogre.Math.RangeRandom(-TestEngine.WorldSizeParam / 1.5f, TestEngine.WorldSizeParam / 1.5f), Mogre.Math.RangeRandom(-TestEngine.WorldSizeParam / 1.5f, TestEngine.WorldSizeParam / 1.5f));
Quaternion curOrientation = Quaternion.IDENTITY;
ShipInit curShipInit = new ShipInit(id, curShipType, curPosition, curOrientation,
server.GetPlayerName(id));
eventMgr.SendEvent(curShipInit);
Util.Log("sent init for ship " + id);
//put them in world
Ship s = new Ship(world, id, curPosition, curOrientation);
shipTable.Add(id, s);
}
//init listeners
ShipControlStatus.FiringEvent += new GameEventFiringHandler(handleShipControlStatus);
}
public Dictionary<int, Ship> ShipTable { get { return shipTable; } }
void handleShipControlStatus(GameEvent e)
{
ShipControlStatus ee = (ShipControlStatus)e;
Ship s;
shipTable.TryGetValue(ee.playerID, out s);
Vector3 torque = s.GetCorrectiveTorque();
if (Mogre.Math.Abs(s.RotationalVelocity.x) < AUTOCORRECT_CUTOFF) {
torque.x = 0;
s.RotationalVelocity = new Vector3(0, s.RotationalVelocity.y, s.RotationalVelocity.z);
}
if (Mogre.Math.Abs(s.RotationalVelocity.y) < AUTOCORRECT_CUTOFF) {
torque.y = 0;
s.RotationalVelocity = new Vector3(s.RotationalVelocity.x, 0, s.RotationalVelocity.z);
}
if (Mogre.Math.Abs(s.RotationalVelocity.z) < AUTOCORRECT_CUTOFF) {
torque.z = 0;
s.RotationalVelocity = new Vector3(s.RotationalVelocity.x, s.RotationalVelocity.y, 0);
}
if (ee.pitch != UserInputManager.AUTOCORRECT)
{
torque.x = ee.pitch / ((float)UserInputManager.POSITIVE);
}
if (ee.yaw != UserInputManager.AUTOCORRECT)
{
torque.y = ee.yaw / ((float)UserInputManager.POSITIVE);
}
if (ee.roll != UserInputManager.AUTOCORRECT)
{
torque.z = ee.roll / ((float)UserInputManager.POSITIVE);
}
//Console.Out.WriteLine(torque.ToString());
//Console.Out.WriteLine();
s.TorqueRelative(torque);
s.ThrustRelative(new Vector3(0.0f, 0.0f, ((float)ee.thrust) / ((float)UserInputManager.FULL)));
}
public void sendShipStateStatus()
{
List<ShipState> l = new List<ShipState>();
foreach (Ship s in shipTable.Values)
l.Add(s.ShipState);
eventMgr.SendEvent(new ShipStateStatus(l));
}
}
}