1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
1 |
|
from il2fb.commons.spatial import Point2D |
4
|
1 |
|
from il2fb.commons.targets import TargetTypes, TargetPriorities |
5
|
|
|
|
6
|
1 |
|
from ..converters import to_bool |
7
|
1 |
|
from . import CollectingParser |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
def to_destruction_level(value): |
11
|
1 |
|
return int(value) / 10 |
12
|
|
|
|
13
|
|
|
|
14
|
1 |
|
class TargetSectionParser(CollectingParser): |
15
|
|
|
""" |
16
|
|
|
Parses ``Target`` section. |
17
|
|
|
View :ref:`detailed description <target-section>`. |
18
|
|
|
""" |
19
|
|
|
|
20
|
1 |
|
def check_section_name(self, section_name): |
21
|
1 |
|
return section_name == "Target" |
22
|
|
|
|
23
|
1 |
|
def parse_line(self, line): |
24
|
1 |
|
params = line.split() |
25
|
|
|
|
26
|
1 |
|
type_code, priority, in_sleep_mode, delay = params[:4] |
27
|
1 |
|
params = params[4:] |
28
|
|
|
|
29
|
1 |
|
target_type = TargetTypes.get_by_value(int(type_code)) |
30
|
1 |
|
target = { |
31
|
|
|
'type': target_type, |
32
|
|
|
'priority': TargetPriorities.get_by_value(int(priority)), |
33
|
|
|
'in_sleep_mode': to_bool(in_sleep_mode), |
34
|
|
|
'delay': int(delay), |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
subparser = self._subparsers.get(target_type) |
38
|
1 |
|
if subparser is not None: |
39
|
1 |
|
target.update(subparser(params)) |
40
|
|
|
|
41
|
1 |
|
self.data.append(target) |
42
|
|
|
|
43
|
1 |
|
def parse_destroy_or_cover_or_escort(params): |
44
|
|
|
""" |
45
|
|
|
Parse extra parameters for targets with type 'destroy' or 'cover' or |
46
|
|
|
'escort'. |
47
|
|
|
""" |
48
|
1 |
|
destruction_level = to_destruction_level(params[0]) |
49
|
1 |
|
pos, waypoint, object_code = params[1:3], params[4], params[5] |
50
|
1 |
|
object_pos = params[6:8] |
51
|
1 |
|
return { |
52
|
|
|
'destruction_level': destruction_level, |
53
|
|
|
'pos': Point2D(*pos), |
54
|
|
|
'object': { |
55
|
|
|
'waypoint': int(waypoint), |
56
|
|
|
'id': object_code, |
57
|
|
|
'pos': Point2D(*object_pos), |
58
|
|
|
}, |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
def parse_destroy_or_cover_bridge(params): |
62
|
|
|
""" |
63
|
|
|
Parse extra parameters for targets with type 'destroy bridge' or |
64
|
|
|
'cover bridge'. |
65
|
|
|
""" |
66
|
1 |
|
pos, object_code, object_pos = params[1:3], params[5], params[6:8] |
67
|
1 |
|
return { |
68
|
|
|
'pos': Point2D(*pos), |
69
|
|
|
'object': { |
70
|
|
|
'id': object_code, |
71
|
|
|
'pos': Point2D(*object_pos), |
72
|
|
|
}, |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
def parse_destroy_or_cover_area(params): |
76
|
|
|
""" |
77
|
|
|
Parse extra parameters for targets with type 'destroy area' or |
78
|
|
|
'cover area'. |
79
|
|
|
""" |
80
|
1 |
|
destruction_level = to_destruction_level(params[0]) |
81
|
1 |
|
pos_x, pos_y, radius = params[1:] |
82
|
1 |
|
return { |
83
|
|
|
'destruction_level': destruction_level, |
84
|
|
|
'pos': Point2D(pos_x, pos_y), |
85
|
|
|
'radius': int(radius), |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
def parse_recon(params): |
89
|
|
|
""" |
90
|
|
|
Parse extra parameters for targets with 'recon' type. |
91
|
|
|
""" |
92
|
1 |
|
requires_landing = params[0] != '500' |
93
|
1 |
|
pos, radius, params = params[1:3], params[3], params[4:] |
94
|
1 |
|
data = { |
95
|
|
|
'radius': int(radius), |
96
|
|
|
'requires_landing': requires_landing, |
97
|
|
|
'pos': Point2D(*pos), |
98
|
|
|
} |
99
|
1 |
|
if params: |
100
|
1 |
|
waypoint, object_code = params[:2] |
101
|
1 |
|
object_pos = params[2:] |
102
|
1 |
|
data['object'] = { |
103
|
|
|
'waypoint': int(waypoint), |
104
|
|
|
'id': object_code, |
105
|
|
|
'pos': Point2D(*object_pos), |
106
|
|
|
} |
107
|
1 |
|
return data |
108
|
|
|
|
109
|
1 |
|
_subparsers = { |
110
|
|
|
TargetTypes.destroy: parse_destroy_or_cover_or_escort, |
111
|
|
|
TargetTypes.destroy_bridge: parse_destroy_or_cover_bridge, |
112
|
|
|
TargetTypes.destroy_area: parse_destroy_or_cover_area, |
113
|
|
|
TargetTypes.recon: parse_recon, |
114
|
|
|
TargetTypes.escort: parse_destroy_or_cover_or_escort, |
115
|
|
|
TargetTypes.cover: parse_destroy_or_cover_or_escort, |
116
|
|
|
TargetTypes.cover_area: parse_destroy_or_cover_area, |
117
|
|
|
TargetTypes.cover_bridge: parse_destroy_or_cover_bridge, |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
def clean(self): |
121
|
|
|
return {'targets': self.data, } |
122
|
|
|
|