1717// DisplayFrame — data the main thread renders in the debug overlay
1818// ─────────────────────────────────────────────────────────────────────────────
1919
20- struct DisplayFrame {
21- cv::Mat bgr;
20+ struct DisplayFrame
21+ {
22+ cv::Mat bgr;
2223 std::vector<cv::Point> contour;
23- int tx_f{0 }, ty_f{0 };
24- float dist{0 .f };
25- float strength{0 .f };
26- int move_dx{0 }, move_dy{0 };
27- bool has_target{false };
28- bool triggerbot_fired{false };
24+ int tx_f{0 }, ty_f{0 };
25+ float dist{0 .f };
26+ float strength{0 .f };
27+ int move_dx{0 }, move_dy{0 };
28+ bool has_target{false };
29+ bool triggerbot_fired{false };
2930};
3031
3132// ─────────────────────────────────────────────────────────────────────────────
3233// Contour scoring
3334// ─────────────────────────────────────────────────────────────────────────────
3435
35- inline double score_contour (const std::vector<cv::Point>& cnt,
36- double ref_x, double ref_y,
37- int min_area, int max_area)
36+ inline double score_contour (const std::vector<cv::Point> & cnt,
37+ double ref_x, double ref_y,
38+ int min_area, int max_area)
3839{
3940 double area = cv::contourArea (cnt);
40- if (area < min_area || area > max_area) return 1e18 ;
41+ if (area < min_area || area > max_area)
42+ return 1e18 ;
4143 cv::Moments M = cv::moments (cnt);
42- if (M.m00 == 0.0 ) return 1e18 ;
44+ if (M.m00 == 0.0 )
45+ return 1e18 ;
4346 double tx = M.m10 / M.m00 ;
4447 double ty = M.m01 / M.m00 ;
4548 double dx = tx - ref_x, dy = ty - ref_y;
46- return (dx* dx + dy* dy) / (area + 1.0 );
49+ return (dx * dx + dy * dy) / (area + 1.0 );
4750}
4851
4952// ─────────────────────────────────────────────────────────────────────────────
@@ -52,38 +55,47 @@ inline double score_contour(const std::vector<cv::Point>& cnt,
5255
5356static constexpr int LOST_THRESH = 4 ;
5457
55- class AimThread {
58+ class AimThread
59+ {
5660public:
57- AimThread (const Config& cfg,
58- Mouse& mouse,
59- TripleBuffer<Frame>& frame_buf,
60- TripleBuffer<DisplayFrame>& disp_buf,
61- std::atomic<bool >& running,
62- std::atomic<bool >& enabled,
63- std::atomic<bool >& debug,
64- std::atomic<bool >& btn_held,
65- std::atomic<bool >& triggerbot)
61+ AimThread (const Config & cfg,
62+ Mouse & mouse,
63+ TripleBuffer<Frame> & frame_buf,
64+ TripleBuffer<DisplayFrame> & disp_buf,
65+ std::atomic<bool > & running,
66+ std::atomic<bool > & enabled,
67+ std::atomic<bool > & debug,
68+ std::atomic<bool > & btn_held,
69+ std::atomic<bool > & triggerbot)
6670 : cfg_(cfg), mouse_(mouse),
6771 frame_buf_ (frame_buf), disp_buf_(disp_buf),
6872 running_(running), enabled_(enabled), debug_(debug),
6973 btn_held_(btn_held), triggerbot_(triggerbot) {}
7074
7175 void start () { thread_ = std::thread (&AimThread::loop, this ); }
72- void join () { if (thread_.joinable ()) thread_.join (); }
76+ void join ()
77+ {
78+ if (thread_.joinable ())
79+ thread_.join ();
80+ }
7381
7482private:
7583 using clk = std::chrono::steady_clock;
7684
77- void loop () {
85+ void loop ()
86+ {
7887 Frame frame;
7988
80- while (running_.load (std::memory_order_relaxed)) {
81- if (!frame_buf_.consume (frame) || !frame.valid ) {
89+ while (running_.load (std::memory_order_relaxed))
90+ {
91+ if (!frame_buf_.consume (frame) || !frame.valid )
92+ {
8293 std::this_thread::sleep_for (std::chrono::microseconds (500 ));
8394 continue ;
8495 }
8596
86- if (!enabled_.load () || !btn_held_.load ()) {
97+ if (!enabled_.load () || !btn_held_.load ())
98+ {
8799 vel_x_ *= cfg_.smoothness ;
88100 vel_y_ *= cfg_.smoothness ;
89101 push_display (frame, {}, 0 , 0 , 0 .f , 0 .f , 0 , 0 , false , false );
@@ -102,21 +114,28 @@ class AimThread {
102114 double ref_y = has_history_ ? last_ty_ : fc_y;
103115
104116 double best_score = 1e18 ;
105- int best_idx = -1 ;
106- for (int i = 0 ; i < (int )contours.size (); ++i) {
117+ int best_idx = -1 ;
118+ for (int i = 0 ; i < (int )contours.size (); ++i)
119+ {
107120 double s = score_contour (contours[i], ref_x, ref_y,
108121 cfg_.min_area , cfg_.max_area );
109- if (s < best_score) { best_score = s; best_idx = i; }
122+ if (s < best_score)
123+ {
124+ best_score = s;
125+ best_idx = i;
126+ }
110127 }
111128
112- if (best_idx < 0 ) {
129+ if (best_idx < 0 )
130+ {
113131 vel_x_ *= cfg_.smoothness ;
114132 vel_y_ *= cfg_.smoothness ;
115133 ++lost_frames_;
116- if (lost_frames_ >= LOST_THRESH ) {
134+ if (lost_frames_ >= LOST_THRESH )
135+ {
117136 has_history_ = false ;
118137 lost_frames_ = 0 ;
119- prev_valid_ = false ;
138+ prev_valid_ = false ;
120139 }
121140 push_display (frame, {}, 0 , 0 , 0 .f , 0 .f , 0 , 0 , false , false );
122141 continue ;
@@ -129,19 +148,24 @@ class AimThread {
129148 double ty_f = M.m01 / M.m00 ;
130149
131150 // ── Velocity EMA ──────────────────────────────────────────────
132- if (!prev_valid_) {
133- prev_tx_ = tx_f; prev_ty_ = ty_f;
151+ if (!prev_valid_)
152+ {
153+ prev_tx_ = tx_f;
154+ prev_ty_ = ty_f;
134155 vel_x_ = vel_y_ = 0.0 ;
135156 prev_valid_ = true ;
136- } else {
137- double s = cfg_.smoothness ;
157+ }
158+ else
159+ {
160+ double s = cfg_.smoothness ;
138161 vel_x_ = s * vel_x_ + (1.0 - s) * (tx_f - prev_tx_);
139162 vel_y_ = s * vel_y_ + (1.0 - s) * (ty_f - prev_ty_);
140- prev_tx_ = tx_f; prev_ty_ = ty_f;
163+ prev_tx_ = tx_f;
164+ prev_ty_ = ty_f;
141165 }
142166
143- last_tx_ = tx_f;
144- last_ty_ = ty_f;
167+ last_tx_ = tx_f;
168+ last_ty_ = ty_f;
145169 has_history_ = true ;
146170
147171 // ── Lead prediction ───────────────────────────────────────────
@@ -150,7 +174,7 @@ class AimThread {
150174
151175 double raw_dx = pred_x - fc_x;
152176 double raw_dy = pred_y - fc_y;
153- double dist = std::hypot (raw_dx, raw_dy);
177+ double dist = std::hypot (raw_dx, raw_dy);
154178
155179 // ── Triggerbot ────────────────────────────────────────────────
156180 // Fires a click when the crosshair is within trigger_dist pixels
@@ -159,26 +183,30 @@ class AimThread {
159183 // Triggerbot is independent of aim movement — it still fires even
160184 // if dist < 0.5 (already on target, no mouse move needed).
161185 bool tb_fired = false ;
162- if (triggerbot_.load () && dist <= cfg_.trigger_dist ) {
186+ if (triggerbot_.load () && dist <= cfg_.trigger_dist )
187+ {
163188 auto now = clk::now ();
164189 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
165- now - last_click_).count ();
166- if (elapsed >= cfg_.trigger_delay ) {
190+ now - last_click_)
191+ .count ();
192+ if (elapsed >= cfg_.trigger_delay )
193+ {
167194 mouse_.left_click ();
168195 last_click_ = now;
169196 tb_fired = true ;
170197 }
171198 }
172199
173- if (dist < 0.5 ) {
200+ if (dist < 0.5 )
201+ {
174202 push_display (frame, contours[best_idx],
175203 (int )tx_f, (int )ty_f, (float )dist, 0 .f ,
176204 0 , 0 , true , tb_fired);
177205 continue ;
178206 }
179207
180208 // ── Strength lerp ─────────────────────────────────────────────
181- double t = std::min (dist / std::max (cfg_.fov , 1 ), 1.0 );
209+ double t = std::min (dist / std::max (cfg_.fov , 1 ), 1.0 );
182210 double strength = cfg_.min_strength +
183211 t * (cfg_.max_strength - cfg_.min_strength );
184212
@@ -196,49 +224,50 @@ class AimThread {
196224 }
197225 }
198226
199- void push_display (const Frame& frame,
200- const std::vector<cv::Point>& cnt,
227+ void push_display (const Frame & frame,
228+ const std::vector<cv::Point> & cnt,
201229 int tx, int ty,
202230 float dist, float strength,
203231 int dx, int dy,
204232 bool has_target, bool triggerbot_fired)
205233 {
206- if (!debug_.load ()) return ;
207- DisplayFrame& slot = disp_buf_.write_slot ();
208- slot.bgr = frame.bgr .clone ();
209- slot.contour = cnt;
210- slot.tx_f = tx;
211- slot.ty_f = ty;
212- slot.dist = dist;
213- slot.strength = strength;
214- slot.move_dx = dx;
215- slot.move_dy = dy;
216- slot.has_target = has_target;
234+ if (!debug_.load ())
235+ return ;
236+ DisplayFrame &slot = disp_buf_.write_slot ();
237+ slot.bgr = frame.bgr .clone ();
238+ slot.contour = cnt;
239+ slot.tx_f = tx;
240+ slot.ty_f = ty;
241+ slot.dist = dist;
242+ slot.strength = strength;
243+ slot.move_dx = dx;
244+ slot.move_dy = dy;
245+ slot.has_target = has_target;
217246 slot.triggerbot_fired = triggerbot_fired;
218247 disp_buf_.publish ();
219248 }
220249
221250 // ── Config + external refs ────────────────────────────────────────────
222- const Config& cfg_;
223- Mouse& mouse_;
224- TripleBuffer<Frame>& frame_buf_;
225- TripleBuffer<DisplayFrame>& disp_buf_;
226- std::atomic<bool >& running_;
227- std::atomic<bool >& enabled_;
228- std::atomic<bool >& debug_;
229- std::atomic<bool >& btn_held_;
230- std::atomic<bool >& triggerbot_;
231- std::thread thread_;
251+ const Config & cfg_;
252+ Mouse & mouse_;
253+ TripleBuffer<Frame> & frame_buf_;
254+ TripleBuffer<DisplayFrame> & disp_buf_;
255+ std::atomic<bool > & running_;
256+ std::atomic<bool > & enabled_;
257+ std::atomic<bool > & debug_;
258+ std::atomic<bool > & btn_held_;
259+ std::atomic<bool > & triggerbot_;
260+ std::thread thread_;
232261
233262 // ── Velocity state ────────────────────────────────────────────────────
234- bool prev_valid_{false };
263+ bool prev_valid_{false };
235264 double prev_tx_{0 }, prev_ty_{0 };
236265 double vel_x_{0 }, vel_y_{0 };
237266
238267 // ── Multi-target tracking state ───────────────────────────────────────
239- bool has_history_{false };
268+ bool has_history_{false };
240269 double last_tx_{0 }, last_ty_{0 };
241- int lost_frames_{0 };
270+ int lost_frames_{0 };
242271
243272 // ── Triggerbot cooldown ───────────────────────────────────────────────
244273 clk::time_point last_click_{clk::now ()};
0 commit comments