Skip to content

Commit

Permalink
Merge pull request #62 from FlowVix/master
Browse files Browse the repository at this point in the history
removed some redundant elses and map() func and more efficient _plus_…
  • Loading branch information
Spu7Nix authored Aug 1, 2021
2 parents 684e822 + 0b90770 commit 10ea3e3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 44 deletions.
56 changes: 12 additions & 44 deletions spwn-lang/libraries/std/counter.spwn
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ impl @counter {
type: @counter,
item: id
}
} else {
throw "Can not make counter from this type"
}

},
Expand All @@ -55,8 +53,8 @@ points.display(75, 75)
")]
(
self,
#[desc("X pos of display in units (1 grid square = 30 units)")] x,
#[desc("Y pos of display in units")] y
#[desc("X pos of display in units (1 grid square = 30 units)")] x: @number,
#[desc("Y pos of display in units")] y: @number
) {
extract import "constants.spwn".obj_props
$.add(obj {
Expand All @@ -81,7 +79,7 @@ a.add_to(b)
self,
#[desc("Counter(s) to add to")] items: [@counter | @item] | @counter | @item,
#[desc("Speed of operation (higher number increases group usage)")] speed: @number = DEFAULT_SPEED,
#[desc("Multiplyer for the value added")] factor: @number = 1,
#[desc("Multiplier for the value added")] factor: @number = 1,
#[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){}
) {
wait()
Expand Down Expand Up @@ -218,8 +216,6 @@ c.multiply(10)
wait()
temp.add_to([self.item], speed)

} else {
throw "Cannot multiply counter by " + factor.type as @string
}
},

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



} else {
throw "Cannot divide counter by " + divisor.type as @string
}


},

//will consume both numbers
Expand Down Expand Up @@ -472,10 +464,8 @@ c2 = c1 + 10
-> return new_counter
} else if other.type == @counter {
new_counter = self.clone()
other.clone().add_to([new_counter.item])
other.copy_to([new_counter.item], factor = 1)
-> return new_counter
} else {
throw "Cannot add counter with " + num.type as @string
}
},

Expand All @@ -489,8 +479,10 @@ c2 = c1 - 3
new_counter = self.clone()
new_counter.add(-other)
-> return new_counter
} else {
throw "Cannot subtract counter with " + num.type as @string
} else if other.type == @counter {
new_counter = self.clone()
other.copy_to([new_counter.item], factor = -1)
-> return new_counter
}
},

Expand Down Expand Up @@ -562,8 +554,6 @@ more = c > 10
other_clone = other.clone()
cmp = self_clone.compare(other_clone)
-> return cmp == 1
} else {
throw "Cannot compare counter with " + other.type as @string
}
},

Expand All @@ -588,8 +578,6 @@ less = c < 42
other_clone = other.clone()
cmp = self_clone.compare(other_clone)
-> return cmp == -1
} else {
throw "Cannot compare counter with " + other.type as @string
}
},

Expand All @@ -614,8 +602,6 @@ more_or_eq = c >= 10
other_clone = other.clone()
cmp = self_clone.compare(other_clone)
-> return cmp == 1 || cmp == 0
} else {
throw "Cannot compare counter with " + other.type as @string
}
},

Expand All @@ -640,8 +626,6 @@ less_or_eq = c <= 42
other_clone = other.clone()
cmp = self_clone.compare(other_clone)
-> return cmp == -1 || cmp == 0
} else {
throw "Cannot compare counter with " + other.type as @string
}
},

Expand All @@ -668,8 +652,6 @@ eq = c == 42
other_clone = other.clone()
cmp = self_clone.compare(other_clone)
-> return cmp == 0
} else {
throw "Cannot compare counter with " + other.type as @string
}
},

Expand Down Expand Up @@ -700,8 +682,6 @@ c += 10
self.add(num)
} else if num.type == @counter {
num.clone().add_to([self])
} else {
throw "Cannot add " + num.type as @string + " to counter"
}
},

Expand Down Expand Up @@ -732,8 +712,6 @@ c -= 5

} else if num.type == @counter {
num.clone().subtract_from([self])
} else {
throw "Cannot subtract " + num.type as @string + " from counter"
}
},

Expand All @@ -747,8 +725,6 @@ c *= 6
self.multiply(num)
} else if num.type == @counter {
self.multiply(num)
} else {
throw "Cannot multiply counter by " + num.type as @string
}
},

Expand All @@ -761,8 +737,6 @@ c /= 6
self.divide(num)
} else if num.type == @counter {
self.divide(num)
} else {
throw "Cannot divide counter by " + num.type as @string
}
},

Expand All @@ -778,8 +752,6 @@ c = 42
}
} else if num.type == @counter {
num.copy_to([self])
} else {
throw "Cannot assign" + num.type as @string + " to counter"
}
},

Expand All @@ -791,14 +763,10 @@ c <=> c2
// c is now 42, c2 is now 23
")]
(self, num: @counter) {
if num.type == @counter {
swap_tmp = @counter::new();
self.add_to(swap_tmp)
num.add_to(self)
swap_tmp.add_to(num)
}else {
throw "Cannot swap counter with " + num.type as @string
}
swap_tmp = @counter::new();
self.add_to(swap_tmp)
num.add_to(self)
swap_tmp.add_to(num)
},

to_const: #[desc("Converts the counter into a normal number (very context-splitting, be careful)") example("
Expand Down
1 change: 1 addition & 0 deletions spwn-lang/libraries/std/lib.spwn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "string.spwn"
import "counter.spwn"
import "fileio.spwn"
import "regex.spwn"
import "number.spwn"

general = import "general_triggers.spwn"
events = import "events.spwn"
Expand Down
10 changes: 10 additions & 0 deletions spwn-lang/libraries/std/number.spwn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[no_std]

impl @number {
map: #[desc("Maps a number linearily from one interval to another") example("$.assert(2.map(1, 4, 5, 11) == 7)")]
(self, istart: @number, istop: @number, ostart: @number, ostop: @number) {
return ostart + (ostop - ostart) * ((self - istart) / (istop - istart));
}
}


0 comments on commit 10ea3e3

Please sign in to comment.