-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2.5.4-luajit: Merge origin/master into luajit
- Loading branch information
Showing
34 changed files
with
609 additions
and
480 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* generic_peripheral/energy_storage.hpp | ||
* CraftOS-PC 2 | ||
* | ||
* This file defines a generic peripheral class for energy-storing peripherals | ||
* to inherit from. | ||
* | ||
* This code is licensed under the MIT license. | ||
* Copyright (c) 2019-2021 JackMacWindows. | ||
*/ | ||
|
||
#ifndef CRAFTOS_PC_GENERIC_PERIPHERAL_ENERGY_STORAGE_HPP | ||
#define CRAFTOS_PC_GENERIC_PERIPHERAL_ENERGY_STORAGE_HPP | ||
#include "../peripheral.hpp" | ||
|
||
class energy_storage : public peripheral { | ||
virtual int getEnergy() = 0; // Returns the current energy of the peripheral. | ||
virtual int getEnergyCapacity() = 0; // Returns the total energy capacity of the peripheral. | ||
public: | ||
int call(lua_State *L, const char * method) override { | ||
const std::string m(method); | ||
if (m == "getEnergy") lua_pushinteger(L, getEnergy()); | ||
else if (m == "getEnergyCapacity") lua_pushinteger(L, getEnergyCapacity()); | ||
else return 0; | ||
return 1; | ||
} | ||
void update() override {} | ||
library_t getMethods() const override { | ||
static luaL_Reg reg[] = { | ||
{"getEnergy", NULL}, | ||
{"getEnergyCapacity", NULL}, | ||
{NULL, NULL} | ||
}; | ||
static library_t methods = {"energy_storage", reg, nullptr, nullptr}; | ||
return methods; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* generic_peripheral/fluid_storage.hpp | ||
* CraftOS-PC 2 | ||
* | ||
* This file defines a generic peripheral class for tank-like peripherals | ||
* to inherit from. | ||
* | ||
* This code is licensed under the MIT license. | ||
* Copyright (c) 2019-2021 JackMacWindows. | ||
*/ | ||
|
||
#ifndef CRAFTOS_PC_GENERIC_PERIPHERAL_FLUID_STORAGE_HPP | ||
#define CRAFTOS_PC_GENERIC_PERIPHERAL_FLUID_STORAGE_HPP | ||
#include "../Computer.hpp" | ||
#include "../peripheral.hpp" | ||
|
||
class fluid_storage : public peripheral { | ||
virtual int tanks(lua_State *L) = 0; // Implements tanks() as a Lua function: https://tweaked.cc/generic_peripheral/fluid_storage.html#v:tanks | ||
virtual int addFluid(const std::string& fluid, int amount) = 0; // Adds the specified amount of fluid to a tank. Returns the amount actually added. | ||
virtual std::list<std::pair<std::string, int>> removeFluid(const std::string& fluid, int amount) = 0; // Removes the specified amount of fluid from a tank. If fluid is empty, remove any type of fluid; otherwise only remove that type. Returns a list of each fluid type & amount removed. (If no fluid is removed, return an empty list.) | ||
public: | ||
int call(lua_State *L, const char * method) override { | ||
const std::string m(method); | ||
if (m == "tanks") return tanks(L); | ||
else if (m == "pushFluid" || m == "pullFluid") { | ||
Computer * comp = get_comp(L); | ||
const char * side = luaL_checkstring(L, 1); | ||
const int limit = luaL_optinteger(L, 2, INT_MAX); | ||
const std::string name = luaL_optstring(L, 3, ""); | ||
|
||
if (comp->peripherals.find(side) == comp->peripherals.end()) return luaL_error(L, "Target '%s' does not exist", side); | ||
fluid_storage * p = dynamic_cast<fluid_storage*>(comp->peripherals[side]); | ||
if (p == NULL) return luaL_error(L, "Target '%s' is not an tank", side); // grammar mistake intentional | ||
fluid_storage *src, *dest; | ||
if (m == "pushFluid") src = this, dest = p; | ||
else src = p, dest = this; | ||
if (limit <= 0) return luaL_error(L, "Limit must be > 0"); | ||
|
||
const auto removed = src->removeFluid(name, limit); | ||
if (removed.empty()) { | ||
lua_pushinteger(L, 0); | ||
return 1; | ||
} | ||
int added = 0; | ||
std::list<std::pair<std::string, int>> returnedFluid; | ||
for (const std::pair<std::string, int>& type : removed) { | ||
const int add = dest->addFluid(type.first, type.second); | ||
added += add; | ||
if (add < type.second) returnedFluid.push_back(std::make_pair(type.first, type.second - add)); | ||
} | ||
for (const std::pair<std::string, int>& type : returnedFluid) src->addFluid(type.first, type.second); | ||
|
||
lua_pushinteger(L, added); | ||
return 1; | ||
} else return 0; | ||
} | ||
void update() override {} | ||
library_t getMethods() const override { | ||
static luaL_Reg reg[] = { | ||
{"tanks", NULL}, | ||
{"pushFluid", NULL}, | ||
{"pullFluid", NULL}, | ||
{NULL, NULL} | ||
}; | ||
static library_t methods = {"fluid_storage", reg, nullptr, nullptr}; | ||
return methods; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* generic_peripheral/inventory.hpp | ||
* CraftOS-PC 2 | ||
* | ||
* This file defines a generic peripheral class for inventory-type peripherals | ||
* to inherit from. | ||
* | ||
* This code is licensed under the MIT license. | ||
* Copyright (c) 2019-2021 JackMacWindows. | ||
*/ | ||
|
||
#ifndef CRAFTOS_PC_GENERIC_PERIPHERAL_INVENTORY_HPP | ||
#define CRAFTOS_PC_GENERIC_PERIPHERAL_INVENTORY_HPP | ||
#include "../Computer.hpp" | ||
#include "../peripheral.hpp" | ||
|
||
class inventory : public peripheral { | ||
virtual int size() = 0; // Returns the number of slots available in the inventory. | ||
virtual void getItemDetail(lua_State *L, int slot) = 0; // Pushes a single Lua value to the stack with details about an item in a slot. If the slot is empty or invalid, pushes nil. (Slots are in the range 1-size().) | ||
virtual int addItems(lua_State *L, int slot, int count) = 0; // Adds the item described at the top of the Lua stack to the slot selected. Only adds up to count items. If slot is 0, determine the best slot available. Returns the number of items added. | ||
virtual int removeItems(int slot, int count) = 0; // Removes up to a number of items from the selected slot. Returns the number of items removed. | ||
public: | ||
int call(lua_State *L, const char * method) override { | ||
const std::string m(method); | ||
if (m == "size") { | ||
lua_pushinteger(L, size()); | ||
return 1; | ||
} else if (m == "list") { | ||
lua_createtable(L, size(), 0); | ||
for (int i = 1; i <= size(); i++) { | ||
lua_pushinteger(L, i); | ||
getItemDetail(L, i); | ||
lua_settable(L, -3); | ||
} | ||
return 1; | ||
} else if (m == "getItemDetail") { | ||
getItemDetail(L, luaL_checkinteger(L, 1)); | ||
return 1; | ||
} else if (m == "pushItems" || m == "pullItems") { | ||
Computer * comp = get_comp(L); | ||
const char * side = luaL_checkstring(L, 1); | ||
const int fromSlot = luaL_checkinteger(L, 2); | ||
const int limit = luaL_optinteger(L, 3, INT_MAX); | ||
const int toSlot = luaL_optinteger(L, 4, 0); | ||
|
||
if (comp->peripherals.find(side) == comp->peripherals.end()) return luaL_error(L, "Target '%s' does not exist", side); | ||
inventory * p = dynamic_cast<inventory*>(comp->peripherals[side]); | ||
if (p == NULL) return luaL_error(L, "Target '%s' is not an inventory", side); | ||
inventory *src, *dest; | ||
if (m == "pushItems") src = this, dest = p; | ||
else src = p, dest = this; | ||
|
||
if (fromSlot < 1 || fromSlot > src->size()) return luaL_error(L, "From slot out of range (between 1 and %d)", src->size()); | ||
if (!lua_isnil(L, 4) && (toSlot < 1 || toSlot > dest->size())) return luaL_error(L, "To slot out of range (between 1 and %d)", dest->size()); | ||
if (limit <= 0) { | ||
lua_pushinteger(L, 0); | ||
return 1; | ||
} | ||
|
||
src->getItemDetail(L, fromSlot); | ||
const int removed = src->removeItems(fromSlot, limit); | ||
if (removed == 0) { | ||
lua_pushinteger(L, 0); | ||
return 1; | ||
} | ||
const int added = dest->addItems(L, toSlot, removed); | ||
if (added < removed) src->addItems(L, fromSlot, removed - added); // hopefully this will still be OK | ||
|
||
lua_pushinteger(L, added); | ||
return 1; | ||
} else return 0; | ||
} | ||
void update() override {} | ||
library_t getMethods() const override { | ||
static luaL_Reg reg[] = { | ||
{"size", NULL}, | ||
{"list", NULL}, | ||
{"getItemDetail", NULL}, | ||
{"pushItems", NULL}, | ||
{"pullItems", NULL}, | ||
{NULL, NULL} | ||
}; | ||
static library_t methods = {"inventory", reg, nullptr, nullptr}; | ||
return methods; | ||
} | ||
}; | ||
|
||
#endif |
Oops, something went wrong.