1212class SaberBase ;
1313extern SaberBase* saberbases;
1414
15-
1615// An effect is a one-shot trigger for something
1716// Effects may have a location, but do not generally have a "duration".
1817// However, durations may be derived from blade styles or sound effect durations
@@ -34,7 +33,7 @@ extern SaberBase* saberbases;
3433 DEFINE_EFFECT(IGNITION) \
3534 DEFINE_EFFECT(RETRACTION) \
3635 DEFINE_EFFECT(CHANGE) \
37- /* just for resetting things, not meant for triggering effects, use NEWFONT/BLADEIN/BLADEOUT instead */ \
36+ /* just for resetting things, not meant for triggering effects, use NEWFONT/BLADEIN/BLADEOUT instead */ \
3837 DEFINE_EFFECT(CHDIR) \
3938 DEFINE_EFFECT(NEWFONT) \
4039 DEFINE_EFFECT(BLADEIN) \
@@ -46,13 +45,13 @@ extern SaberBase* saberbases;
4645 DEFINE_EFFECT(ACCENT_SWING) \
4746 DEFINE_EFFECT(ACCENT_SLASH) \
4847 DEFINE_EFFECT(SPIN) \
49- /* Allows style to turn blade ON for interactive effects if prop/style support, FAST_ON skips PREON. */ \
48+ /* Allows style to turn blade ON for interactive effects if prop/style support, FAST_ON skips PREON. */ \
5049 DEFINE_EFFECT(ON) \
5150 DEFINE_EFFECT(FAST_ON) \
5251 DEFINE_EFFECT(QUOTE) \
5352 DEFINE_EFFECT(SECONDARY_IGNITION) \
5453 DEFINE_EFFECT(SECONDARY_RETRACTION) \
55- /* Allows style to turn blade OFF for interactive effects if prop/style support, FAST_OFF skips POSTOFF. */ \
54+ /* Allows style to turn blade OFF for interactive effects if prop/style support, FAST_OFF skips POSTOFF. */ \
5655 DEFINE_EFFECT(OFF) \
5756 DEFINE_EFFECT(FAST_OFF) \
5857 DEFINE_EFFECT(OFF_CLASH) \
@@ -172,18 +171,23 @@ class BladeSet {
172171 constexpr BladeSet operator |(const BladeSet& other) const {
173172 return BladeSet (blades_ | other.blades_ );
174173 }
174+
175175 constexpr BladeSet operator &(const BladeSet& other) const {
176176 return BladeSet (blades_ & other.blades_ );
177177 }
178+
178179 constexpr BladeSet operator ~() const {
179180 return BladeSet (~blades_);
180181 }
182+
181183 constexpr bool operator [](int blade) const {
182184 return (blades_ & (1 << blade)) != 0 ;
183185 }
186+
184187 constexpr bool off () const {
185188 return blades_ == 0 ;
186189 }
190+
187191 // Should this be if "other" is on, or if anything is on?
188192 // Right now it's if anything is on.
189193 constexpr bool on () const {
@@ -196,6 +200,7 @@ class BladeSet {
196200 void operator |=(const BladeSet& other) {
197201 blades_ |= other.blades_ ;
198202 }
203+
199204 void operator &=(const BladeSet& other) {
200205 blades_ &= other.blades_ ;
201206 }
@@ -229,9 +234,11 @@ class EffectLocation {
229234 static EffectLocation rnd () {
230235 return EffectLocation (65535 + random (22937 ), BladeSet::all ());
231236 }
237+
232238 static EffectLocation rnd (BladeSet blades) {
233239 return EffectLocation (65535 + random (22937 ), blades);
234240 }
241+
235242 BladeSet blades () const { return blades_; }
236243 bool on_blade (int bladenum) const { return blades_[bladenum]; }
237244 uint16_t fixed () const { return location_; }
@@ -241,6 +248,7 @@ class EffectLocation {
241248 p.write (' @' );
242249 blades_.printTo (p);
243250 }
251+
244252private:
245253 friend class SaberBase ;
246254 uint16_t location_;
@@ -256,7 +264,6 @@ struct BladeEffect {
256264 int wavnum;
257265};
258266
259-
260267extern int GetBladeNumber (BladeBase *blade);
261268
262269class SaberBase {
@@ -269,6 +276,7 @@ class SaberBase {
269276 interrupts ();
270277 CHECK_LL (SaberBase, saberbases, next_saber_);
271278 }
279+
272280 void Unlink (const SaberBase* x) {
273281 CHECK_LL (SaberBase, saberbases, next_saber_);
274282 for (SaberBase** i = &saberbases; *i; i = &(*i)->next_saber_ ) {
@@ -299,12 +307,14 @@ class SaberBase {
299307 // Right now it's if anything is on.
300308 return on_.on ();
301309 }
310+
302311 static bool BladeIsOn (int blade) { return on_[blade]; }
303312 static BladeSet OnBlades () { return on_; }
304313 static void TurnOn () {
305314 on_ = EffectLocation::ALL_BLADES;
306315 SaberBase::DoOn (0 );
307316 }
317+
308318 static void TurnOn (EffectLocation location) {
309319 PVLOG_DEBUG << " TurnOn " << location << " \n " ;
310320 // You can't turn on a blade that's already on.
@@ -315,11 +325,13 @@ class SaberBase {
315325 on_ |= location.blades ();
316326 SaberBase::DoOn (location);
317327 }
328+
318329 static void TurnOff (OffType off_type) {
319330 on_ = BladeSet ();
320331 last_motion_request_ = millis ();
321332 SaberBase::DoOff (off_type, 0 );
322333 }
334+
323335 static void TurnOff (OffType off_type, EffectLocation location) {
324336 PVLOG_DEBUG << " TurnOff " << location << " \n " ;
325337 location.blades_ &= on_; // can only turn off blades which are on
@@ -338,6 +350,7 @@ class SaberBase {
338350 return IsOn () || (millis () - last_motion_request_) < 20000 ;
339351#endif
340352 }
353+
341354 static void RequestMotion () {
342355 last_motion_request_ = millis ();
343356 }
@@ -357,13 +370,15 @@ class SaberBase {
357370 LOCKUP_MELT, // For cutting through doors...
358371 LOCKUP_LIGHTNING_BLOCK, // Lightning block lockup
359372 };
373+
360374 static LockupType lockup_;
361375 static BladeSet lockup_blades_;
362376 static LockupType Lockup () { return lockup_; }
363377 static LockupType LockupForBlade (int blade) {
364378 if (!lockup_blades_[blade]) return LOCKUP_NONE;
365379 return lockup_;
366380 }
381+
367382 static void SetLockup (LockupType lockup, BladeSet blades = BladeSet::all()) {
368383 lockup_ = lockup;
369384 lockup_blades_ = blades;
@@ -422,22 +437,24 @@ public: \
422437
423438 SABERBASEFUNCTIONS ();
424439
425-
426440private:
427441 static void DoEffectInternal2 (EffectType effect, EffectLocation location) {
428442 DoEffectInternal (effect, location);
429443 PushEffect (effect, location);
430444 }
445+
431446public:
432447 static void DoEffect (EffectType effect, EffectLocation location) {
433448 ClearSoundInfo ();
434449 DoEffectInternal2 (effect, location);
435450 }
451+
436452 static void DoOn (EffectLocation location) {
437453 ClearSoundInfo ();
438454 DoOnInternal (location);
439455 DoEffectInternal2 (EFFECT_IGNITION, location);
440456 }
457+
441458 static void DoOff (OffType off_type, EffectLocation location) {
442459 ClearSoundInfo ();
443460 DoOffInternal (off_type, location);
@@ -454,8 +471,8 @@ public: \
454471 // do nothing
455472 break ;
456473 }
457-
458474 }
475+
459476 static void DoBladeDetect (bool detected) {
460477 ClearSoundInfo ();
461478 if (detected) {
@@ -464,14 +481,17 @@ public: \
464481 DoEffect (EFFECT_BLADEOUT, 0 );
465482 }
466483 }
484+
467485 static void DoChange (ChangeType change_type) {
468486 ClearSoundInfo ();
469487 DoChangeInternal (change_type);
470488 }
489+
471490 static void DoTop (uint64_t total_cycles) {
472491 ClearSoundInfo ();
473492 DoTopInternal (total_cycles);
474493 }
494+
475495 static void DoIsOn (bool * on) {
476496 ClearSoundInfo ();
477497 DoIsOnInternal (on);
@@ -482,6 +502,7 @@ public: \
482502 sound_number = N;
483503 DoEffectInternal2 (e, location);
484504 }
505+
485506 static void DoEffectR (EffectType e) { DoEffect (e, EffectLocation::rnd ()); }
486507 static void DoBlast () { DoEffectR (EFFECT_BLAST); }
487508 static void DoForce () { DoEffectR (EFFECT_FORCE); }
@@ -509,12 +530,15 @@ public: \
509530 void SetClashStrength (float strength) {
510531 clash_strength_ = strength;
511532 }
533+
512534 static void DoClash () {
513535 DoEffectR (EFFECT_CLASH);
514536 }
537+
515538 static void DoStab () {
516539 DoEffect (EFFECT_STAB, 1 .0f );
517540 }
541+
518542 static void UpdateClashStrength (float strength) {
519543 if (strength > clash_strength_) {
520544 clash_strength_ = strength;
@@ -535,6 +559,7 @@ public: \
535559 }
536560 CHECK_LL (SaberBase, saberbases, next_saber_);
537561 }
562+
538563 virtual void SB_Motion (const Vec3& gyro, bool clear) {}
539564
540565 /* Acceleration in g */
@@ -548,6 +573,7 @@ public: \
548573 }
549574 CHECK_LL (SaberBase, saberbases, next_saber_);
550575 }
576+
551577 virtual void SB_Accel (const Vec3& gyro, bool clear) {}
552578
553579 static uint32_t GetCurrentVariation () {
@@ -559,6 +585,7 @@ public: \
559585 current_variation_ += delta;
560586 DoChange (CHANGE_COLOR);
561587 }
588+
562589 // For smooth updates or restore.
563590 static void SetVariation (uint32_t v) {
564591 current_variation_ = v;
@@ -597,7 +624,6 @@ public: \
597624 return num_effects_;
598625 }
599626
600-
601627 // Not private for debugging purposes only.
602628 static uint32_t last_motion_request_;
603629
@@ -644,7 +670,6 @@ public: \
644670 num_effects_ = std::min (num_effects_ + 1 , NELEM (effects_));
645671 }
646672
647-
648673 static size_t num_effects_;
649674 static BladeEffect effects_[10 ];
650675 static BladeSet on_;
@@ -663,4 +688,4 @@ constexpr BladeSet const EffectLocation::ALL_BLADES;
663688constexpr BladeSet const EffectLocation::MOST_BLADES;
664689ONCEPERBLADE (DECLARE_BLADE_BITS);
665690
666- #endif
691+ #endif // COMMON_SABER_BASE_H
0 commit comments