Skip to content

Commit 0b90770

Browse files
committed
removed some redundant elses and map() func and more efficient _plus_ and _minus_
1 parent 3a6da29 commit 0b90770

File tree

3 files changed

+23
-44
lines changed

3 files changed

+23
-44
lines changed

spwn-lang/libraries/std/counter.spwn

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ impl @counter {
4343
type: @counter,
4444
item: id
4545
}
46-
} else {
47-
throw "Can not make counter from this type"
4846
}
4947

5048
},
@@ -55,8 +53,8 @@ points.display(75, 75)
5553
")]
5654
(
5755
self,
58-
#[desc("X pos of display in units (1 grid square = 30 units)")] x,
59-
#[desc("Y pos of display in units")] y
56+
#[desc("X pos of display in units (1 grid square = 30 units)")] x: @number,
57+
#[desc("Y pos of display in units")] y: @number
6058
) {
6159
extract import "constants.spwn".obj_props
6260
$.add(obj {
@@ -81,7 +79,7 @@ a.add_to(b)
8179
self,
8280
#[desc("Counter(s) to add to")] items: [@counter | @item] | @counter | @item,
8381
#[desc("Speed of operation (higher number increases group usage)")] speed: @number = DEFAULT_SPEED,
84-
#[desc("Multiplyer for the value added")] factor: @number = 1,
82+
#[desc("Multiplier for the value added")] factor: @number = 1,
8583
#[desc("Macro to be called for each decrease of the counter. Takes one argument representing the number the counter is being decreased by (if speed = 1 this will always be 1)")] for_each: @macro = (n){}
8684
) {
8785
wait()
@@ -218,8 +216,6 @@ c.multiply(10)
218216
wait()
219217
temp.add_to([self.item], speed)
220218

221-
} else {
222-
throw "Cannot multiply counter by " + factor.type as @string
223219
}
224220
},
225221

@@ -332,11 +328,7 @@ c.divide(2, remainder = r)
332328

333329

334330

335-
} else {
336-
throw "Cannot divide counter by " + divisor.type as @string
337331
}
338-
339-
340332
},
341333

342334
//will consume both numbers
@@ -472,10 +464,8 @@ c2 = c1 + 10
472464
-> return new_counter
473465
} else if other.type == @counter {
474466
new_counter = self.clone()
475-
other.clone().add_to([new_counter.item])
467+
other.copy_to([new_counter.item], factor = 1)
476468
-> return new_counter
477-
} else {
478-
throw "Cannot add counter with " + num.type as @string
479469
}
480470
},
481471

@@ -489,8 +479,10 @@ c2 = c1 - 3
489479
new_counter = self.clone()
490480
new_counter.add(-other)
491481
-> return new_counter
492-
} else {
493-
throw "Cannot subtract counter with " + num.type as @string
482+
} else if other.type == @counter {
483+
new_counter = self.clone()
484+
other.copy_to([new_counter.item], factor = -1)
485+
-> return new_counter
494486
}
495487
},
496488

@@ -562,8 +554,6 @@ more = c > 10
562554
other_clone = other.clone()
563555
cmp = self_clone.compare(other_clone)
564556
-> return cmp == 1
565-
} else {
566-
throw "Cannot compare counter with " + other.type as @string
567557
}
568558
},
569559

@@ -588,8 +578,6 @@ less = c < 42
588578
other_clone = other.clone()
589579
cmp = self_clone.compare(other_clone)
590580
-> return cmp == -1
591-
} else {
592-
throw "Cannot compare counter with " + other.type as @string
593581
}
594582
},
595583

@@ -614,8 +602,6 @@ more_or_eq = c >= 10
614602
other_clone = other.clone()
615603
cmp = self_clone.compare(other_clone)
616604
-> return cmp == 1 || cmp == 0
617-
} else {
618-
throw "Cannot compare counter with " + other.type as @string
619605
}
620606
},
621607

@@ -640,8 +626,6 @@ less_or_eq = c <= 42
640626
other_clone = other.clone()
641627
cmp = self_clone.compare(other_clone)
642628
-> return cmp == -1 || cmp == 0
643-
} else {
644-
throw "Cannot compare counter with " + other.type as @string
645629
}
646630
},
647631

@@ -668,8 +652,6 @@ eq = c == 42
668652
other_clone = other.clone()
669653
cmp = self_clone.compare(other_clone)
670654
-> return cmp == 0
671-
} else {
672-
throw "Cannot compare counter with " + other.type as @string
673655
}
674656
},
675657

@@ -700,8 +682,6 @@ c += 10
700682
self.add(num)
701683
} else if num.type == @counter {
702684
num.clone().add_to([self])
703-
} else {
704-
throw "Cannot add " + num.type as @string + " to counter"
705685
}
706686
},
707687

@@ -732,8 +712,6 @@ c -= 5
732712

733713
} else if num.type == @counter {
734714
num.clone().subtract_from([self])
735-
} else {
736-
throw "Cannot subtract " + num.type as @string + " from counter"
737715
}
738716
},
739717

@@ -747,8 +725,6 @@ c *= 6
747725
self.multiply(num)
748726
} else if num.type == @counter {
749727
self.multiply(num)
750-
} else {
751-
throw "Cannot multiply counter by " + num.type as @string
752728
}
753729
},
754730

@@ -761,8 +737,6 @@ c /= 6
761737
self.divide(num)
762738
} else if num.type == @counter {
763739
self.divide(num)
764-
} else {
765-
throw "Cannot divide counter by " + num.type as @string
766740
}
767741
},
768742

@@ -778,8 +752,6 @@ c = 42
778752
}
779753
} else if num.type == @counter {
780754
num.copy_to([self])
781-
} else {
782-
throw "Cannot assign" + num.type as @string + " to counter"
783755
}
784756
},
785757

@@ -791,14 +763,10 @@ c <=> c2
791763
// c is now 42, c2 is now 23
792764
")]
793765
(self, num: @counter) {
794-
if num.type == @counter {
795-
swap_tmp = @counter::new();
796-
self.add_to(swap_tmp)
797-
num.add_to(self)
798-
swap_tmp.add_to(num)
799-
}else {
800-
throw "Cannot swap counter with " + num.type as @string
801-
}
766+
swap_tmp = @counter::new();
767+
self.add_to(swap_tmp)
768+
num.add_to(self)
769+
swap_tmp.add_to(num)
802770
},
803771

804772
to_const: #[desc("Converts the counter into a normal number (very context-splitting, be careful)") example("

spwn-lang/libraries/std/lib.spwn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import "string.spwn"
1414
import "counter.spwn"
1515
import "fileio.spwn"
1616
import "regex.spwn"
17+
import "number.spwn"
1718

1819
general = import "general_triggers.spwn"
1920
events = import "events.spwn"

spwn-lang/libraries/std/number.spwn

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[no_std]
2+
3+
impl @number {
4+
map: #[desc("Maps a number linearily from one interval to another") example("$.assert(2.map(1, 4, 5, 11) == 7)")]
5+
(self, istart: @number, istop: @number, ostart: @number, ostop: @number) {
6+
return ostart + (ostop - ostart) * ((self - istart) / (istop - istart));
7+
}
8+
}
9+
10+

0 commit comments

Comments
 (0)