An OpenMW mod that lets you play the whole Elder Scrolls III: Morrowind as a top-down isometric game — with full online co-op or MMO modes.
C++
View switching by F7 only; party rings and arrows to players off the screen
Claudecfa32841737d
diff --git a/apps/openmw/mwlua/nearbybindings.cpp b/apps/openmw/mwlua/nearbybindings.cppindex 69c926a..e5e1585 100644--- a/apps/openmw/mwlua/nearbybindings.cpp+++ b/apps/openmw/mwlua/nearbybindings.cpp@@ -203,6 +203,11 @@ namespace MWLuaapi["players"] = LObjectList{ objectLists->getPlayers() };// Isowind multiplayer: whether an actor is another player (to the engine they are ordinary NPCs)api["isOtherPlayer"] = [](const LObject& object) { return mwmp::hooks::isDedicatedPlayer(object.ptr()); };+ // ...whether another player is allied with this one (in MMO mode, in their party), and whether the server is MMO+ api["isAlly"] = [](const LObject& object) {+ return mwmp::hooks::isAlliedPlayer(MWBase::Environment::get().getWorld()->getPlayerPtr(), object.ptr());+ };+ api["isMmo"] = []() { return mwmp::hooks::isMmoMode(); };api["NAVIGATOR_FLAGS"]= LuaUtil::makeStrictReadOnly(LuaUtil::tableFromPairs<std::string_view, DetourNavigator::Flag>(lua,diff --git a/isowind/README.txt b/isowind/README.txtindex 127559c..1a81338 100644--- a/isowind/README.txt+++ b/isowind/README.txt@@ -87,6 +87,8 @@ own loot. Type in the chat:/party who is in your party/p MESSAGE talk to your party/party leave leave it; you keep the quests you have+In the top-down view your party members have a green ring under them, and a green arrow at the edge of+the screen points to any of them who are nearby but off it (on a co-op server, to every other player).Looks-----@@ -107,9 +109,9 @@ E Use what is under the cursor, or the nearest thing in front ofSpace JumpF / R Ready weapon / ready magicTab Inventory-Mouse wheel Zoom. All the way out is the top-down view; zoom in and it becomes Morrowind's third person- view, all the way in first person.-F7 Hop between the top-down view and the normal one.+Mouse wheel Zoom, within the view you are in.+F7 Switch view: top-down, then Morrowind's third person view, then first person. Change the+ key, or add a controller button for it, in Options > Scripts > Isowind.1 - 0 Quick keys, shown along the bottom of the screen. To set one, open the inventory (Tab),point at an item, spell or enchanted item and press the number. Right-click a slot forMorrowind's own quick key menu.diff --git a/isowind/mod/l10n/Isowind/en.yaml b/isowind/mod/l10n/Isowind/en.yamlindex d096b14..28829ee 100644--- a/isowind/mod/l10n/Isowind/en.yaml+++ b/isowind/mod/l10n/Isowind/en.yaml@@ -3,3 +3,8 @@ settings_page_desc: "Isowind's own options."settings_multiplayer: "Multiplayer"settings_player_names: "Show player names"settings_player_names_desc: "Always show the other players' names over them."+settings_controls: "Controls"+switch_view: "Switch view"+switch_view_desc: "Top-down, then third person, then first person."+switch_view_key_desc: "Top-down, then third person, then first person (F7 unless changed). Click, then press a key or button."+switch_view_button_desc: "A second binding, for a controller button. Click, then press the button."diff --git a/isowind/mod/scripts/isowind/settings.lua b/isowind/mod/scripts/isowind/settings.luaindex 18b96d5..91b3639 100644--- a/isowind/mod/scripts/isowind/settings.lua+++ b/isowind/mod/scripts/isowind/settings.lua@@ -1,7 +1,17 @@-- Isowind's page in the game's Options -> Scripts (a MENU script, where settings pages are registered). The player-- scripts read these from storage.playerSection('Settings/Isowind/Multiplayer').+-- The switch view trigger is registered here too, so it lasts (triggers from a MENU script do) and its bindings show+-- on the page; topdown.lua handles it and gives the keyboard binding its default, F7.local I = require('openmw.interfaces')+local input = require('openmw.input')++input.registerTrigger {+ key = 'IsowindSwitchView',+ l10n = 'Isowind',+ name = 'switch_view',+ description = 'switch_view_desc',+}I.Settings.registerPage {key = 'Isowind',@@ -26,3 +36,30 @@ I.Settings.registerGroup {},},}++-- Two bindings, so a key and a controller button can both switch the view (one button each)+I.Settings.registerGroup {+ key = 'Settings/Isowind/Controls',+ page = 'Isowind',+ l10n = 'Isowind',+ name = 'settings_controls',+ permanentStorage = true,+ settings = {+ {+ key = 'SwitchView',+ renderer = 'inputBinding',+ name = 'switch_view',+ description = 'switch_view_key_desc',+ default = 'IsowindSwitchView',+ argument = { type = 'trigger', key = 'IsowindSwitchView' },+ },+ {+ key = 'SwitchViewController',+ renderer = 'inputBinding',+ name = 'switch_view',+ description = 'switch_view_button_desc',+ default = 'IsowindSwitchViewController',+ argument = { type = 'trigger', key = 'IsowindSwitchView' },+ },+ },+}diff --git a/isowind/mod/scripts/isowind/topdown.lua b/isowind/mod/scripts/isowind/topdown.luaindex ec0f74a..22bc93e 100644--- a/isowind/mod/scripts/isowind/topdown.lua+++ b/isowind/mod/scripts/isowind/topdown.lua@@ -7,9 +7,9 @@-- left click attack / cast with a weapon or spell ready, otherwise interact with what is under the cursor-- right click power attack with a weapon ready (the weapon's strongest attack type)-- E (Activate) interact with what is under the cursor, or else the nearest thing in reach in front--- mouse wheel zoom: all the way out is the top-down view, in a little is Morrowind's third person view,--- all the way in first person (the D-pad on a controller)--- F7 hop between the top-down view and the normal one+-- mouse wheel zoom, within the view (the D-pad on a controller)+-- F7 switch view: top-down, then Morrowind's third person view, then first person (rebindable, and+-- bindable to a controller button, in Options -> Scripts -> Isowind)---- With a controller (it takes over as soon as a stick or button is used, the mouse as soon as it moves):-- left stick move right stick aim / pick what X uses@@ -17,7 +17,7 @@-- X use / talk Y ready magic-- right trigger attack / cast left trigger power attack-- bumpers previous / next quick key--- D-pad up / down zoom (hold to keep zooming), through the three views+-- D-pad up / down zoom (hold to keep zooming), within the view-- D-pad left quick keys menu (on a server: the chat, typed with the keyboard on the screen)-- Start menu Back inventory (hold: journal)-- left stick in sneak right stick in rest@@ -26,12 +26,14 @@-- The menus are the engine's controller menus ('controller menus' in the profile's settings.cfg): D-pad and A / B,-- the triggers switch pages, Y puts an item or spell on a quick key, and a keyboard on the screen types.----- The zoom picks the view: zoomed out to TOPDOWN_FROM or farther it is this top-down view; in closer, Morrowind's--- third person view at that distance; closer than FIRST_PERSON_BELOW, first person. Zooming and the first / third--- person switch are taken from the built-in camera script for good (claimCamera).+-- The zoom distance says the view: TOPDOWN_FROM or farther is this top-down view; in closer, Morrowind's third person+-- view at that distance; closer than FIRST_PERSON_BELOW, first person. Zooming stays within the view's range and the+-- switch view trigger (F7) moves between them, each keeping its own zoom. Zooming and the first / third person switch+-- are taken from the built-in camera script for good (claimCamera).-- The view is a fixed isometric angle: turned 45 degrees, looking down at the classic isometric 35.26 degrees.-- The camera slides in toward the player rather than go underground on slopes, and creatures (and hurt or hostile--- people) get health bars (healthbars.lua).+-- people) get health bars (healthbars.lua). In multiplayer, party members (MMO) get a green ring, and other players+-- off the screen an arrow at its edge pointing their way: party members in MMO mode, everyone in co-op.local async = require('openmw.async')local camera = require('openmw.camera')@@ -71,7 +73,7 @@ local FOV = math.rad(30) -- a narrow view from far away looks close tlocal MIN_DISTANCE, MAX_DISTANCE = 25, 3500 -- zoom range, from first person out to the farthest top-down viewlocal TOPDOWN_FROM = 500 -- zoomed out to here or farther the view is top-down...local FIRST_PERSON_BELOW = 50 -- ...zoomed in closer than this it is first person; third person in between-local THIRD_PERSON_DISTANCE = 200 -- where F7 puts the zoom when leaving the top-down view+local THIRD_PERSON_DISTANCE = 200 -- the third person view's zoom until it is zoomedlocal ZOOM_SPEED = 8 -- how quickly the camera eases to the zoom level (per second)local GROUND_CLEARANCE = 60 -- the camera stays at least this far above the groundlocal CLOSEST_DISTANCE = 150 -- how close it may slide in to stay above the ground@@ -96,7 +98,8 @@ local DPAD_ZOOM_RATE = 1.6 -- ...this many times faster (or slower) perlocal active = false -- whether the top-down view's overrides are currently appliedlocal viewMode = nil -- 'topdown', 'third' or 'first', from the zoom (wantedView)local cameraClaimed = false -- zoom and the first / third person switch are this script's, once claimed-local lastTopDownDistance = 1000 -- where F7 brings the zoom back to+local lastTopDownDistance = 1000 -- the zoom of the views not in, for switching back to them+local lastThirdDistance = THIRD_PERSON_DISTANCElocal viewYaw = VIEW_YAWlocal targetDistance = 1000 -- the zoom level asked for with the wheellocal distance = targetDistance -- the camera's actual distance (eased, and kept above the ground)@@ -266,16 +269,21 @@ local function cursorDirection()return (view.forward + view.right * x + view.up * y):normalize()end--- Normalised screen position of a world position, or nil when it is off screen or behind the camera.-local function worldToScreen(pos)+-- Where a world position is in the view: x and y run from -1 to 1 across the screen (y up) and beyond off it, and its+-- depth in front of the camera (negative behind it).+local function viewCoordinates(pos)local rel = pos - view.positionlocal depth = rel:dot(view.forward)- if depth < 1 then return nil endlocal screen = ui.screenSize()local tanHalf = math.tan(FOV / 2)- local x = rel:dot(view.right) / (depth * tanHalf * screen.x / screen.y)- local y = rel:dot(view.up) / (depth * tanHalf)- if math.abs(x) > 1 or math.abs(y) > 1 then return nil end+ local d = math.max(math.abs(depth), 1)+ return rel:dot(view.right) / (d * tanHalf * screen.x / screen.y), rel:dot(view.up) / (d * tanHalf), depth+end++-- Normalised screen position of a world position, or nil when it is off screen or behind the camera.+local function worldToScreen(pos)+ local x, y, depth = viewCoordinates(pos)+ if depth < 1 or math.abs(x) > 1 or math.abs(y) > 1 then return nil endreturn v2((x + 1) / 2, (1 - y) / 2)end@@ -286,12 +294,19 @@ local function updateCursor()util.clamp(cursor.y + input.getMouseMoveY() * CURSOR_SPEED / screen.y, 0, 1))end+-- Zoom by a factor, within the view: zooming never changes it (first person doesn't zoom)+local function zoomBy(factor)+ if targetDistance >= TOPDOWN_FROM then+ targetDistance = util.clamp(targetDistance * factor, TOPDOWN_FROM, MAX_DISTANCE)+ elseif targetDistance >= FIRST_PERSON_BELOW then+ targetDistance = util.clamp(targetDistance * factor, FIRST_PERSON_BELOW, TOPDOWN_FROM - 1)+ end+end+local function updateZoom()-- Zoom3rdPerson is 10 per wheel notch (scripts/omw/input/actionbindings.lua).local notches = input.getNumberActionValue('Zoom3rdPerson') / 10- if notches ~= 0 then- targetDistance = util.clamp(targetDistance * math.exp(-notches * ZOOM_STEP), MIN_DISTANCE, MAX_DISTANCE)- end+ if notches ~= 0 then zoomBy(math.exp(-notches * ZOOM_STEP)) endendlocal function segmentDistance(p, a, b)@@ -400,6 +415,10 @@ local HOSTILE_FIGHT = 80local RANGED_REACH = 4000local RING_SCALE = 1.7 -- ring radius relative to the creature's widthlocal RING_MIN_RADIUS = 50+local MARKS = 8 -- party rings and arrows the shader has room for (its uParty and uArrows)+local ARROW_MARGIN = 40 -- the arrows sit this far in from the screen's edge (pixels at 1080 lines)+local noMarks = {}+for i = 1, MARKS do noMarks[i] = util.vector4(0, 0, 0, 0) endlocal ringShader = nillocal ringLoaded = false@@ -427,6 +446,8 @@ local function hidesRing()local shader = getRingShader()if shader and shader:isEnabled() thenshader:setVector4('uSelection', util.vector4(0, 0, 0, 0))+ shader:setVector4Array('uParty', noMarks)+ shader:setVector4Array('uArrows', noMarks)endend@@ -468,6 +489,45 @@ local function updateRing()shader:setVector4('uSelectionColor', util.vector4(color.x, color.y, color.z, 0.85))end+-- Party members (MMO mode) get a green ring like the selection ring, and other players off the screen an arrow at its+-- edge pointing their way: party members in MMO mode, everyone in co-op. Only players in the cells this game has+-- loaded: it knows where no one else is.+local function updateParty()+ local shader = getRingShader()+ if not shader then return end+ local rings, arrows = {}, {}+ if nearby.isAlly then+ local mmo = nearby.isMmo()+ local screen = ui.screenSize()+ local margin = ARROW_MARGIN * screen.y / 1080+ local maxX, maxY = 1 - 2 * margin / screen.x, 1 - 2 * margin / screen.y+ for _, actor in ipairs(nearby.actors) do+ if nearby.isOtherPlayer(actor) then+ local ally = nearby.isAlly(actor)+ local box = actor:getBoundingBox()+ if mmo and ally and #rings < MARKS then+ local feet = actor.position+ local radius = math.max(RING_MIN_RADIUS, math.max(box.halfSize.x, box.halfSize.y) * RING_SCALE)+ rings[#rings + 1] = util.vector4(feet.x, feet.y, feet.z, radius)+ end+ local x, y, depth = viewCoordinates(box.center)+ if (ally or not mmo) and #arrows < MARKS and (depth < 1 or math.abs(x) > 1 or math.abs(y) > 1) then+ if depth < 0 then x, y = -x, -y end+ -- On the edge, the way they are from the middle of the screen (the player)+ local scale = math.min(maxX / math.max(math.abs(x), 1e-4), maxY / math.max(math.abs(y), 1e-4))+ local direction = v2(x * screen.x, y * screen.y):normalize()+ arrows[#arrows + 1] = util.vector4((x * scale + 1) / 2, (y * scale + 1) / 2, direction.x,+ direction.y)+ end+ end+ end+ end+ for i = #rings + 1, MARKS do rings[i] = noMarks[i] end+ for i = #arrows + 1, MARKS do arrows[i] = noMarks[i] end+ shader:setVector4Array('uParty', rings)+ shader:setVector4Array('uArrows', arrows)+end+-- What the game's tooltip says besides the name: where a door leads, and whether it (or a container) is locked or-- trapped, in the game's own words.local function gmst(id, fallback)@@ -704,7 +764,7 @@ local function updateControllerHolds(dt)if zoom ~= 0 thendpadHeld = dpadHeld + dtif dpadHeld > DPAD_ZOOM_DELAY then- targetDistance = util.clamp(targetDistance * DPAD_ZOOM_RATE ^ (zoom * dt), MIN_DISTANCE, MAX_DISTANCE)+ zoomBy(DPAD_ZOOM_RATE ^ (zoom * dt))endelsedpadHeld = 0@@ -774,6 +834,7 @@ local function onFrame(dt)setUiVisible(true)setRingVisible(true)updateRing()+ updateParty()healthbars.update(worldToScreen)local dx, dy = wantedDirection()updateFacing(dt, dx, dy)@@ -850,15 +911,33 @@ local function onInputAction(action)if object then activate(object) endend-local function onKeyPress(key)- if key.code == input.KEY.F7 then- if targetDistance >= TOPDOWN_FROM then- lastTopDownDistance = targetDistance- targetDistance = THIRD_PERSON_DISTANCE- ui.showMessage('Normal view')- else- targetDistance = lastTopDownDistance- ui.showMessage('Top-down view')+-- Switch view (the trigger is registered in settings.lua): top-down, then third person, then first person, each+-- view back at the zoom it was left at+input.registerTriggerHandler('IsowindSwitchView', async:callback(function()+ if not controlsAllowed() then return end+ local current = wantedView()+ if current == 'topdown' then+ lastTopDownDistance = targetDistance+ targetDistance = lastThirdDistance+ ui.showMessage('Third person view')+ elseif current == 'third' then+ lastThirdDistance = targetDistance+ targetDistance = MIN_DISTANCE+ ui.showMessage('First person view')+ else+ targetDistance = lastTopDownDistance+ ui.showMessage('Top-down view')+ end+end))++-- F7 switches the view until the player binds it to something else (Options -> Scripts -> Isowind)+do+ local bindings = storage.playerSection('OMWInputBindings')+ if not ownSettings:get('SwitchViewBound') then+ ownSettings:set('SwitchViewBound', true)+ if bindings:get('IsowindSwitchView') == nil then+ bindings:set('IsowindSwitchView',+ { device = 'keyboard', button = input.KEY.F7, type = 'trigger', key = 'IsowindSwitchView' })endendend@@ -883,16 +962,20 @@ return {onFrame = onFrameSafe,onMouseButtonPress = onMouseButtonPress,onInputAction = onInputAction,- onKeyPress = onKeyPress,onControllerButtonPress = function() gamepad = true end,onTeleported = function() camera.instantTransition() end,onSave = function()- return { distance = targetDistance, topDownDistance = lastTopDownDistance }+ return {+ distance = targetDistance,+ topDownDistance = lastTopDownDistance,+ thirdDistance = lastThirdDistance,+ }end,onLoad = function(data)if not data then return endtargetDistance = data.distance or targetDistancelastTopDownDistance = data.topDownDistance or lastTopDownDistance+ lastThirdDistance = data.thirdDistance or lastThirdDistance-- A save from before the zoom chose the view, made in the normal viewif data.enabled == false and targetDistance >= TOPDOWN_FROM thentargetDistance = THIRD_PERSON_DISTANCEdiff --git a/isowind/mod/shaders/isowind_selection.omwfx b/isowind/mod/shaders/isowind_selection.omwfxindex 9aac9e6..179d8b1 100644--- a/isowind/mod/shaders/isowind_selection.omwfx+++ b/isowind/mod/shaders/isowind_selection.omwfx@@ -10,6 +10,18 @@ uniform_vec4 uSelectionColor {description = "Ring colour (rgb) and strength (a).";}+uniform_vec4 uParty {+ size = 8;+ static = false;+ description = "Party members' feet (xyz) and ring radius (w, 0 = none), as uSelection.";+}++uniform_vec4 uArrows {+ size = 8;+ static = false;+ description = "Arrows at the screen's edge to players off it: position (xy, 0 to 1 from the bottom left) and the way they point (zw, a unit vector; 0 = none).";+}+fragment ring {// Selection ring for the Isowind top-down mode (scripts/isowind/topdown.lua sets the uniforms): a ring// painted onto the ground around the hovered creature's feet. It is placed using each pixel's world position from@@ -17,23 +29,73 @@ fragment ring {omw_In vec2 omw_TexCoord;+ // Party members' rings and the arrows to players off the screen (scripts/isowind/topdown.lua, updateParty) are+ // drawn here too, under the selection ring.+const float WIDTH = 10.0; // ring thickness, world unitsconst float GROUND_RANGE = 40.0; // how far above or below the feet the ground may be+ const vec3 PARTY_COLOR = vec3(0.2, 0.9, 0.3);+ // The arrows: wide, flat triangles in the HUD's muted colours+ const float ARROW_LENGTH = 18.0; // pixels at 1080 lines+ const float ARROW_WIDTH = 40.0;+ const float ARROW_OUTLINE = 2.5;+ const vec3 ARROW_COLOR = vec3(0.47, 0.5, 0.27);+ const vec3 ARROW_OUTLINE_COLOR = vec3(0.16, 0.11, 0.06);++ // How much of a ring around a creature's feet (xyz, radius w) the ground at this pixel shows+ float ringAmount(vec3 world, vec4 ring)+ {+ float dist = length(world.xy - ring.xy);+ float line = 1.0 - smoothstep(WIDTH * 0.5, WIDTH, abs(dist - ring.w));+ float ground = 1.0 - smoothstep(GROUND_RANGE * 0.5, GROUND_RANGE, abs(world.z - ring.z));+ // A faint fill inside the ring helps it read on busy ground.+ float fill = (1.0 - smoothstep(ring.w - WIDTH, ring.w, dist)) * 0.18;+ return max(line, fill) * ground;+ }++ // Distance in pixels outside a triangle pointing along +x, its tip at x = tip and its base at x = tip - size+ float triangleDistance(float along, float side, float tip, float size, float halfWidth)+ {+ float slope = halfWidth / size;+ return max(tip - size - along, (side - slope * (tip - along)) / sqrt(1.0 + slope * slope));+ }++ // Distance in pixels outside an arrow (a triangle, its middle at the arrow's position; negative inside)+ float arrowDistance(vec2 pixel, vec4 arrow, float scale)+ {+ vec2 offset = pixel - arrow.xy * omw.resolution;+ float along = dot(offset, arrow.zw);+ float side = abs(dot(offset, vec2(-arrow.w, arrow.z)));+ float size = ARROW_LENGTH * scale;+ return triangleDistance(along, side, size * 0.5, size, ARROW_WIDTH * 0.5 * scale);+ }void main(){vec4 scene = omw_GetLastShader(omw_TexCoord);+ vec3 world = omw_GetWorldPosFromUV(omw_TexCoord);++ for (int i = 0; i < 8; ++i)+ {+ if (uParty[i].w > 0.0)+ scene.rgb = mix(scene.rgb, PARTY_COLOR, ringAmount(world, uParty[i]) * 0.85);+ }if (uSelection.w > 0.0)+ scene.rgb = mix(scene.rgb, uSelectionColor.rgb, ringAmount(world, uSelection) * uSelectionColor.a);++ vec2 pixel = omw_TexCoord * omw.resolution;+ float scale = omw.resolution.y / 1080.0;+ for (int i = 0; i < 8; ++i){- vec3 world = omw_GetWorldPosFromUV(omw_TexCoord);- float dist = length(world.xy - uSelection.xy);- float ring = 1.0 - smoothstep(WIDTH * 0.5, WIDTH, abs(dist - uSelection.w));- float ground = 1.0 - smoothstep(GROUND_RANGE * 0.5, GROUND_RANGE, abs(world.z - uSelection.z));- // A faint fill inside the ring helps it read on busy ground.- float fill = (1.0 - smoothstep(uSelection.w - WIDTH, uSelection.w, dist)) * 0.18;- float amount = max(ring, fill) * ground * uSelectionColor.a;- scene.rgb = mix(scene.rgb, uSelectionColor.rgb, amount);+ if (dot(uArrows[i].zw, uArrows[i].zw) > 0.0)+ {+ float dist = arrowDistance(pixel, uArrows[i], scale);+ float outline = 1.0 - smoothstep(-0.5, 0.5, dist - ARROW_OUTLINE * scale);+ float fill = 1.0 - smoothstep(-0.5, 0.5, dist);+ scene.rgb = mix(scene.rgb, ARROW_OUTLINE_COLOR, outline * 0.9);+ scene.rgb = mix(scene.rgb, ARROW_COLOR, fill);+ }}omw_FragColor = scene;@@ -41,7 +103,7 @@ fragment ring {}technique {- description = "Selection ring under the creature under the cursor (Isowind top-down mode).";+ description = "Selection ring under the creature under the cursor, party rings and arrows to players off the screen (Isowind top-down mode).";version = "1.0";author = "Isowind";passes = ring;