{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Vehicle Creation and Control Example\n", "\n", "This notebook demonstrates how to spawn a vehicle and control it remotely using DCS Jupyter kernel.\n", "\n", "## Prerequisites\n", "\n", "1. **Place markers on the DCS map:**\n", " - Press `F10` to open the map\n", " - Right-click where you want to spawn the tank and select \"Add mark\"\n", " - Label it \"SPAWN\" (or any name you prefer)\n", " - Right-click somewhere else and add another marker labeled \"TARGET\"\n", " - Make sure both markers are on **land**, not water!\n", "\n", "2. **Start the mission** and run this notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\n", "\n", "First, let's set up our utility function for JSON output:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil" ] } ], "source": [ "_ = net.lua2json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Find Map Markers\n", "\n", "First, let's find the spawn position from the map markers you placed:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"target_pos\":{\"y\":175.62407699109,\"x\":11503.03665637,\"z\":15321.948520692},\"spawn_text\":\"SPAWN\",\"target_text\":\"TARGET\",\"spawn_pos\":{\"y\":161.00019151761,\"x\":10646.219712053,\"z\":13325.847507556}}" ] } ], "source": [ "-- Get all map markers\n", "markers = world.getMarkPanels()\n", "\n", "-- Find SPAWN marker\n", "spawn_marker = nil\n", "target_marker = nil\n", "\n", "for i, marker in pairs(markers) do\n", " if string.upper(marker.text) == \"SPAWN\" then\n", " spawn_marker = marker\n", " elseif string.upper(marker.text) == \"TARGET\" then\n", " target_marker = marker\n", " end\n", "end\n", "\n", "if not spawn_marker then\n", " error(\"Please place a marker labeled 'SPAWN' on the map!\")\n", "end\n", "\n", "if not target_marker then\n", " error(\"Please place a marker labeled 'TARGET' on the map!\")\n", "end\n", "\n", "-- Display marker information\n", "return _({\n", " spawn_pos = spawn_marker.pos,\n", " target_pos = target_marker.pos,\n", " spawn_text = spawn_marker.text,\n", " target_text = target_marker.text\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Spawn Vehicle at Marker\n", "\n", "Now let's spawn a tank at the SPAWN marker position:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil" ] } ], "source": [ "-- Use spawn marker position\n", "spawn_pos = spawn_marker.pos\n", "\n", "-- Create a simple ground unit group\n", "local tank_group = {\n", " name = \"Remote_Tank_1\",\n", " task = \"Ground Nothing\",\n", " units = {\n", " [1] = {\n", " name = \"Tank_Unit_1\",\n", " type = \"M-1 Abrams\",\n", " x = spawn_pos.x,\n", " y = spawn_pos.z,\n", " heading = 0,\n", " skill = \"Average\",\n", " playerCanDrive = true\n", " }\n", " }\n", "}\n", "\n", "-- Spawn the group\n", "coalition.addGroup(country.id.USA, Group.Category.GROUND, tank_group)\n", "\n", "-- Add green smoke at spawn position to mark it\n", "trigger.action.smoke(spawn_pos, trigger.smokeColor.Green)\n", "\n", "trigger.action.outText(\"Tank spawned at SPAWN marker (green smoke)\", 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Get Vehicle Reference\n", "\n", "Find our spawned vehicle and get its controller:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"y\":161.00120660078,\"x\":10646.219726566,\"z\":13325.847656255}" ] } ], "source": [ "-- Find the group we just created\n", "tank_group = Group.getByName(\"Remote_Tank_1\")\n", "tank_unit = tank_group:getUnit(1)\n", "tank_controller = tank_unit:getController()\n", "\n", "-- Get current position\n", "current_pos = tank_unit:getPoint()\n", "return _(current_pos)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Move Tank to TARGET Marker\n", "\n", "Let's move the tank to the TARGET marker position:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil" ] } ], "source": [ "-- Use target marker position\n", "target_pos = target_marker.pos\n", "\n", "-- Create a simple move mission\n", "move_mission = {\n", " id = 'Mission',\n", " params = {\n", " route = {\n", " points = {\n", " [1] = {\n", " x = current_pos.x,\n", " y = current_pos.z,\n", " speed = 10,\n", " action = \"Off Road\"\n", " },\n", " [2] = {\n", " x = target_pos.x,\n", " y = target_pos.z,\n", " speed = 15,\n", " action = \"Off Road\"\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "-- Send the mission to the tank\n", "tank_controller:pushTask(move_mission)\n", "\n", "-- Add red smoke at target position\n", "trigger.action.smoke(target_pos, trigger.smokeColor.Red)\n", "\n", "trigger.action.outText(\"Tank ordered to move to TARGET marker (red smoke)\", 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 5: Monitor Vehicle Status\n", "\n", "Check if the tank is moving and its current status:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"is_active\":true,\"life\":32,\"has_task\":true,\"position\":{\"y\":161.11590769191,\"x\":10665.395771044,\"z\":13370.488194936},\"distance_to_target\":2123.6383062494}" ] } ], "source": [ "-- Check if tank has tasks\n", "has_task = tank_controller:hasTask()\n", "\n", "-- Get current position\n", "new_pos = tank_unit:getPoint()\n", "\n", "-- Check if tank is active\n", "is_active = tank_unit:isActive()\n", "\n", "-- Get health status\n", "life = tank_unit:getLife()\n", "\n", "-- Calculate distance to target\n", "distance_to_target = math.sqrt((new_pos.x - target_pos.x)^2 + (new_pos.z - target_pos.z)^2)\n", "\n", "status = {\n", " has_task = has_task,\n", " position = new_pos,\n", " is_active = is_active,\n", " life = life,\n", " distance_to_target = distance_to_target\n", "}\n", "\n", "return _(status)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 6: Additional Visual Effects\n", "\n", "Add a flare to mark the tank's current position:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil" ] } ], "source": [ "-- Add flare at tank's current position\n", "current_pos = tank_unit:getPoint()\n", "trigger.action.signalFlare(current_pos, trigger.flareColor.Yellow, 0)\n", "\n", "-- Show progress message\n", "distance_remaining = math.sqrt((current_pos.x - target_pos.x)^2 + (current_pos.z - target_pos.z)^2)\n", "trigger.action.outText(\"Tank position marked with YELLOW flare. Distance to target: \" .. \n", " math.floor(distance_remaining) .. \" meters\", 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 7: Advanced Control\n", "\n", "Stop the tank and give it a new task:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil" ] } ], "source": [ "-- Stop current tasks\n", "tank_controller:resetTask()\n", "\n", "-- Set tank to hold position\n", "hold_task = {\n", " id = 'Hold',\n", " params = {}\n", "}\n", "\n", "tank_controller:pushTask(hold_task)\n", "\n", "trigger.action.outText(\"Tank ordered to hold position\", 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cleanup\n", "\n", "Remove the spawned vehicle:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil" ] } ], "source": [ "-- Destroy the group\n", "tank_group:destroy()\n", "\n", "trigger.action.outText(\"Tank removed\", 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "dcs-lua", "language": "", "name": "dcs-lua" }, "language_info": { "file_extension": ".lua", "mimetype": "text/plain", "name": "lua", "version": "5.1" } }, "nbformat": 4, "nbformat_minor": 4 }