corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Drawing.Imaging;
7 using System.Text;
8 using System.Windows.Forms;
9 using OpenMetaverse;
10 using OpenMetaverse.Packets;
11  
12 namespace Heightmap
13 {
14 public partial class frmHeightmap : Form
15 {
16 private GridClient Client = new GridClient();
17 private PictureBox[,] Boxes = new PictureBox[16, 16];
18 private System.Timers.Timer UpdateTimer = new System.Timers.Timer(1000);
19 private string FirstName, LastName, Password;
20  
21 double heading = -Math.PI;
22  
23 public frmHeightmap(string firstName, string lastName, string password)
24 {
25 FirstName = firstName;
26 LastName = lastName;
27 Password = password;
28  
29 Client.Network.LoginProgress += Network_OnLogin;
30  
31 // Throttle land up and other things down
32 Client.Throttle.Cloud = 0;
33 Client.Throttle.Land = 1000000;
34 Client.Throttle.Wind = 0;
35  
36 Client.Settings.MULTIPLE_SIMS = false;
37  
38 // Build the picture boxes
39 this.SuspendLayout();
40 for (int y = 0; y < 16; y++) // Box 0,0 is on the top left
41 {
42 for (int x = 0; x < 16; x++)
43 {
44 Boxes[x, y] = new System.Windows.Forms.PictureBox();
45 PictureBox box = Boxes[x, y];
46 ((System.ComponentModel.ISupportInitialize)(box)).BeginInit();
47 box.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
48 box.Name = x + "," + y;
49 box.Location = new System.Drawing.Point(x * 18, y * 18);
50 box.Size = new System.Drawing.Size(18, 18);
51 box.Visible = true;
52 box.MouseUp += new MouseEventHandler(box_MouseUp);
53 ((System.ComponentModel.ISupportInitialize)(box)).EndInit();
54  
55 this.Controls.Add(box);
56 }
57 }
58 this.ResumeLayout();
59  
60 InitializeComponent();
61 }
62  
63 private void Network_OnLogin(object sender, LoginProgressEventArgs e)
64 {
65 if (e.Status == LoginStatus.Success)
66 {
67 UpdateTimer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateTimer_Elapsed);
68 UpdateTimer.Start();
69 }
70 else if (e.Status == LoginStatus.Failed)
71 {
72 Console.WriteLine("Login failed: " + Client.Network.LoginMessage);
73 Console.ReadKey();
74 this.Close();
75 return;
76 }
77 }
78  
79 private void frmHeightmap_Load(object sender, EventArgs e)
80 {
81 Client.Terrain.LandPatchReceived += new EventHandler<LandPatchReceivedEventArgs>(Terrain_LandPatchReceived);
82 // Only needed so we can do lookups with TerrainHeightAtPoint
83 Client.Settings.STORE_LAND_PATCHES = true;
84  
85 LoginParams loginParams = Client.Network.DefaultLoginParams(FirstName, LastName, Password, "Heightmap",
86 "1.0.0");
87 Client.Network.BeginLogin(loginParams);
88  
89 this.SetDesktopLocation(1600, 0);
90 // FIXME: This really should be modified in frmHeightmap.Designer.cs, but the Prebuild bug is
91 // preventing that right now
92 this.SetClientSizeCore(18 * 16, 18 * 16);
93 }
94  
95 private void box_MouseUp(object sender, MouseEventArgs e)
96 {
97 for (int y = 0; y < 16; y++)
98 {
99 for (int x = 0; x < 16; x++)
100 {
101 if (Boxes[x, y] == sender)
102 {
103 float height;
104 if (Client.Network.CurrentSim.TerrainHeightAtPoint(x * 16 + e.X, y * 16 + e.Y, out height))
105 MessageBox.Show( string.Format("{0},{1}:{2}",x*16+e.X,255-(y*16+e.Y),height) );
106 else
107 MessageBox.Show("Unknown height");
108 return;
109 }
110 }
111 }
112 }
113  
114 void UpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
115 {
116 // Spin our camera in circles at the center of the sim to load all the terrain
117 heading += 0.5d;
118 if (heading > Math.PI) heading = -Math.PI;
119  
120 Client.Self.Movement.UpdateFromHeading(heading, false);
121 }
122  
123 void Terrain_LandPatchReceived(object sender, LandPatchReceivedEventArgs e)
124 {
125 if (e.X >= 16 || e.Y >= 16)
126 {
127 Console.WriteLine("Bad patch coordinates, x = " + e.X + ", y = " + e.Y);
128 return;
129 }
130  
131 if (e.PatchSize != 16)
132 {
133 Console.WriteLine("Unhandled patch size " + e.PatchSize + "x" + e.PatchSize);
134 return;
135 }
136  
137 Bitmap patch = new Bitmap(16, 16, PixelFormat.Format24bppRgb);
138  
139 for (int yp = 0; yp < 16; yp++)
140 {
141 for (int xp = 0; xp < 16; xp++)
142 {
143 float height = e.HeightMap[(15-yp) * 16 + xp]; // data[0] is south west
144 Color color;
145 if (height >= e.Simulator.WaterHeight)
146 {
147 float maxVal = (float)Math.Log(Math.Abs(512+1-e.Simulator.WaterHeight),2);
148 float lgHeight = (float)Math.Log(Math.Abs(height + 1 - e.Simulator.WaterHeight), 2);
149 int colorVal1 = Utils.FloatToByte(lgHeight, e.Simulator.WaterHeight, maxVal);
150 int colorVal2 = Utils.FloatToByte(height, e.Simulator.WaterHeight, 25.0f);
151 color = Color.FromArgb(255, colorVal2, colorVal1);
152 }
153 else
154 {
155 const float minVal = -5.0f;
156 float maxVal = e.Simulator.WaterHeight;
157 int colorVal1 = Utils.FloatToByte(height, -5.0f, minVal + (maxVal - minVal) * 1.5f);
158 int colorVal2 = Utils.FloatToByte(height, -5.0f, maxVal);
159 color = Color.FromArgb(colorVal1, colorVal2, 255);
160 }
161 patch.SetPixel(xp, yp, color); // 0, 0 is top left
162 }
163 }
164  
165 Boxes[e.X, 15-e.Y].Image = (System.Drawing.Image)patch;
166 }
167  
168 private void frmHeightmap_FormClosing(object sender, FormClosingEventArgs e)
169 {
170 Client.Network.Logout();
171 }
172 }
173 }