OSDN Git Service

出撃統計で輸送船がRになるのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / FleetPanel.cs
1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.Collections;\r
17 using System.Collections.Generic;\r
18 using System.Drawing;\r
19 using System.Linq;\r
20 using System.Windows.Forms;\r
21 \r
22 namespace KancolleSniffer\r
23 {\r
24     public class FleetPanel : Panel\r
25     {\r
26         private const int LineHeight = 14;\r
27         private const int LabelHeight = 12;\r
28         private Record[] _table;\r
29         private readonly List<FleetLabels> _labelList = new List<FleetLabels>();\r
30         private readonly List<Panel> _panelList = new List<Panel>();\r
31         private readonly ResizableToolTip _toolTip = new ResizableToolTip {ShowAlways = true, AutoPopDelay = 10000};\r
32 \r
33         private class Record\r
34         {\r
35             public string Fleet { get; set; }\r
36             public string Fleet2 { get; set; }\r
37             public string Ship { get; set; }\r
38             public string Ship2 { get; set; }\r
39             public int Id { get; set; }\r
40             public string Equip { get; set; }\r
41             public Color Color { get; set; }\r
42             public string Spec { get; set; }\r
43             public string Spec2 { get; set; }\r
44             public string AircraftSpec { get; set; }\r
45 \r
46             public Record()\r
47             {\r
48                 Fleet = Ship = Equip = AircraftSpec = "";\r
49                 Color = DefaultBackColor;\r
50             }\r
51         }\r
52 \r
53         public void Update(Sniffer sniffer)\r
54         {\r
55             CreateTable(sniffer);\r
56             SuspendLayout();\r
57             CreateLabels();\r
58             SetRecords();\r
59             ResumeLayout();\r
60         }\r
61 \r
62         private class Total\r
63         {\r
64             public int Drum;\r
65             public int DrumShips;\r
66             public int Level;\r
67             public int FirePower;\r
68             public int AntiSubmarine;\r
69             public int AntiAir;\r
70             public int LoS;\r
71             public int Fuel;\r
72             public int Bull;\r
73 \r
74             public void Add(ShipStatus s)\r
75             {\r
76                 var drum = s.Slot.Count(item => item.Spec.Name == "ドラム缶(輸送用)");\r
77                 DrumShips += drum != 0 ? 1 : 0;\r
78                 Drum += drum;\r
79                 Level += s.Level;\r
80                 FirePower += s.Firepower;\r
81                 AntiSubmarine += s.MissionAntiSubmarine;\r
82                 AntiAir += s.AntiAir;\r
83                 LoS += s.LoS;\r
84                 Fuel += s.EffectiveFuelMax;\r
85                 Bull += s.EffectiveBullMax;\r
86             }\r
87         }\r
88 \r
89         private void CreateTable(Sniffer sniffer)\r
90         {\r
91             var list = new List<Record>();\r
92             var fleets = sniffer.Fleets;\r
93             var fn = new[] {"第一", "第二", "第三", "第四"};\r
94             for (var f = 0; f < fn.Length; f++)\r
95             {\r
96                 var total = new Total();\r
97                 var ships = new List<Record>();\r
98                 var fleet = fleets[f];\r
99                 foreach (var s in fleet.Ships)\r
100                 {\r
101                     var equips = new List<Record>();\r
102                     for (var i = 0; i < s.Slot.Length; i++)\r
103                     {\r
104                         var item = s.Slot[i];\r
105                         var onslot = s.OnSlot[i];\r
106                         var max = s.Spec.MaxEq[i];\r
107                         if (item.Id == -1)\r
108                             continue;\r
109                         var airspec = "";\r
110                         if (item.Spec.IsDiveBomber) // 爆撃\r
111                         {\r
112                             airspec = "航空戦 " +\r
113                                       (25 + (int)((item.Spec.Bomber + item.BomberLevelBonus) * Math.Sqrt(onslot)));\r
114                         }\r
115                         else if (item.Spec.IsTorpedoBomber)\r
116                         {\r
117                             var normal = 25 + item.Spec.Torpedo * Math.Sqrt(onslot);\r
118                             airspec = "航空戦 " + (int)(normal * 0.8) + "/" + (int)(normal * 1.5);\r
119                         }\r
120                         equips.Add(new Record\r
121                         {\r
122                             Equip = GenEquipString(item),\r
123                             Spec = item.Spec.IsAircraft ? $"+{item.Alv} {onslot}/{max}" : "",\r
124                             AircraftSpec = airspec,\r
125                             Color = item.Spec.Color\r
126                         });\r
127                     }\r
128                     if (s.SlotEx.Id > 0)\r
129                     {\r
130                         var item = s.SlotEx;\r
131                         equips.Add(new Record {Equip = GenEquipString(item), Color = item.Spec.Color});\r
132                     }\r
133                     total.Add(s);\r
134                     var fire = s.EffectiveFirepower;\r
135                     var subm = s.EffectiveAntiSubmarine;\r
136                     var torp = s.EffectiveTorpedo;\r
137                     var night = s.NightBattlePower;\r
138                     var oasa = s.CanOpeningAntiSubmarineAttack ? "*" : "";\r
139                     var ship = new Record\r
140                     {\r
141                         Ship = (s.Escaped ? "[避]" : "") + s.Name + " Lv" + s.Level,\r
142                         Ship2 = $"燃{s.EffectiveFuelMax} 弾{s.EffectiveBullMax}",\r
143                         Id = s.Id,\r
144                         Spec = HideIfZero("砲", fire) + HideIfZero(" 潜", subm) + oasa,\r
145                         Spec2 = (HideIfZero("雷", torp) + HideIfZero(" 夜", night)).TrimStart(' ')\r
146                     };\r
147                     if (ship.Spec == "")\r
148                     {\r
149                         ship.Spec = ship.Spec2;\r
150                         ship.Spec2 = "";\r
151                     }\r
152                     ships.Add(ship);\r
153                     ships.AddRange(equips);\r
154                 }\r
155                 var daihatsu = fleet.DaihatsuBonus;\r
156                 var tp = fleet.TransportPoint;\r
157                 if (sniffer.CombinedFleetType != 0 && f == 0)\r
158                     tp += fleets[1].TransportPoint;\r
159                 list.Add(new Record\r
160                 {\r
161                     Fleet = fn[f] + HideIfZero(" Lv", total.Level) +\r
162                             HideIfZero(" ドラム缶", total.Drum) + HideIfZero("(", total.DrumShips, "隻)") +\r
163                             HideIfZero(" 大発", daihatsu * 100, "%"),\r
164                     Fleet2 = "計:" +\r
165                              "火" + CutOverFlow(total.FirePower) +\r
166                              " 空" + CutOverFlow(total.AntiAir) +\r
167                              " 潜" + CutOverFlow(total.AntiSubmarine) +\r
168                              " 索" + CutOverFlow(total.LoS) + "\r\n" +\r
169                              $"戦闘:燃{total.Fuel / 5}弾{total.Bull / 5} 支援:燃{total.Fuel / 2}弾{(int)(total.Bull * 0.8)}" +\r
170                              (sniffer.CombinedFleetType != 0 && f == 1 ? "" : $"\r\nTP:S{(int)tp} A{(int)(tp * 0.7)}")\r
171                 });\r
172                 list.AddRange(ships);\r
173             }\r
174             if (sniffer.BaseAirCorps != null)\r
175             {\r
176                 var name = new[] {"第一", "第二", "第三"};\r
177                 foreach (var baseInfo in sniffer.BaseAirCorps)\r
178                 {\r
179                     list.Add(new Record {Fleet = baseInfo.AreaName + " 基地航空隊"});\r
180                     var i = 0;\r
181                     foreach (var airCorps in baseInfo.AirCorps)\r
182                     {\r
183                         if (i >= name.Length)\r
184                             break;\r
185                         var corpsFp = airCorps.FighterPower;\r
186                         string spec;\r
187                         string spec2;\r
188                         if (airCorps.Action == 2)\r
189                         {\r
190                             spec = "制空:" + RangeString(corpsFp.Interception);\r
191                             spec2 = corpsFp.IsInterceptor ? "制空(出撃):" + RangeString(corpsFp.AirCombat) : "";\r
192                         }\r
193                         else\r
194                         {\r
195                             spec = "制空:" + RangeString(corpsFp.AirCombat);\r
196                             spec2 = corpsFp.IsInterceptor ? "制空(防空):" + RangeString(corpsFp.Interception) : "";\r
197                         }\r
198                         var cost = airCorps.CostForSortie;\r
199                         list.Add(new Record\r
200                         {\r
201                             Ship = name[i++] + " " + airCorps.ActionName,\r
202                             Ship2 = $"出撃コスト:燃{cost[0]}弾{cost[1]}",\r
203                             Spec = spec + " 距離:" + airCorps.Distance,\r
204                             Spec2 = spec2\r
205                         });\r
206                         list.AddRange(airCorps.Planes.Select(plane =>\r
207                         {\r
208                             var planeFp = plane.FighterPower;\r
209                             return new Record\r
210                             {\r
211                                 Equip = plane.State != 1 ? plane.StateName : GenEquipString(plane.Slot),\r
212                                 Spec = plane.State != 1 ? "" : $"+{plane.Slot.Alv} {plane.Count}/{plane.MaxCount}",\r
213                                 AircraftSpec =\r
214                                     $"距離:{plane.Slot.Spec.Distance} 制空:{RangeString(planeFp.AirCombat)}" +\r
215                                     (planeFp.IsInterceptor ? $" 防空:{RangeString(planeFp.Interception)}" : ""),\r
216                                 Color = plane.Slot.Spec.Color\r
217                             };\r
218                         }));\r
219                     }\r
220                 }\r
221             }\r
222             _table = list.ToArray();\r
223         }\r
224 \r
225         private string RangeString(int[] fp) => fp[0] == fp[1] ? fp[0].ToString() : $"{fp[0]}~{fp[1]}";\r
226 \r
227         private int CutOverFlow(int value) => value > 999 ? 999 : value;\r
228 \r
229         private string HideIfZero(string name, double value, string suffix = "")\r
230         {\r
231             return value > 0 ? name + value.ToString("f1") + suffix : "";\r
232         }\r
233 \r
234         private string HideIfZero(string name, int value, string suffix = "")\r
235         {\r
236             return value > 0 ? name + value + suffix : "";\r
237         }\r
238 \r
239         private string GenEquipString(ItemStatus item)\r
240         {\r
241             var name = item.Spec.Name;\r
242             var attr = item.Level == 0 ? "" : "★" + item.Level;\r
243             var proposed = new Size(int.MaxValue, int.MaxValue);\r
244             var maxWidth = item.Spec.IsAircraft ? 132 : 180;\r
245             var result = name + attr;\r
246             if (TextRenderer.MeasureText(result, Font, proposed).Width <= maxWidth)\r
247                 return result;\r
248             var truncated = "";\r
249             foreach (var ch in name)\r
250             {\r
251                 var tmp = truncated + ch;\r
252                 if (TextRenderer.MeasureText(tmp + attr, Font, proposed).Width > maxWidth)\r
253                     break;\r
254                 truncated = tmp;\r
255             }\r
256             return truncated + attr;\r
257         }\r
258 \r
259         private void CreateLabels()\r
260         {\r
261             for (var i = _labelList.Count; i < _table.Length; i++)\r
262                 CreateLabels(i);\r
263         }\r
264 \r
265         private class FleetLabels : IEnumerable<ShipLabel>\r
266         {\r
267             public ShipLabel Fleet { get; set; }\r
268             public ShipLabel Name { get; set; }\r
269             public ShipLabel Equip { get; set; }\r
270             public ShipLabel EquipColor { get; set; }\r
271             public ShipLabel Spec { get; set; }\r
272 \r
273             public IEnumerator<ShipLabel> GetEnumerator() =>\r
274                 ((IEnumerable<ShipLabel>)new[] {Fleet, Name, Equip, EquipColor, Spec}).GetEnumerator();\r
275 \r
276             IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();\r
277         }\r
278 \r
279         private void CreateLabels(int i)\r
280         {\r
281             var y = 1 + LineHeight * i;\r
282             var lbp = new Panel\r
283             {\r
284                 Location = new Point(0, y),\r
285                 Size = new Size(ListForm.PanelWidth, LineHeight),\r
286                 BackColor = ShipLabel.ColumnColors[(i + 1) % 2],\r
287                 Visible = false\r
288             };\r
289             lbp.Scale(ShipLabel.ScaleFactor);\r
290             lbp.Tag = lbp.Location.Y;\r
291             var labels = new FleetLabels\r
292             {\r
293                 Fleet = new ShipLabel {Location = new Point(1, 2), AutoSize = true},\r
294                 Name = new ShipLabel {Location = new Point(10, 2), AutoSize = true},\r
295                 Equip = new ShipLabel {Location = new Point(38, 2), AutoSize = true},\r
296                 EquipColor = new ShipLabel {Location = new Point(35, 2), Size = new Size(4, LabelHeight - 2)},\r
297                 Spec = new ShipLabel {Location = new Point(217, 2), AutoSize = true, AnchorRight = true}\r
298             };\r
299             _labelList.Add(labels);\r
300             _panelList.Add(lbp);\r
301             lbp.Controls.AddRange(labels.Cast<Control>().ToArray());\r
302             Controls.Add(lbp);\r
303             foreach (var label in labels)\r
304             {\r
305                 label.Scale();\r
306                 label.PresetColor =\r
307                     label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];\r
308             }\r
309         }\r
310 \r
311         private void SetRecords()\r
312         {\r
313             for (var i = 0; i < _table.Length; i++)\r
314                 SetRecord(i);\r
315             for (var i = _table.Length; i < _labelList.Count; i++)\r
316                 _panelList[i].Visible = false;\r
317         }\r
318 \r
319         private void SetRecord(int i)\r
320         {\r
321             var lbp = _panelList[i];\r
322             if (!lbp.Visible)\r
323                 lbp.Location = new Point(lbp.Left, (int)lbp.Tag + AutoScrollPosition.Y);\r
324             var e = _table[i];\r
325             var labels = _labelList[i];\r
326             labels.Fleet.Text = e.Fleet;\r
327             labels.Name.SetName(e.Ship);\r
328             if (e.Ship2 != "")\r
329                 _toolTip.SetToolTip(labels.Name, e.Ship2);\r
330             labels.Equip.Text = e.Equip;\r
331             labels.EquipColor.Visible = e.Equip != "";\r
332             labels.EquipColor.BackColor = e.Color;\r
333             labels.Spec.Text = e.Spec;\r
334             if (e.Fleet != "" && e.Fleet2 != "")\r
335                 _toolTip.SetToolTip(labels.Fleet, e.Fleet2);\r
336             _toolTip.SetToolTip(labels.Equip, e.AircraftSpec != "" ? e.AircraftSpec : "");\r
337             _toolTip.SetToolTip(labels.Spec, e.Spec2 != "" ? e.Spec2 : "");\r
338             lbp.Visible = true;\r
339         }\r
340 \r
341         public void ShowShip(int id)\r
342         {\r
343             var i = Array.FindIndex(_table, e => e.Id == id);\r
344             if (i == -1)\r
345                 return;\r
346             var y = (int)Math.Round(ShipLabel.ScaleFactor.Height * LineHeight * i);\r
347             AutoScrollPosition = new Point(0, y);\r
348         }\r
349 \r
350         public void ShowFleet(string fn)\r
351         {\r
352             var i = Array.FindIndex(_table, e => e.Fleet.StartsWith(fn));\r
353             if (i == -1)\r
354                 return;\r
355             var y = (int)Math.Round(ShipLabel.ScaleFactor.Height * LineHeight * i);\r
356             AutoScrollPosition = new Point(0, y);\r
357         }\r
358 \r
359         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)\r
360         {\r
361             base.ScaleControl(factor, specified);\r
362             if (factor.Height > 1)\r
363                 _toolTip.Font = new Font(_toolTip.Font.FontFamily, _toolTip.Font.Size * factor.Height);\r
364         }\r
365     }\r
366 }