diff --git a/src/main/java/carpet/CarpetSettings.java b/src/main/java/carpet/CarpetSettings.java index 86004ff1..39675149 100644 --- a/src/main/java/carpet/CarpetSettings.java +++ b/src/main/java/carpet/CarpetSettings.java @@ -73,6 +73,7 @@ public class CarpetSettings /* These will be turned when events can be added / removed in code + Then also gotta remember to remove relevant rules @Rule( desc = "Turns on internal camera path tracing app", @@ -97,9 +98,29 @@ public class CarpetSettings appSource = "ai_tracker" ) public static boolean commandAITracker = true; + + @Rule( + desc = "Enables /draw commands", + extra = { + "... allows for drawing simple shapes or", + "other shapes which are sorta difficult to do normally" + }, + appSource = "draw", + category = {FEATURE, SCARPET, COMMAND} + ) + public static String commandDraw = "true"; + + @Rule( + desc = "Enables /distance command to measure in game distance between points", + extra = "Also enables brown carpet placement action if 'carpets' rule is turned on as well", + appSource = "distance", + category = {FEATURE, SCARPET, COMMAND} + ) + public static String commandDistance = "true"; */ + @Rule(desc = "Gbhs sgnf sadsgras fhskdpri!", category = EXPERIMENTAL) public static boolean superSecretSetting = false; @@ -374,6 +395,7 @@ public class CarpetSettings @Rule(desc = "Enables /draw commands", extra = {"... allows for drawing simple shapes or","other shapes which are sorta difficult to do normally"}, category = COMMAND) public static String commandDraw = "true"; + @Rule( desc = "Enables /script command", extra = "An in-game scripting API for Scarpet programming language", diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index 3bb77a7d..2b96b604 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -84,6 +84,8 @@ public class CarpetScriptServer registerBuiltInScript(BundledModule.carpetNative("math", true)); registerBuiltInScript(BundledModule.carpetNative("chunk_display", false)); registerBuiltInScript(BundledModule.carpetNative("ai_tracker", false)); + registerSettingsApp(BundledModule.carpetNative("draw_rc_1", false)); + registerSettingsApp(BundledModule.carpetNative("distance_rc_1", false)); } public CarpetScriptServer(MinecraftServer server) diff --git a/src/main/resources/assets/carpet/scripts/distance_rc_1.sc b/src/main/resources/assets/carpet/scripts/distance_rc_1.sc new file mode 100644 index 00000000..a3428132 --- /dev/null +++ b/src/main/resources/assets/carpet/scripts/distance_rc_1.sc @@ -0,0 +1,88 @@ +import('math','_euclidean','_manhattan','_round'); + +__config()->{ + 'commands'->{ + 'from '->'set_start', + 'from to '->'_calculate', + 'to '->_(end_pos)->(_calculate(null,end_pos)), + 'toggle_line'->'_toggle_line' + }, + 'scope'->'player' +}; + +global_position=null; +global_end_position=null; + +global_show_line=false; + +global_line = { + 'start_pos'->global_position, + 'end_pos'->global_end_position +}; + +set_start(pos)->( + global_position= pos; + print(player(),format('gi Initial point set to: ', 'g '+pos,'?/tp '+(str(pos)-','-'['-']'))) +); + +_toggle_line()->( + global_show_line= !global_show_line; + if(global_show_line, + print('Toggled lines on'); + if(global_end_position,_draw_line()),//making sure that there is a start an end pos, and Ive made it so there is no end pos without start pos + print('Toggled lines off') + ); + null +); + +_calculate(start_pos, end_pos)->( + if(start_pos!=null, + set_start(start_pos); + calculate(end_pos), + if(global_position, + calculate(end_pos), + print(player(),format('r There was no initial point for '+player())); + set_start(end_pos) + ) + ) +); + +__on_player_right_clicks_block(player, item_tuple, hand, block, face, hitvec) ->( + pos = pos_offset(block,face);//for clicking against the wall. + if(block=='brown_carpet' && system_info('world_carpet_rules'):'carpets'=='true', + if(global_position==null||player~'sneaking',//wont complain for first carpet + set_start(pos), + _calculate(null,pos) + ) + ) +); + +calculate(end_pos)->( + start_pos = global_position; + [dx,dy,dz] = map(start_pos-end_pos,abs(_)); + + manhattan = _round(_manhattan([dx, dy, dz],[0,0,0]),100);//rounding to nearest 0.01 for precision, but not ridiculous decimal places + spherical = _round(_euclidean([dx, dy, dz],[0,0,0]),100); + cylindrical = _round(_euclidean([dx, 0, dz],[0,0,0]),100);//useful for chunk distances etc. + + print(player(),format( + 'w Distance between ', + 'c '+start_pos,'?/tp '+(str(start_pos)-','-'['-']'), 'w and ', + 'c '+end_pos,'?/tp '+(str(end_pos)-','-'['-']'), 'w :\n', + 'w - Spherical: ', 'wb '+spherical+'\n', + 'w - Cylindrical: ', 'wb '+cylindrical+'\n', + 'w - Manhattan: ', 'wb '+manhattan + )); + + global_end_position = end_pos; + + schedule(0,'_draw_line'); +); + +_draw_line()->( + if(global_show_line,//It will always draw from the global positions, so it will turn on if there is a line + draw_shape('line',20,'from',global_position,'to',global_end_position, 'player',player(),'color',0x000000FF); + draw_shape('label',20,'pos',global_end_position,'text',_round(_euclidean(global_position,global_end_position),100),'color',0x000000FF); + schedule(20,'_draw_line') + ) +); diff --git a/src/main/resources/assets/carpet/scripts/draw_rc_1.sc b/src/main/resources/assets/carpet/scripts/draw_rc_1.sc new file mode 100644 index 00000000..0927b318 --- /dev/null +++ b/src/main/resources/assets/carpet/scripts/draw_rc_1.sc @@ -0,0 +1,142 @@ +import('math','_euclidean_sq','_vec_length'); + +__config() -> { + 'commands'->{ + 'sphere
'->['draw_sphere',null,true], + 'sphere
replace '->['draw_sphere',true], + 'ball
'->['draw_sphere',null,false], + 'ball
replace '->['draw_sphere',false], + 'diamond
'->['draw_diamond',null], + 'diamond
replace '->'draw_diamond', + 'pyramid
'->['draw_pyramid',null, true], + 'pyramid
replace '->['draw_pyramid', true], + 'cone
'->['draw_pyramid',null, false], + 'cone
replace '->['draw_pyramid', false], + 'cuboid
'->['draw_prism', null, true], + 'cuboid
replace '->['draw_prism', true], + 'cylinder
'->['draw_prism', null, false], + 'cylinder
replace '->['draw_prism', false] + }, + 'arguments'->{ + 'center'->{'type'->'pos', 'loaded'->'true'}, + 'radius'->{'type'->'int', 'suggest'->[], 'min'->0},//to avoid default suggestions + 'replacement'->{'type'->'block'}, + 'height'->{'type'->'int', 'suggest'->[],'min'->0}, + 'orientation'->{'type'->'term', 'suggest'->['x','y','z']}, + 'pointing'->{'type'->'term','suggest'->['up','down']}, + 'hollow'->{'type'->'term','suggest'->['hollow','solid']}, + }, + 'scope'->'global' +}; + + +//"Boilerplate" code + +set_block(x, y, z, block, replacement)-> ( + if(block != block(x, y, z), + without_updates(set([x, y, z],block)); + global_affected:0 += [x, y, z]; + global_affected:1 += 1; + ); +); + +global_affected = [[],0]; + +affected(player) -> ( + print(player,format('gi Filled ' + global_affected:1 + ' blocks')); + affected = global_affected:1; + + if(system_info('world_carpet_rules'):'fillUpdates', + for(global_affected:0,update(_));//updating if fillUpdates is true + ); + + global_affected = [[],0]; + affected +); + +length_sq(vec) -> _vec_length(vec)^2; + +fill_flat(pos, offset, dr, rectangle, orientation, block, hollow, replacement)->( + r = floor(dr); + drsq = dr^2; + if(orientation=='x', + scan(pos,0,-r,-r, + if((!hollow && (rectangle || _y^2 + _z^2 <= drsq))||//if not hollow, vry simple + (hollow && ((rectangle && (abs(_y) == r || abs(_z) ==r)) || //If hollow and it's a rectangle + (!rectangle && (_y^2 + _z^2 <= drsq && (abs(_y)+1)^ 2 + (abs(_z)+1)^2 >= drsq)))),//If hollow and not rectangle + set_block(_x+offset,_y,_z,block, replacement) + ) + ), + orientation == 'y', + scan(pos,-r,0,-r, + if((!hollow && (rectangle || _x^2 + _z^2 <= drsq))||//if not hollow, vry simple + (hollow && ((rectangle && (abs(_x) == r || abs(_z) ==r)) || //If hollow and it's a rectangle + (!rectangle && (_x^2 + _z^2 <= drsq && (abs(_x)+1)^ 2 + (abs(_z)+1)^2 >= drsq)))),//If hollow and not rectangle + set_block(_x,_y+offset,_z,block, replacement) + ) + ), + orientation == 'z', + scan(pos,-r,-r,0, + if((!hollow && (rectangle || _y^2 + _x^2 <= drsq))||//if not hollow, vry simple + (hollow && ((rectangle && (abs(_y) == r || abs(_x) ==r)) || //If hollow and it's a rectangle + (!rectangle && (_y^2 + _x^2 <= drsq && (abs(_y)+1)^ 2 + (abs(_x)+1)^2 >= drsq)))),//If hollow and not rectangle + set_block(_x,_y,_z+offset,block, replacement) + ) + ), + print(player(),format('r Error while running command: orientation can only be "x", "y" or "z", '+orientation+' is invalid.')); + global_affected = [[],0]; + ); +); + +//drawing commands + +draw_sphere(centre, radius, block, replacement, hollow)->( + scan(centre,[radius,radius,radius], + l = length_sq([_x,_y,_z]-centre); + if((l<=radius^2+radius) && (!hollow || l>=radius^2-radius), + set_block(_x, _y, _z, block, replacement) + ) + ); + + affected(player()); +); + +draw_diamond(pos, radius, block, replacement)->( + + c_for(r=0, r( + + hollow = fill_type=='hollow'; + pointup = pointing=='up'; + + for(range(height), + r = if(pointup, radius - radius * _ / height -1, radius * _ / height); + fill_flat(pos, _, r, is_square, orientation, block, if((pointup&&_==0)||(!pointup && _==height-1),false,hollow),replacement)//Always close bottom off + ); + affected(player()) +); + +draw_prism(pos, rad, height, orientation, block, fill_type, replacement, is_square)->( + + hollow = fill_type =='hollow'; + radius = rad+0.5; + + for(range(height), + fill_flat(pos, _, radius, is_square, orientation, block, if(_==0 || _==height-1,false,hollow), replacement)//Always close ends off + ); + affected(player()) +); diff --git a/src/main/resources/assets/carpet/scripts/math.scl b/src/main/resources/assets/carpet/scripts/math.scl index adef1621..d6e7c26b 100644 --- a/src/main/resources/assets/carpet/scripts/math.scl +++ b/src/main/resources/assets/carpet/scripts/math.scl @@ -1,3 +1,5 @@ _euclidean_sq(vec1, vec2) -> reduce(vec1 - vec2, _a + _*_, 0); _euclidean(vec1, vec2) -> sqrt(reduce(vec1 - vec2, _a + _*_, 0)); -_vec_length(vec) -> sqrt(reduce(vec, _a + _*_, 0)); \ No newline at end of file +_manhattan(vec1, vec2) -> reduce(vec1-vec2,_a+abs(_),0); +_vec_length(vec) -> sqrt(reduce(vec, _a + _*_, 0)); +_round(num,precision) -> round(num*precision)/precision;