Add files via upload

This commit is contained in:
loosewheel 2021-03-23 16:03:46 +10:00 committed by GitHub
parent 88e858164b
commit 6017824e08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 104 additions and 146 deletions

View File

@ -1,3 +1,9 @@
v 0.1.0
* initial alpha
v 0.1.1
* Redesign form
* Implement value slot to set number values.
* Bug fixes.

View File

@ -1,4 +1,4 @@
local version = "0.1.0"
local version = "0.1.1"

View File

@ -1,6 +1,6 @@
author = loosewheel
description = Scratch programmable robots.
release = 0.1.0
release = 0.1.1
title = LWScratch
name = lwscratch
depends = default, lwdrops

View File

@ -112,6 +112,7 @@ local function check_condition (program, last_condition)
cmd = program:next_cell (false)
end
return true
end
@ -1066,8 +1067,6 @@ function program_obj:check ()
self:init ()
for l = 1, self:lines () do
self:next_line ()
local indent = self:line_indent ()
if indent then
@ -1087,6 +1086,8 @@ function program_obj:check ()
end
end
end
self:next_line ()
end
self:init ()

View File

@ -13,7 +13,7 @@ CC-BY-SA 3.0
Version
=======
0.1.0
0.1.1
Minetest Version
@ -57,16 +57,16 @@ retained across world startups. Robots retain their persistence state when
moved. The maximum force loaded blocks is limited to the
max_forceloaded_blocks setting (default is 16).
Each robot can be given a name, by entering the name in the name field and
Each robot can be given a name, by entering the name in the Name field and
clicking the Set button. The name will display when the robot is pointed
at or as the tool tip if it is in an inventory.
Each robot has a storage area (bottom left).
Each robot has a storage area (center left).
While a robot is running sneak + punch will open a form to stop it.
Robots are programmed graphically, by dragging a command from a pallet
(top left) to the program sheet (top right). Items can be dragged from the
(top right) to the program sheet (left). Items can be dragged from the
inventories. These are only markers, the item is not used. To remove an
item from the program sheet, drag it to an empty space in the command pallet.
To clear the whole program click the clear button. Commands are run in order,
@ -105,10 +105,10 @@ If
Values:
Number
Can be set with an number value when placed (or moved) in the program
sheet. An input field and button labelled Number appear. Set the value
and click the button to set the value. Hovering over the number item
the tool tip displays its current value.
Can be set with an number value. To set the value, place it in the value
slot at the top, enter the desired value in the Value field and click
the Set button. Hovering over the number item, the tool tip displays its
current value.
Conditions:

154
robot.lua
View File

@ -48,6 +48,28 @@ local function on_receive_fields (pos, formname, fields, sender)
end
end
elseif fields.set_number then
local meta = minetest.get_meta (pos)
if meta then
local value = tonumber (fields.number_value or 0) or 0
local inv = meta:get_inventory ()
if inv then
local stack = inv:get_stack ("value", 1)
if stack and utils.is_number_item (stack:get_name ()) then
local imeta = stack:get_meta ()
if imeta then
imeta:set_int ("value", value)
imeta:set_int ("description", value)
inv:set_stack ("value", 1, stack)
end
end
end
end
elseif fields.persists then
local meta = minetest.get_meta (pos)
@ -97,34 +119,6 @@ local function on_receive_fields (pos, formname, fields, sender)
end
end
elseif fields.number_value then
if fields.key_enter_field == "number_value" or fields.set_number then
local meta = minetest.get_meta (pos)
if meta then
local index = meta:get_int ("number_index")
if index > 0 then
local value = tonumber (fields.number_value or 0) or 0
local inv = meta:get_inventory ()
if inv then
local stack = inv:get_stack ("program", index)
if stack and utils.is_number_item (stack:get_name ()) then
local imeta = stack:get_meta ()
if imeta then
imeta:set_int ("value", value)
imeta:set_int ("description", value)
meta:set_int ("number_value", value)
inv:set_stack ("program", index, stack)
end
end
end
end
end
end
end
end
@ -201,12 +195,13 @@ local function after_place_node (pos, placer, itemstack, pointed_thing)
meta:set_int ("persists", persists)
meta:set_int ("running", 0)
meta:set_int ("delay_counter", 0)
meta:set_int ("number_index", 0)
meta:set_string ("formspec", utils.get_robot_formspec (pos))
local inv = meta:get_inventory ()
inv:set_size("value", 1)
inv:set_width("value", 1)
inv:set_size("program", utils.program_inv_size)
inv:set_width("program", 9)
inv:set_size("commands", utils.commands_inv_size)
@ -329,11 +324,35 @@ local function allow_metadata_inventory_move (pos, from_list, from_index, to_lis
return 0
end
if to_list == "value" then
local meta = minetest.get_meta (pos)
if meta then
local inv = meta:get_inventory ()
if inv then
local stack = inv:get_stack (from_list, from_index)
if stack and utils.is_number_item (stack:get_name ()) then
return 1
end
end
end
return 0
end
if from_list == "program" then
if to_list == "commands" or to_list == "program" then
return 1
end
elseif from_list == "value" then
if to_list == "commands" or to_list == "program" then
return 1
end
elseif from_list == "commands" then
if to_list == "program" then
local meta = minetest.get_meta (pos)
@ -485,12 +504,9 @@ local function allow_metadata_inventory_take (pos, listname, index, stack, playe
return 0
end
if stack and not stack:is_empty () then
if listname == "program" or listname == "commands" or
utils.is_command_item (stack:get_name ()) then
if listname == "program" or listname == "commands" then
return 0
end
return 0
end
return utils.settings.default_stack_max
@ -499,77 +515,16 @@ end
local function on_metadata_inventory_put (pos, listname, index, stack, player)
if listname == "commands" then
local meta = minetest.get_meta (pos)
if meta then
local inv = meta:get_inventory ()
if inv then
utils.prep_inventory (inv, nil)
end
if meta:get_int ("number_index") > 0 then
meta:set_int ("number_index", 0)
meta:set_string ("formspec", utils.get_robot_formspec (pos))
end
end
end
end
local function on_metadata_inventory_take (pos, listname, index, stack, player)
if listname == "commands" then
local meta minetest.get_meta (pos)
if meta then
local inv = meta:get_inventory ()
if inv then
inv:set_stack ("commands", index, stack)
end
if meta:get_int ("number_index") > 0 then
meta:set_int ("number_index", 0)
meta:set_string ("formspec", utils.get_robot_formspec (pos))
end
end
end
end
local function on_metadata_inventory_move (pos, from_list, from_index, to_list, to_index, count, player)
local number_set = false
if to_list == "program" then
local meta = minetest.get_meta (pos)
if meta then
local inv = meta:get_inventory ()
if inv then
local stack = inv:get_stack ("program", to_index)
if stack and utils.is_number_item (stack:get_name ()) then
local imeta = stack:get_meta ()
if imeta then
local value = imeta:get_int ("value")
meta:set_int ("number_index", to_index)
meta:set_int ("number_value", value)
meta:set_string ("formspec", utils.get_robot_formspec (pos))
number_set = true
end
end
end
end
end
if (from_list == "program" and to_list == "commands") or
(from_list == "commands" and to_list == "program") then
@ -583,15 +538,6 @@ local function on_metadata_inventory_move (pos, from_list, from_index, to_list,
end
end
end
if not number_set then
local meta = minetest.get_meta (pos)
if meta then
meta:set_int ("number_index", 0)
meta:set_string ("formspec", utils.get_robot_formspec (pos))
end
end
end

View File

@ -132,6 +132,8 @@ function utils.robot_move (robot_pos, side)
storage[s] = inv:get_stack ("storage", s)
end
local value_slot = inv:get_stack ("value", 1)
if persists == 1 then
minetest.forceload_free_block (robot_pos, false)
end
@ -154,6 +156,8 @@ function utils.robot_move (robot_pos, side)
meta:set_string ("program", minetest.serialize (program))
meta:set_string ("formspec", formspec)
inv:set_size("value", 1)
inv:set_width("value", 1)
inv:set_size("program", utils.program_inv_size)
inv:set_width("program", 10)
inv:set_size("commands", utils.commands_inv_size)
@ -168,6 +172,8 @@ function utils.robot_move (robot_pos, side)
inv:set_stack ("storage", s, storage[s])
end
inv:set_stack ("value", 1, value_slot)
if persists == 1 then
minetest.forceload_block (pos, false)
end

View File

@ -105,6 +105,7 @@ function utils.new_inventory ()
local inv =
"{ "..
"value = { [1] = '' }, "..
"program = { "..program.."}, "..
"commands = { "..commands.."}, "..
"storage = { [1] = '', [2] = '', [3] = '', [4] = '', [5] = '', [6] = '', [7] = '', [8] = '', "..
@ -443,29 +444,23 @@ end
function utils.get_robot_formspec (pos)
local set_number = ""
local persists =
"image_button[21.45,3.0;0.7,0.7;persist_button_off.png;persists;;false;false;persist_button_off.png]"
"image_button[21.05,3.0;0.7,0.7;persist_button_off.png;persists;;false;false;persist_button_off.png]"
local power =
"image_button[21.1,1.0;1.4,1.4;power_button_off.png;power;;false;false;power_button_off.png]"
"image_button[20.7,1.0;1.4,1.4;power_button_off.png;power;;false;false;power_button_off.png]"
local error_msg = ""
local meta = minetest.get_meta (pos)
if meta then
if meta:get_int ("number_index") > 0 then
set_number =
"field[5.5,2.0;2.0,0.8;number_value;;"..tonumber (meta:get_int ("number_value")).."]"..
"button[7.7,2.0;2.0,0.8;set_number;Number]"
end
if meta:get_int ("persists") == 1 then
persists =
"image_button[21.45,3.0;0.7,0.7;persist_button_on.png;persists;;false;false;persist_button_on.png]"
"image_button[21.05,3.0;0.7,0.7;persist_button_on.png;persists;;false;false;persist_button_on.png]"
end
if meta:get_int ("running") == 1 then
power =
"image_button[21.1,1.0;1.4,1.4;power_button_on.png;power;;false;false;power_button_on.png]"
"image_button[20.7,1.0;1.4,1.4;power_button_on.png;power;;false;false;power_button_on.png]"
end
local msg = meta:get_string ("error")
@ -473,44 +468,49 @@ function utils.get_robot_formspec (pos)
if msg:len () > 0 then
error_msg =
"style_type[label;textcolor=red]"..
"label[10.0,9.4;"..minetest.formspec_escape (msg).."]"..
"label[1.0,17.4;"..minetest.formspec_escape (msg).."]"..
"style_type[label;textcolor=white]"
end
end
local spec =
"formspec_version[3]"..
"size[23.5,15.65,false]"..
"size[22.8,18.0,false]"..
"no_prepend[]"..
"bgcolor[#769BE6]"..
"field[1.0,1.0;4.0,0.8;name;name;${name}]"..
"button[5.5,1.0;1.5,0.8;setname;Set]"..
"label[1.0,2.6;Commands]"..
set_number..
"field[1.0,1.0;2.5,0.8;name;Name;${name}]"..
"button[3.5,1.0;1.0,0.8;setname;Set]"..
"button[5.3,1.0;1.4,0.8;clear_program;Clear]"..
"style_type[list;noclip=false;size=1.0,1.0;spacing=0.0,0.0]"..
"scrollbaroptions[min=0;max=40;smallstep=10;largestep=10;thumbsize=10;arrows=default]"..
"scrollbar[9.0,3.0;0.5,6.0;vertical;commands_scroll;0-40]"..
"scroll_container[1.0,3.0;8.0,6.0;commands_scroll;vertical;0.1]"..
"list[context;commands;0.0,0.0;8,10;]\n"..
"scroll_container_end[]"..
-- value
"list[context;value;7.4,0.9;1,1;]\n"..
"field[8.5,1.0;2.0,0.8;number_value;Value;]"..
"button[10.5,1.0;1.0,0.8;set_number;Set]"..
"scrollbaroptions[min=0;max=420;smallstep=30;largestep=70;thumbsize=50;arrows=default]"..
"scrollbar[20.0,1.0;0.5,8.0;vertical;program_scroll;0-420]"..
"scroll_container[10.0,1.0;10.0,8.0;program_scroll;vertical;0.1]"..
-- program
"scrollbaroptions[min=0;max=350;smallstep=30;largestep=70;thumbsize=105;arrows=default]"..
"scrollbar[11.0,2.0;0.5,15.0;vertical;program_scroll;0-350]"..
"scroll_container[1.0,2.0;10.0,15.0;program_scroll;vertical;0.1]"..
"list[context;program;0.0,0.0;10,50;]\n"..
"scroll_container_end[]"..
"button[21.1,8.2;1.4,0.8;clear_program;Clear]"..
-- commands
"scrollbaroptions[min=0;max=50;smallstep=10;largestep=10;thumbsize=25;arrows=default]"..
"scrollbar[20.0,1.0;0.5,5.0;vertical;commands_scroll;0-50]"..
"scroll_container[12.0,1.0;8.0,5.0;commands_scroll;vertical;0.1]"..
"list[context;commands;0.0,0.0;8,10;]\n"..
"scroll_container_end[]"..
power..
persists..
"label[1.0,9.5;Storage]"..
error_msg..
-- inventories
"style_type[list;noclip=false;size=1.0,1.0;spacing=0.25,0.25]"..
"list[context;storage;1.0,9.9;8,4;]"..
"list[current_player;main;12.75,9.9;8,4;]"..
"list[context;storage;12.0,6.7;8,4;]"..
"list[current_player;main;12.0,12.2;8,4;]"..
"listring[]"..
"listcolors[#545454;#6E6E6E;#6281BF]"
@ -519,5 +519,4 @@ end
--