aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/world/Avatar.cs
blob: c8469f8ef086986472bdfdc2f1a66eed664af28c (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using libsecondlife;
using libsecondlife.Packets;
using PhysicsSystem;
using Axiom.MathLib;

namespace OpenSim.world
{
    public class Avatar : Entity
    {
    	public string firstname;
    	public string lastname;
    	public OpenSimClient ControllingClient;
    	private PhysicsActor _physActor;
    	private libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock AvatarTemplate;
		private bool updateflag;
		private bool walking;
		private List<NewForce> forcesList = new List<NewForce>();
		private short _updateCount;
		
    	public Avatar(OpenSimClient TheClient) {
    		ServerConsole.MainConsole.Instance.WriteLine("Avatar.cs - Loading details from grid (DUMMY)");
    		ControllingClient=TheClient;
    		SetupTemplate("avatar-template.dat");
			position = new LLVector3(100.0f,100.0f,30.0f);
			position.Z = OpenSim_Main.local_world.LandMap[(int)position.Y * 256 + (int)position.X]+1;
		}
    	
    	public PhysicsActor PhysActor
    	{
    		set
    		{
    			this._physActor = value;
    		}
    	}
		public override void addForces()
		{
			lock(this.forcesList)
			{
				if(this.forcesList.Count>0)
				{
					for(int i=0 ; i < this.forcesList.Count; i++)
					{
						NewForce force = this.forcesList[i];
						PhysicsVector phyVector = new PhysicsVector(force.X, force.Y, force.Z);
						this._physActor.Velocity = phyVector;
						this.updateflag = true;
						this.velocity = new LLVector3(force.X, force.Y, force.Z); //shouldn't really be doing this
						// but as we are setting the velocity (rather than using real forces) at the moment it is okay.
					}
					for(int i=0 ; i < this.forcesList.Count; i++)
					{
						this.forcesList.RemoveAt(0);
					}
				}
			}
		}
    	
    	public override void update()
    	{
    		
    		if(this.updateflag)
    		{
    			//need to send movement info
    			//so create the improvedterseobjectupdate packet
    			//use CreateTerseBlock()
    			ImprovedTerseObjectUpdatePacket.ObjectDataBlock terseBlock = CreateTerseBlock();
    			ImprovedTerseObjectUpdatePacket terse = new ImprovedTerseObjectUpdatePacket();
    			terse.RegionData.RegionHandle = OpenSim_Main.cfg.RegionHandle; // FIXME
    			terse.RegionData.TimeDilation = 64096;
    			terse.ObjectData = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock[1];
    			terse.ObjectData[0] = terseBlock;
    			foreach(OpenSimClient client in OpenSim_Main.sim.ClientThreads.Values) {
    				client.OutPacket(terse);
    			}
    			
    			updateflag =false;
    			this._updateCount = 0;
    		}
    		else
    		{
    			if(walking)
    			{
    				_updateCount++;
    				if(_updateCount>3)
    				{
    					//It has been a while since last update was sent so lets send one.
    					ImprovedTerseObjectUpdatePacket.ObjectDataBlock terseBlock = CreateTerseBlock();
    					ImprovedTerseObjectUpdatePacket terse = new ImprovedTerseObjectUpdatePacket();
    					terse.RegionData.RegionHandle = OpenSim_Main.cfg.RegionHandle; // FIXME
    					terse.RegionData.TimeDilation = 64096;
    					terse.ObjectData = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock[1];
    					terse.ObjectData[0] = terseBlock;
    					foreach(OpenSimClient client in OpenSim_Main.sim.ClientThreads.Values) {
    						client.OutPacket(terse);
    					}
    					_updateCount = 0;
    				}
    			}
    		}
    	}

    	private void SetupTemplate(string name)
    	{
    		
    		int i = 0;
    		FileInfo fInfo = new FileInfo(name);
    		long numBytes = fInfo.Length;
    		FileStream fStream = new FileStream(name, FileMode.Open, FileAccess.Read);
    		BinaryReader br = new BinaryReader(fStream);
    		byte [] data1 = br.ReadBytes((int)numBytes);
    		br.Close();
    		fStream.Close();
    		
    		libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock objdata = new libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock(data1, ref i);
    		
    		System.Text.Encoding enc = System.Text.Encoding.ASCII;
    		libsecondlife.LLVector3 pos = new LLVector3(objdata.ObjectData, 16);
    		pos.X = 100f;
    		objdata.ID = 8880000;
    		objdata.NameValue = enc.GetBytes("FirstName STRING RW SV Test \nLastName STRING RW SV User \0");
    		libsecondlife.LLVector3 pos2 = new LLVector3(100f,100f,23f);
    		//objdata.FullID=user.AgentID;
    		byte[] pb = pos.GetBytes();
    		Array.Copy(pb, 0, objdata.ObjectData, 16, pb.Length);
    		
    		AvatarTemplate = objdata;
    		
    	}

    	public void CompleteMovement(World RegionInfo) {
    		ServerConsole.MainConsole.Instance.WriteLine("Avatar.cs:CompleteMovement() - Constructing AgentMovementComplete packet");
    		AgentMovementCompletePacket mov = new AgentMovementCompletePacket();
    		mov.AgentData.SessionID = this.ControllingClient.SessionID;
    		mov.AgentData.AgentID = this.ControllingClient.AgentID;
    		mov.Data.RegionHandle = OpenSim_Main.cfg.RegionHandle;
    		// TODO - dynamicalise this stuff
    		mov.Data.Timestamp = 1172750370;
    		mov.Data.Position = new LLVector3(100f, 100f, 23f);
    		mov.Data.LookAt = new LLVector3(0.99f, 0.042f, 0);
    		
    		ControllingClient.OutPacket(mov);
    	}

    	public void SendInitialPosition() {
    		
    		System.Text.Encoding _enc = System.Text.Encoding.ASCII;
    		//send a objectupdate packet with information about the clients avatar
    		ObjectUpdatePacket objupdate = new ObjectUpdatePacket();
    		objupdate.RegionData.RegionHandle = OpenSim_Main.cfg.RegionHandle;
    		objupdate.RegionData.TimeDilation = 64096;
    		objupdate.ObjectData = new libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock[1];
    		
    		objupdate.ObjectData[0] = AvatarTemplate;
    		//give this avatar object a local id and assign the user a name
    		objupdate.ObjectData[0].ID = this.localid;
    		objupdate.ObjectData[0].FullID = ControllingClient.AgentID;
    		objupdate.ObjectData[0].NameValue = _enc.GetBytes("FirstName STRING RW SV " + firstname + "\nLastName STRING RW SV " + lastname + " \0");
    		
    		libsecondlife.LLVector3 pos2 = new LLVector3((float)this.position.X, (float)this.position.Y, (float)this.position.Z);
		
    		byte[] pb = pos2.GetBytes();
    		
    		Array.Copy(pb, 0, objupdate.ObjectData[0].ObjectData, 16, pb.Length);
    		OpenSim_Main.local_world._localNumber++;
    		this.ControllingClient.OutPacket(objupdate);
    	}
    	
    	public void SendInitialAppearance() {
    		AgentWearablesUpdatePacket aw = new AgentWearablesUpdatePacket();
    		aw.AgentData.AgentID = this.ControllingClient.AgentID;
    		aw.AgentData.SerialNum = 0;
    		aw.AgentData.SessionID = ControllingClient.SessionID;
    		
    		aw.WearableData = new AgentWearablesUpdatePacket.WearableDataBlock[13];
    		AgentWearablesUpdatePacket.WearableDataBlock awb = new AgentWearablesUpdatePacket.WearableDataBlock();
    		awb.WearableType = (byte)0;
    		awb.AssetID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73");
    		awb.ItemID = LLUUID.Random();
    		aw.WearableData[0] = awb;
    		
    		for(int i=1; i<13; i++) {
    			awb = new AgentWearablesUpdatePacket.WearableDataBlock();
    			awb.WearableType = (byte)i;
    			awb.AssetID = new LLUUID("00000000-0000-0000-0000-000000000000");
    			awb.ItemID = new LLUUID("00000000-0000-0000-0000-000000000000");
    			aw.WearableData[i] = awb;
    		}
    		
    		ControllingClient.OutPacket(aw);
    	}
    	
    	public void HandleUpdate(AgentUpdatePacket pack) {
           if(((uint)pack.AgentData.ControlFlags & (uint)MainAvatar.AgentUpdateFlags.AGENT_CONTROL_AT_POS) !=0) {
    			if(!walking)
    			{
    				//we should add a new force to the list 
    				// but for now we will deal with velocities
    				NewForce newVelocity = new NewForce();
    				Axiom.MathLib.Vector3 v3 = new Axiom.MathLib.Vector3(1, 0, 0);
    				Axiom.MathLib.Quaternion q = new Axiom.MathLib.Quaternion(pack.AgentData.BodyRotation.W, pack.AgentData.BodyRotation.X, pack.AgentData.BodyRotation.Y, pack.AgentData.BodyRotation.Z);
    				Axiom.MathLib.Vector3 direc = q * v3;
    				direc.Normalize();
    				
    				//work out velocity for sim physics system
    				direc = direc * ((0.03f) * 128f);
    				newVelocity.X = direc.x;
    				newVelocity.Y = direc.y;
    				newVelocity.Z = direc.z;
    				this.forcesList.Add(newVelocity);
    				walking=true;
    			}
    		}
    		else
    		{
    			if(walking)
    			{
    				NewForce newVelocity = new NewForce();
    				newVelocity.X = 0;
    				newVelocity.Y = 0;
    				newVelocity.Z = 0;
    				this.forcesList.Add(newVelocity);
    				walking = false;
    			}
    		}
    	}
    	
		//should be moved somewhere else
    	public void SendRegionHandshake(World RegionInfo) {
    		ServerConsole.MainConsole.Instance.WriteLine("Avatar.cs:SendRegionHandshake() - Creating empty RegionHandshake packet");
    		System.Text.Encoding _enc = System.Text.Encoding.ASCII;
    		RegionHandshakePacket handshake = new RegionHandshakePacket();
    		
    		ServerConsole.MainConsole.Instance.WriteLine("Avatar.cs:SendRegionhandshake() - Filling in RegionHandshake details");
    		handshake.RegionInfo.BillableFactor = 0;
    		handshake.RegionInfo.IsEstateManager = false;
    		handshake.RegionInfo.TerrainHeightRange00 = 60;
    		handshake.RegionInfo.TerrainHeightRange01 = 60;
    		handshake.RegionInfo.TerrainHeightRange10 = 60;
    		handshake.RegionInfo.TerrainHeightRange11 = 60;
    		handshake.RegionInfo.TerrainStartHeight00 = 10;
    		handshake.RegionInfo.TerrainStartHeight01 = 10;
    		handshake.RegionInfo.TerrainStartHeight10 = 10;
    		handshake.RegionInfo.TerrainStartHeight11 = 10;
    		handshake.RegionInfo.SimAccess = 13;
    		handshake.RegionInfo.WaterHeight = 5;
    		handshake.RegionInfo.RegionFlags = 72458694;
    		handshake.RegionInfo.SimName = _enc.GetBytes(OpenSim_Main.cfg.RegionName + "\0");
    		handshake.RegionInfo.SimOwner = new LLUUID("00000000-0000-0000-0000-000000000000");
    		handshake.RegionInfo.TerrainBase0 = new LLUUID("b8d3965a-ad78-bf43-699b-bff8eca6c975");
    		handshake.RegionInfo.TerrainBase1 = new LLUUID("abb783e6-3e93-26c0-248a-247666855da3");
    		handshake.RegionInfo.TerrainBase2 = new LLUUID("179cdabd-398a-9b6b-1391-4dc333ba321f");
    		handshake.RegionInfo.TerrainBase3 = new LLUUID("beb169c7-11ea-fff2-efe5-0f24dc881df2");
    		handshake.RegionInfo.TerrainDetail0 = new LLUUID("00000000-0000-0000-0000-000000000000");
    		handshake.RegionInfo.TerrainDetail1 = new LLUUID("00000000-0000-0000-0000-000000000000");
    		handshake.RegionInfo.TerrainDetail2 = new LLUUID("00000000-0000-0000-0000-000000000000");
    		handshake.RegionInfo.TerrainDetail3 = new LLUUID("00000000-0000-0000-0000-000000000000");
    		handshake.RegionInfo.CacheID = new LLUUID("545ec0a5-5751-1026-8a0b-216e38a7ab37");
    		
    		ServerConsole.MainConsole.Instance.WriteLine("Avatar.cs:SendRegionHandshake() - Sending RegionHandshake packet");
    		this.ControllingClient.OutPacket(handshake);
    	}
		
		public ImprovedTerseObjectUpdatePacket.ObjectDataBlock CreateTerseBlock()
		{
			byte[] bytes = new byte[60];
			int i=0;
			ImprovedTerseObjectUpdatePacket.ObjectDataBlock dat = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock();
			
			dat.TextureEntry = AvatarTemplate.TextureEntry;
			libsecondlife.LLVector3 pos2 = new LLVector3(this._physActor.Position.X, this._physActor.Position.Y, this._physActor.Position.Z);
			
			uint ID = this.localid;
			
			bytes[i++] = (byte)(ID % 256);
			bytes[i++] = (byte)((ID >> 8) % 256);
			bytes[i++] = (byte)((ID >> 16) % 256);
			bytes[i++] = (byte)((ID >> 24) % 256);
			bytes[i++] = 0;
			bytes[i++] = 1;
			i += 14;
			bytes[i++] = 128;
			bytes[i++] = 63;
			
			byte[] pb = pos2.GetBytes();
			Array.Copy(pb, 0, bytes, i, pb.Length);
			i += 12;
			ushort InternVelocityX;
			ushort InternVelocityY;
			ushort InternVelocityZ;
			
			Axiom.MathLib.Vector3 internDirec = new Axiom.MathLib.Vector3(this._physActor.Velocity.X, this._physActor.Velocity.Y, this._physActor.Velocity.Z);
			internDirec = internDirec /128.0f;
			internDirec.x += 1;
			internDirec.y += 1;
			internDirec.z += 1;
			
			InternVelocityX = (ushort)(32768 * internDirec.x);
			InternVelocityY = (ushort)(32768 * internDirec.y);
			InternVelocityZ = (ushort)(32768 * internDirec.z);
			
			ushort ac = 32767;
			bytes[i++] = (byte)(InternVelocityX % 256);
			bytes[i++] = (byte)((InternVelocityX >> 8) % 256);
			bytes[i++] = (byte)(InternVelocityY % 256);
			bytes[i++] = (byte)((InternVelocityY>> 8) % 256);
			bytes[i++] = (byte)(InternVelocityZ % 256);
			bytes[i++] = (byte)((InternVelocityZ >> 8) % 256);
			
			//accel
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);
			
			//rot
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);	
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);
			
			//rotation vel
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);			
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);	
			bytes[i++] = (byte)(ac % 256);
			bytes[i++] = (byte)((ac >> 8) % 256);
			
			dat.Data=bytes;
			return(dat);
		}
    }
    
    public class NewForce
    {
    	public float X;
    	public float Y;
    	public float Z;
    	
    	public NewForce()
    	{
    		
    	}
    }
}